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