Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryTreeTest.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Model;
7 
17 class CategoryTreeTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $_model;
23 
24  protected function setUp()
25  {
27  \Magento\Catalog\Model\Category::class
28  );
29  }
30 
37  protected function loadCategory($categoryId)
38  {
39  $this->_model->setData([]);
40  $this->_model->load($categoryId);
41  return $this->_model;
42  }
43 
44  public function testMovePosition()
45  {
46  //move category 9 to new parent 6 with afterCategoryId = null
47  $category = $this->loadCategory(9);
48  $category->move(6, null);
49  $category = $this->loadCategory(9);
50  $this->assertEquals(1, $category->getPosition(), 'Position must be 1, if $afterCategoryId was null|false|0');
51  $category = $this->loadCategory(10);
52  $this->assertEquals(5, $category->getPosition(), 'Category 10 position must decrease after Category 9 moved');
53  $category = $this->loadCategory(11);
54  $this->assertEquals(6, $category->getPosition(), 'Category 11 position must decrease after Category 9 moved');
55  $category = $this->loadCategory(6);
56  $this->assertEquals(2, $category->getPosition(), 'Category 6 position must be the same');
57 
58  //move category 11 to new parent 6 with afterCategoryId = 9
59  $category = $this->loadCategory(11);
60  $category->move(6, 9);
61  $category = $this->loadCategory(11);
62  $this->assertEquals(2, $category->getPosition(), 'Category 11 position must be after category 9');
63  $category = $this->loadCategory(10);
64  $this->assertEquals(5, $category->getPosition(), 'Category 10 position must be the same');
65  $category = $this->loadCategory(9);
66  $this->assertEquals(1, $category->getPosition(), 'Category 9 position must be 1');
67  }
68 
69  public function testMove()
70  {
71  $this->_model->load(7);
72  $this->assertEquals(2, $this->_model->getParentId());
73  $this->_model->move(6, 0);
74  /* load is not enough to reset category data */
75  $this->_model->setData([]);
76  $this->_model->load(7);
77  $this->assertEquals(6, $this->_model->getParentId());
78  }
79 
83  public function testMoveWrongParent()
84  {
85  $this->_model->load(7);
86  $this->_model->move(100, 0);
87  }
88 
92  public function testMoveWrongId()
93  {
94  $this->_model->move(100, 0);
95  }
96 
101  public function testGetUrlPath()
102  {
103  $this->assertNull($this->_model->getUrlPath());
104  $this->_model->load(4);
105  $this->assertEquals('category-1/category-1-1', $this->_model->getUrlPath());
106  }
107 
108  public function testGetParentId()
109  {
110  $this->assertEquals(0, $this->_model->getParentId());
111  $this->_model->unsetData();
112  $this->_model->load(4);
113  $this->assertEquals(3, $this->_model->getParentId());
114  }
115 
116  public function testGetParentIds()
117  {
118  $this->assertEquals([], $this->_model->getParentIds());
119  $this->_model->unsetData();
120  $this->_model->load(4);
121  $this->assertContains(3, $this->_model->getParentIds());
122  $this->assertNotContains(4, $this->_model->getParentIds());
123  }
124 
125  public function testGetChildren()
126  {
127  $this->_model->load(3);
128  $this->assertEquals(array_diff([4, 13], explode(',', $this->_model->getChildren())), []);
129  }
130 
131  public function testGetChildrenSorted()
132  {
133  $this->_model->load(2);
134  $unsorted = explode(',', $this->_model->getChildren());
135  sort($unsorted);
136  $this->assertEquals(array_diff($unsorted, explode(',', $this->_model->getChildren(true, true, true))), []);
137  }
138 
139  public function testGetPathInStore()
140  {
141  $this->_model->load(5);
142  $this->assertEquals('5,4,3', $this->_model->getPathInStore());
143  }
144 
145  public function testGetAllChildren()
146  {
147  $this->_model->load(4);
148  $this->assertEquals('4,5', $this->_model->getAllChildren());
149  $this->_model->load(5);
150  $this->assertEquals('5', $this->_model->getAllChildren());
151  }
152 
153  public function testGetPathIds()
154  {
155  $this->assertEquals([''], $this->_model->getPathIds());
156  $this->_model->setPathIds([1]);
157  $this->assertEquals([1], $this->_model->getPathIds());
158 
159  $this->_model->unsetData();
160  $this->_model->setPath('1/2/3');
161  $this->assertEquals([1, 2, 3], $this->_model->getPathIds());
162  }
163 
164  public function testGetLevel()
165  {
166  $this->assertEquals(0, $this->_model->getLevel());
167  $this->_model->setData('level', 1);
168  $this->assertEquals(1, $this->_model->getLevel());
169  }
170 
171  public function testGetAnchorsAbove()
172  {
173  $this->_model->load(4);
174  $this->assertContains(3, $this->_model->getAnchorsAbove());
175  $this->_model->load(5);
176  $this->assertContains(4, $this->_model->getAnchorsAbove());
177  }
178 
179  public function testGetParentCategories()
180  {
181  $this->_model->load(5);
182  $parents = $this->_model->getParentCategories();
183  $this->assertEquals(3, count($parents));
184  }
185 
187  {
188  $this->_model->load(1);
189  $parents = $this->_model->getParentCategories();
190  $this->assertEquals(0, count($parents));
191  }
192 
193  public function testGetChildrenCategories()
194  {
195  $this->_model->load(3);
196  $children = $this->_model->getChildrenCategories();
197  $this->assertEquals(2, count($children));
198  }
199 
201  {
202  $this->_model->load(5);
203  $children = $this->_model->getChildrenCategories();
204  $this->assertEquals(0, count($children));
205  }
206 
207  public function testGetParentDesignCategory()
208  {
209  $this->_model->load(5);
210  $parent = $this->_model->getParentDesignCategory();
211  $this->assertEquals(5, $parent->getId());
212  }
213 
214  public function testIsInRootCategoryList()
215  {
216  $this->assertFalse($this->_model->isInRootCategoryList());
217  $this->_model->unsetData();
218  $this->_model->load(3);
219  $this->assertTrue($this->_model->isInRootCategoryList());
220  }
221 }
$children
Definition: actions.phtml:11