TreeBehaviorTest.php 41 KB

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