TreeBehaviorTest.php 46 KB

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