TreeBehaviorTest.php 46 KB

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