TreeBehaviorTest.php 47 KB

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