TreeBehaviorTest.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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 moveUp() method
  298. *
  299. * @return void
  300. */
  301. public function testMoveUp()
  302. {
  303. $table = TableRegistry::get('MenuLinkTrees');
  304. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  305. // top level, won't move
  306. $node = $this->table->moveUp($table->get(1), 10);
  307. $this->assertEquals(['lft' => 1, 'rght' => 10], $node->extract(['lft', 'rght']));
  308. $expected = [
  309. ' 1:10 - 1:Link 1',
  310. '_ 2: 3 - 2:Link 2',
  311. '_ 4: 9 - 3:Link 3',
  312. '__ 5: 8 - 4:Link 4',
  313. '___ 6: 7 - 5:Link 5',
  314. '11:14 - 6:Link 6',
  315. '_12:13 - 7:Link 7',
  316. '15:16 - 8:Link 8'
  317. ];
  318. $this->assertMpttValues($expected, $table);
  319. // edge cases
  320. $this->assertFalse($this->table->moveUp($table->get(1), 0));
  321. $this->assertFalse($this->table->moveUp($table->get(1), -10));
  322. $expected = [
  323. ' 1:10 - 1:Link 1',
  324. '_ 2: 3 - 2:Link 2',
  325. '_ 4: 9 - 3:Link 3',
  326. '__ 5: 8 - 4:Link 4',
  327. '___ 6: 7 - 5:Link 5',
  328. '11:14 - 6:Link 6',
  329. '_12:13 - 7:Link 7',
  330. '15:16 - 8:Link 8'
  331. ];
  332. $this->assertMpttValues($expected, $table);
  333. // move inner node
  334. $node = $table->moveUp($table->get(3), 1);
  335. $nodes = $table->find('children', ['for' => 1])->all();
  336. $this->assertEquals(['lft' => 2, 'rght' => 7], $node->extract(['lft', 'rght']));
  337. $expected = [
  338. ' 1:10 - 1:Link 1',
  339. '_ 2: 7 - 3:Link 3',
  340. '__ 3: 6 - 4:Link 4',
  341. '___ 4: 5 - 5:Link 5',
  342. '_ 8: 9 - 2:Link 2',
  343. '11:14 - 6:Link 6',
  344. '_12:13 - 7:Link 7',
  345. '15:16 - 8:Link 8'
  346. ];
  347. $this->assertMpttValues($expected, $table);
  348. }
  349. /**
  350. * Tests moving a node with no siblings
  351. *
  352. * @return void
  353. */
  354. public function testMoveLeaf()
  355. {
  356. $table = TableRegistry::get('MenuLinkTrees');
  357. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  358. $node = $table->moveUp($table->get(5), 1);
  359. $this->assertEquals(['lft' => 6, 'rght' => 7], $node->extract(['lft', 'rght']));
  360. $expected = [
  361. ' 1:10 - 1:Link 1',
  362. '_ 2: 3 - 2:Link 2',
  363. '_ 4: 9 - 3:Link 3',
  364. '__ 5: 8 - 4:Link 4',
  365. '___ 6: 7 - 5:Link 5',
  366. '11:14 - 6:Link 6',
  367. '_12:13 - 7:Link 7',
  368. '15:16 - 8:Link 8'
  369. ];
  370. $this->assertMpttValues($expected, $table);
  371. }
  372. /**
  373. * Tests moving a node to the top
  374. *
  375. * @return void
  376. */
  377. public function testMoveTop()
  378. {
  379. $table = TableRegistry::get('MenuLinkTrees');
  380. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  381. $node = $table->moveUp($table->get(8), true);
  382. $expected = [
  383. ' 1: 2 - 8:Link 8',
  384. ' 3:12 - 1:Link 1',
  385. '_ 4: 5 - 2:Link 2',
  386. '_ 6:11 - 3:Link 3',
  387. '__ 7:10 - 4:Link 4',
  388. '___ 8: 9 - 5:Link 5',
  389. '13:16 - 6:Link 6',
  390. '_14:15 - 7:Link 7'
  391. ];
  392. $this->assertMpttValues($expected, $table);
  393. }
  394. /**
  395. * Tests moving a node with no lft and rght
  396. *
  397. * @return void
  398. */
  399. public function testMoveNoTreeColumns()
  400. {
  401. $table = TableRegistry::get('MenuLinkTrees');
  402. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  403. $node = $table->get(8);
  404. $node->unsetProperty('lft');
  405. $node->unsetProperty('rght');
  406. $node = $table->moveUp($node, true);
  407. $this->assertEquals(['lft' => 1, 'rght' => 2], $node->extract(['lft', 'rght']));
  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 the moveDown() method
  422. *
  423. * @return void
  424. */
  425. public function testMoveDown()
  426. {
  427. $table = TableRegistry::get('MenuLinkTrees');
  428. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  429. // latest node, won't move
  430. $node = $table->moveDown($table->get(8), 10);
  431. $this->assertEquals(['lft' => 15, 'rght' => 16], $node->extract(['lft', 'rght']));
  432. $expected = [
  433. ' 1:10 - 1:Link 1',
  434. '_ 2: 3 - 2:Link 2',
  435. '_ 4: 9 - 3:Link 3',
  436. '__ 5: 8 - 4:Link 4',
  437. '___ 6: 7 - 5:Link 5',
  438. '11:14 - 6:Link 6',
  439. '_12:13 - 7:Link 7',
  440. '15:16 - 8:Link 8'
  441. ];
  442. $this->assertMpttValues($expected, $table);
  443. // edge cases
  444. $this->assertFalse($this->table->moveDown($table->get(8), 0));
  445. $this->assertFalse($this->table->moveDown($table->get(8), -10));
  446. $expected = [
  447. ' 1:10 - 1:Link 1',
  448. '_ 2: 3 - 2:Link 2',
  449. '_ 4: 9 - 3:Link 3',
  450. '__ 5: 8 - 4:Link 4',
  451. '___ 6: 7 - 5:Link 5',
  452. '11:14 - 6:Link 6',
  453. '_12:13 - 7:Link 7',
  454. '15:16 - 8:Link 8'
  455. ];
  456. $this->assertMpttValues($expected, $table);
  457. // move inner node
  458. $node = $table->moveDown($table->get(2), 1);
  459. $this->assertEquals(['lft' => 8, 'rght' => 9], $node->extract(['lft', 'rght']));
  460. $expected = [
  461. ' 1:10 - 1:Link 1',
  462. '_ 2: 7 - 3:Link 3',
  463. '__ 3: 6 - 4:Link 4',
  464. '___ 4: 5 - 5:Link 5',
  465. '_ 8: 9 - 2:Link 2',
  466. '11:14 - 6:Link 6',
  467. '_12:13 - 7:Link 7',
  468. '15:16 - 8:Link 8'
  469. ];
  470. $this->assertMpttValues($expected, $table);
  471. }
  472. /**
  473. * Tests moving a node that has no siblings
  474. *
  475. * @return void
  476. */
  477. public function testMoveLeafDown()
  478. {
  479. $table = TableRegistry::get('MenuLinkTrees');
  480. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  481. $node = $table->moveDown($table->get(5), 1);
  482. $this->assertEquals(['lft' => 6, 'rght' => 7], $node->extract(['lft', 'rght']));
  483. $expected = [
  484. ' 1:10 - 1:Link 1',
  485. '_ 2: 3 - 2:Link 2',
  486. '_ 4: 9 - 3:Link 3',
  487. '__ 5: 8 - 4:Link 4',
  488. '___ 6: 7 - 5:Link 5',
  489. '11:14 - 6:Link 6',
  490. '_12:13 - 7:Link 7',
  491. '15:16 - 8:Link 8'
  492. ];
  493. $this->assertMpttValues($expected, $table);
  494. }
  495. /**
  496. * Tests moving a node to the bottom
  497. *
  498. * @return void
  499. */
  500. public function testMoveToBottom()
  501. {
  502. $table = TableRegistry::get('MenuLinkTrees');
  503. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  504. $node = $table->moveDown($table->get(1), true);
  505. $this->assertEquals(['lft' => 7, 'rght' => 16], $node->extract(['lft', 'rght']));
  506. $expected = [
  507. ' 1: 4 - 6:Link 6',
  508. '_ 2: 3 - 7:Link 7',
  509. ' 5: 6 - 8:Link 8',
  510. ' 7:16 - 1:Link 1',
  511. '_ 8: 9 - 2:Link 2',
  512. '_10:15 - 3:Link 3',
  513. '__11:14 - 4:Link 4',
  514. '___12:13 - 5:Link 5'
  515. ];
  516. $this->assertMpttValues($expected, $table);
  517. }
  518. /**
  519. * Tests moving a node with no lft and rght columns
  520. *
  521. * @return void
  522. */
  523. public function testMoveDownNoTreeColumns()
  524. {
  525. $table = TableRegistry::get('MenuLinkTrees');
  526. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  527. $node = $table->get(1);
  528. $node->unsetProperty('lft');
  529. $node->unsetProperty('rght');
  530. $node = $table->moveDown($node, 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. public function testMoveDownMultiplePositions()
  545. {
  546. $node = $this->table->moveDown($this->table->get(3), 2);
  547. $this->assertEquals(['lft' => 7, 'rght' => 8], $node->extract(['lft', 'rght']));
  548. $expected = [
  549. ' 1:20 - 1:electronics',
  550. '_ 2: 9 - 2:televisions',
  551. '__ 3: 4 - 4:lcd',
  552. '__ 5: 6 - 5:plasma',
  553. '__ 7: 8 - 3:tube',
  554. '_10:19 - 6:portable',
  555. '__11:14 - 7:mp3',
  556. '___12:13 - 8:flash',
  557. '__15:16 - 9:cd',
  558. '__17:18 - 10:radios',
  559. '21:22 - 11:alien hardware'
  560. ];
  561. $this->assertMpttValues($expected, $this->table);
  562. }
  563. /**
  564. * Tests the recover function
  565. *
  566. * @return void
  567. */
  568. public function testRecover()
  569. {
  570. $table = $this->table;
  571. $expectedLevels = $table
  572. ->find('list', ['valueField' => 'depth'])
  573. ->order('lft')
  574. ->toArray();
  575. $table->updateAll(['lft' => null, 'rght' => null, 'depth' => null], []);
  576. $table->behaviors()->Tree->config('level', 'depth');
  577. $table->recover();
  578. $expected = [
  579. ' 1:20 - 1:electronics',
  580. '_ 2: 9 - 2:televisions',
  581. '__ 3: 4 - 3:tube',
  582. '__ 5: 6 - 4:lcd',
  583. '__ 7: 8 - 5:plasma',
  584. '_10:19 - 6:portable',
  585. '__11:14 - 7:mp3',
  586. '___12:13 - 8:flash',
  587. '__15:16 - 9:cd',
  588. '__17:18 - 10:radios',
  589. '21:22 - 11:alien hardware'
  590. ];
  591. $this->assertMpttValues($expected, $table);
  592. $result = $table
  593. ->find('list', ['valueField' => 'depth'])
  594. ->order('lft')
  595. ->toArray();
  596. $this->assertSame($expectedLevels, $result);
  597. }
  598. /**
  599. * Tests the recover function with a custom scope
  600. *
  601. * @return void
  602. */
  603. public function testRecoverScoped()
  604. {
  605. $table = TableRegistry::get('MenuLinkTrees');
  606. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  607. $expected = $table->find()
  608. ->where(['menu' => 'main-menu'])
  609. ->order('lft')
  610. ->hydrate(false)
  611. ->toArray();
  612. $table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
  613. $table->recover();
  614. $expected = [
  615. ' 1:10 - 1:Link 1',
  616. '_ 2: 3 - 2:Link 2',
  617. '_ 4: 9 - 3:Link 3',
  618. '__ 5: 8 - 4:Link 4',
  619. '___ 6: 7 - 5:Link 5',
  620. '11:14 - 6:Link 6',
  621. '_12:13 - 7:Link 7',
  622. '15:16 - 8:Link 8'
  623. ];
  624. $this->assertMpttValues($expected, $table);
  625. $table->removeBehavior('Tree');
  626. $table->addBehavior('Tree', ['scope' => ['menu' => 'categories']]);
  627. $expected = [
  628. ' 1:10 - 9:electronics',
  629. '_ 2: 9 - 10:televisions',
  630. '__ 3: 4 - 11:tube',
  631. '__ 5: 8 - 12:lcd',
  632. '___ 6: 7 - 13:plasma',
  633. '11:20 - 14:portable',
  634. '_12:15 - 15:mp3',
  635. '__13:14 - 16:flash',
  636. '_16:17 - 17:cd',
  637. '_18:19 - 18:radios'
  638. ];
  639. $this->assertMpttValues($expected, $table);
  640. }
  641. /**
  642. * Tests adding a new orphan node
  643. *
  644. * @return void
  645. */
  646. public function testAddOrphan()
  647. {
  648. $table = $this->table;
  649. $entity = new Entity(
  650. ['name' => 'New Orphan', 'parent_id' => null, 'level' => null],
  651. ['markNew' => true]
  652. );
  653. $this->assertSame($entity, $table->save($entity));
  654. $this->assertEquals(23, $entity->lft);
  655. $this->assertEquals(24, $entity->rght);
  656. $expected = [
  657. ' 1:20 - 1:electronics',
  658. '_ 2: 9 - 2:televisions',
  659. '__ 3: 4 - 3:tube',
  660. '__ 5: 6 - 4:lcd',
  661. '__ 7: 8 - 5:plasma',
  662. '_10:19 - 6:portable',
  663. '__11:14 - 7:mp3',
  664. '___12:13 - 8:flash',
  665. '__15:16 - 9:cd',
  666. '__17:18 - 10:radios',
  667. '21:22 - 11:alien hardware',
  668. '23:24 - 12:New Orphan',
  669. ];
  670. $this->assertMpttValues($expected, $this->table);
  671. }
  672. /**
  673. * Tests that adding a child node as a decendant of one of the roots works
  674. *
  675. * @return void
  676. */
  677. public function testAddMiddle()
  678. {
  679. $table = $this->table;
  680. $entity = new Entity(
  681. ['name' => 'laptops', 'parent_id' => 1],
  682. ['markNew' => true]
  683. );
  684. $this->assertSame($entity, $table->save($entity));
  685. $this->assertEquals(20, $entity->lft);
  686. $this->assertEquals(21, $entity->rght);
  687. $expected = [
  688. ' 1:22 - 1:electronics',
  689. '_ 2: 9 - 2:televisions',
  690. '__ 3: 4 - 3:tube',
  691. '__ 5: 6 - 4:lcd',
  692. '__ 7: 8 - 5:plasma',
  693. '_10:19 - 6:portable',
  694. '__11:14 - 7:mp3',
  695. '___12:13 - 8:flash',
  696. '__15:16 - 9:cd',
  697. '__17:18 - 10:radios',
  698. '_20:21 - 12:laptops',
  699. '23:24 - 11:alien hardware',
  700. ];
  701. $this->assertMpttValues($expected, $this->table);
  702. }
  703. /**
  704. * Tests adding a leaf to the tree
  705. *
  706. * @return void
  707. */
  708. public function testAddLeaf()
  709. {
  710. $table = $this->table;
  711. $entity = new Entity(
  712. ['name' => 'laptops', 'parent_id' => 2],
  713. ['markNew' => true]
  714. );
  715. $this->assertSame($entity, $table->save($entity));
  716. $this->assertEquals(9, $entity->lft);
  717. $this->assertEquals(10, $entity->rght);
  718. $expected = [
  719. ' 1:22 - 1:electronics',
  720. '_ 2:11 - 2:televisions',
  721. '__ 3: 4 - 3:tube',
  722. '__ 5: 6 - 4:lcd',
  723. '__ 7: 8 - 5:plasma',
  724. '__ 9:10 - 12:laptops',
  725. '_12:21 - 6:portable',
  726. '__13:16 - 7:mp3',
  727. '___14:15 - 8:flash',
  728. '__17:18 - 9:cd',
  729. '__19:20 - 10:radios',
  730. '23:24 - 11:alien hardware'
  731. ];
  732. $this->assertMpttValues($expected, $this->table);
  733. }
  734. /**
  735. * Tests moving a subtree to the right
  736. *
  737. * @return void
  738. */
  739. public function testReParentSubTreeRight()
  740. {
  741. $table = $this->table;
  742. $entity = $table->get(2);
  743. $entity->parent_id = 6;
  744. $this->assertSame($entity, $table->save($entity));
  745. $this->assertEquals(11, $entity->lft);
  746. $this->assertEquals(18, $entity->rght);
  747. $expected = [
  748. ' 1:20 - 1:electronics',
  749. '_ 2:19 - 6:portable',
  750. '__ 3: 6 - 7:mp3',
  751. '___ 4: 5 - 8:flash',
  752. '__ 7: 8 - 9:cd',
  753. '__ 9:10 - 10:radios',
  754. '__11:18 - 2:televisions',
  755. '___12:13 - 3:tube',
  756. '___14:15 - 4:lcd',
  757. '___16:17 - 5:plasma',
  758. '21:22 - 11:alien hardware'
  759. ];
  760. $this->assertMpttValues($expected, $table);
  761. }
  762. /**
  763. * Tests moving a subtree to the left
  764. *
  765. * @return void
  766. */
  767. public function testReParentSubTreeLeft()
  768. {
  769. $table = $this->table;
  770. $entity = $table->get(6);
  771. $entity->parent_id = 2;
  772. $this->assertSame($entity, $table->save($entity));
  773. $this->assertEquals(9, $entity->lft);
  774. $this->assertEquals(18, $entity->rght);
  775. $expected = [
  776. ' 1:20 - 1:electronics',
  777. '_ 2:19 - 2:televisions',
  778. '__ 3: 4 - 3:tube',
  779. '__ 5: 6 - 4:lcd',
  780. '__ 7: 8 - 5:plasma',
  781. '__ 9:18 - 6:portable',
  782. '___10:13 - 7:mp3',
  783. '____11:12 - 8:flash',
  784. '___14:15 - 9:cd',
  785. '___16:17 - 10:radios',
  786. '21:22 - 11:alien hardware'
  787. ];
  788. $this->assertMpttValues($expected, $this->table);
  789. }
  790. /**
  791. * Test moving a leaft to the left
  792. *
  793. * @return void
  794. */
  795. public function testReParentLeafLeft()
  796. {
  797. $table = $this->table;
  798. $entity = $table->get(10);
  799. $entity->parent_id = 2;
  800. $this->assertSame($entity, $table->save($entity));
  801. $this->assertEquals(9, $entity->lft);
  802. $this->assertEquals(10, $entity->rght);
  803. $expected = [
  804. ' 1:20 - 1:electronics',
  805. '_ 2:11 - 2:televisions',
  806. '__ 3: 4 - 3:tube',
  807. '__ 5: 6 - 4:lcd',
  808. '__ 7: 8 - 5:plasma',
  809. '__ 9:10 - 10:radios',
  810. '_12:19 - 6:portable',
  811. '__13:16 - 7:mp3',
  812. '___14:15 - 8:flash',
  813. '__17:18 - 9:cd',
  814. '21:22 - 11:alien hardware'
  815. ];
  816. $this->assertMpttValues($expected, $this->table);
  817. }
  818. /**
  819. * Test moving a leaf to the left
  820. *
  821. * @return void
  822. */
  823. public function testReParentLeafRight()
  824. {
  825. $table = $this->table;
  826. $entity = $table->get(5);
  827. $entity->parent_id = 6;
  828. $this->assertSame($entity, $table->save($entity));
  829. $this->assertEquals(17, $entity->lft);
  830. $this->assertEquals(18, $entity->rght);
  831. $result = $table->find()->order('lft')->hydrate(false);
  832. $expected = [
  833. ' 1:20 - 1:electronics',
  834. '_ 2: 7 - 2:televisions',
  835. '__ 3: 4 - 3:tube',
  836. '__ 5: 6 - 4:lcd',
  837. '_ 8:19 - 6:portable',
  838. '__ 9:12 - 7:mp3',
  839. '___10:11 - 8:flash',
  840. '__13:14 - 9:cd',
  841. '__15:16 - 10:radios',
  842. '__17:18 - 5:plasma',
  843. '21:22 - 11:alien hardware'
  844. ];
  845. $this->assertMpttValues($expected, $table);
  846. }
  847. /**
  848. * Tests moving a subtree with a node having no lft and rght columns
  849. *
  850. * @return void
  851. */
  852. public function testReParentNoTreeColumns()
  853. {
  854. $table = $this->table;
  855. $entity = $table->get(6);
  856. $entity->unsetProperty('lft');
  857. $entity->unsetProperty('rght');
  858. $entity->parent_id = 2;
  859. $this->assertSame($entity, $table->save($entity));
  860. $this->assertEquals(9, $entity->lft);
  861. $this->assertEquals(18, $entity->rght);
  862. $expected = [
  863. ' 1:20 - 1:electronics',
  864. '_ 2:19 - 2:televisions',
  865. '__ 3: 4 - 3:tube',
  866. '__ 5: 6 - 4:lcd',
  867. '__ 7: 8 - 5:plasma',
  868. '__ 9:18 - 6:portable',
  869. '___10:13 - 7:mp3',
  870. '____11:12 - 8:flash',
  871. '___14:15 - 9:cd',
  872. '___16:17 - 10:radios',
  873. '21:22 - 11:alien hardware'
  874. ];
  875. $this->assertMpttValues($expected, $this->table);
  876. }
  877. /**
  878. * Tests moving a subtree as a new root
  879. *
  880. * @return void
  881. */
  882. public function testRootingSubTree()
  883. {
  884. $table = $this->table;
  885. $entity = $table->get(2);
  886. $entity->parent_id = null;
  887. $this->assertSame($entity, $table->save($entity));
  888. $this->assertEquals(15, $entity->lft);
  889. $this->assertEquals(22, $entity->rght);
  890. $expected = [
  891. ' 1:12 - 1:electronics',
  892. '_ 2:11 - 6:portable',
  893. '__ 3: 6 - 7:mp3',
  894. '___ 4: 5 - 8:flash',
  895. '__ 7: 8 - 9:cd',
  896. '__ 9:10 - 10:radios',
  897. '13:14 - 11:alien hardware',
  898. '15:22 - 2:televisions',
  899. '_16:17 - 3:tube',
  900. '_18:19 - 4:lcd',
  901. '_20:21 - 5:plasma'
  902. ];
  903. $this->assertMpttValues($expected, $table);
  904. }
  905. /**
  906. * Tests moving a subtree with no tree columns
  907. *
  908. * @return void
  909. */
  910. public function testRootingNoTreeColumns()
  911. {
  912. $table = $this->table;
  913. $entity = $table->get(2);
  914. $entity->unsetProperty('lft');
  915. $entity->unsetProperty('rght');
  916. $entity->parent_id = null;
  917. $this->assertSame($entity, $table->save($entity));
  918. $this->assertEquals(15, $entity->lft);
  919. $this->assertEquals(22, $entity->rght);
  920. $expected = [
  921. ' 1:12 - 1:electronics',
  922. '_ 2:11 - 6:portable',
  923. '__ 3: 6 - 7:mp3',
  924. '___ 4: 5 - 8:flash',
  925. '__ 7: 8 - 9:cd',
  926. '__ 9:10 - 10:radios',
  927. '13:14 - 11:alien hardware',
  928. '15:22 - 2:televisions',
  929. '_16:17 - 3:tube',
  930. '_18:19 - 4:lcd',
  931. '_20:21 - 5:plasma'
  932. ];
  933. $this->assertMpttValues($expected, $table);
  934. }
  935. /**
  936. * Tests that trying to create a cycle throws an exception
  937. *
  938. * @expectedException \RuntimeException
  939. * @expectedExceptionMessage Cannot use node "5" as parent for entity "2"
  940. * @return void
  941. */
  942. public function testReparentCycle()
  943. {
  944. $table = $this->table;
  945. $entity = $table->get(2);
  946. $entity->parent_id = 5;
  947. $table->save($entity);
  948. }
  949. /**
  950. * Tests deleting a leaf in the tree
  951. *
  952. * @return void
  953. */
  954. public function testDeleteLeaf()
  955. {
  956. $table = $this->table;
  957. $entity = $table->get(4);
  958. $this->assertTrue($table->delete($entity));
  959. $expected = [
  960. ' 1:18 - 1:electronics',
  961. '_ 2: 7 - 2:televisions',
  962. '__ 3: 4 - 3:tube',
  963. '__ 5: 6 - 5:plasma',
  964. '_ 8:17 - 6:portable',
  965. '__ 9:12 - 7:mp3',
  966. '___10:11 - 8:flash',
  967. '__13:14 - 9:cd',
  968. '__15:16 - 10:radios',
  969. '19:20 - 11:alien hardware'
  970. ];
  971. $this->assertMpttValues($expected, $this->table);
  972. }
  973. /**
  974. * Tests deleting a subtree
  975. *
  976. * @return void
  977. */
  978. public function testDeleteSubTree()
  979. {
  980. $table = $this->table;
  981. $entity = $table->get(6);
  982. $this->assertTrue($table->delete($entity));
  983. $expected = [
  984. ' 1:10 - 1:electronics',
  985. '_ 2: 9 - 2:televisions',
  986. '__ 3: 4 - 3:tube',
  987. '__ 5: 6 - 4:lcd',
  988. '__ 7: 8 - 5:plasma',
  989. '11:12 - 11:alien hardware'
  990. ];
  991. $this->assertMpttValues($expected, $this->table);
  992. }
  993. /**
  994. * Test deleting a root node
  995. *
  996. * @return void
  997. */
  998. public function testDeleteRoot()
  999. {
  1000. $table = $this->table;
  1001. $entity = $table->get(1);
  1002. $this->assertTrue($table->delete($entity));
  1003. $expected = [
  1004. ' 1: 2 - 11:alien hardware'
  1005. ];
  1006. $this->assertMpttValues($expected, $this->table);
  1007. }
  1008. /**
  1009. * Test deleting a node with no tree columns
  1010. *
  1011. * @return void
  1012. */
  1013. public function testDeleteRootNoTreeColumns()
  1014. {
  1015. $table = $this->table;
  1016. $entity = $table->get(1);
  1017. $entity->unsetProperty('lft');
  1018. $entity->unsetProperty('rght');
  1019. $this->assertTrue($table->delete($entity));
  1020. $expected = [
  1021. ' 1: 2 - 11:alien hardware'
  1022. ];
  1023. $this->assertMpttValues($expected, $this->table);
  1024. }
  1025. /**
  1026. * Tests that a leaf can be taken out of the tree and put in as a root
  1027. *
  1028. * @return void
  1029. */
  1030. public function testRemoveFromLeafFromTree()
  1031. {
  1032. $table = $this->table;
  1033. $entity = $table->get(10);
  1034. $this->assertSame($entity, $table->removeFromTree($entity));
  1035. $this->assertEquals(21, $entity->lft);
  1036. $this->assertEquals(22, $entity->rght);
  1037. $this->assertEquals(null, $entity->parent_id);
  1038. $result = $table->find()->order('lft')->hydrate(false);
  1039. $expected = [
  1040. ' 1:18 - 1:electronics',
  1041. '_ 2: 9 - 2:televisions',
  1042. '__ 3: 4 - 3:tube',
  1043. '__ 5: 6 - 4:lcd',
  1044. '__ 7: 8 - 5:plasma',
  1045. '_10:17 - 6:portable',
  1046. '__11:14 - 7:mp3',
  1047. '___12:13 - 8:flash',
  1048. '__15:16 - 9:cd',
  1049. '19:20 - 11:alien hardware',
  1050. '21:22 - 10:radios'
  1051. ];
  1052. $this->assertMpttValues($expected, $table);
  1053. }
  1054. /**
  1055. * Test removing a middle node from a tree
  1056. *
  1057. * @return void
  1058. */
  1059. public function testRemoveMiddleNodeFromTree()
  1060. {
  1061. $table = $this->table;
  1062. $entity = $table->get(6);
  1063. $this->assertSame($entity, $table->removeFromTree($entity));
  1064. $result = $table->find('threaded')->order('lft')->hydrate(false)->toArray();
  1065. $this->assertEquals(21, $entity->lft);
  1066. $this->assertEquals(22, $entity->rght);
  1067. $this->assertEquals(null, $entity->parent_id);
  1068. $result = $table->find()->order('lft')->hydrate(false);
  1069. $expected = [
  1070. ' 1:18 - 1:electronics',
  1071. '_ 2: 9 - 2:televisions',
  1072. '__ 3: 4 - 3:tube',
  1073. '__ 5: 6 - 4:lcd',
  1074. '__ 7: 8 - 5:plasma',
  1075. '_10:13 - 7:mp3',
  1076. '__11:12 - 8:flash',
  1077. '_14:15 - 9:cd',
  1078. '_16:17 - 10:radios',
  1079. '19:20 - 11:alien hardware',
  1080. '21:22 - 6:portable'
  1081. ];
  1082. $this->assertMpttValues($expected, $table);
  1083. }
  1084. /**
  1085. * Tests removing the root of a tree
  1086. *
  1087. * @return void
  1088. */
  1089. public function testRemoveRootFromTree()
  1090. {
  1091. $table = $this->table;
  1092. $entity = $table->get(1);
  1093. $this->assertSame($entity, $table->removeFromTree($entity));
  1094. $result = $table->find('threaded')->order('lft')->hydrate(false)->toArray();
  1095. $this->assertEquals(21, $entity->lft);
  1096. $this->assertEquals(22, $entity->rght);
  1097. $this->assertEquals(null, $entity->parent_id);
  1098. $expected = [
  1099. ' 1: 8 - 2:televisions',
  1100. '_ 2: 3 - 3:tube',
  1101. '_ 4: 5 - 4:lcd',
  1102. '_ 6: 7 - 5:plasma',
  1103. ' 9:18 - 6:portable',
  1104. '_10:13 - 7:mp3',
  1105. '__11:12 - 8:flash',
  1106. '_14:15 - 9:cd',
  1107. '_16:17 - 10:radios',
  1108. '19:20 - 11:alien hardware',
  1109. '21:22 - 1:electronics'
  1110. ];
  1111. $this->assertMpttValues($expected, $table);
  1112. }
  1113. /**
  1114. * Tests that using associations having tree fields in the schema
  1115. * does not generate SQL errors
  1116. *
  1117. * @return void
  1118. */
  1119. public function testFindPathWithAssociation()
  1120. {
  1121. $table = $this->table;
  1122. $other = TableRegistry::get('FriendlyTrees', [
  1123. 'table' => $table->table()
  1124. ]);
  1125. $table->hasOne('FriendlyTrees', [
  1126. 'foreignKey' => 'id'
  1127. ]);
  1128. $result = $table
  1129. ->find('children', ['for' => 1])
  1130. ->contain('FriendlyTrees')
  1131. ->toArray();
  1132. $this->assertCount(9, $result);
  1133. }
  1134. /**
  1135. * Tests getting the depth level of a node in the tree.
  1136. *
  1137. * @return void
  1138. */
  1139. public function testGetLevel()
  1140. {
  1141. $entity = $this->table->get(8);
  1142. $result = $this->table->getLevel($entity);
  1143. $this->assertEquals(3, $result);
  1144. $result = $this->table->getLevel($entity->id);
  1145. $this->assertEquals(3, $result);
  1146. $result = $this->table->getLevel(5);
  1147. $this->assertEquals(2, $result);
  1148. $result = $this->table->getLevel(99999);
  1149. $this->assertFalse($result);
  1150. }
  1151. /**
  1152. * Test setting level for new nodes
  1153. *
  1154. * @return void
  1155. */
  1156. public function testSetLevelNewNode()
  1157. {
  1158. $this->table->behaviors()->Tree->config('level', 'depth');
  1159. $entity = new Entity(['parent_id' => null, 'name' => 'Depth 0']);
  1160. $this->table->save($entity);
  1161. $entity = $this->table->get(12);
  1162. $this->assertEquals(0, $entity->depth);
  1163. $entity = new Entity(['parent_id' => 1, 'name' => 'Depth 1']);
  1164. $this->table->save($entity);
  1165. $entity = $this->table->get(13);
  1166. $this->assertEquals(1, $entity->depth);
  1167. $entity = new Entity(['parent_id' => 8, 'name' => 'Depth 4']);
  1168. $this->table->save($entity);
  1169. $entity = $this->table->get(14);
  1170. $this->assertEquals(4, $entity->depth);
  1171. }
  1172. /**
  1173. * Test setting level for existing nodes
  1174. *
  1175. * @return void
  1176. */
  1177. public function testSetLevelExistingNode()
  1178. {
  1179. $this->table->behaviors()->Tree->config('level', 'depth');
  1180. // Leaf node
  1181. $entity = $this->table->get(4);
  1182. $this->assertEquals(2, $entity->depth);
  1183. $this->table->save($entity);
  1184. $entity = $this->table->get(4);
  1185. $this->assertEquals(2, $entity->depth);
  1186. // Non leaf node so depth of descendents will also change
  1187. $entity = $this->table->get(6);
  1188. $this->assertEquals(1, $entity->depth);
  1189. $entity->parent_id = null;
  1190. $this->table->save($entity);
  1191. $entity = $this->table->get(6);
  1192. $this->assertEquals(0, $entity->depth);
  1193. $entity = $this->table->get(7);
  1194. $this->assertEquals(1, $entity->depth);
  1195. $entity = $this->table->get(8);
  1196. $this->assertEquals(2, $entity->depth);
  1197. }
  1198. /**
  1199. * Assert MPTT values
  1200. *
  1201. * Custom assert method to make identifying the differences between expected
  1202. * and actual db state easier to identify.
  1203. *
  1204. * @param array $expected tree state to be expected
  1205. * @param \Cake\ORM\Table $table Table instance
  1206. * @param \Cake\ORM\Query $query Optional query object
  1207. * @return void
  1208. */
  1209. public function assertMpttValues($expected, $table, $query = null)
  1210. {
  1211. $query = $query ?: $table->find();
  1212. $primaryKey = $table->primaryKey();
  1213. if (is_array($primaryKey)) {
  1214. $primaryKey = $primaryKey[0];
  1215. }
  1216. $displayField = $table->displayField();
  1217. $options = [
  1218. 'valuePath' => function ($item, $key, $iterator) use ($primaryKey, $displayField) {
  1219. return sprintf(
  1220. '%s:%s - %s:%s',
  1221. str_pad($item->lft, 2, ' ', STR_PAD_LEFT),
  1222. str_pad($item->rght, 2, ' ', STR_PAD_LEFT),
  1223. str_pad($item->$primaryKey, 2, ' ', STR_PAD_LEFT),
  1224. $item->{$displayField}
  1225. );
  1226. }
  1227. ];
  1228. $result = array_values($query->find('treeList', $options)->toArray());
  1229. if (count($result) === count($expected)) {
  1230. $subExpected = array_diff($expected, $result);
  1231. if ($subExpected) {
  1232. $subResult = array_intersect_key($result, $subExpected);
  1233. $this->assertSame($subExpected, $subResult, 'Differences in the tree were found (lft:rght id:display-name)');
  1234. }
  1235. }
  1236. $this->assertSame($expected, $result, 'The tree is not the same (lft:rght id:display-name)');
  1237. }
  1238. }