TreeBehaviorTest.php 40 KB

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