TreeBehaviorTest.php 46 KB

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