TreeBehaviorTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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->assertEquals(false, $this->table->moveUp(1, 10));
  146. // edge cases
  147. $this->assertEquals(false, $this->table->moveUp(1, 0));
  148. $this->assertEquals(false, $this->table->moveUp(1, -10));
  149. // move inner node
  150. $nodeIds = [];
  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->assertEquals(true, $result);
  155. // move leaf
  156. $this->assertEquals(false, $table->moveUp(5, 1));
  157. // move to first position
  158. $table->moveUp(8, true);
  159. $nodes = $table->find()
  160. ->select(['id'])
  161. ->where(['parent_id' => 0, 'menu' => 'main-menu'])
  162. ->order(['lft' => 'ASC'])
  163. ->all();
  164. $this->assertEquals([8, 1, 6], $nodes->extract('id')->toArray());
  165. }
  166. /**
  167. * Tests that moveUp() will throw an exception if the node was not found
  168. *
  169. * @expectedException \Cake\ORM\Error\RecordNotFoundException
  170. * @return void
  171. */
  172. public function testMoveUpException() {
  173. $table = TableRegistry::get('MenuLinkTrees');
  174. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  175. $table->moveUp(500, 1);
  176. }
  177. /**
  178. * Tests the moveDown() method
  179. *
  180. * @return void
  181. */
  182. public function testMoveDown() {
  183. $table = TableRegistry::get('MenuLinkTrees');
  184. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  185. // latest node, wont move
  186. $this->assertEquals(false, $this->table->moveDown(8, 10));
  187. // edge cases
  188. $this->assertEquals(false, $this->table->moveDown(8, 0));
  189. $this->assertEquals(false, $this->table->moveUp(8, -10));
  190. // move inner node
  191. $nodeIds = [];
  192. $result = $table->moveDown(2, 1);
  193. $nodes = $table->find('children', ['for' => 1])->all();
  194. $this->assertEquals([3, 4, 5, 2], $nodes->extract('id')->toArray());
  195. $this->assertEquals(true, $result);
  196. // move leaf
  197. $this->assertEquals(false, $table->moveDown(5, 1));
  198. // move to last position
  199. $table->moveDown(1, true);
  200. $nodes = $table->find()
  201. ->select(['id'])
  202. ->where(['parent_id' => 0, 'menu' => 'main-menu'])
  203. ->order(['lft' => 'ASC'])
  204. ->all();
  205. $this->assertEquals([6, 8, 1], $nodes->extract('id')->toArray());
  206. }
  207. /**
  208. * Tests that moveDown() will throw an exception if the node was not found
  209. *
  210. * @expectedException \Cake\ORM\Error\RecordNotFoundException
  211. * @return void
  212. */
  213. public function testMoveDownException() {
  214. $table = TableRegistry::get('MenuLinkTrees');
  215. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  216. $table->moveDown(500, 1);
  217. }
  218. /**
  219. * Tests the recover function
  220. *
  221. * @return void
  222. */
  223. public function testRecover() {
  224. $table = TableRegistry::get('NumberTrees');
  225. $table->addBehavior('Tree');
  226. $expected = $table->find()->order('lft')->hydrate(false)->toArray();
  227. $table->updateAll(['lft' => null, 'rght' => null], []);
  228. $table->recover();
  229. $result = $table->find()->order('lft')->hydrate(false)->toArray();
  230. $this->assertEquals($expected, $result);
  231. }
  232. /**
  233. * Tests the recover function with a custom scope
  234. *
  235. * @return void
  236. */
  237. public function testRecoverScoped() {
  238. $table = TableRegistry::get('MenuLinkTrees');
  239. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  240. $expected = $table->find()
  241. ->where(['menu' => 'main-menu'])
  242. ->order('lft')
  243. ->hydrate(false)
  244. ->toArray();
  245. $expected2 = $table->find()
  246. ->where(['menu' => 'categories'])
  247. ->order('lft')
  248. ->hydrate(false)
  249. ->toArray();
  250. $table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
  251. $table->recover();
  252. $result = $table->find()
  253. ->where(['menu' => 'main-menu'])
  254. ->order('lft')
  255. ->hydrate(false)
  256. ->toArray();
  257. $this->assertEquals($expected, $result);
  258. $result2 = $table->find()
  259. ->where(['menu' => 'categories'])
  260. ->order('lft')
  261. ->hydrate(false)
  262. ->toArray();
  263. $this->assertEquals($expected2, $result2);
  264. }
  265. }