TreeBehaviorTest.php 46 KB

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