TreeBehaviorNumberTest.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. <?php
  2. /**
  3. * TreeBehaviorNumberTest file
  4. *
  5. * This is the basic Tree behavior test
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  10. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice
  14. *
  15. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  17. * @package Cake.Test.Case.Model.Behavior
  18. * @since CakePHP(tm) v 1.2.0.5330
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Model', 'Model');
  22. App::uses('AppModel', 'Model');
  23. require_once dirname(dirname(__FILE__)) . DS . 'models.php';
  24. /**
  25. * TreeBehaviorNumberTest class
  26. *
  27. * @package Cake.Test.Case.Model.Behavior
  28. */
  29. class TreeBehaviorNumberTest extends CakeTestCase {
  30. /**
  31. * Whether backup global state for each test method or not
  32. *
  33. * @var bool false
  34. */
  35. public $backupGlobals = false;
  36. /**
  37. * settings property
  38. *
  39. * @var array
  40. */
  41. public $settings = array(
  42. 'modelClass' => 'NumberTree',
  43. 'leftField' => 'lft',
  44. 'rightField' => 'rght',
  45. 'parentField' => 'parent_id'
  46. );
  47. /**
  48. * fixtures property
  49. *
  50. * @var array
  51. */
  52. public $fixtures = array('core.number_tree', 'core.person');
  53. /**
  54. * testInitialize method
  55. *
  56. * @return void
  57. */
  58. public function testInitialize() {
  59. extract($this->settings);
  60. $this->Tree = new $modelClass();
  61. $this->Tree->initialize(2, 2);
  62. $result = $this->Tree->find('count');
  63. $this->assertEquals(7, $result);
  64. $validTree = $this->Tree->verify();
  65. $this->assertSame($validTree, true);
  66. }
  67. /**
  68. * testDetectInvalidLeft method
  69. *
  70. * @return void
  71. */
  72. public function testDetectInvalidLeft() {
  73. extract($this->settings);
  74. $this->Tree = new $modelClass();
  75. $this->Tree->initialize(2, 2);
  76. $result = $this->Tree->findByName('1.1');
  77. $save[$modelClass]['id'] = $result[$modelClass]['id'];
  78. $save[$modelClass][$leftField] = 0;
  79. $this->Tree->save($save);
  80. $result = $this->Tree->verify();
  81. $this->assertNotSame($result, true);
  82. $result = $this->Tree->recover();
  83. $this->assertSame($result, true);
  84. $result = $this->Tree->verify();
  85. $this->assertSame($result, true);
  86. }
  87. /**
  88. * testDetectInvalidRight method
  89. *
  90. * @return void
  91. */
  92. public function testDetectInvalidRight() {
  93. extract($this->settings);
  94. $this->Tree = new $modelClass();
  95. $this->Tree->initialize(2, 2);
  96. $result = $this->Tree->findByName('1.1');
  97. $save[$modelClass]['id'] = $result[$modelClass]['id'];
  98. $save[$modelClass][$rightField] = 0;
  99. $this->Tree->save($save);
  100. $result = $this->Tree->verify();
  101. $this->assertNotSame($result, true);
  102. $result = $this->Tree->recover();
  103. $this->assertSame($result, true);
  104. $result = $this->Tree->verify();
  105. $this->assertSame($result, true);
  106. }
  107. /**
  108. * testDetectInvalidParent method
  109. *
  110. * @return void
  111. */
  112. public function testDetectInvalidParent() {
  113. extract($this->settings);
  114. $this->Tree = new $modelClass();
  115. $this->Tree->initialize(2, 2);
  116. $result = $this->Tree->findByName('1.1');
  117. // Bypass behavior and any other logic
  118. $this->Tree->updateAll(array($parentField => null), array('id' => $result[$modelClass]['id']));
  119. $result = $this->Tree->verify();
  120. $this->assertNotSame($result, true);
  121. $result = $this->Tree->recover();
  122. $this->assertSame($result, true);
  123. $result = $this->Tree->verify();
  124. $this->assertSame($result, true);
  125. }
  126. /**
  127. * testDetectNoneExistentParent method
  128. *
  129. * @return void
  130. */
  131. public function testDetectNoneExistentParent() {
  132. extract($this->settings);
  133. $this->Tree = new $modelClass();
  134. $this->Tree->initialize(2, 2);
  135. $result = $this->Tree->findByName('1.1');
  136. $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
  137. $result = $this->Tree->verify();
  138. $this->assertNotSame($result, true);
  139. $result = $this->Tree->recover('MPTT');
  140. $this->assertSame($result, true);
  141. $result = $this->Tree->verify();
  142. $this->assertSame($result, true);
  143. }
  144. /**
  145. * testRecoverUsingParentMode method
  146. *
  147. * @return void
  148. */
  149. public function testRecoverUsingParentMode() {
  150. extract($this->settings);
  151. $this->Tree = new $modelClass();
  152. $this->Tree->Behaviors->disable('Tree');
  153. $this->Tree->save(array('parent_id' => null, 'name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0));
  154. $node1 = $this->Tree->id;
  155. $this->Tree->create();
  156. $this->Tree->save(array('parent_id' => null, 'name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0));
  157. $node11 = $this->Tree->id;
  158. $this->Tree->create();
  159. $this->Tree->save(array('parent_id' => null, 'name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0));
  160. $node12 = $this->Tree->id;
  161. $this->Tree->create();
  162. $this->Tree->save(array('parent_id' => null, 'name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0));
  163. $this->Tree->create();
  164. $this->Tree->save(array('parent_id' => null, 'name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0));
  165. $this->Tree->Behaviors->enable('Tree');
  166. $result = $this->Tree->verify();
  167. $this->assertNotSame($result, true);
  168. $result = $this->Tree->recover();
  169. $this->assertTrue($result);
  170. $result = $this->Tree->verify();
  171. $this->assertTrue($result);
  172. $result = $this->Tree->find('first', array(
  173. 'fields' => array('name', $parentField, $leftField, $rightField),
  174. 'conditions' => array('name' => 'Main'),
  175. 'recursive' => -1
  176. ));
  177. $expected = array(
  178. $modelClass => array(
  179. 'name' => 'Main',
  180. $parentField => null,
  181. $leftField => 1,
  182. $rightField => 10
  183. )
  184. );
  185. $this->assertEquals($expected, $result);
  186. }
  187. /**
  188. * testRecoverUsingParentModeAndDelete method
  189. *
  190. * @return void
  191. */
  192. public function testRecoverUsingParentModeAndDelete() {
  193. extract($this->settings);
  194. $this->Tree = new $modelClass();
  195. $this->Tree->Behaviors->disable('Tree');
  196. $this->Tree->save(array('parent_id' => null, 'name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0));
  197. $node1 = $this->Tree->id;
  198. $this->Tree->create();
  199. $this->Tree->save(array('parent_id' => null, 'name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0));
  200. $node11 = $this->Tree->id;
  201. $this->Tree->create();
  202. $this->Tree->save(array('parent_id' => null, 'name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0));
  203. $node12 = $this->Tree->id;
  204. $this->Tree->create();
  205. $this->Tree->save(array('parent_id' => null, 'name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0));
  206. $this->Tree->create();
  207. $this->Tree->save(array('parent_id' => null, 'name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0));
  208. $this->Tree->create();
  209. $this->Tree->save(array('parent_id' => null, 'name' => 'Lost', $parentField => 9, $leftField => 0, $rightField => 0));
  210. $this->Tree->Behaviors->enable('Tree');
  211. $result = $this->Tree->verify();
  212. $this->assertNotSame($result, true);
  213. $count = $this->Tree->find('count');
  214. $this->assertEquals(6, $count);
  215. $result = $this->Tree->recover('parent', 'delete');
  216. $this->assertTrue($result);
  217. $result = $this->Tree->verify();
  218. $this->assertTrue($result);
  219. $count = $this->Tree->find('count');
  220. $this->assertEquals(5, $count);
  221. $result = $this->Tree->find('first', array(
  222. 'fields' => array('name', $parentField, $leftField, $rightField),
  223. 'conditions' => array('name' => 'Main'),
  224. 'recursive' => -1
  225. ));
  226. $expected = array(
  227. $modelClass => array(
  228. 'name' => 'Main',
  229. $parentField => null,
  230. $leftField => 1,
  231. $rightField => 10
  232. )
  233. );
  234. $this->assertEquals($expected, $result);
  235. }
  236. /**
  237. * testRecoverFromMissingParent method
  238. *
  239. * @return void
  240. */
  241. public function testRecoverFromMissingParent() {
  242. extract($this->settings);
  243. $this->Tree = new $modelClass();
  244. $this->Tree->initialize(2, 2);
  245. $result = $this->Tree->findByName('1.1');
  246. $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
  247. $result = $this->Tree->verify();
  248. $this->assertNotSame($result, true);
  249. $result = $this->Tree->recover();
  250. $this->assertSame($result, true);
  251. $result = $this->Tree->verify();
  252. $this->assertSame($result, true);
  253. }
  254. /**
  255. * testDetectInvalidParents method
  256. *
  257. * @return void
  258. */
  259. public function testDetectInvalidParents() {
  260. extract($this->settings);
  261. $this->Tree = new $modelClass();
  262. $this->Tree->initialize(2, 2);
  263. $this->Tree->updateAll(array($parentField => null));
  264. $result = $this->Tree->verify();
  265. $this->assertNotSame($result, true);
  266. $result = $this->Tree->recover();
  267. $this->assertSame($result, true);
  268. $result = $this->Tree->verify();
  269. $this->assertSame($result, true);
  270. }
  271. /**
  272. * testDetectInvalidLftsRghts method
  273. *
  274. * @return void
  275. */
  276. public function testDetectInvalidLftsRghts() {
  277. extract($this->settings);
  278. $this->Tree = new $modelClass();
  279. $this->Tree->initialize(2, 2);
  280. $this->Tree->updateAll(array($leftField => 0, $rightField => 0));
  281. $result = $this->Tree->verify();
  282. $this->assertNotSame($result, true);
  283. $this->Tree->recover();
  284. $result = $this->Tree->verify();
  285. $this->assertSame($result, true);
  286. }
  287. /**
  288. * Reproduces a situation where a single node has lft= rght, and all other lft and rght fields follow sequentially
  289. *
  290. * @return void
  291. */
  292. public function testDetectEqualLftsRghts() {
  293. extract($this->settings);
  294. $this->Tree = new $modelClass();
  295. $this->Tree->initialize(1, 3);
  296. $result = $this->Tree->findByName('1.1');
  297. $this->Tree->updateAll(array($rightField => $result[$modelClass][$leftField]), array('id' => $result[$modelClass]['id']));
  298. $this->Tree->updateAll(array($leftField => $this->Tree->escapeField($leftField) . ' -1'),
  299. array($leftField . ' >' => $result[$modelClass][$leftField]));
  300. $this->Tree->updateAll(array($rightField => $this->Tree->escapeField($rightField) . ' -1'),
  301. array($rightField . ' >' => $result[$modelClass][$leftField]));
  302. $result = $this->Tree->verify();
  303. $this->assertNotSame($result, true);
  304. $result = $this->Tree->recover();
  305. $this->assertTrue($result);
  306. $result = $this->Tree->verify();
  307. $this->assertTrue($result);
  308. }
  309. /**
  310. * testAddOrphan method
  311. *
  312. * @return void
  313. */
  314. public function testAddOrphan() {
  315. extract($this->settings);
  316. $this->Tree = new $modelClass();
  317. $this->Tree->initialize(2, 2);
  318. $this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
  319. $result = $this->Tree->find('first', array('fields' => array('name', $parentField), 'order' => $modelClass . '.' . $leftField . ' desc'));
  320. $expected = array($modelClass => array('name' => 'testAddOrphan', $parentField => null));
  321. $this->assertEquals($expected, $result);
  322. $validTree = $this->Tree->verify();
  323. $this->assertSame($validTree, true);
  324. }
  325. /**
  326. * testAddMiddle method
  327. *
  328. * @return void
  329. */
  330. public function testAddMiddle() {
  331. extract($this->settings);
  332. $this->Tree = new $modelClass();
  333. $this->Tree->initialize(2, 2);
  334. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  335. $initialCount = $this->Tree->find('count');
  336. $this->Tree->create();
  337. $result = $this->Tree->save(array($modelClass => array('name' => 'testAddMiddle', $parentField => $data[$modelClass]['id'])));
  338. $expected = array_merge(array($modelClass => array('name' => 'testAddMiddle', $parentField => '2')), $result);
  339. $this->assertSame($expected, $result);
  340. $laterCount = $this->Tree->find('count');
  341. $this->assertEquals($initialCount + 1, $laterCount);
  342. $children = $this->Tree->children($data[$modelClass]['id'], true, array('name'));
  343. $expects = array(array($modelClass => array('name' => '1.1.1')),
  344. array($modelClass => array('name' => '1.1.2')),
  345. array($modelClass => array('name' => 'testAddMiddle')));
  346. $this->assertSame($children, $expects);
  347. $validTree = $this->Tree->verify();
  348. $this->assertSame($validTree, true);
  349. }
  350. /**
  351. * testAddWithPreSpecifiedId method
  352. *
  353. * @return void
  354. */
  355. public function testAddWithPreSpecifiedId() {
  356. extract($this->settings);
  357. $this->Tree = new $modelClass();
  358. $this->Tree->initialize(2, 2);
  359. $data = $this->Tree->find('first', array(
  360. 'fields' => array('id'),
  361. 'conditions' => array($modelClass . '.name' => '1.1')
  362. ));
  363. $this->Tree->create();
  364. $result = $this->Tree->save(array($modelClass => array(
  365. 'id' => 100,
  366. 'name' => 'testAddMiddle',
  367. $parentField => $data[$modelClass]['id'])
  368. ));
  369. $expected = array_merge(
  370. array($modelClass => array('id' => 100, 'name' => 'testAddMiddle', $parentField => '2')),
  371. $result
  372. );
  373. $this->assertSame($expected, $result);
  374. $this->assertTrue($this->Tree->verify());
  375. }
  376. /**
  377. * testAddInvalid method
  378. *
  379. * @return void
  380. */
  381. public function testAddInvalid() {
  382. extract($this->settings);
  383. $this->Tree = new $modelClass();
  384. $this->Tree->initialize(2, 2);
  385. $this->Tree->id = null;
  386. $initialCount = $this->Tree->find('count');
  387. //$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
  388. $saveSuccess = $this->Tree->save(array($modelClass => array('name' => 'testAddInvalid', $parentField => 99999)));
  389. $this->assertSame($saveSuccess, false);
  390. $laterCount = $this->Tree->find('count');
  391. $this->assertSame($initialCount, $laterCount);
  392. $validTree = $this->Tree->verify();
  393. $this->assertSame($validTree, true);
  394. }
  395. /**
  396. * testAddNotIndexedByModel method
  397. *
  398. * @return void
  399. */
  400. public function testAddNotIndexedByModel() {
  401. extract($this->settings);
  402. $this->Tree = new $modelClass();
  403. $this->Tree->initialize(2, 2);
  404. $this->Tree->save(array('name' => 'testAddNotIndexed', $parentField => null));
  405. $result = $this->Tree->find('first', array('fields' => array('name', $parentField), 'order' => $modelClass . '.' . $leftField . ' desc'));
  406. $expected = array($modelClass => array('name' => 'testAddNotIndexed', $parentField => null));
  407. $this->assertEquals($expected, $result);
  408. $validTree = $this->Tree->verify();
  409. $this->assertSame($validTree, true);
  410. }
  411. /**
  412. * testMovePromote method
  413. *
  414. * @return void
  415. */
  416. public function testMovePromote() {
  417. extract($this->settings);
  418. $this->Tree = new $modelClass();
  419. $this->Tree->initialize(2, 2);
  420. $this->Tree->id = null;
  421. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  422. $parentId = $parent[$modelClass]['id'];
  423. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  424. $this->Tree->id = $data[$modelClass]['id'];
  425. $this->Tree->saveField($parentField, $parentId);
  426. $direct = $this->Tree->children($parentId, true, array('id', 'name', $parentField, $leftField, $rightField));
  427. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
  428. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
  429. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
  430. $this->assertEquals($direct, $expects);
  431. $validTree = $this->Tree->verify();
  432. $this->assertSame($validTree, true);
  433. }
  434. /**
  435. * testMoveWithWhitelist method
  436. *
  437. * @return void
  438. */
  439. public function testMoveWithWhitelist() {
  440. extract($this->settings);
  441. $this->Tree = new $modelClass();
  442. $this->Tree->initialize(2, 2);
  443. $this->Tree->id = null;
  444. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  445. $parentId = $parent[$modelClass]['id'];
  446. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  447. $this->Tree->id = $data[$modelClass]['id'];
  448. $this->Tree->whitelist = array($parentField, 'name', 'description');
  449. $this->Tree->saveField($parentField, $parentId);
  450. $result = $this->Tree->children($parentId, true, array('id', 'name', $parentField, $leftField, $rightField));
  451. $expected = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
  452. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
  453. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
  454. $this->assertEquals($expected, $result);
  455. $this->assertTrue($this->Tree->verify());
  456. }
  457. /**
  458. * testInsertWithWhitelist method
  459. *
  460. * @return void
  461. */
  462. public function testInsertWithWhitelist() {
  463. extract($this->settings);
  464. $this->Tree = new $modelClass();
  465. $this->Tree->initialize(2, 2);
  466. $this->Tree->whitelist = array('name', $parentField);
  467. $this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
  468. $result = $this->Tree->findByName('testAddOrphan', array('name', $parentField, $leftField, $rightField));
  469. $expected = array('name' => 'testAddOrphan', $parentField => null, $leftField => '15', $rightField => 16);
  470. $this->assertEquals($expected, $result[$modelClass]);
  471. $this->assertSame($this->Tree->verify(), true);
  472. }
  473. /**
  474. * testMoveBefore method
  475. *
  476. * @return void
  477. */
  478. public function testMoveBefore() {
  479. extract($this->settings);
  480. $this->Tree = new $modelClass();
  481. $this->Tree->initialize(2, 2);
  482. $this->Tree->id = null;
  483. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
  484. $parentId = $parent[$modelClass]['id'];
  485. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
  486. $this->Tree->id = $data[$modelClass]['id'];
  487. $this->Tree->saveField($parentField, $parentId);
  488. $result = $this->Tree->children($parentId, true, array('name'));
  489. $expects = array(array($modelClass => array('name' => '1.1.1')),
  490. array($modelClass => array('name' => '1.1.2')),
  491. array($modelClass => array('name' => '1.2')));
  492. $this->assertEquals($expects, $result);
  493. $validTree = $this->Tree->verify();
  494. $this->assertSame($validTree, true);
  495. }
  496. /**
  497. * testMoveAfter method
  498. *
  499. * @return void
  500. */
  501. public function testMoveAfter() {
  502. extract($this->settings);
  503. $this->Tree = new $modelClass();
  504. $this->Tree->initialize(2, 2);
  505. $this->Tree->id = null;
  506. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
  507. $parentId = $parent[$modelClass]['id'];
  508. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  509. $this->Tree->id = $data[$modelClass]['id'];
  510. $this->Tree->saveField($parentField, $parentId);
  511. $result = $this->Tree->children($parentId, true, array('name'));
  512. $expects = array(array($modelClass => array('name' => '1.2.1')),
  513. array($modelClass => array('name' => '1.2.2')),
  514. array($modelClass => array('name' => '1.1')));
  515. $this->assertEquals($expects, $result);
  516. $validTree = $this->Tree->verify();
  517. $this->assertSame($validTree, true);
  518. }
  519. /**
  520. * testMoveDemoteInvalid method
  521. *
  522. * @return void
  523. */
  524. public function testMoveDemoteInvalid() {
  525. extract($this->settings);
  526. $this->Tree = new $modelClass();
  527. $this->Tree->initialize(2, 2);
  528. $this->Tree->id = null;
  529. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  530. $parentId = $parent[$modelClass]['id'];
  531. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  532. $expects = $this->Tree->find('all');
  533. $before = $this->Tree->read(null, $data[$modelClass]['id']);
  534. $this->Tree->id = $parentId;
  535. $this->Tree->saveField($parentField, $data[$modelClass]['id']);
  536. $results = $this->Tree->find('all');
  537. $after = $this->Tree->read(null, $data[$modelClass]['id']);
  538. $this->assertEquals($expects, $results);
  539. $this->assertEquals($before, $after);
  540. $validTree = $this->Tree->verify();
  541. $this->assertSame($validTree, true);
  542. }
  543. /**
  544. * testMoveInvalid method
  545. *
  546. * @return void
  547. */
  548. public function testMoveInvalid() {
  549. extract($this->settings);
  550. $this->Tree = new $modelClass();
  551. $this->Tree->initialize(2, 2);
  552. $this->Tree->id = null;
  553. $initialCount = $this->Tree->find('count');
  554. $data = $this->Tree->findByName('1.1');
  555. $this->Tree->id = $data[$modelClass]['id'];
  556. $this->Tree->saveField($parentField, 999999);
  557. $laterCount = $this->Tree->find('count');
  558. $this->assertSame($initialCount, $laterCount);
  559. $validTree = $this->Tree->verify();
  560. $this->assertSame($validTree, true);
  561. }
  562. /**
  563. * testMoveSelfInvalid method
  564. *
  565. * @return void
  566. */
  567. public function testMoveSelfInvalid() {
  568. extract($this->settings);
  569. $this->Tree = new $modelClass();
  570. $this->Tree->initialize(2, 2);
  571. $this->Tree->id = null;
  572. $initialCount = $this->Tree->find('count');
  573. $data = $this->Tree->findByName('1.1');
  574. $this->Tree->id = $data[$modelClass]['id'];
  575. $saveSuccess = $this->Tree->saveField($parentField, $this->Tree->id);
  576. $this->assertSame($saveSuccess, false);
  577. $laterCount = $this->Tree->find('count');
  578. $this->assertSame($initialCount, $laterCount);
  579. $validTree = $this->Tree->verify();
  580. $this->assertSame($validTree, true);
  581. }
  582. /**
  583. * testMoveUpSuccess method
  584. *
  585. * @return void
  586. */
  587. public function testMoveUpSuccess() {
  588. extract($this->settings);
  589. $this->Tree = new $modelClass();
  590. $this->Tree->initialize(2, 2);
  591. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
  592. $this->Tree->moveUp($data[$modelClass]['id']);
  593. $parent = $this->Tree->findByName('1. Root', array('id'));
  594. $this->Tree->id = $parent[$modelClass]['id'];
  595. $result = $this->Tree->children(null, true, array('name'));
  596. $expected = array(array($modelClass => array('name' => '1.2', )),
  597. array($modelClass => array('name' => '1.1', )));
  598. $this->assertSame($expected, $result);
  599. }
  600. /**
  601. * testMoveUpFail method
  602. *
  603. * @return void
  604. */
  605. public function testMoveUpFail() {
  606. extract($this->settings);
  607. $this->Tree = new $modelClass();
  608. $this->Tree->initialize(2, 2);
  609. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
  610. $this->Tree->moveUp($data[$modelClass]['id']);
  611. $parent = $this->Tree->findByName('1. Root', array('id'));
  612. $this->Tree->id = $parent[$modelClass]['id'];
  613. $result = $this->Tree->children(null, true, array('name'));
  614. $expected = array(array($modelClass => array('name' => '1.1', )),
  615. array($modelClass => array('name' => '1.2', )));
  616. $this->assertSame($expected, $result);
  617. }
  618. /**
  619. * testMoveUp2 method
  620. *
  621. * @return void
  622. */
  623. public function testMoveUp2() {
  624. extract($this->settings);
  625. $this->Tree = new $modelClass();
  626. $this->Tree->initialize(1, 10);
  627. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  628. $this->Tree->moveUp($data[$modelClass]['id'], 2);
  629. $parent = $this->Tree->findByName('1. Root', array('id'));
  630. $this->Tree->id = $parent[$modelClass]['id'];
  631. $result = $this->Tree->children(null, true, array('name'));
  632. $expected = array(
  633. array($modelClass => array('name' => '1.1', )),
  634. array($modelClass => array('name' => '1.2', )),
  635. array($modelClass => array('name' => '1.5', )),
  636. array($modelClass => array('name' => '1.3', )),
  637. array($modelClass => array('name' => '1.4', )),
  638. array($modelClass => array('name' => '1.6', )),
  639. array($modelClass => array('name' => '1.7', )),
  640. array($modelClass => array('name' => '1.8', )),
  641. array($modelClass => array('name' => '1.9', )),
  642. array($modelClass => array('name' => '1.10', )));
  643. $this->assertSame($expected, $result);
  644. }
  645. /**
  646. * testMoveUpFirst method
  647. *
  648. * @return void
  649. */
  650. public function testMoveUpFirst() {
  651. extract($this->settings);
  652. $this->Tree = new $modelClass();
  653. $this->Tree->initialize(1, 10);
  654. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  655. $this->Tree->moveUp($data[$modelClass]['id'], true);
  656. $parent = $this->Tree->findByName('1. Root', array('id'));
  657. $this->Tree->id = $parent[$modelClass]['id'];
  658. $result = $this->Tree->children(null, true, array('name'));
  659. $expected = array(
  660. array($modelClass => array('name' => '1.5', )),
  661. array($modelClass => array('name' => '1.1', )),
  662. array($modelClass => array('name' => '1.2', )),
  663. array($modelClass => array('name' => '1.3', )),
  664. array($modelClass => array('name' => '1.4', )),
  665. array($modelClass => array('name' => '1.6', )),
  666. array($modelClass => array('name' => '1.7', )),
  667. array($modelClass => array('name' => '1.8', )),
  668. array($modelClass => array('name' => '1.9', )),
  669. array($modelClass => array('name' => '1.10', )));
  670. $this->assertSame($expected, $result);
  671. }
  672. /**
  673. * testMoveDownSuccess method
  674. *
  675. * @return void
  676. */
  677. public function testMoveDownSuccess() {
  678. extract($this->settings);
  679. $this->Tree = new $modelClass();
  680. $this->Tree->initialize(2, 2);
  681. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  682. $this->Tree->moveDown($data[$modelClass]['id']);
  683. $parent = $this->Tree->findByName('1. Root', array('id'));
  684. $this->Tree->id = $parent[$modelClass]['id'];
  685. $result = $this->Tree->children(null, true, array('name'));
  686. $expected = array(array($modelClass => array('name' => '1.2', )),
  687. array($modelClass => array('name' => '1.1', )));
  688. $this->assertSame($expected, $result);
  689. }
  690. /**
  691. * testMoveDownFail method
  692. *
  693. * @return void
  694. */
  695. public function testMoveDownFail() {
  696. extract($this->settings);
  697. $this->Tree = new $modelClass();
  698. $this->Tree->initialize(2, 2);
  699. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
  700. $this->Tree->moveDown($data[$modelClass]['id']);
  701. $parent = $this->Tree->findByName('1. Root', array('id'));
  702. $this->Tree->id = $parent[$modelClass]['id'];
  703. $result = $this->Tree->children(null, true, array('name'));
  704. $expected = array(array($modelClass => array('name' => '1.1', )),
  705. array($modelClass => array('name' => '1.2', )));
  706. $this->assertSame($expected, $result);
  707. }
  708. /**
  709. * testMoveDownLast method
  710. *
  711. * @return void
  712. */
  713. public function testMoveDownLast() {
  714. extract($this->settings);
  715. $this->Tree = new $modelClass();
  716. $this->Tree->initialize(1, 10);
  717. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  718. $this->Tree->moveDown($data[$modelClass]['id'], true);
  719. $parent = $this->Tree->findByName('1. Root', array('id'));
  720. $this->Tree->id = $parent[$modelClass]['id'];
  721. $result = $this->Tree->children(null, true, array('name'));
  722. $expected = array(
  723. array($modelClass => array('name' => '1.1', )),
  724. array($modelClass => array('name' => '1.2', )),
  725. array($modelClass => array('name' => '1.3', )),
  726. array($modelClass => array('name' => '1.4', )),
  727. array($modelClass => array('name' => '1.6', )),
  728. array($modelClass => array('name' => '1.7', )),
  729. array($modelClass => array('name' => '1.8', )),
  730. array($modelClass => array('name' => '1.9', )),
  731. array($modelClass => array('name' => '1.10', )),
  732. array($modelClass => array('name' => '1.5', )));
  733. $this->assertSame($expected, $result);
  734. }
  735. /**
  736. * testMoveDown2 method
  737. *
  738. * @return void
  739. */
  740. public function testMoveDown2() {
  741. extract($this->settings);
  742. $this->Tree = new $modelClass();
  743. $this->Tree->initialize(1, 10);
  744. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  745. $this->Tree->moveDown($data[$modelClass]['id'], 2);
  746. $parent = $this->Tree->findByName('1. Root', array('id'));
  747. $this->Tree->id = $parent[$modelClass]['id'];
  748. $result = $this->Tree->children(null, true, array('name'));
  749. $expected = array(
  750. array($modelClass => array('name' => '1.1', )),
  751. array($modelClass => array('name' => '1.2', )),
  752. array($modelClass => array('name' => '1.3', )),
  753. array($modelClass => array('name' => '1.4', )),
  754. array($modelClass => array('name' => '1.6', )),
  755. array($modelClass => array('name' => '1.7', )),
  756. array($modelClass => array('name' => '1.5', )),
  757. array($modelClass => array('name' => '1.8', )),
  758. array($modelClass => array('name' => '1.9', )),
  759. array($modelClass => array('name' => '1.10', )));
  760. $this->assertSame($expected, $result);
  761. }
  762. /**
  763. * testSaveNoMove method
  764. *
  765. * @return void
  766. */
  767. public function testSaveNoMove() {
  768. extract($this->settings);
  769. $this->Tree = new $modelClass();
  770. $this->Tree->initialize(1, 10);
  771. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  772. $this->Tree->id = $data[$modelClass]['id'];
  773. $this->Tree->saveField('name', 'renamed');
  774. $parent = $this->Tree->findByName('1. Root', array('id'));
  775. $this->Tree->id = $parent[$modelClass]['id'];
  776. $result = $this->Tree->children(null, true, array('name'));
  777. $expected = array(
  778. array($modelClass => array('name' => '1.1', )),
  779. array($modelClass => array('name' => '1.2', )),
  780. array($modelClass => array('name' => '1.3', )),
  781. array($modelClass => array('name' => '1.4', )),
  782. array($modelClass => array('name' => 'renamed', )),
  783. array($modelClass => array('name' => '1.6', )),
  784. array($modelClass => array('name' => '1.7', )),
  785. array($modelClass => array('name' => '1.8', )),
  786. array($modelClass => array('name' => '1.9', )),
  787. array($modelClass => array('name' => '1.10', )));
  788. $this->assertSame($expected, $result);
  789. }
  790. /**
  791. * testMoveToRootAndMoveUp method
  792. *
  793. * @return void
  794. */
  795. public function testMoveToRootAndMoveUp() {
  796. extract($this->settings);
  797. $this->Tree = new $modelClass();
  798. $this->Tree->initialize(1, 1);
  799. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  800. $this->Tree->id = $data[$modelClass]['id'];
  801. $this->Tree->save(array($parentField => null));
  802. $result = $this->Tree->verify();
  803. $this->assertSame($result, true);
  804. $this->Tree->moveUp();
  805. $result = $this->Tree->find('all', array('fields' => 'name', 'order' => $modelClass . '.' . $leftField . ' ASC'));
  806. $expected = array(array($modelClass => array('name' => '1.1')),
  807. array($modelClass => array('name' => '1. Root')));
  808. $this->assertSame($expected, $result);
  809. }
  810. /**
  811. * testDelete method
  812. *
  813. * @return void
  814. */
  815. public function testDelete() {
  816. extract($this->settings);
  817. $this->Tree = new $modelClass();
  818. $this->Tree->initialize(2, 2);
  819. $initialCount = $this->Tree->find('count');
  820. $result = $this->Tree->findByName('1.1.1');
  821. $return = $this->Tree->delete($result[$modelClass]['id']);
  822. $this->assertEquals(true, $return);
  823. $laterCount = $this->Tree->find('count');
  824. $this->assertEquals($initialCount - 1, $laterCount);
  825. $validTree = $this->Tree->verify();
  826. $this->assertSame($validTree, true);
  827. $initialCount = $this->Tree->find('count');
  828. $result = $this->Tree->findByName('1.1');
  829. $return = $this->Tree->delete($result[$modelClass]['id']);
  830. $this->assertEquals(true, $return);
  831. $laterCount = $this->Tree->find('count');
  832. $this->assertEquals($initialCount - 2, $laterCount);
  833. $validTree = $this->Tree->verify();
  834. $this->assertSame($validTree, true);
  835. }
  836. /**
  837. * Test deleting a record that doesn't exist.
  838. *
  839. * @return void
  840. */
  841. public function testDeleteDoesNotExist() {
  842. extract($this->settings);
  843. $this->Tree = new $modelClass();
  844. $this->Tree->initialize(2, 2);
  845. $this->Tree->delete(99999);
  846. }
  847. /**
  848. * testRemove method
  849. *
  850. * @return void
  851. */
  852. public function testRemove() {
  853. extract($this->settings);
  854. $this->Tree = new $modelClass();
  855. $this->Tree->initialize(2, 2);
  856. $initialCount = $this->Tree->find('count');
  857. $result = $this->Tree->findByName('1.1');
  858. $this->Tree->removeFromTree($result[$modelClass]['id']);
  859. $laterCount = $this->Tree->find('count');
  860. $this->assertEquals($initialCount, $laterCount);
  861. $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'));
  862. $expects = array(array($modelClass => array('name' => '1.1.1')),
  863. array($modelClass => array('name' => '1.1.2')),
  864. array($modelClass => array('name' => '1.2')));
  865. $this->assertEquals($children, $expects);
  866. $topNodes = $this->Tree->children(false, true,array('name'));
  867. $expects = array(array($modelClass => array('name' => '1. Root')),
  868. array($modelClass => array('name' => '1.1')));
  869. $this->assertEquals($topNodes, $expects);
  870. $validTree = $this->Tree->verify();
  871. $this->assertSame($validTree, true);
  872. }
  873. /**
  874. * testRemoveLastTopParent method
  875. *
  876. * @return void
  877. */
  878. public function testRemoveLastTopParent() {
  879. extract($this->settings);
  880. $this->Tree = new $modelClass();
  881. $this->Tree->initialize(2, 2);
  882. $initialCount = $this->Tree->find('count');
  883. $initialTopNodes = $this->Tree->childCount(false);
  884. $result = $this->Tree->findByName('1. Root');
  885. $this->Tree->removeFromTree($result[$modelClass]['id']);
  886. $laterCount = $this->Tree->find('count');
  887. $laterTopNodes = $this->Tree->childCount(false);
  888. $this->assertEquals($initialCount, $laterCount);
  889. $this->assertEquals($initialTopNodes, $laterTopNodes);
  890. $topNodes = $this->Tree->children(false, true,array('name'));
  891. $expects = array(array($modelClass => array('name' => '1.1')),
  892. array($modelClass => array('name' => '1.2')),
  893. array($modelClass => array('name' => '1. Root')));
  894. $this->assertEquals($topNodes, $expects);
  895. $validTree = $this->Tree->verify();
  896. $this->assertSame($validTree, true);
  897. }
  898. /**
  899. * testRemoveNoChildren method
  900. *
  901. * @return void
  902. */
  903. public function testRemoveNoChildren() {
  904. extract($this->settings);
  905. $this->Tree = new $modelClass();
  906. $this->Tree->initialize(2, 2);
  907. $initialCount = $this->Tree->find('count');
  908. $result = $this->Tree->findByName('1.1.1');
  909. $this->Tree->removeFromTree($result[$modelClass]['id']);
  910. $laterCount = $this->Tree->find('count');
  911. $this->assertEquals($initialCount, $laterCount);
  912. $nodes = $this->Tree->find('list', array('order' => $leftField));
  913. $expects = array(
  914. 1 => '1. Root',
  915. 2 => '1.1',
  916. 4 => '1.1.2',
  917. 5 => '1.2',
  918. 6 => '1.2.1',
  919. 7 => '1.2.2',
  920. 3 => '1.1.1',
  921. );
  922. $this->assertEquals($nodes, $expects);
  923. $validTree = $this->Tree->verify();
  924. $this->assertSame($validTree, true);
  925. }
  926. /**
  927. * testRemoveAndDelete method
  928. *
  929. * @return void
  930. */
  931. public function testRemoveAndDelete() {
  932. extract($this->settings);
  933. $this->Tree = new $modelClass();
  934. $this->Tree->initialize(2, 2);
  935. $initialCount = $this->Tree->find('count');
  936. $result = $this->Tree->findByName('1.1');
  937. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  938. $laterCount = $this->Tree->find('count');
  939. $this->assertEquals($initialCount - 1, $laterCount);
  940. $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'), $leftField . ' asc');
  941. $expects = array(
  942. array($modelClass => array('name' => '1.1.1')),
  943. array($modelClass => array('name' => '1.1.2')),
  944. array($modelClass => array('name' => '1.2'))
  945. );
  946. $this->assertEquals($children, $expects);
  947. $topNodes = $this->Tree->children(false, true,array('name'));
  948. $expects = array(array($modelClass => array('name' => '1. Root')));
  949. $this->assertEquals($topNodes, $expects);
  950. $validTree = $this->Tree->verify();
  951. $this->assertSame($validTree, true);
  952. }
  953. /**
  954. * testRemoveAndDeleteNoChildren method
  955. *
  956. * @return void
  957. */
  958. public function testRemoveAndDeleteNoChildren() {
  959. extract($this->settings);
  960. $this->Tree = new $modelClass();
  961. $this->Tree->initialize(2, 2);
  962. $initialCount = $this->Tree->find('count');
  963. $result = $this->Tree->findByName('1.1.1');
  964. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  965. $laterCount = $this->Tree->find('count');
  966. $this->assertEquals($initialCount - 1, $laterCount);
  967. $nodes = $this->Tree->find('list', array('order' => $leftField));
  968. $expects = array(
  969. 1 => '1. Root',
  970. 2 => '1.1',
  971. 4 => '1.1.2',
  972. 5 => '1.2',
  973. 6 => '1.2.1',
  974. 7 => '1.2.2',
  975. );
  976. $this->assertEquals($nodes, $expects);
  977. $validTree = $this->Tree->verify();
  978. $this->assertSame($validTree, true);
  979. }
  980. /**
  981. * testChildren method
  982. *
  983. * @return void
  984. */
  985. public function testChildren() {
  986. extract($this->settings);
  987. $this->Tree = new $modelClass();
  988. $this->Tree->initialize(2, 2);
  989. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  990. $this->Tree->id = $data[$modelClass]['id'];
  991. $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
  992. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  993. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
  994. $this->assertEquals($direct, $expects);
  995. $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
  996. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  997. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
  998. array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
  999. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
  1000. array($modelClass => array('id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
  1001. array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12)));
  1002. $this->assertEquals($total, $expects);
  1003. $this->assertEquals(array(), $this->Tree->children(10000));
  1004. }
  1005. /**
  1006. * testCountChildren method
  1007. *
  1008. * @return void
  1009. */
  1010. public function testCountChildren() {
  1011. extract($this->settings);
  1012. $this->Tree = new $modelClass();
  1013. $this->Tree->initialize(2, 2);
  1014. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  1015. $this->Tree->id = $data[$modelClass]['id'];
  1016. $direct = $this->Tree->childCount(null, true);
  1017. $this->assertEquals(2, $direct);
  1018. $total = $this->Tree->childCount();
  1019. $this->assertEquals(6, $total);
  1020. $this->Tree->read(null, $data[$modelClass]['id']);
  1021. $id = $this->Tree->field('id', array($modelClass . '.name' => '1.2'));
  1022. $total = $this->Tree->childCount($id);
  1023. $this->assertEquals(2, $total);
  1024. }
  1025. /**
  1026. * testGetParentNode method
  1027. *
  1028. * @return void
  1029. */
  1030. public function testGetParentNode() {
  1031. extract($this->settings);
  1032. $this->Tree = new $modelClass();
  1033. $this->Tree->initialize(2, 2);
  1034. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
  1035. $this->Tree->id = $data[$modelClass]['id'];
  1036. $result = $this->Tree->getParentNode(null, array('name'));
  1037. $expects = array($modelClass => array('name' => '1.2'));
  1038. $this->assertSame($expects, $result);
  1039. }
  1040. /**
  1041. * testGetPath method
  1042. *
  1043. * @return void
  1044. */
  1045. public function testGetPath() {
  1046. extract($this->settings);
  1047. $this->Tree = new $modelClass();
  1048. $this->Tree->initialize(2, 2);
  1049. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
  1050. $this->Tree->id = $data[$modelClass]['id'];
  1051. $result = $this->Tree->getPath(null, array('name'));
  1052. $expects = array(array($modelClass => array('name' => '1. Root')),
  1053. array($modelClass => array('name' => '1.2')),
  1054. array($modelClass => array('name' => '1.2.2')));
  1055. $this->assertSame($expects, $result);
  1056. }
  1057. /**
  1058. * testNoAmbiguousColumn method
  1059. *
  1060. * @return void
  1061. */
  1062. public function testNoAmbiguousColumn() {
  1063. extract($this->settings);
  1064. $this->Tree = new $modelClass();
  1065. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  1066. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  1067. $this->Tree->initialize(2, 2);
  1068. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  1069. $this->Tree->id = $data[$modelClass]['id'];
  1070. $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
  1071. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  1072. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
  1073. $this->assertEquals($direct, $expects);
  1074. $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
  1075. $expects = array(
  1076. array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  1077. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
  1078. array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
  1079. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
  1080. array($modelClass => array('id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
  1081. array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12))
  1082. );
  1083. $this->assertEquals($total, $expects);
  1084. }
  1085. /**
  1086. * testReorderTree method
  1087. *
  1088. * @return void
  1089. */
  1090. public function testReorderTree() {
  1091. extract($this->settings);
  1092. $this->Tree = new $modelClass();
  1093. $this->Tree->initialize(3, 3);
  1094. $nodes = $this->Tree->find('list', array('order' => $leftField));
  1095. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  1096. $this->Tree->moveDown($data[$modelClass]['id']);
  1097. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2.1')));
  1098. $this->Tree->moveDown($data[$modelClass]['id']);
  1099. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.3.2.2')));
  1100. $this->Tree->moveDown($data[$modelClass]['id']);
  1101. $unsortedNodes = $this->Tree->find('list', array('order' => $leftField));
  1102. $this->assertEquals($nodes, $unsortedNodes);
  1103. $this->assertNotEquals(array_keys($nodes), array_keys($unsortedNodes));
  1104. $this->Tree->reorder();
  1105. $sortedNodes = $this->Tree->find('list', array('order' => $leftField));
  1106. $this->assertSame($nodes, $sortedNodes);
  1107. }
  1108. /**
  1109. * test reordering large-ish trees with cacheQueries = true.
  1110. * This caused infinite loops when moving down elements as stale data is returned
  1111. * from the memory cache
  1112. *
  1113. * @return void
  1114. */
  1115. public function testReorderBigTreeWithQueryCaching() {
  1116. extract($this->settings);
  1117. $this->Tree = new $modelClass();
  1118. $this->Tree->initialize(2, 10);
  1119. $original = $this->Tree->cacheQueries;
  1120. $this->Tree->cacheQueries = true;
  1121. $this->Tree->reorder(array('field' => 'name', 'direction' => 'DESC'));
  1122. $this->assertTrue($this->Tree->cacheQueries, 'cacheQueries was not restored after reorder(). %s');
  1123. $this->Tree->cacheQueries = $original;
  1124. }
  1125. /**
  1126. * testGenerateTreeListWithSelfJoin method
  1127. *
  1128. * @return void
  1129. */
  1130. public function testGenerateTreeListWithSelfJoin() {
  1131. extract($this->settings);
  1132. $this->Tree = new $modelClass();
  1133. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  1134. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  1135. $this->Tree->initialize(2, 2);
  1136. $result = $this->Tree->generateTreeList();
  1137. $expected = array(1 => '1. Root', 2 => '_1.1', 3 => '__1.1.1', 4 => '__1.1.2', 5 => '_1.2', 6 => '__1.2.1', 7 => '__1.2.2');
  1138. $this->assertSame($expected, $result);
  1139. }
  1140. /**
  1141. * Test the formatting options of generateTreeList()
  1142. *
  1143. * @return void
  1144. */
  1145. public function testGenerateTreeListFormatting() {
  1146. extract($this->settings);
  1147. $this->Tree = new $modelClass();
  1148. $this->Tree->initialize(2, 2);
  1149. $result = $this->Tree->generateTreeList(
  1150. null,
  1151. "{n}.$modelClass.id",
  1152. array('%s - %s', "{n}.$modelClass.id", "{n}.$modelClass.name")
  1153. );
  1154. $this->assertEquals('1 - 1. Root', $result[1]);
  1155. $this->assertEquals('_2 - 1.1', $result[2]);
  1156. $this->assertEquals('__3 - 1.1.1', $result[3]);
  1157. }
  1158. /**
  1159. * testArraySyntax method
  1160. *
  1161. * @return void
  1162. */
  1163. public function testArraySyntax() {
  1164. extract($this->settings);
  1165. $this->Tree = new $modelClass();
  1166. $this->Tree->initialize(3, 3);
  1167. $this->assertSame($this->Tree->childCount(2), $this->Tree->childCount(array('id' => 2)));
  1168. $this->assertSame($this->Tree->getParentNode(2), $this->Tree->getParentNode(array('id' => 2)));
  1169. $this->assertSame($this->Tree->getPath(4), $this->Tree->getPath(array('id' => 4)));
  1170. }
  1171. /**
  1172. * testFindThreaded method
  1173. *
  1174. * @return void
  1175. */
  1176. public function testFindThreaded() {
  1177. $Model = new Person();
  1178. $Model->recursive = -1;
  1179. $Model->Behaviors->attach('Tree', array('parent' => 'mother_id'));
  1180. $result = $Model->find('threaded');
  1181. $expected = array(
  1182. array(
  1183. 'Person' => array(
  1184. 'id' => '4',
  1185. 'name' => 'mother - grand mother',
  1186. 'mother_id' => '0',
  1187. 'father_id' => '0'
  1188. ),
  1189. 'children' => array(
  1190. array(
  1191. 'Person' => array(
  1192. 'id' => '2',
  1193. 'name' => 'mother',
  1194. 'mother_id' => '4',
  1195. 'father_id' => '5'
  1196. ),
  1197. 'children' => array(
  1198. array(
  1199. 'Person' => array(
  1200. 'id' => '1',
  1201. 'name' => 'person',
  1202. 'mother_id' => '2',
  1203. 'father_id' => '3'
  1204. ),
  1205. 'children' => array()
  1206. )
  1207. )
  1208. )
  1209. )
  1210. ),
  1211. array(
  1212. 'Person' => array(
  1213. 'id' => '5',
  1214. 'name' => 'mother - grand father',
  1215. 'mother_id' => '0',
  1216. 'father_id' => '0'
  1217. ),
  1218. 'children' => array()
  1219. ),
  1220. array(
  1221. 'Person' => array(
  1222. 'id' => '6',
  1223. 'name' => 'father - grand mother',
  1224. 'mother_id' => '0',
  1225. 'father_id' => '0'
  1226. ),
  1227. 'children' => array(
  1228. array(
  1229. 'Person' => array(
  1230. 'id' => '3',
  1231. 'name' => 'father',
  1232. 'mother_id' => '6',
  1233. 'father_id' => '7'
  1234. ),
  1235. 'children' => array()
  1236. )
  1237. )
  1238. ),
  1239. array(
  1240. 'Person' => array(
  1241. 'id' => '7',
  1242. 'name' => 'father - grand father',
  1243. 'mother_id' => '0',
  1244. 'father_id' => '0'
  1245. ),
  1246. 'children' => array()
  1247. )
  1248. );
  1249. $this->assertEquals($expected, $result);
  1250. }
  1251. }