TreeBehaviorTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 CakePHP(tm) v 3.0.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\Model\Behavior;
  16. use Cake\Collection\Collection;
  17. use Cake\Event\Event;
  18. use Cake\Model\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. * fixtures
  28. *
  29. * @var array
  30. */
  31. public $fixtures = [
  32. 'core.number_tree',
  33. 'core.menu_link_tree'
  34. ];
  35. public function setUp() {
  36. parent::setUp();
  37. $this->table = TableRegistry::get('NumberTrees');
  38. $this->table->addBehavior('Tree');
  39. }
  40. public function tearDown() {
  41. parent::tearDown();
  42. TableRegistry::clear();
  43. }
  44. /**
  45. * Tests the find('path') method
  46. *
  47. * @return void
  48. */
  49. public function testFindPath() {
  50. $nodes = $this->table->find('path', ['for' => 9]);
  51. $this->assertEquals([1, 6, 9], $nodes->extract('id')->toArray());
  52. $nodes = $this->table->find('path', ['for' => 10]);
  53. $this->assertEquals([1, 6, 10], $nodes->extract('id')->toArray());
  54. $nodes = $this->table->find('path', ['for' => 5]);
  55. $this->assertEquals([1, 2, 5], $nodes->extract('id')->toArray());
  56. $nodes = $this->table->find('path', ['for' => 1]);
  57. $this->assertEquals([1], $nodes->extract('id')->toArray());
  58. // find path with scope
  59. $table = TableRegistry::get('MenuLinkTrees');
  60. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  61. $nodes = $table->find('path', ['for' => 5]);
  62. $this->assertEquals([1, 3, 4, 5], $nodes->extract('id')->toArray());
  63. }
  64. /**
  65. * Tests the childCount() method
  66. *
  67. * @return void
  68. */
  69. public function testChildCount() {
  70. // direct children for the root node
  71. $countDirect = $this->table->childCount(1, true);
  72. $this->assertEquals(2, $countDirect);
  73. // counts all the children of root
  74. $count = $this->table->childCount(1, false);
  75. $this->assertEquals(9, $count);
  76. // counts direct children
  77. $count = $this->table->childCount(2, false);
  78. $this->assertEquals(3, $count);
  79. // count children for a middle-node
  80. $count = $this->table->childCount(6, false);
  81. $this->assertEquals(4, $count);
  82. // count leaf children
  83. $count = $this->table->childCount(10, false);
  84. $this->assertEquals(0, $count);
  85. // test scoping
  86. $table = TableRegistry::get('MenuLinkTrees');
  87. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  88. $count = $table->childCount(3, false);
  89. $this->assertEquals(2, $count);
  90. }
  91. /**
  92. * Tests the childCount() plus callable scoping
  93. *
  94. * @return void
  95. */
  96. public function testCallableScoping() {
  97. $table = TableRegistry::get('MenuLinkTrees');
  98. $table->addBehavior('Tree', [
  99. 'scope' => function ($query) {
  100. return $query->where(['menu' => 'main-menu']);
  101. }
  102. ]);
  103. $count = $table->childCount(1, false);
  104. $this->assertEquals(4, $count);
  105. }
  106. /**
  107. * Tests the find('children') method
  108. *
  109. * @return void
  110. */
  111. public function testFindChildren() {
  112. $table = TableRegistry::get('MenuLinkTrees');
  113. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  114. // root
  115. $nodeIds = [];
  116. $nodes = $table->find('children', ['for' => 1])->all();
  117. $this->assertEquals([2, 3, 4, 5], $nodes->extract('id')->toArray());
  118. // leaf
  119. $nodeIds = [];
  120. $nodes = $table->find('children', ['for' => 5])->all();
  121. $this->assertEquals(0, count($nodes->extract('id')->toArray()));
  122. // direct children
  123. $nodes = $table->find('children', ['for' => 1, 'direct' => true])->all();
  124. $this->assertEquals([2, 3], $nodes->extract('id')->toArray());
  125. }
  126. /**
  127. * Tests that find('children') will throw an exception if the node was not found
  128. *
  129. * @expectedException \Cake\ORM\Error\RecordNotFoundException
  130. * @return void
  131. */
  132. public function testFindChildrenException() {
  133. $table = TableRegistry::get('MenuLinkTrees');
  134. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  135. $query = $table->find('children', ['for' => 500]);
  136. }
  137. /**
  138. * Tests the moveUp() method
  139. *
  140. * @return void
  141. */
  142. public function testMoveUp() {
  143. $table = TableRegistry::get('MenuLinkTrees');
  144. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  145. // top level, wont move
  146. $this->assertFalse($this->table->moveUp(1, 10));
  147. // edge cases
  148. $this->assertFalse($this->table->moveUp(1, 0));
  149. $this->assertFalse($this->table->moveUp(1, -10));
  150. // move inner node
  151. $result = $table->moveUp(3, 1);
  152. $nodes = $table->find('children', ['for' => 1])->all();
  153. $this->assertEquals([3, 4, 5, 2], $nodes->extract('id')->toArray());
  154. $this->assertTrue($result);
  155. // move leaf
  156. $this->assertFalse($table->moveUp(5, 1));
  157. // move to first position
  158. $table->moveUp(8, true);
  159. $nodes = $table->find()
  160. ->select(['id'])
  161. ->where(function($exp) {
  162. return $exp->isNull('parent_id');
  163. })
  164. ->where(['menu' => 'main-menu'])
  165. ->order(['lft' => 'ASC'])
  166. ->all();
  167. $this->assertEquals([8, 1, 6], $nodes->extract('id')->toArray());
  168. }
  169. /**
  170. * Tests that moveUp() will throw an exception if the node was not found
  171. *
  172. * @expectedException \Cake\ORM\Error\RecordNotFoundException
  173. * @return void
  174. */
  175. public function testMoveUpException() {
  176. $table = TableRegistry::get('MenuLinkTrees');
  177. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  178. $table->moveUp(500, 1);
  179. }
  180. /**
  181. * Tests the moveDown() method
  182. *
  183. * @return void
  184. */
  185. public function testMoveDown() {
  186. $table = TableRegistry::get('MenuLinkTrees');
  187. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  188. // latest node, wont move
  189. $this->assertFalse($this->table->moveDown(8, 10));
  190. // edge cases
  191. $this->assertFalse($this->table->moveDown(8, 0));
  192. $this->assertFalse($this->table->moveUp(8, -10));
  193. // move inner node
  194. $result = $table->moveDown(2, 1);
  195. $nodes = $table->find('children', ['for' => 1])->all();
  196. $this->assertEquals([3, 4, 5, 2], $nodes->extract('id')->toArray());
  197. $this->assertTrue($result);
  198. // move leaf
  199. $this->assertFalse( $table->moveDown(5, 1));
  200. // move to last position
  201. $table->moveDown(1, true);
  202. $nodes = $table->find()
  203. ->select(['id'])
  204. ->where(function($exp) {
  205. return $exp->isNull('parent_id');
  206. })
  207. ->where(['menu' => 'main-menu'])
  208. ->order(['lft' => 'ASC'])
  209. ->all();
  210. $this->assertEquals([6, 8, 1], $nodes->extract('id')->toArray());
  211. }
  212. /**
  213. * Tests that moveDown() will throw an exception if the node was not found
  214. *
  215. * @expectedException \Cake\ORM\Error\RecordNotFoundException
  216. * @return void
  217. */
  218. public function testMoveDownException() {
  219. $table = TableRegistry::get('MenuLinkTrees');
  220. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  221. $table->moveDown(500, 1);
  222. }
  223. /**
  224. * Tests the recover function
  225. *
  226. * @return void
  227. */
  228. public function testRecover() {
  229. $table = TableRegistry::get('NumberTrees');
  230. $table->addBehavior('Tree');
  231. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  232. $table->updateAll(['lft' => null, 'rght' => null], []);
  233. $table->recover();
  234. $result = $table->find()->order('lft')->hydrate(false)->toArray();
  235. $this->assertEquals($expected, $result);
  236. }
  237. /**
  238. * Tests the recover function with a custom scope
  239. *
  240. * @return void
  241. */
  242. public function testRecoverScoped() {
  243. $table = TableRegistry::get('MenuLinkTrees');
  244. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  245. $expected = $table->find()
  246. ->where(['menu' => 'main-menu'])
  247. ->order('lft')
  248. ->hydrate(false)
  249. ->toArray();
  250. $expected2 = $table->find()
  251. ->where(['menu' => 'categories'])
  252. ->order('lft')
  253. ->hydrate(false)
  254. ->toArray();
  255. $table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
  256. $table->recover();
  257. $result = $table->find()
  258. ->where(['menu' => 'main-menu'])
  259. ->order('lft')
  260. ->hydrate(false)
  261. ->toArray();
  262. $this->assertEquals($expected, $result);
  263. $result2 = $table->find()
  264. ->where(['menu' => 'categories'])
  265. ->order('lft')
  266. ->hydrate(false)
  267. ->toArray();
  268. $this->assertEquals($expected2, $result2);
  269. }
  270. /**
  271. * Tests adding a new orphan node
  272. *
  273. * @return void
  274. */
  275. public function testAddOrphan() {
  276. $table = TableRegistry::get('NumberTrees');
  277. $table->addBehavior('Tree');
  278. $entity = new Entity(
  279. ['name' => 'New Orphan', 'parent_id' => null],
  280. ['markNew' => true]
  281. );
  282. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  283. $this->assertSame($entity, $table->save($entity));
  284. $this->assertEquals(23, $entity->lft);
  285. $this->assertEquals(24, $entity->rght);
  286. $expected[] = $entity->toArray();
  287. $results = $table->find()->order('lft')->hydrate(false)->toArray();
  288. $this->assertEquals($expected, $results);
  289. }
  290. /**
  291. * Tests that adding a child node as a decendant of one of the roots works
  292. *
  293. * @return void
  294. */
  295. public function testAddMiddle() {
  296. $table = TableRegistry::get('NumberTrees');
  297. $table->addBehavior('Tree');
  298. $entity = new Entity(
  299. ['name' => 'laptops', 'parent_id' => 1],
  300. ['markNew' => true]
  301. );
  302. $this->assertSame($entity, $table->save($entity));
  303. $this->assertEquals(20, $entity->lft);
  304. $this->assertEquals(21, $entity->rght);
  305. $result = $table->find()->order('lft')->hydrate(false)->toArray();
  306. $table->recover();
  307. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  308. $this->assertEquals($expected, $result);
  309. }
  310. /**
  311. * Tests adding a leaf to the tree
  312. *
  313. * @return void
  314. */
  315. public function testAddLeaf() {
  316. $table = TableRegistry::get('NumberTrees');
  317. $table->addBehavior('Tree');
  318. $entity = new Entity(
  319. ['name' => 'laptops', 'parent_id' => 2],
  320. ['markNew' => true]
  321. );
  322. $this->assertSame($entity, $table->save($entity));
  323. $this->assertEquals(9, $entity->lft);
  324. $this->assertEquals(10, $entity->rght);
  325. $results = $table->find()->order('lft')->hydrate(false)->toArray();
  326. $table->recover();
  327. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  328. $this->assertEquals($expected, $results);
  329. }
  330. /**
  331. * Tests moving a subtree to the right
  332. *
  333. * @return void
  334. */
  335. public function testReParentSubTreeRight() {
  336. $table = TableRegistry::get('NumberTrees');
  337. $table->addBehavior('Tree');
  338. $entity = $table->get(2);
  339. $entity->parent_id = 6;
  340. $this->assertSame($entity, $table->save($entity));
  341. $this->assertEquals(11, $entity->lft);
  342. $this->assertEquals(18, $entity->rght);
  343. $result = $table->find()->order('lft')->hydrate(false);
  344. $expected = [1, 6, 7, 8, 9, 10, 2, 3, 4, 5, 11];
  345. $this->assertEquals($expected, $result->extract('id')->toArray());
  346. $numbers = [];
  347. $result->each(function($v) use (&$numbers) {
  348. $numbers[] = $v['lft'];
  349. $numbers[] = $v['rght'];
  350. });
  351. sort($numbers);
  352. $this->assertEquals(range(1, 22), $numbers);
  353. }
  354. /**
  355. * Tests moving a subtree to the left
  356. *
  357. * @return void
  358. */
  359. public function testReParentSubTreeLeft() {
  360. $table = TableRegistry::get('NumberTrees');
  361. $table->addBehavior('Tree');
  362. $entity = $table->get(6);
  363. $entity->parent_id = 2;
  364. $this->assertSame($entity, $table->save($entity));
  365. $this->assertEquals(9, $entity->lft);
  366. $this->assertEquals(18, $entity->rght);
  367. $result = $table->find()->order('lft')->hydrate(false)->toArray();
  368. $table->recover();
  369. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  370. $this->assertEquals($expected, $result);
  371. }
  372. /**
  373. * Test moving a leaft to the left
  374. *
  375. * @return void
  376. */
  377. public function testReParentLeafLeft() {
  378. $table = TableRegistry::get('NumberTrees');
  379. $table->addBehavior('Tree');
  380. $entity = $table->get(10);
  381. $entity->parent_id = 2;
  382. $this->assertSame($entity, $table->save($entity));
  383. $this->assertEquals(9, $entity->lft);
  384. $this->assertEquals(10, $entity->rght);
  385. $result = $table->find()->order('lft')->hydrate(false)->toArray();
  386. $table->recover();
  387. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  388. $this->assertEquals($expected, $result);
  389. }
  390. /**
  391. * Test moving a leaft to the left
  392. *
  393. * @return void
  394. */
  395. public function testReParentLeafRight() {
  396. $table = TableRegistry::get('NumberTrees');
  397. $table->addBehavior('Tree');
  398. $entity = $table->get(5);
  399. $entity->parent_id = 6;
  400. $this->assertSame($entity, $table->save($entity));
  401. $this->assertEquals(17, $entity->lft);
  402. $this->assertEquals(18, $entity->rght);
  403. $result = $table->find()->order('lft')->hydrate(false);
  404. $expected = [1, 2, 3, 4, 6, 7, 8, 9, 10, 5, 11];
  405. $this->assertEquals($expected, $result->extract('id')->toArray());
  406. $numbers = [];
  407. $result->each(function($v) use (&$numbers) {
  408. $numbers[] = $v['lft'];
  409. $numbers[] = $v['rght'];
  410. });
  411. sort($numbers);
  412. $this->assertEquals(range(1, 22), $numbers);
  413. }
  414. /**
  415. * Tests moving a subtree as a new root
  416. *
  417. * @return void
  418. */
  419. public function testRootingSubTree() {
  420. $table = TableRegistry::get('NumberTrees');
  421. $table->addBehavior('Tree');
  422. $entity = $table->get(2);
  423. $entity->parent_id = null;
  424. $this->assertSame($entity, $table->save($entity));
  425. $this->assertEquals(15, $entity->lft);
  426. $this->assertEquals(22, $entity->rght);
  427. $result = $table->find()->order('lft')->hydrate(false);
  428. $expected = [1, 6, 7, 8, 9, 10, 11, 2, 3, 4, 5];
  429. $this->assertEquals($expected, $result->extract('id')->toArray());
  430. $numbers = [];
  431. $result->each(function($v) use (&$numbers) {
  432. $numbers[] = $v['lft'];
  433. $numbers[] = $v['rght'];
  434. });
  435. sort($numbers);
  436. $this->assertEquals(range(1, 22), $numbers);
  437. }
  438. /**
  439. * Tests that trying to create a cycle throws an exception
  440. *
  441. * @expectedException RuntimeException
  442. * @expectedExceptionMessage Cannot use node "5" as parent for entity "2"
  443. * @return void
  444. */
  445. public function testReparentCycle() {
  446. $table = TableRegistry::get('NumberTrees');
  447. $table->addBehavior('Tree');
  448. $entity = $table->get(2);
  449. $entity->parent_id = 5;
  450. $table->save($entity);
  451. }
  452. /**
  453. * Tests deleting a leaf in the tree
  454. *
  455. * @return void
  456. */
  457. public function testDeleteLeaf() {
  458. $table = TableRegistry::get('NumberTrees');
  459. $table->addBehavior('Tree');
  460. $entity = $table->get(4);
  461. $this->assertTrue($table->delete($entity));
  462. $result = $table->find()->order('lft')->hydrate(false)->toArray();
  463. $table->recover();
  464. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  465. $this->assertEquals($expected, $result);
  466. }
  467. /**
  468. * Tests deleting a subtree
  469. *
  470. * @return void
  471. */
  472. public function testDeleteSubTree() {
  473. $table = TableRegistry::get('NumberTrees');
  474. $table->addBehavior('Tree');
  475. $entity = $table->get(6);
  476. $this->assertTrue($table->delete($entity));
  477. $result = $table->find()->order('lft')->hydrate(false)->toArray();
  478. $table->recover();
  479. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  480. $this->assertEquals($expected, $result);
  481. }
  482. /**
  483. * Test deleting a root node
  484. *
  485. * @return void
  486. */
  487. public function testDeleteRoot() {
  488. $table = TableRegistry::get('NumberTrees');
  489. $table->addBehavior('Tree');
  490. $entity = $table->get(1);
  491. $this->assertTrue($table->delete($entity));
  492. $result = $table->find()->order('lft')->hydrate(false)->toArray();
  493. $table->recover();
  494. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  495. $this->assertEquals($expected, $result);
  496. }
  497. }