TreeBehaviorTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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(['parent_id IS' => null, 'menu' => 'main-menu'])
  161. ->order(['lft' => 'ASC'])
  162. ->all();
  163. $this->assertEquals([8, 1, 6], $nodes->extract('id')->toArray());
  164. }
  165. /**
  166. * Tests that moveUp() will throw an exception if the node was not found
  167. *
  168. * @expectedException \Cake\ORM\Error\RecordNotFoundException
  169. * @return void
  170. */
  171. public function testMoveUpException() {
  172. $table = TableRegistry::get('MenuLinkTrees');
  173. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  174. $table->moveUp(500, 1);
  175. }
  176. /**
  177. * Tests the moveDown() method
  178. *
  179. * @return void
  180. */
  181. public function testMoveDown() {
  182. $table = TableRegistry::get('MenuLinkTrees');
  183. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  184. // latest node, wont move
  185. $this->assertFalse($this->table->moveDown(8, 10));
  186. // edge cases
  187. $this->assertFalse($this->table->moveDown(8, 0));
  188. $this->assertFalse($this->table->moveUp(8, -10));
  189. // move inner node
  190. $result = $table->moveDown(2, 1);
  191. $nodes = $table->find('children', ['for' => 1])->all();
  192. $this->assertEquals([3, 4, 5, 2], $nodes->extract('id')->toArray());
  193. $this->assertTrue($result);
  194. // move leaf
  195. $this->assertFalse( $table->moveDown(5, 1));
  196. // move to last position
  197. $table->moveDown(1, true);
  198. $nodes = $table->find()
  199. ->select(['id'])
  200. ->where(['parent_id IS' => null, 'menu' => 'main-menu'])
  201. ->order(['lft' => 'ASC'])
  202. ->all();
  203. $this->assertEquals([6, 8, 1], $nodes->extract('id')->toArray());
  204. }
  205. /**
  206. * Tests that moveDown() will throw an exception if the node was not found
  207. *
  208. * @expectedException \Cake\ORM\Error\RecordNotFoundException
  209. * @return void
  210. */
  211. public function testMoveDownException() {
  212. $table = TableRegistry::get('MenuLinkTrees');
  213. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  214. $table->moveDown(500, 1);
  215. }
  216. /**
  217. * Tests the recover function
  218. *
  219. * @return void
  220. */
  221. public function testRecover() {
  222. $table = TableRegistry::get('NumberTrees');
  223. $table->addBehavior('Tree');
  224. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  225. $table->updateAll(['lft' => null, 'rght' => null], []);
  226. $table->recover();
  227. $result = $table->find()->order('lft')->hydrate(false)->toArray();
  228. $this->assertEquals($expected, $result);
  229. }
  230. /**
  231. * Tests the recover function with a custom scope
  232. *
  233. * @return void
  234. */
  235. public function testRecoverScoped() {
  236. $table = TableRegistry::get('MenuLinkTrees');
  237. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  238. $expected = $table->find()
  239. ->where(['menu' => 'main-menu'])
  240. ->order('lft')
  241. ->hydrate(false)
  242. ->toArray();
  243. $expected2 = $table->find()
  244. ->where(['menu' => 'categories'])
  245. ->order('lft')
  246. ->hydrate(false)
  247. ->toArray();
  248. $table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
  249. $table->recover();
  250. $result = $table->find()
  251. ->where(['menu' => 'main-menu'])
  252. ->order('lft')
  253. ->hydrate(false)
  254. ->toArray();
  255. $this->assertEquals($expected, $result);
  256. $result2 = $table->find()
  257. ->where(['menu' => 'categories'])
  258. ->order('lft')
  259. ->hydrate(false)
  260. ->toArray();
  261. $this->assertEquals($expected2, $result2);
  262. }
  263. /**
  264. * Tests adding a new orphan node
  265. *
  266. * @return void
  267. */
  268. public function testAddOrphan() {
  269. $table = TableRegistry::get('NumberTrees');
  270. $table->addBehavior('Tree');
  271. $entity = new Entity(
  272. ['name' => 'New Orphan', 'parent_id' => null],
  273. ['markNew' => true]
  274. );
  275. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  276. $this->assertSame($entity, $table->save($entity));
  277. $this->assertEquals(23, $entity->lft);
  278. $this->assertEquals(24, $entity->rght);
  279. $expected[] = $entity->toArray();
  280. $results = $table->find()->order('lft')->hydrate(false)->toArray();
  281. $this->assertEquals($expected, $results);
  282. }
  283. /**
  284. * Tests that adding a child node as a decendant of one of the roots works
  285. *
  286. * @return void
  287. */
  288. public function testAddMiddle() {
  289. $table = TableRegistry::get('NumberTrees');
  290. $table->addBehavior('Tree');
  291. $entity = new Entity(
  292. ['name' => 'laptops', 'parent_id' => 1],
  293. ['markNew' => true]
  294. );
  295. $this->assertSame($entity, $table->save($entity));
  296. $results = $table->find()->order('lft')->hydrate(false)->toArray();
  297. $this->assertEquals(20, $entity->lft);
  298. $this->assertEquals(21, $entity->rght);
  299. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  300. $table->recover();
  301. $result = $table->find()->order('lft')->hydrate(false)->toArray();
  302. $this->assertEquals($expected, $results);
  303. }
  304. }