TreeBehaviorTest.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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->find('list', ['valueField' => 'depth'])->toArray();
  572. $table->updateAll(['lft' => null, 'rght' => null, 'depth' => null], []);
  573. $table->behaviors()->Tree->config('level', 'depth');
  574. $table->recover();
  575. $expected = [
  576. ' 1:20 - 1:electronics',
  577. '_ 2: 9 - 2:televisions',
  578. '__ 3: 4 - 3:tube',
  579. '__ 5: 6 - 4:lcd',
  580. '__ 7: 8 - 5:plasma',
  581. '_10:19 - 6:portable',
  582. '__11:14 - 7:mp3',
  583. '___12:13 - 8:flash',
  584. '__15:16 - 9:cd',
  585. '__17:18 - 10:radios',
  586. '21:22 - 11:alien hardware'
  587. ];
  588. $this->assertMpttValues($expected, $table);
  589. $result = $table->find('list', ['valueField' => 'depth'])->toArray();
  590. $this->assertSame($expectedLevels, $result);
  591. }
  592. /**
  593. * Tests the recover function with a custom scope
  594. *
  595. * @return void
  596. */
  597. public function testRecoverScoped()
  598. {
  599. $table = TableRegistry::get('MenuLinkTrees');
  600. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  601. $expected = $table->find()
  602. ->where(['menu' => 'main-menu'])
  603. ->order('lft')
  604. ->hydrate(false)
  605. ->toArray();
  606. $table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
  607. $table->recover();
  608. $expected = [
  609. ' 1:10 - 1:Link 1',
  610. '_ 2: 3 - 2:Link 2',
  611. '_ 4: 9 - 3:Link 3',
  612. '__ 5: 8 - 4:Link 4',
  613. '___ 6: 7 - 5:Link 5',
  614. '11:14 - 6:Link 6',
  615. '_12:13 - 7:Link 7',
  616. '15:16 - 8:Link 8'
  617. ];
  618. $this->assertMpttValues($expected, $table);
  619. $table->removeBehavior('Tree');
  620. $table->addBehavior('Tree', ['scope' => ['menu' => 'categories']]);
  621. $expected = [
  622. ' 1:10 - 9:electronics',
  623. '_ 2: 9 - 10:televisions',
  624. '__ 3: 4 - 11:tube',
  625. '__ 5: 8 - 12:lcd',
  626. '___ 6: 7 - 13:plasma',
  627. '11:20 - 14:portable',
  628. '_12:15 - 15:mp3',
  629. '__13:14 - 16:flash',
  630. '_16:17 - 17:cd',
  631. '_18:19 - 18:radios'
  632. ];
  633. $this->assertMpttValues($expected, $table);
  634. }
  635. /**
  636. * Tests adding a new orphan node
  637. *
  638. * @return void
  639. */
  640. public function testAddOrphan()
  641. {
  642. $table = $this->table;
  643. $entity = new Entity(
  644. ['name' => 'New Orphan', 'parent_id' => null, 'level' => null],
  645. ['markNew' => true]
  646. );
  647. $this->assertSame($entity, $table->save($entity));
  648. $this->assertEquals(23, $entity->lft);
  649. $this->assertEquals(24, $entity->rght);
  650. $expected = [
  651. ' 1:20 - 1:electronics',
  652. '_ 2: 9 - 2:televisions',
  653. '__ 3: 4 - 3:tube',
  654. '__ 5: 6 - 4:lcd',
  655. '__ 7: 8 - 5:plasma',
  656. '_10:19 - 6:portable',
  657. '__11:14 - 7:mp3',
  658. '___12:13 - 8:flash',
  659. '__15:16 - 9:cd',
  660. '__17:18 - 10:radios',
  661. '21:22 - 11:alien hardware',
  662. '23:24 - 12:New Orphan',
  663. ];
  664. $this->assertMpttValues($expected, $this->table);
  665. }
  666. /**
  667. * Tests that adding a child node as a decendant of one of the roots works
  668. *
  669. * @return void
  670. */
  671. public function testAddMiddle()
  672. {
  673. $table = $this->table;
  674. $entity = new Entity(
  675. ['name' => 'laptops', 'parent_id' => 1],
  676. ['markNew' => true]
  677. );
  678. $this->assertSame($entity, $table->save($entity));
  679. $this->assertEquals(20, $entity->lft);
  680. $this->assertEquals(21, $entity->rght);
  681. $expected = [
  682. ' 1:22 - 1:electronics',
  683. '_ 2: 9 - 2:televisions',
  684. '__ 3: 4 - 3:tube',
  685. '__ 5: 6 - 4:lcd',
  686. '__ 7: 8 - 5:plasma',
  687. '_10:19 - 6:portable',
  688. '__11:14 - 7:mp3',
  689. '___12:13 - 8:flash',
  690. '__15:16 - 9:cd',
  691. '__17:18 - 10:radios',
  692. '_20:21 - 12:laptops',
  693. '23:24 - 11:alien hardware',
  694. ];
  695. $this->assertMpttValues($expected, $this->table);
  696. }
  697. /**
  698. * Tests adding a leaf to the tree
  699. *
  700. * @return void
  701. */
  702. public function testAddLeaf()
  703. {
  704. $table = $this->table;
  705. $entity = new Entity(
  706. ['name' => 'laptops', 'parent_id' => 2],
  707. ['markNew' => true]
  708. );
  709. $this->assertSame($entity, $table->save($entity));
  710. $this->assertEquals(9, $entity->lft);
  711. $this->assertEquals(10, $entity->rght);
  712. $expected = [
  713. ' 1:22 - 1:electronics',
  714. '_ 2:11 - 2:televisions',
  715. '__ 3: 4 - 3:tube',
  716. '__ 5: 6 - 4:lcd',
  717. '__ 7: 8 - 5:plasma',
  718. '__ 9:10 - 12:laptops',
  719. '_12:21 - 6:portable',
  720. '__13:16 - 7:mp3',
  721. '___14:15 - 8:flash',
  722. '__17:18 - 9:cd',
  723. '__19:20 - 10:radios',
  724. '23:24 - 11:alien hardware'
  725. ];
  726. $this->assertMpttValues($expected, $this->table);
  727. }
  728. /**
  729. * Tests moving a subtree to the right
  730. *
  731. * @return void
  732. */
  733. public function testReParentSubTreeRight()
  734. {
  735. $table = $this->table;
  736. $entity = $table->get(2);
  737. $entity->parent_id = 6;
  738. $this->assertSame($entity, $table->save($entity));
  739. $this->assertEquals(11, $entity->lft);
  740. $this->assertEquals(18, $entity->rght);
  741. $expected = [
  742. ' 1:20 - 1:electronics',
  743. '_ 2:19 - 6:portable',
  744. '__ 3: 6 - 7:mp3',
  745. '___ 4: 5 - 8:flash',
  746. '__ 7: 8 - 9:cd',
  747. '__ 9:10 - 10:radios',
  748. '__11:18 - 2:televisions',
  749. '___12:13 - 3:tube',
  750. '___14:15 - 4:lcd',
  751. '___16:17 - 5:plasma',
  752. '21:22 - 11:alien hardware'
  753. ];
  754. $this->assertMpttValues($expected, $table);
  755. }
  756. /**
  757. * Tests moving a subtree to the left
  758. *
  759. * @return void
  760. */
  761. public function testReParentSubTreeLeft()
  762. {
  763. $table = $this->table;
  764. $entity = $table->get(6);
  765. $entity->parent_id = 2;
  766. $this->assertSame($entity, $table->save($entity));
  767. $this->assertEquals(9, $entity->lft);
  768. $this->assertEquals(18, $entity->rght);
  769. $expected = [
  770. ' 1:20 - 1:electronics',
  771. '_ 2:19 - 2:televisions',
  772. '__ 3: 4 - 3:tube',
  773. '__ 5: 6 - 4:lcd',
  774. '__ 7: 8 - 5:plasma',
  775. '__ 9:18 - 6:portable',
  776. '___10:13 - 7:mp3',
  777. '____11:12 - 8:flash',
  778. '___14:15 - 9:cd',
  779. '___16:17 - 10:radios',
  780. '21:22 - 11:alien hardware'
  781. ];
  782. $this->assertMpttValues($expected, $this->table);
  783. }
  784. /**
  785. * Test moving a leaft to the left
  786. *
  787. * @return void
  788. */
  789. public function testReParentLeafLeft()
  790. {
  791. $table = $this->table;
  792. $entity = $table->get(10);
  793. $entity->parent_id = 2;
  794. $this->assertSame($entity, $table->save($entity));
  795. $this->assertEquals(9, $entity->lft);
  796. $this->assertEquals(10, $entity->rght);
  797. $expected = [
  798. ' 1:20 - 1:electronics',
  799. '_ 2:11 - 2:televisions',
  800. '__ 3: 4 - 3:tube',
  801. '__ 5: 6 - 4:lcd',
  802. '__ 7: 8 - 5:plasma',
  803. '__ 9:10 - 10:radios',
  804. '_12:19 - 6:portable',
  805. '__13:16 - 7:mp3',
  806. '___14:15 - 8:flash',
  807. '__17:18 - 9:cd',
  808. '21:22 - 11:alien hardware'
  809. ];
  810. $this->assertMpttValues($expected, $this->table);
  811. }
  812. /**
  813. * Test moving a leaf to the left
  814. *
  815. * @return void
  816. */
  817. public function testReParentLeafRight()
  818. {
  819. $table = $this->table;
  820. $entity = $table->get(5);
  821. $entity->parent_id = 6;
  822. $this->assertSame($entity, $table->save($entity));
  823. $this->assertEquals(17, $entity->lft);
  824. $this->assertEquals(18, $entity->rght);
  825. $result = $table->find()->order('lft')->hydrate(false);
  826. $expected = [
  827. ' 1:20 - 1:electronics',
  828. '_ 2: 7 - 2:televisions',
  829. '__ 3: 4 - 3:tube',
  830. '__ 5: 6 - 4:lcd',
  831. '_ 8:19 - 6:portable',
  832. '__ 9:12 - 7:mp3',
  833. '___10:11 - 8:flash',
  834. '__13:14 - 9:cd',
  835. '__15:16 - 10:radios',
  836. '__17:18 - 5:plasma',
  837. '21:22 - 11:alien hardware'
  838. ];
  839. $this->assertMpttValues($expected, $table);
  840. }
  841. /**
  842. * Tests moving a subtree with a node having no lft and rght columns
  843. *
  844. * @return void
  845. */
  846. public function testReParentNoTreeColumns()
  847. {
  848. $table = $this->table;
  849. $entity = $table->get(6);
  850. $entity->unsetProperty('lft');
  851. $entity->unsetProperty('rght');
  852. $entity->parent_id = 2;
  853. $this->assertSame($entity, $table->save($entity));
  854. $this->assertEquals(9, $entity->lft);
  855. $this->assertEquals(18, $entity->rght);
  856. $expected = [
  857. ' 1:20 - 1:electronics',
  858. '_ 2:19 - 2:televisions',
  859. '__ 3: 4 - 3:tube',
  860. '__ 5: 6 - 4:lcd',
  861. '__ 7: 8 - 5:plasma',
  862. '__ 9:18 - 6:portable',
  863. '___10:13 - 7:mp3',
  864. '____11:12 - 8:flash',
  865. '___14:15 - 9:cd',
  866. '___16:17 - 10:radios',
  867. '21:22 - 11:alien hardware'
  868. ];
  869. $this->assertMpttValues($expected, $this->table);
  870. }
  871. /**
  872. * Tests moving a subtree as a new root
  873. *
  874. * @return void
  875. */
  876. public function testRootingSubTree()
  877. {
  878. $table = $this->table;
  879. $entity = $table->get(2);
  880. $entity->parent_id = null;
  881. $this->assertSame($entity, $table->save($entity));
  882. $this->assertEquals(15, $entity->lft);
  883. $this->assertEquals(22, $entity->rght);
  884. $expected = [
  885. ' 1:12 - 1:electronics',
  886. '_ 2:11 - 6:portable',
  887. '__ 3: 6 - 7:mp3',
  888. '___ 4: 5 - 8:flash',
  889. '__ 7: 8 - 9:cd',
  890. '__ 9:10 - 10:radios',
  891. '13:14 - 11:alien hardware',
  892. '15:22 - 2:televisions',
  893. '_16:17 - 3:tube',
  894. '_18:19 - 4:lcd',
  895. '_20:21 - 5:plasma'
  896. ];
  897. $this->assertMpttValues($expected, $table);
  898. }
  899. /**
  900. * Tests moving a subtree with no tree columns
  901. *
  902. * @return void
  903. */
  904. public function testRootingNoTreeColumns()
  905. {
  906. $table = $this->table;
  907. $entity = $table->get(2);
  908. $entity->unsetProperty('lft');
  909. $entity->unsetProperty('rght');
  910. $entity->parent_id = null;
  911. $this->assertSame($entity, $table->save($entity));
  912. $this->assertEquals(15, $entity->lft);
  913. $this->assertEquals(22, $entity->rght);
  914. $expected = [
  915. ' 1:12 - 1:electronics',
  916. '_ 2:11 - 6:portable',
  917. '__ 3: 6 - 7:mp3',
  918. '___ 4: 5 - 8:flash',
  919. '__ 7: 8 - 9:cd',
  920. '__ 9:10 - 10:radios',
  921. '13:14 - 11:alien hardware',
  922. '15:22 - 2:televisions',
  923. '_16:17 - 3:tube',
  924. '_18:19 - 4:lcd',
  925. '_20:21 - 5:plasma'
  926. ];
  927. $this->assertMpttValues($expected, $table);
  928. }
  929. /**
  930. * Tests that trying to create a cycle throws an exception
  931. *
  932. * @expectedException \RuntimeException
  933. * @expectedExceptionMessage Cannot use node "5" as parent for entity "2"
  934. * @return void
  935. */
  936. public function testReparentCycle()
  937. {
  938. $table = $this->table;
  939. $entity = $table->get(2);
  940. $entity->parent_id = 5;
  941. $table->save($entity);
  942. }
  943. /**
  944. * Tests deleting a leaf in the tree
  945. *
  946. * @return void
  947. */
  948. public function testDeleteLeaf()
  949. {
  950. $table = $this->table;
  951. $entity = $table->get(4);
  952. $this->assertTrue($table->delete($entity));
  953. $expected = [
  954. ' 1:18 - 1:electronics',
  955. '_ 2: 7 - 2:televisions',
  956. '__ 3: 4 - 3:tube',
  957. '__ 5: 6 - 5:plasma',
  958. '_ 8:17 - 6:portable',
  959. '__ 9:12 - 7:mp3',
  960. '___10:11 - 8:flash',
  961. '__13:14 - 9:cd',
  962. '__15:16 - 10:radios',
  963. '19:20 - 11:alien hardware'
  964. ];
  965. $this->assertMpttValues($expected, $this->table);
  966. }
  967. /**
  968. * Tests deleting a subtree
  969. *
  970. * @return void
  971. */
  972. public function testDeleteSubTree()
  973. {
  974. $table = $this->table;
  975. $entity = $table->get(6);
  976. $this->assertTrue($table->delete($entity));
  977. $expected = [
  978. ' 1:10 - 1:electronics',
  979. '_ 2: 9 - 2:televisions',
  980. '__ 3: 4 - 3:tube',
  981. '__ 5: 6 - 4:lcd',
  982. '__ 7: 8 - 5:plasma',
  983. '11:12 - 11:alien hardware'
  984. ];
  985. $this->assertMpttValues($expected, $this->table);
  986. }
  987. /**
  988. * Test deleting a root node
  989. *
  990. * @return void
  991. */
  992. public function testDeleteRoot()
  993. {
  994. $table = $this->table;
  995. $entity = $table->get(1);
  996. $this->assertTrue($table->delete($entity));
  997. $expected = [
  998. ' 1: 2 - 11:alien hardware'
  999. ];
  1000. $this->assertMpttValues($expected, $this->table);
  1001. }
  1002. /**
  1003. * Test deleting a node with no tree columns
  1004. *
  1005. * @return void
  1006. */
  1007. public function testDeleteRootNoTreeColumns()
  1008. {
  1009. $table = $this->table;
  1010. $entity = $table->get(1);
  1011. $entity->unsetProperty('lft');
  1012. $entity->unsetProperty('rght');
  1013. $this->assertTrue($table->delete($entity));
  1014. $expected = [
  1015. ' 1: 2 - 11:alien hardware'
  1016. ];
  1017. $this->assertMpttValues($expected, $this->table);
  1018. }
  1019. /**
  1020. * Tests that a leaf can be taken out of the tree and put in as a root
  1021. *
  1022. * @return void
  1023. */
  1024. public function testRemoveFromLeafFromTree()
  1025. {
  1026. $table = $this->table;
  1027. $entity = $table->get(10);
  1028. $this->assertSame($entity, $table->removeFromTree($entity));
  1029. $this->assertEquals(21, $entity->lft);
  1030. $this->assertEquals(22, $entity->rght);
  1031. $this->assertEquals(null, $entity->parent_id);
  1032. $result = $table->find()->order('lft')->hydrate(false);
  1033. $expected = [
  1034. ' 1:18 - 1:electronics',
  1035. '_ 2: 9 - 2:televisions',
  1036. '__ 3: 4 - 3:tube',
  1037. '__ 5: 6 - 4:lcd',
  1038. '__ 7: 8 - 5:plasma',
  1039. '_10:17 - 6:portable',
  1040. '__11:14 - 7:mp3',
  1041. '___12:13 - 8:flash',
  1042. '__15:16 - 9:cd',
  1043. '19:20 - 11:alien hardware',
  1044. '21:22 - 10:radios'
  1045. ];
  1046. $this->assertMpttValues($expected, $table);
  1047. }
  1048. /**
  1049. * Test removing a middle node from a tree
  1050. *
  1051. * @return void
  1052. */
  1053. public function testRemoveMiddleNodeFromTree()
  1054. {
  1055. $table = $this->table;
  1056. $entity = $table->get(6);
  1057. $this->assertSame($entity, $table->removeFromTree($entity));
  1058. $result = $table->find('threaded')->order('lft')->hydrate(false)->toArray();
  1059. $this->assertEquals(21, $entity->lft);
  1060. $this->assertEquals(22, $entity->rght);
  1061. $this->assertEquals(null, $entity->parent_id);
  1062. $result = $table->find()->order('lft')->hydrate(false);
  1063. $expected = [
  1064. ' 1:18 - 1:electronics',
  1065. '_ 2: 9 - 2:televisions',
  1066. '__ 3: 4 - 3:tube',
  1067. '__ 5: 6 - 4:lcd',
  1068. '__ 7: 8 - 5:plasma',
  1069. '_10:13 - 7:mp3',
  1070. '__11:12 - 8:flash',
  1071. '_14:15 - 9:cd',
  1072. '_16:17 - 10:radios',
  1073. '19:20 - 11:alien hardware',
  1074. '21:22 - 6:portable'
  1075. ];
  1076. $this->assertMpttValues($expected, $table);
  1077. }
  1078. /**
  1079. * Tests removing the root of a tree
  1080. *
  1081. * @return void
  1082. */
  1083. public function testRemoveRootFromTree()
  1084. {
  1085. $table = $this->table;
  1086. $entity = $table->get(1);
  1087. $this->assertSame($entity, $table->removeFromTree($entity));
  1088. $result = $table->find('threaded')->order('lft')->hydrate(false)->toArray();
  1089. $this->assertEquals(21, $entity->lft);
  1090. $this->assertEquals(22, $entity->rght);
  1091. $this->assertEquals(null, $entity->parent_id);
  1092. $expected = [
  1093. ' 1: 8 - 2:televisions',
  1094. '_ 2: 3 - 3:tube',
  1095. '_ 4: 5 - 4:lcd',
  1096. '_ 6: 7 - 5:plasma',
  1097. ' 9:18 - 6:portable',
  1098. '_10:13 - 7:mp3',
  1099. '__11:12 - 8:flash',
  1100. '_14:15 - 9:cd',
  1101. '_16:17 - 10:radios',
  1102. '19:20 - 11:alien hardware',
  1103. '21:22 - 1:electronics'
  1104. ];
  1105. $this->assertMpttValues($expected, $table);
  1106. }
  1107. /**
  1108. * Tests that using associations having tree fields in the schema
  1109. * does not generate SQL errors
  1110. *
  1111. * @return void
  1112. */
  1113. public function testFindPathWithAssociation()
  1114. {
  1115. $table = $this->table;
  1116. $other = TableRegistry::get('FriendlyTrees', [
  1117. 'table' => $table->table()
  1118. ]);
  1119. $table->hasOne('FriendlyTrees', [
  1120. 'foreignKey' => 'id'
  1121. ]);
  1122. $result = $table
  1123. ->find('children', ['for' => 1])
  1124. ->contain('FriendlyTrees')
  1125. ->toArray();
  1126. $this->assertCount(9, $result);
  1127. }
  1128. /**
  1129. * Tests getting the depth level of a node in the tree.
  1130. *
  1131. * @return void
  1132. */
  1133. public function testGetLevel()
  1134. {
  1135. $entity = $this->table->get(8);
  1136. $result = $this->table->getLevel($entity);
  1137. $this->assertEquals(3, $result);
  1138. $result = $this->table->getLevel($entity->id);
  1139. $this->assertEquals(3, $result);
  1140. $result = $this->table->getLevel(5);
  1141. $this->assertEquals(2, $result);
  1142. $result = $this->table->getLevel(99999);
  1143. $this->assertFalse($result);
  1144. }
  1145. /**
  1146. * Test setting level for new nodes
  1147. *
  1148. * @return void
  1149. */
  1150. public function testSetLevelNewNode()
  1151. {
  1152. $this->table->behaviors()->Tree->config('level', 'depth');
  1153. $entity = new Entity(['parent_id' => null, 'name' => 'Depth 0']);
  1154. $this->table->save($entity);
  1155. $entity = $this->table->get(12);
  1156. $this->assertEquals(0, $entity->depth);
  1157. $entity = new Entity(['parent_id' => 1, 'name' => 'Depth 1']);
  1158. $this->table->save($entity);
  1159. $entity = $this->table->get(13);
  1160. $this->assertEquals(1, $entity->depth);
  1161. $entity = new Entity(['parent_id' => 8, 'name' => 'Depth 4']);
  1162. $this->table->save($entity);
  1163. $entity = $this->table->get(14);
  1164. $this->assertEquals(4, $entity->depth);
  1165. }
  1166. /**
  1167. * Test setting level for existing nodes
  1168. *
  1169. * @return void
  1170. */
  1171. public function testSetLevelExistingNode()
  1172. {
  1173. $this->table->behaviors()->Tree->config('level', 'depth');
  1174. // Leaf node
  1175. $entity = $this->table->get(4);
  1176. $this->assertEquals(2, $entity->depth);
  1177. $this->table->save($entity);
  1178. $entity = $this->table->get(4);
  1179. $this->assertEquals(2, $entity->depth);
  1180. // Non leaf node so depth of descendents will also change
  1181. $entity = $this->table->get(6);
  1182. $this->assertEquals(1, $entity->depth);
  1183. $entity->parent_id = null;
  1184. $this->table->save($entity);
  1185. $entity = $this->table->get(6);
  1186. $this->assertEquals(0, $entity->depth);
  1187. $entity = $this->table->get(7);
  1188. $this->assertEquals(1, $entity->depth);
  1189. $entity = $this->table->get(8);
  1190. $this->assertEquals(2, $entity->depth);
  1191. }
  1192. /**
  1193. * Assert MPTT values
  1194. *
  1195. * Custom assert method to make identifying the differences between expected
  1196. * and actual db state easier to identify.
  1197. *
  1198. * @param array $expected tree state to be expected
  1199. * @param \Cake\ORM\Table $table Table instance
  1200. * @param \Cake\ORM\Query $query Optional query object
  1201. * @return void
  1202. */
  1203. public function assertMpttValues($expected, $table, $query = null)
  1204. {
  1205. $query = $query ?: $table->find();
  1206. $primaryKey = $table->primaryKey();
  1207. if (is_array($primaryKey)) {
  1208. $primaryKey = $primaryKey[0];
  1209. }
  1210. $displayField = $table->displayField();
  1211. $options = [
  1212. 'valuePath' => function ($item, $key, $iterator) use ($primaryKey, $displayField) {
  1213. return sprintf(
  1214. '%s:%s - %s:%s',
  1215. str_pad($item->lft, 2, ' ', STR_PAD_LEFT),
  1216. str_pad($item->rght, 2, ' ', STR_PAD_LEFT),
  1217. str_pad($item->$primaryKey, 2, ' ', STR_PAD_LEFT),
  1218. $item->{$displayField}
  1219. );
  1220. }
  1221. ];
  1222. $result = array_values($query->find('treeList', $options)->toArray());
  1223. if (count($result) === count($expected)) {
  1224. $subExpected = array_diff($expected, $result);
  1225. if ($subExpected) {
  1226. $subResult = array_intersect_key($result, $subExpected);
  1227. $this->assertSame($subExpected, $subResult, 'Differences in the tree were found (lft:rght id:display-name)');
  1228. }
  1229. }
  1230. $this->assertSame($expected, $result, 'The tree is not the same (lft:rght id:display-name)');
  1231. }
  1232. }