TreeBehaviorTest.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM\Behavior;
  16. use Cake\ORM\Entity;
  17. use Cake\ORM\TableRegistry;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Translate behavior test case
  21. */
  22. class TreeBehaviorTest extends TestCase
  23. {
  24. /**
  25. * fixtures
  26. *
  27. * @var array
  28. */
  29. public $fixtures = [
  30. 'core.menu_link_trees',
  31. 'core.number_trees'
  32. ];
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. $this->table = TableRegistry::get('NumberTrees');
  37. $this->table->primaryKey(['id']);
  38. $this->table->addBehavior('Tree');
  39. }
  40. public function tearDown()
  41. {
  42. parent::tearDown();
  43. TableRegistry::clear();
  44. }
  45. /**
  46. * Sanity test
  47. *
  48. * Make sure the assert method acts as you'd expect, this is the expected
  49. * initial db state
  50. *
  51. * @return void
  52. */
  53. public function testAssertMpttValues()
  54. {
  55. $expected = [
  56. ' 1:20 - 1:electronics',
  57. '_ 2: 9 - 2:televisions',
  58. '__ 3: 4 - 3:tube',
  59. '__ 5: 6 - 4:lcd',
  60. '__ 7: 8 - 5:plasma',
  61. '_10:19 - 6:portable',
  62. '__11:14 - 7:mp3',
  63. '___12:13 - 8:flash',
  64. '__15:16 - 9:cd',
  65. '__17:18 - 10:radios',
  66. '21:22 - 11:alien hardware'
  67. ];
  68. $this->assertMpttValues($expected, $this->table);
  69. $table = TableRegistry::get('MenuLinkTrees');
  70. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  71. $expected = [
  72. ' 1:10 - 1:Link 1',
  73. '_ 2: 3 - 2:Link 2',
  74. '_ 4: 9 - 3:Link 3',
  75. '__ 5: 8 - 4:Link 4',
  76. '___ 6: 7 - 5:Link 5',
  77. '11:14 - 6:Link 6',
  78. '_12:13 - 7:Link 7',
  79. '15:16 - 8:Link 8'
  80. ];
  81. $this->assertMpttValues($expected, $table);
  82. $table->removeBehavior('Tree');
  83. $table->addBehavior('Tree', ['scope' => ['menu' => 'categories']]);
  84. $expected = [
  85. ' 1:10 - 9:electronics',
  86. '_ 2: 9 - 10:televisions',
  87. '__ 3: 4 - 11:tube',
  88. '__ 5: 8 - 12:lcd',
  89. '___ 6: 7 - 13:plasma',
  90. '11:20 - 14:portable',
  91. '_12:15 - 15:mp3',
  92. '__13:14 - 16:flash',
  93. '_16:17 - 17:cd',
  94. '_18:19 - 18:radios'
  95. ];
  96. $this->assertMpttValues($expected, $table);
  97. }
  98. /**
  99. * Tests the find('path') method
  100. *
  101. * @return void
  102. */
  103. public function testFindPath()
  104. {
  105. $nodes = $this->table->find('path', ['for' => 9]);
  106. $this->assertEquals([1, 6, 9], $nodes->extract('id')->toArray());
  107. $nodes = $this->table->find('path', ['for' => 10]);
  108. $this->assertSame([1, 6, 10], $nodes->extract('id')->toArray());
  109. $nodes = $this->table->find('path', ['for' => 5]);
  110. $this->assertSame([1, 2, 5], $nodes->extract('id')->toArray());
  111. $nodes = $this->table->find('path', ['for' => 1]);
  112. $this->assertSame([1], $nodes->extract('id')->toArray());
  113. $entity = $this->table->newEntity(['name' => 'odd one', 'parent_id' => 1]);
  114. $entity = $this->table->save($entity);
  115. $newId = $entity->id;
  116. $entity = $this->table->get(2);
  117. $entity->parent_id = $newId;
  118. $this->table->save($entity);
  119. $nodes = $this->table->find('path', ['for' => 4]);
  120. $this->assertSame([1, $newId, 2, 4], $nodes->extract('id')->toArray());
  121. // find path with scope
  122. $table = TableRegistry::get('MenuLinkTrees');
  123. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  124. $nodes = $table->find('path', ['for' => 5]);
  125. $this->assertSame([1, 3, 4, 5], $nodes->extract('id')->toArray());
  126. }
  127. /**
  128. * Tests the childCount() method
  129. *
  130. * @return void
  131. */
  132. public function testChildCount()
  133. {
  134. // direct children for the root node
  135. $table = $this->table;
  136. $countDirect = $this->table->childCount($table->get(1), true);
  137. $this->assertEquals(2, $countDirect);
  138. // counts all the children of root
  139. $count = $this->table->childCount($table->get(1), false);
  140. $this->assertEquals(9, $count);
  141. // counts direct children
  142. $count = $this->table->childCount($table->get(2), false);
  143. $this->assertEquals(3, $count);
  144. // count children for a middle-node
  145. $count = $this->table->childCount($table->get(6), false);
  146. $this->assertEquals(4, $count);
  147. // count leaf children
  148. $count = $this->table->childCount($table->get(10), false);
  149. $this->assertEquals(0, $count);
  150. // test scoping
  151. $table = TableRegistry::get('MenuLinkTrees');
  152. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  153. $count = $table->childCount($table->get(3), false);
  154. $this->assertEquals(2, $count);
  155. }
  156. /**
  157. * Tests that childCount will provide the correct lft and rght values
  158. *
  159. * @return void
  160. */
  161. public function testChildCountNoTreeColumns()
  162. {
  163. $table = $this->table;
  164. $node = $table->get(6);
  165. $node->unsetProperty('lft');
  166. $node->unsetProperty('rght');
  167. $count = $this->table->childCount($node, false);
  168. $this->assertEquals(4, $count);
  169. }
  170. /**
  171. * Tests the childCount() plus callable scoping
  172. *
  173. * @return void
  174. */
  175. public function testCallableScoping()
  176. {
  177. $table = TableRegistry::get('MenuLinkTrees');
  178. $table->addBehavior('Tree', [
  179. 'scope' => function ($query) {
  180. return $query->where(['menu' => 'main-menu']);
  181. }
  182. ]);
  183. $count = $table->childCount($table->get(1), false);
  184. $this->assertEquals(4, $count);
  185. }
  186. /**
  187. * Tests the find('children') method
  188. *
  189. * @return void
  190. */
  191. public function testFindChildren()
  192. {
  193. $table = TableRegistry::get('MenuLinkTrees');
  194. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  195. // root
  196. $nodeIds = [];
  197. $nodes = $table->find('children', ['for' => 1])->all();
  198. $this->assertEquals([2, 3, 4, 5], $nodes->extract('id')->toArray());
  199. // leaf
  200. $nodeIds = [];
  201. $nodes = $table->find('children', ['for' => 5])->all();
  202. $this->assertEquals(0, count($nodes->extract('id')->toArray()));
  203. // direct children
  204. $nodes = $table->find('children', ['for' => 1, 'direct' => true])->all();
  205. $this->assertEquals([2, 3], $nodes->extract('id')->toArray());
  206. }
  207. /**
  208. * Tests that find('children') will throw an exception if the node was not found
  209. *
  210. * @expectedException \Cake\Datasource\Exception\RecordNotFoundException
  211. * @return void
  212. */
  213. public function testFindChildrenException()
  214. {
  215. $table = TableRegistry::get('MenuLinkTrees');
  216. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  217. $query = $table->find('children', ['for' => 500]);
  218. }
  219. /**
  220. * Tests the find('treeList') method
  221. *
  222. * @return void
  223. */
  224. public function testFindTreeList()
  225. {
  226. $table = TableRegistry::get('MenuLinkTrees');
  227. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  228. $result = $table->find('treeList')->toArray();
  229. $expected = [
  230. 1 => 'Link 1',
  231. 2 => '_Link 2',
  232. 3 => '_Link 3',
  233. 4 => '__Link 4',
  234. 5 => '___Link 5',
  235. 6 => 'Link 6',
  236. 7 => '_Link 7',
  237. 8 => 'Link 8'
  238. ];
  239. $this->assertEquals($expected, $result);
  240. }
  241. /**
  242. * Tests the find('treeList') method after moveUp, moveDown
  243. *
  244. * @return void
  245. */
  246. public function testFindTreeListAfterMove()
  247. {
  248. $table = TableRegistry::get('MenuLinkTrees');
  249. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  250. // moveUp
  251. $table->moveUp($table->get(3), 1);
  252. $expected = [
  253. ' 1:10 - 1:Link 1',
  254. '_ 2: 7 - 3:Link 3',
  255. '__ 3: 6 - 4:Link 4',
  256. '___ 4: 5 - 5:Link 5',
  257. '_ 8: 9 - 2:Link 2',
  258. '11:14 - 6:Link 6',
  259. '_12:13 - 7:Link 7',
  260. '15:16 - 8:Link 8'
  261. ];
  262. $this->assertMpttValues($expected, $table);
  263. // moveDown
  264. $table->moveDown($table->get(6), 1);
  265. $expected = [
  266. ' 1:10 - 1:Link 1',
  267. '_ 2: 7 - 3:Link 3',
  268. '__ 3: 6 - 4:Link 4',
  269. '___ 4: 5 - 5:Link 5',
  270. '_ 8: 9 - 2:Link 2',
  271. '11:12 - 8:Link 8',
  272. '13:16 - 6:Link 6',
  273. '_14:15 - 7:Link 7'
  274. ];
  275. $this->assertMpttValues($expected, $table);
  276. }
  277. /**
  278. * Tests the find('treeList') method with custom options
  279. *
  280. * @return void
  281. */
  282. public function testFindTreeListCustom()
  283. {
  284. $table = TableRegistry::get('MenuLinkTrees');
  285. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  286. $result = $table
  287. ->find('treeList', ['keyPath' => 'url', 'valuePath' => 'id', 'spacer' => ' '])
  288. ->toArray();
  289. $expected = [
  290. '/link1.html' => '1',
  291. 'http://example.com' => ' 2',
  292. '/what/even-more-links.html' => ' 3',
  293. '/lorem/ipsum.html' => ' 4',
  294. '/what/the.html' => ' 5',
  295. '/yeah/another-link.html' => '6',
  296. 'http://cakephp.org' => ' 7',
  297. '/page/who-we-are.html' => '8'
  298. ];
  299. $this->assertEquals($expected, $result);
  300. }
  301. /**
  302. * Tests the testFormatTreeListCustom() method.
  303. *
  304. * @return void
  305. */
  306. public function testFormatTreeListCustom()
  307. {
  308. $table = TableRegistry::get('MenuLinkTrees');
  309. $table->addBehavior('Tree');
  310. $query = $table
  311. ->find('threaded')
  312. ->where(['menu' => 'main-menu']);
  313. $options = ['keyPath' => 'url', 'valuePath' => 'id', 'spacer' => ' '];
  314. $result = $table->formatTreeList($query, $options)->toArray();
  315. $expected = [
  316. '/link1.html' => '1',
  317. 'http://example.com' => ' 2',
  318. '/what/even-more-links.html' => ' 3',
  319. '/lorem/ipsum.html' => ' 4',
  320. '/what/the.html' => ' 5',
  321. '/yeah/another-link.html' => '6',
  322. 'http://cakephp.org' => ' 7',
  323. '/page/who-we-are.html' => '8'
  324. ];
  325. $this->assertEquals($expected, $result);
  326. }
  327. /**
  328. * Tests the moveUp() method
  329. *
  330. * @return void
  331. */
  332. public function testMoveUp()
  333. {
  334. $table = TableRegistry::get('MenuLinkTrees');
  335. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  336. // top level, won't move
  337. $node = $this->table->moveUp($table->get(1), 10);
  338. $this->assertEquals(['lft' => 1, 'rght' => 10], $node->extract(['lft', 'rght']));
  339. $expected = [
  340. ' 1:10 - 1:Link 1',
  341. '_ 2: 3 - 2:Link 2',
  342. '_ 4: 9 - 3:Link 3',
  343. '__ 5: 8 - 4:Link 4',
  344. '___ 6: 7 - 5:Link 5',
  345. '11:14 - 6:Link 6',
  346. '_12:13 - 7:Link 7',
  347. '15:16 - 8:Link 8'
  348. ];
  349. $this->assertMpttValues($expected, $table);
  350. // edge cases
  351. $this->assertFalse($this->table->moveUp($table->get(1), 0));
  352. $this->assertFalse($this->table->moveUp($table->get(1), -10));
  353. $expected = [
  354. ' 1:10 - 1:Link 1',
  355. '_ 2: 3 - 2:Link 2',
  356. '_ 4: 9 - 3:Link 3',
  357. '__ 5: 8 - 4:Link 4',
  358. '___ 6: 7 - 5:Link 5',
  359. '11:14 - 6:Link 6',
  360. '_12:13 - 7:Link 7',
  361. '15:16 - 8:Link 8'
  362. ];
  363. $this->assertMpttValues($expected, $table);
  364. // move inner node
  365. $node = $table->moveUp($table->get(3), 1);
  366. $nodes = $table->find('children', ['for' => 1])->all();
  367. $this->assertEquals(['lft' => 2, 'rght' => 7], $node->extract(['lft', 'rght']));
  368. $expected = [
  369. ' 1:10 - 1:Link 1',
  370. '_ 2: 7 - 3:Link 3',
  371. '__ 3: 6 - 4:Link 4',
  372. '___ 4: 5 - 5:Link 5',
  373. '_ 8: 9 - 2:Link 2',
  374. '11:14 - 6:Link 6',
  375. '_12:13 - 7:Link 7',
  376. '15:16 - 8:Link 8'
  377. ];
  378. $this->assertMpttValues($expected, $table);
  379. }
  380. /**
  381. * Tests moving a node with no siblings
  382. *
  383. * @return void
  384. */
  385. public function testMoveLeaf()
  386. {
  387. $table = TableRegistry::get('MenuLinkTrees');
  388. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  389. $node = $table->moveUp($table->get(5), 1);
  390. $this->assertEquals(['lft' => 6, 'rght' => 7], $node->extract(['lft', 'rght']));
  391. $expected = [
  392. ' 1:10 - 1:Link 1',
  393. '_ 2: 3 - 2:Link 2',
  394. '_ 4: 9 - 3:Link 3',
  395. '__ 5: 8 - 4:Link 4',
  396. '___ 6: 7 - 5:Link 5',
  397. '11:14 - 6:Link 6',
  398. '_12:13 - 7:Link 7',
  399. '15:16 - 8:Link 8'
  400. ];
  401. $this->assertMpttValues($expected, $table);
  402. }
  403. /**
  404. * Tests moving a node to the top
  405. *
  406. * @return void
  407. */
  408. public function testMoveTop()
  409. {
  410. $table = TableRegistry::get('MenuLinkTrees');
  411. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  412. $node = $table->moveUp($table->get(8), true);
  413. $expected = [
  414. ' 1: 2 - 8:Link 8',
  415. ' 3:12 - 1:Link 1',
  416. '_ 4: 5 - 2:Link 2',
  417. '_ 6:11 - 3:Link 3',
  418. '__ 7:10 - 4:Link 4',
  419. '___ 8: 9 - 5:Link 5',
  420. '13:16 - 6:Link 6',
  421. '_14:15 - 7:Link 7'
  422. ];
  423. $this->assertMpttValues($expected, $table);
  424. }
  425. /**
  426. * Tests moving a node with no lft and rght
  427. *
  428. * @return void
  429. */
  430. public function testMoveNoTreeColumns()
  431. {
  432. $table = TableRegistry::get('MenuLinkTrees');
  433. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  434. $node = $table->get(8);
  435. $node->unsetProperty('lft');
  436. $node->unsetProperty('rght');
  437. $node = $table->moveUp($node, true);
  438. $this->assertEquals(['lft' => 1, 'rght' => 2], $node->extract(['lft', 'rght']));
  439. $expected = [
  440. ' 1: 2 - 8:Link 8',
  441. ' 3:12 - 1:Link 1',
  442. '_ 4: 5 - 2:Link 2',
  443. '_ 6:11 - 3:Link 3',
  444. '__ 7:10 - 4:Link 4',
  445. '___ 8: 9 - 5:Link 5',
  446. '13:16 - 6:Link 6',
  447. '_14:15 - 7:Link 7'
  448. ];
  449. $this->assertMpttValues($expected, $table);
  450. }
  451. /**
  452. * Tests the moveDown() method
  453. *
  454. * @return void
  455. */
  456. public function testMoveDown()
  457. {
  458. $table = TableRegistry::get('MenuLinkTrees');
  459. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  460. // latest node, won't move
  461. $node = $table->moveDown($table->get(8), 10);
  462. $this->assertEquals(['lft' => 15, 'rght' => 16], $node->extract(['lft', 'rght']));
  463. $expected = [
  464. ' 1:10 - 1:Link 1',
  465. '_ 2: 3 - 2:Link 2',
  466. '_ 4: 9 - 3:Link 3',
  467. '__ 5: 8 - 4:Link 4',
  468. '___ 6: 7 - 5:Link 5',
  469. '11:14 - 6:Link 6',
  470. '_12:13 - 7:Link 7',
  471. '15:16 - 8:Link 8'
  472. ];
  473. $this->assertMpttValues($expected, $table);
  474. // edge cases
  475. $this->assertFalse($this->table->moveDown($table->get(8), 0));
  476. $this->assertFalse($this->table->moveDown($table->get(8), -10));
  477. $expected = [
  478. ' 1:10 - 1:Link 1',
  479. '_ 2: 3 - 2:Link 2',
  480. '_ 4: 9 - 3:Link 3',
  481. '__ 5: 8 - 4:Link 4',
  482. '___ 6: 7 - 5:Link 5',
  483. '11:14 - 6:Link 6',
  484. '_12:13 - 7:Link 7',
  485. '15:16 - 8:Link 8'
  486. ];
  487. $this->assertMpttValues($expected, $table);
  488. // move inner node
  489. $node = $table->moveDown($table->get(2), 1);
  490. $this->assertEquals(['lft' => 8, 'rght' => 9], $node->extract(['lft', 'rght']));
  491. $expected = [
  492. ' 1:10 - 1:Link 1',
  493. '_ 2: 7 - 3:Link 3',
  494. '__ 3: 6 - 4:Link 4',
  495. '___ 4: 5 - 5:Link 5',
  496. '_ 8: 9 - 2:Link 2',
  497. '11:14 - 6:Link 6',
  498. '_12:13 - 7:Link 7',
  499. '15:16 - 8:Link 8'
  500. ];
  501. $this->assertMpttValues($expected, $table);
  502. }
  503. /**
  504. * Tests moving a node that has no siblings
  505. *
  506. * @return void
  507. */
  508. public function testMoveLeafDown()
  509. {
  510. $table = TableRegistry::get('MenuLinkTrees');
  511. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  512. $node = $table->moveDown($table->get(5), 1);
  513. $this->assertEquals(['lft' => 6, 'rght' => 7], $node->extract(['lft', 'rght']));
  514. $expected = [
  515. ' 1:10 - 1:Link 1',
  516. '_ 2: 3 - 2:Link 2',
  517. '_ 4: 9 - 3:Link 3',
  518. '__ 5: 8 - 4:Link 4',
  519. '___ 6: 7 - 5:Link 5',
  520. '11:14 - 6:Link 6',
  521. '_12:13 - 7:Link 7',
  522. '15:16 - 8:Link 8'
  523. ];
  524. $this->assertMpttValues($expected, $table);
  525. }
  526. /**
  527. * Tests moving a node to the bottom
  528. *
  529. * @return void
  530. */
  531. public function testMoveToBottom()
  532. {
  533. $table = TableRegistry::get('MenuLinkTrees');
  534. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  535. $node = $table->moveDown($table->get(1), true);
  536. $this->assertEquals(['lft' => 7, 'rght' => 16], $node->extract(['lft', 'rght']));
  537. $expected = [
  538. ' 1: 4 - 6:Link 6',
  539. '_ 2: 3 - 7:Link 7',
  540. ' 5: 6 - 8:Link 8',
  541. ' 7:16 - 1:Link 1',
  542. '_ 8: 9 - 2:Link 2',
  543. '_10:15 - 3:Link 3',
  544. '__11:14 - 4:Link 4',
  545. '___12:13 - 5:Link 5'
  546. ];
  547. $this->assertMpttValues($expected, $table);
  548. }
  549. /**
  550. * Tests moving a node with no lft and rght columns
  551. *
  552. * @return void
  553. */
  554. public function testMoveDownNoTreeColumns()
  555. {
  556. $table = TableRegistry::get('MenuLinkTrees');
  557. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  558. $node = $table->get(1);
  559. $node->unsetProperty('lft');
  560. $node->unsetProperty('rght');
  561. $node = $table->moveDown($node, true);
  562. $this->assertEquals(['lft' => 7, 'rght' => 16], $node->extract(['lft', 'rght']));
  563. $expected = [
  564. ' 1: 4 - 6:Link 6',
  565. '_ 2: 3 - 7:Link 7',
  566. ' 5: 6 - 8:Link 8',
  567. ' 7:16 - 1:Link 1',
  568. '_ 8: 9 - 2:Link 2',
  569. '_10:15 - 3:Link 3',
  570. '__11:14 - 4:Link 4',
  571. '___12:13 - 5:Link 5'
  572. ];
  573. $this->assertMpttValues($expected, $table);
  574. }
  575. public function testMoveDownMultiplePositions()
  576. {
  577. $node = $this->table->moveDown($this->table->get(3), 2);
  578. $this->assertEquals(['lft' => 7, 'rght' => 8], $node->extract(['lft', 'rght']));
  579. $expected = [
  580. ' 1:20 - 1:electronics',
  581. '_ 2: 9 - 2:televisions',
  582. '__ 3: 4 - 4:lcd',
  583. '__ 5: 6 - 5:plasma',
  584. '__ 7: 8 - 3:tube',
  585. '_10:19 - 6:portable',
  586. '__11:14 - 7:mp3',
  587. '___12:13 - 8:flash',
  588. '__15:16 - 9:cd',
  589. '__17:18 - 10:radios',
  590. '21:22 - 11:alien hardware'
  591. ];
  592. $this->assertMpttValues($expected, $this->table);
  593. }
  594. /**
  595. * Tests the recover function
  596. *
  597. * @return void
  598. */
  599. public function testRecover()
  600. {
  601. $table = $this->table;
  602. $expectedLevels = $table
  603. ->find('list', ['valueField' => 'depth'])
  604. ->order('lft')
  605. ->toArray();
  606. $table->updateAll(['lft' => null, 'rght' => null, 'depth' => null], []);
  607. $table->behaviors()->Tree->config('level', 'depth');
  608. $table->recover();
  609. $expected = [
  610. ' 1:20 - 1:electronics',
  611. '_ 2: 9 - 2:televisions',
  612. '__ 3: 4 - 3:tube',
  613. '__ 5: 6 - 4:lcd',
  614. '__ 7: 8 - 5:plasma',
  615. '_10:19 - 6:portable',
  616. '__11:14 - 7:mp3',
  617. '___12:13 - 8:flash',
  618. '__15:16 - 9:cd',
  619. '__17:18 - 10:radios',
  620. '21:22 - 11:alien hardware'
  621. ];
  622. $this->assertMpttValues($expected, $table);
  623. $result = $table
  624. ->find('list', ['valueField' => 'depth'])
  625. ->order('lft')
  626. ->toArray();
  627. $this->assertSame($expectedLevels, $result);
  628. }
  629. /**
  630. * Tests the recover function with a custom scope
  631. *
  632. * @return void
  633. */
  634. public function testRecoverScoped()
  635. {
  636. $table = TableRegistry::get('MenuLinkTrees');
  637. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  638. $table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
  639. $table->recover();
  640. $expected = [
  641. ' 1:10 - 1:Link 1',
  642. '_ 2: 3 - 2:Link 2',
  643. '_ 4: 9 - 3:Link 3',
  644. '__ 5: 8 - 4:Link 4',
  645. '___ 6: 7 - 5:Link 5',
  646. '11:14 - 6:Link 6',
  647. '_12:13 - 7:Link 7',
  648. '15:16 - 8:Link 8'
  649. ];
  650. $this->assertMpttValues($expected, $table);
  651. $table->removeBehavior('Tree');
  652. $table->addBehavior('Tree', ['scope' => ['menu' => 'categories']]);
  653. $expected = [
  654. ' 1:10 - 9:electronics',
  655. '_ 2: 9 - 10:televisions',
  656. '__ 3: 4 - 11:tube',
  657. '__ 5: 8 - 12:lcd',
  658. '___ 6: 7 - 13:plasma',
  659. '11:20 - 14:portable',
  660. '_12:15 - 15:mp3',
  661. '__13:14 - 16:flash',
  662. '_16:17 - 17:cd',
  663. '_18:19 - 18:radios'
  664. ];
  665. $this->assertMpttValues($expected, $table);
  666. }
  667. /**
  668. * Test recover function with a custom order clause
  669. *
  670. * @return void
  671. */
  672. public function testRecoverWithCustomOrder()
  673. {
  674. $table = TableRegistry::get('MenuLinkTrees');
  675. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu'], 'recoverOrder' => ['MenuLinkTrees.title' => 'desc']]);
  676. $table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
  677. $table->recover();
  678. $expected = [
  679. ' 1: 2 - 8:Link 8',
  680. ' 3: 6 - 6:Link 6',
  681. '_ 4: 5 - 7:Link 7',
  682. ' 7:16 - 1:Link 1',
  683. '_ 8:13 - 3:Link 3',
  684. '__ 9:12 - 4:Link 4',
  685. '___10:11 - 5:Link 5',
  686. '_14:15 - 2:Link 2'
  687. ];
  688. $this->assertMpttValues($expected, $table);
  689. }
  690. /**
  691. * Tests adding a new orphan node
  692. *
  693. * @return void
  694. */
  695. public function testAddOrphan()
  696. {
  697. $table = $this->table;
  698. $entity = new Entity(
  699. ['name' => 'New Orphan', 'parent_id' => null, 'level' => null],
  700. ['markNew' => true]
  701. );
  702. $this->assertSame($entity, $table->save($entity));
  703. $this->assertEquals(23, $entity->lft);
  704. $this->assertEquals(24, $entity->rght);
  705. $expected = [
  706. ' 1:20 - 1:electronics',
  707. '_ 2: 9 - 2:televisions',
  708. '__ 3: 4 - 3:tube',
  709. '__ 5: 6 - 4:lcd',
  710. '__ 7: 8 - 5:plasma',
  711. '_10:19 - 6:portable',
  712. '__11:14 - 7:mp3',
  713. '___12:13 - 8:flash',
  714. '__15:16 - 9:cd',
  715. '__17:18 - 10:radios',
  716. '21:22 - 11:alien hardware',
  717. '23:24 - 12:New Orphan',
  718. ];
  719. $this->assertMpttValues($expected, $this->table);
  720. }
  721. /**
  722. * Tests that adding a child node as a decendant of one of the roots works
  723. *
  724. * @return void
  725. */
  726. public function testAddMiddle()
  727. {
  728. $table = $this->table;
  729. $entity = new Entity(
  730. ['name' => 'laptops', 'parent_id' => 1],
  731. ['markNew' => true]
  732. );
  733. $this->assertSame($entity, $table->save($entity));
  734. $this->assertEquals(20, $entity->lft);
  735. $this->assertEquals(21, $entity->rght);
  736. $expected = [
  737. ' 1:22 - 1:electronics',
  738. '_ 2: 9 - 2:televisions',
  739. '__ 3: 4 - 3:tube',
  740. '__ 5: 6 - 4:lcd',
  741. '__ 7: 8 - 5:plasma',
  742. '_10:19 - 6:portable',
  743. '__11:14 - 7:mp3',
  744. '___12:13 - 8:flash',
  745. '__15:16 - 9:cd',
  746. '__17:18 - 10:radios',
  747. '_20:21 - 12:laptops',
  748. '23:24 - 11:alien hardware',
  749. ];
  750. $this->assertMpttValues($expected, $this->table);
  751. }
  752. /**
  753. * Tests adding a leaf to the tree
  754. *
  755. * @return void
  756. */
  757. public function testAddLeaf()
  758. {
  759. $table = $this->table;
  760. $entity = new Entity(
  761. ['name' => 'laptops', 'parent_id' => 2],
  762. ['markNew' => true]
  763. );
  764. $this->assertSame($entity, $table->save($entity));
  765. $this->assertEquals(9, $entity->lft);
  766. $this->assertEquals(10, $entity->rght);
  767. $expected = [
  768. ' 1:22 - 1:electronics',
  769. '_ 2:11 - 2:televisions',
  770. '__ 3: 4 - 3:tube',
  771. '__ 5: 6 - 4:lcd',
  772. '__ 7: 8 - 5:plasma',
  773. '__ 9:10 - 12:laptops',
  774. '_12:21 - 6:portable',
  775. '__13:16 - 7:mp3',
  776. '___14:15 - 8:flash',
  777. '__17:18 - 9:cd',
  778. '__19:20 - 10:radios',
  779. '23:24 - 11:alien hardware'
  780. ];
  781. $this->assertMpttValues($expected, $this->table);
  782. }
  783. /**
  784. * Tests adding a root element to the tree when all other root elements have children
  785. *
  786. * @return void
  787. */
  788. public function testAddRoot()
  789. {
  790. $table = $this->table;
  791. //First add a child to the empty root element
  792. $alien = $table->find()->where(['name' => 'alien hardware'])->first();
  793. $entity = new Entity(['name' => 'plasma rifle', 'parent_id' => $alien->id], ['markNew' => true]);
  794. $table->save($entity);
  795. $entity = new Entity(['name' => 'carpentry', 'parent_id' => null], ['markNew' => true]);
  796. $this->assertSame($entity, $table->save($entity));
  797. $this->assertEquals(25, $entity->lft);
  798. $this->assertEquals(26, $entity->rght);
  799. $expected = [
  800. ' 1:20 - 1:electronics',
  801. '_ 2: 9 - 2:televisions',
  802. '__ 3: 4 - 3:tube',
  803. '__ 5: 6 - 4:lcd',
  804. '__ 7: 8 - 5:plasma',
  805. '_10:19 - 6:portable',
  806. '__11:14 - 7:mp3',
  807. '___12:13 - 8:flash',
  808. '__15:16 - 9:cd',
  809. '__17:18 - 10:radios',
  810. '21:24 - 11:alien hardware',
  811. '_22:23 - 12:plasma rifle',
  812. '25:26 - 13:carpentry',
  813. ];
  814. $this->assertMpttValues($expected, $this->table);
  815. }
  816. /**
  817. * Tests making a node its own parent as an existing entity
  818. *
  819. * @expectedException RuntimeException
  820. * @expectedExceptionMessage Cannot set a node's parent as itself
  821. * @return void
  822. */
  823. public function testReParentSelf()
  824. {
  825. $entity = $this->table->get(1);
  826. $entity->parent_id = $entity->id;
  827. $this->table->save($entity);
  828. }
  829. /**
  830. * Tests making a node its own parent as a new entity.
  831. *
  832. * @expectedException RuntimeException
  833. * @expectedExceptionMessage Cannot set a node's parent as itself
  834. * @return void
  835. */
  836. public function testReParentSelfNewEntity()
  837. {
  838. $entity = $this->table->newEntity(['name' => 'root']);
  839. $entity->id = 1;
  840. $entity->parent_id = $entity->id;
  841. $this->table->save($entity);
  842. }
  843. /**
  844. * Tests moving a subtree to the right
  845. *
  846. * @return void
  847. */
  848. public function testReParentSubTreeRight()
  849. {
  850. $table = $this->table;
  851. $entity = $table->get(2);
  852. $entity->parent_id = 6;
  853. $this->assertSame($entity, $table->save($entity));
  854. $this->assertEquals(11, $entity->lft);
  855. $this->assertEquals(18, $entity->rght);
  856. $expected = [
  857. ' 1:20 - 1:electronics',
  858. '_ 2:19 - 6:portable',
  859. '__ 3: 6 - 7:mp3',
  860. '___ 4: 5 - 8:flash',
  861. '__ 7: 8 - 9:cd',
  862. '__ 9:10 - 10:radios',
  863. '__11:18 - 2:televisions',
  864. '___12:13 - 3:tube',
  865. '___14:15 - 4:lcd',
  866. '___16:17 - 5:plasma',
  867. '21:22 - 11:alien hardware'
  868. ];
  869. $this->assertMpttValues($expected, $table);
  870. }
  871. /**
  872. * Tests moving a subtree to the left
  873. *
  874. * @return void
  875. */
  876. public function testReParentSubTreeLeft()
  877. {
  878. $table = $this->table;
  879. $entity = $table->get(6);
  880. $entity->parent_id = 2;
  881. $this->assertSame($entity, $table->save($entity));
  882. $this->assertEquals(9, $entity->lft);
  883. $this->assertEquals(18, $entity->rght);
  884. $expected = [
  885. ' 1:20 - 1:electronics',
  886. '_ 2:19 - 2:televisions',
  887. '__ 3: 4 - 3:tube',
  888. '__ 5: 6 - 4:lcd',
  889. '__ 7: 8 - 5:plasma',
  890. '__ 9:18 - 6:portable',
  891. '___10:13 - 7:mp3',
  892. '____11:12 - 8:flash',
  893. '___14:15 - 9:cd',
  894. '___16:17 - 10:radios',
  895. '21:22 - 11:alien hardware'
  896. ];
  897. $this->assertMpttValues($expected, $this->table);
  898. }
  899. /**
  900. * Test moving a leaft to the left
  901. *
  902. * @return void
  903. */
  904. public function testReParentLeafLeft()
  905. {
  906. $table = $this->table;
  907. $entity = $table->get(10);
  908. $entity->parent_id = 2;
  909. $this->assertSame($entity, $table->save($entity));
  910. $this->assertEquals(9, $entity->lft);
  911. $this->assertEquals(10, $entity->rght);
  912. $expected = [
  913. ' 1:20 - 1:electronics',
  914. '_ 2:11 - 2:televisions',
  915. '__ 3: 4 - 3:tube',
  916. '__ 5: 6 - 4:lcd',
  917. '__ 7: 8 - 5:plasma',
  918. '__ 9:10 - 10:radios',
  919. '_12:19 - 6:portable',
  920. '__13:16 - 7:mp3',
  921. '___14:15 - 8:flash',
  922. '__17:18 - 9:cd',
  923. '21:22 - 11:alien hardware'
  924. ];
  925. $this->assertMpttValues($expected, $this->table);
  926. }
  927. /**
  928. * Test moving a leaf to the left
  929. *
  930. * @return void
  931. */
  932. public function testReParentLeafRight()
  933. {
  934. $table = $this->table;
  935. $entity = $table->get(5);
  936. $entity->parent_id = 6;
  937. $this->assertSame($entity, $table->save($entity));
  938. $this->assertEquals(17, $entity->lft);
  939. $this->assertEquals(18, $entity->rght);
  940. $result = $table->find()->order('lft')->hydrate(false);
  941. $expected = [
  942. ' 1:20 - 1:electronics',
  943. '_ 2: 7 - 2:televisions',
  944. '__ 3: 4 - 3:tube',
  945. '__ 5: 6 - 4:lcd',
  946. '_ 8:19 - 6:portable',
  947. '__ 9:12 - 7:mp3',
  948. '___10:11 - 8:flash',
  949. '__13:14 - 9:cd',
  950. '__15:16 - 10:radios',
  951. '__17:18 - 5:plasma',
  952. '21:22 - 11:alien hardware'
  953. ];
  954. $this->assertMpttValues($expected, $table);
  955. }
  956. /**
  957. * Tests moving a subtree with a node having no lft and rght columns
  958. *
  959. * @return void
  960. */
  961. public function testReParentNoTreeColumns()
  962. {
  963. $table = $this->table;
  964. $entity = $table->get(6);
  965. $entity->unsetProperty('lft');
  966. $entity->unsetProperty('rght');
  967. $entity->parent_id = 2;
  968. $this->assertSame($entity, $table->save($entity));
  969. $this->assertEquals(9, $entity->lft);
  970. $this->assertEquals(18, $entity->rght);
  971. $expected = [
  972. ' 1:20 - 1:electronics',
  973. '_ 2:19 - 2:televisions',
  974. '__ 3: 4 - 3:tube',
  975. '__ 5: 6 - 4:lcd',
  976. '__ 7: 8 - 5:plasma',
  977. '__ 9:18 - 6:portable',
  978. '___10:13 - 7:mp3',
  979. '____11:12 - 8:flash',
  980. '___14:15 - 9:cd',
  981. '___16:17 - 10:radios',
  982. '21:22 - 11:alien hardware'
  983. ];
  984. $this->assertMpttValues($expected, $this->table);
  985. }
  986. /**
  987. * Tests moving a subtree as a new root
  988. *
  989. * @return void
  990. */
  991. public function testRootingSubTree()
  992. {
  993. $table = $this->table;
  994. $entity = $table->get(2);
  995. $entity->parent_id = null;
  996. $this->assertSame($entity, $table->save($entity));
  997. $this->assertEquals(15, $entity->lft);
  998. $this->assertEquals(22, $entity->rght);
  999. $expected = [
  1000. ' 1:12 - 1:electronics',
  1001. '_ 2:11 - 6:portable',
  1002. '__ 3: 6 - 7:mp3',
  1003. '___ 4: 5 - 8:flash',
  1004. '__ 7: 8 - 9:cd',
  1005. '__ 9:10 - 10:radios',
  1006. '13:14 - 11:alien hardware',
  1007. '15:22 - 2:televisions',
  1008. '_16:17 - 3:tube',
  1009. '_18:19 - 4:lcd',
  1010. '_20:21 - 5:plasma'
  1011. ];
  1012. $this->assertMpttValues($expected, $table);
  1013. }
  1014. /**
  1015. * Tests moving a subtree with no tree columns
  1016. *
  1017. * @return void
  1018. */
  1019. public function testRootingNoTreeColumns()
  1020. {
  1021. $table = $this->table;
  1022. $entity = $table->get(2);
  1023. $entity->unsetProperty('lft');
  1024. $entity->unsetProperty('rght');
  1025. $entity->parent_id = null;
  1026. $this->assertSame($entity, $table->save($entity));
  1027. $this->assertEquals(15, $entity->lft);
  1028. $this->assertEquals(22, $entity->rght);
  1029. $expected = [
  1030. ' 1:12 - 1:electronics',
  1031. '_ 2:11 - 6:portable',
  1032. '__ 3: 6 - 7:mp3',
  1033. '___ 4: 5 - 8:flash',
  1034. '__ 7: 8 - 9:cd',
  1035. '__ 9:10 - 10:radios',
  1036. '13:14 - 11:alien hardware',
  1037. '15:22 - 2:televisions',
  1038. '_16:17 - 3:tube',
  1039. '_18:19 - 4:lcd',
  1040. '_20:21 - 5:plasma'
  1041. ];
  1042. $this->assertMpttValues($expected, $table);
  1043. }
  1044. /**
  1045. * Tests that trying to create a cycle throws an exception
  1046. *
  1047. * @expectedException \RuntimeException
  1048. * @expectedExceptionMessage Cannot use node "5" as parent for entity "2"
  1049. * @return void
  1050. */
  1051. public function testReparentCycle()
  1052. {
  1053. $table = $this->table;
  1054. $entity = $table->get(2);
  1055. $entity->parent_id = 5;
  1056. $table->save($entity);
  1057. }
  1058. /**
  1059. * Tests deleting a leaf in the tree
  1060. *
  1061. * @return void
  1062. */
  1063. public function testDeleteLeaf()
  1064. {
  1065. $table = $this->table;
  1066. $entity = $table->get(4);
  1067. $this->assertTrue($table->delete($entity));
  1068. $expected = [
  1069. ' 1:18 - 1:electronics',
  1070. '_ 2: 7 - 2:televisions',
  1071. '__ 3: 4 - 3:tube',
  1072. '__ 5: 6 - 5:plasma',
  1073. '_ 8:17 - 6:portable',
  1074. '__ 9:12 - 7:mp3',
  1075. '___10:11 - 8:flash',
  1076. '__13:14 - 9:cd',
  1077. '__15:16 - 10:radios',
  1078. '19:20 - 11:alien hardware'
  1079. ];
  1080. $this->assertMpttValues($expected, $this->table);
  1081. }
  1082. /**
  1083. * Tests deleting a subtree
  1084. *
  1085. * @return void
  1086. */
  1087. public function testDeleteSubTree()
  1088. {
  1089. $table = $this->table;
  1090. $entity = $table->get(6);
  1091. $this->assertTrue($table->delete($entity));
  1092. $expected = [
  1093. ' 1:10 - 1:electronics',
  1094. '_ 2: 9 - 2:televisions',
  1095. '__ 3: 4 - 3:tube',
  1096. '__ 5: 6 - 4:lcd',
  1097. '__ 7: 8 - 5:plasma',
  1098. '11:12 - 11:alien hardware'
  1099. ];
  1100. $this->assertMpttValues($expected, $this->table);
  1101. }
  1102. /**
  1103. * Test deleting a root node
  1104. *
  1105. * @return void
  1106. */
  1107. public function testDeleteRoot()
  1108. {
  1109. $table = $this->table;
  1110. $entity = $table->get(1);
  1111. $this->assertTrue($table->delete($entity));
  1112. $expected = [
  1113. ' 1: 2 - 11:alien hardware'
  1114. ];
  1115. $this->assertMpttValues($expected, $this->table);
  1116. }
  1117. /**
  1118. * Test deleting a node with no tree columns
  1119. *
  1120. * @return void
  1121. */
  1122. public function testDeleteRootNoTreeColumns()
  1123. {
  1124. $table = $this->table;
  1125. $entity = $table->get(1);
  1126. $entity->unsetProperty('lft');
  1127. $entity->unsetProperty('rght');
  1128. $this->assertTrue($table->delete($entity));
  1129. $expected = [
  1130. ' 1: 2 - 11:alien hardware'
  1131. ];
  1132. $this->assertMpttValues($expected, $this->table);
  1133. }
  1134. /**
  1135. * Tests that a leaf can be taken out of the tree and put in as a root
  1136. *
  1137. * @return void
  1138. */
  1139. public function testRemoveFromLeafFromTree()
  1140. {
  1141. $table = $this->table;
  1142. $entity = $table->get(10);
  1143. $this->assertSame($entity, $table->removeFromTree($entity));
  1144. $this->assertEquals(21, $entity->lft);
  1145. $this->assertEquals(22, $entity->rght);
  1146. $this->assertEquals(null, $entity->parent_id);
  1147. $result = $table->find()->order('lft')->hydrate(false);
  1148. $expected = [
  1149. ' 1:18 - 1:electronics',
  1150. '_ 2: 9 - 2:televisions',
  1151. '__ 3: 4 - 3:tube',
  1152. '__ 5: 6 - 4:lcd',
  1153. '__ 7: 8 - 5:plasma',
  1154. '_10:17 - 6:portable',
  1155. '__11:14 - 7:mp3',
  1156. '___12:13 - 8:flash',
  1157. '__15:16 - 9:cd',
  1158. '19:20 - 11:alien hardware',
  1159. '21:22 - 10:radios'
  1160. ];
  1161. $this->assertMpttValues($expected, $table);
  1162. }
  1163. /**
  1164. * Test removing a middle node from a tree
  1165. *
  1166. * @return void
  1167. */
  1168. public function testRemoveMiddleNodeFromTree()
  1169. {
  1170. $table = $this->table;
  1171. $entity = $table->get(6);
  1172. $this->assertSame($entity, $table->removeFromTree($entity));
  1173. $result = $table->find('threaded')->order('lft')->hydrate(false)->toArray();
  1174. $this->assertEquals(21, $entity->lft);
  1175. $this->assertEquals(22, $entity->rght);
  1176. $this->assertEquals(null, $entity->parent_id);
  1177. $result = $table->find()->order('lft')->hydrate(false);
  1178. $expected = [
  1179. ' 1:18 - 1:electronics',
  1180. '_ 2: 9 - 2:televisions',
  1181. '__ 3: 4 - 3:tube',
  1182. '__ 5: 6 - 4:lcd',
  1183. '__ 7: 8 - 5:plasma',
  1184. '_10:13 - 7:mp3',
  1185. '__11:12 - 8:flash',
  1186. '_14:15 - 9:cd',
  1187. '_16:17 - 10:radios',
  1188. '19:20 - 11:alien hardware',
  1189. '21:22 - 6:portable'
  1190. ];
  1191. $this->assertMpttValues($expected, $table);
  1192. }
  1193. /**
  1194. * Tests removing the root of a tree
  1195. *
  1196. * @return void
  1197. */
  1198. public function testRemoveRootFromTree()
  1199. {
  1200. $table = $this->table;
  1201. $entity = $table->get(1);
  1202. $this->assertSame($entity, $table->removeFromTree($entity));
  1203. $result = $table->find('threaded')->order('lft')->hydrate(false)->toArray();
  1204. $this->assertEquals(21, $entity->lft);
  1205. $this->assertEquals(22, $entity->rght);
  1206. $this->assertEquals(null, $entity->parent_id);
  1207. $expected = [
  1208. ' 1: 8 - 2:televisions',
  1209. '_ 2: 3 - 3:tube',
  1210. '_ 4: 5 - 4:lcd',
  1211. '_ 6: 7 - 5:plasma',
  1212. ' 9:18 - 6:portable',
  1213. '_10:13 - 7:mp3',
  1214. '__11:12 - 8:flash',
  1215. '_14:15 - 9:cd',
  1216. '_16:17 - 10:radios',
  1217. '19:20 - 11:alien hardware',
  1218. '21:22 - 1:electronics'
  1219. ];
  1220. $this->assertMpttValues($expected, $table);
  1221. }
  1222. /**
  1223. * Tests that using associations having tree fields in the schema
  1224. * does not generate SQL errors
  1225. *
  1226. * @return void
  1227. */
  1228. public function testFindPathWithAssociation()
  1229. {
  1230. $table = $this->table;
  1231. $other = TableRegistry::get('FriendlyTrees', [
  1232. 'table' => $table->table()
  1233. ]);
  1234. $table->hasOne('FriendlyTrees', [
  1235. 'foreignKey' => 'id'
  1236. ]);
  1237. $result = $table
  1238. ->find('children', ['for' => 1])
  1239. ->contain('FriendlyTrees')
  1240. ->toArray();
  1241. $this->assertCount(9, $result);
  1242. }
  1243. /**
  1244. * Tests getting the depth level of a node in the tree.
  1245. *
  1246. * @return void
  1247. */
  1248. public function testGetLevel()
  1249. {
  1250. $entity = $this->table->get(8);
  1251. $result = $this->table->getLevel($entity);
  1252. $this->assertEquals(3, $result);
  1253. $result = $this->table->getLevel($entity->id);
  1254. $this->assertEquals(3, $result);
  1255. $result = $this->table->getLevel(5);
  1256. $this->assertEquals(2, $result);
  1257. $result = $this->table->getLevel(99999);
  1258. $this->assertFalse($result);
  1259. }
  1260. /**
  1261. * Test setting level for new nodes
  1262. *
  1263. * @return void
  1264. */
  1265. public function testSetLevelNewNode()
  1266. {
  1267. $this->table->behaviors()->Tree->config('level', 'depth');
  1268. $entity = new Entity(['parent_id' => null, 'name' => 'Depth 0']);
  1269. $this->table->save($entity);
  1270. $entity = $this->table->get(12);
  1271. $this->assertEquals(0, $entity->depth);
  1272. $entity = new Entity(['parent_id' => 1, 'name' => 'Depth 1']);
  1273. $this->table->save($entity);
  1274. $entity = $this->table->get(13);
  1275. $this->assertEquals(1, $entity->depth);
  1276. $entity = new Entity(['parent_id' => 8, 'name' => 'Depth 4']);
  1277. $this->table->save($entity);
  1278. $entity = $this->table->get(14);
  1279. $this->assertEquals(4, $entity->depth);
  1280. }
  1281. /**
  1282. * Test setting level for existing nodes
  1283. *
  1284. * @return void
  1285. */
  1286. public function testSetLevelExistingNode()
  1287. {
  1288. $this->table->behaviors()->Tree->config('level', 'depth');
  1289. // Leaf node
  1290. $entity = $this->table->get(4);
  1291. $this->assertEquals(2, $entity->depth);
  1292. $this->table->save($entity);
  1293. $entity = $this->table->get(4);
  1294. $this->assertEquals(2, $entity->depth);
  1295. // Non leaf node so depth of descendents will also change
  1296. $entity = $this->table->get(6);
  1297. $this->assertEquals(1, $entity->depth);
  1298. $entity->parent_id = null;
  1299. $this->table->save($entity);
  1300. $entity = $this->table->get(6);
  1301. $this->assertEquals(0, $entity->depth);
  1302. $entity = $this->table->get(7);
  1303. $this->assertEquals(1, $entity->depth);
  1304. $entity = $this->table->get(8);
  1305. $this->assertEquals(2, $entity->depth);
  1306. $entity->parent_id = 6;
  1307. $this->table->save($entity);
  1308. $entity = $this->table->get(8);
  1309. $this->assertEquals(1, $entity->depth);
  1310. }
  1311. /**
  1312. * Assert MPTT values
  1313. *
  1314. * Custom assert method to make identifying the differences between expected
  1315. * and actual db state easier to identify.
  1316. *
  1317. * @param array $expected tree state to be expected
  1318. * @param \Cake\ORM\Table $table Table instance
  1319. * @param \Cake\ORM\Query $query Optional query object
  1320. * @return void
  1321. */
  1322. public function assertMpttValues($expected, $table, $query = null)
  1323. {
  1324. $query = $query ?: $table->find();
  1325. $primaryKey = $table->primaryKey();
  1326. if (is_array($primaryKey)) {
  1327. $primaryKey = $primaryKey[0];
  1328. }
  1329. $displayField = $table->displayField();
  1330. $options = [
  1331. 'valuePath' => function ($item, $key, $iterator) use ($primaryKey, $displayField) {
  1332. return sprintf(
  1333. '%s:%s - %s:%s',
  1334. str_pad($item->lft, 2, ' ', STR_PAD_LEFT),
  1335. str_pad($item->rght, 2, ' ', STR_PAD_LEFT),
  1336. str_pad($item->$primaryKey, 2, ' ', STR_PAD_LEFT),
  1337. $item->{$displayField}
  1338. );
  1339. }
  1340. ];
  1341. $result = array_values($query->find('treeList', $options)->toArray());
  1342. if (count($result) === count($expected)) {
  1343. $subExpected = array_diff($expected, $result);
  1344. if ($subExpected) {
  1345. $subResult = array_intersect_key($result, $subExpected);
  1346. $this->assertSame($subExpected, $subResult, 'Differences in the tree were found (lft:rght id:display-name)');
  1347. }
  1348. }
  1349. $this->assertSame($expected, $result, 'The tree is not the same (lft:rght id:display-name)');
  1350. }
  1351. }