TreeBehaviorTest.php 44 KB

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