TreeBehaviorTest.php 41 KB

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