TreeBehaviorTest.php 14 KB

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