TreeBehaviorTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 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])
  116. ->all();
  117. foreach ($nodes as $node) {
  118. $nodeIds[] = $node->id;
  119. }
  120. $this->assertEquals([2, 3, 4, 5], $nodeIds);
  121. // unexisting node
  122. $query = $table->find('children', ['for' => 500]);
  123. $this->assertEquals($query, $table->find('children', ['for' => 500]));
  124. // leaf
  125. $nodeIds = [];
  126. $nodes = $table->find('children', ['for' => 5])->all();
  127. foreach ($nodes as $node) {
  128. $nodeIds[] = $node->id;
  129. }
  130. $this->assertEquals(0, count($nodeIds));
  131. // direct children
  132. $nodes = $table->find('children', ['for' => 1, 'direct' => true])->all();
  133. foreach ($nodes as $node) {
  134. $nodeIds[] = $node->id;
  135. }
  136. $this->assertEquals([2, 3], $nodeIds);
  137. }
  138. /**
  139. * Tests the moveUp() method
  140. *
  141. * @return void
  142. */
  143. public function testMoveUp() {
  144. $table = TableRegistry::get('MenuLinkTrees');
  145. $table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
  146. // top level, wont move
  147. $this->assertEquals(false, $this->table->moveUp(1, 10));
  148. // edge cases
  149. $this->assertEquals(false, $this->table->moveUp(1, 0));
  150. $this->assertEquals(false, $this->table->moveUp(1, -10));
  151. // move inner node
  152. $nodeIds = [];
  153. $result = $table->moveUp(3, 1);
  154. $nodes = $table->find('children', ['for' => 1])->all();
  155. foreach ($nodes as $node) {
  156. $nodeIds[] = $node->id;
  157. }
  158. $this->assertEquals([3, 4, 5, 2], $nodeIds);
  159. $this->assertEquals(true, $result);
  160. // move leaf
  161. $this->assertEquals(false, $table->moveUp(5, 1));
  162. // move to first position
  163. $table->moveUp(8, true);
  164. $nodeIds = [];
  165. $results = $table->find()
  166. ->select(['id'])
  167. ->where(['parent_id' => 0, 'menu' => 'main-menu'])
  168. ->order(['lft' => 'ASC'])
  169. ->all();
  170. foreach ($results as $node) {
  171. $nodeIds[] = $node->id;
  172. }
  173. $this->assertEquals([8, 1, 6], $nodeIds);
  174. }
  175. }