TreeBehaviorTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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($countDirect, 2);
  72. // counts all the children of root
  73. $count = $this->table->childCount(1, false);
  74. $this->assertEquals($count, 9);
  75. // counts direct children
  76. $count = $this->table->childCount(2, false);
  77. $this->assertEquals($count, 3);
  78. // count children for a middle-node
  79. $count = $this->table->childCount(6, false);
  80. $this->assertEquals($count, 4);
  81. // count leaf children
  82. $count = $this->table->childCount(10, false);
  83. $this->assertEquals($count, 0);
  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($count, 2);
  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(['url LIKE' => '/what%']);
  100. }
  101. ]);
  102. $count = $table->childCount(2, false);
  103. $this->assertEquals($count, 2);
  104. }
  105. }