TreeBehaviorTest.php 43 KB

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