Skip to content

Commit a4d9976

Browse files
authored
Merge pull request #773 from magento-tsg/2.0-pr10
[TSG] Backporting for 2.0 (pr10)
2 parents 1c4d908 + 67ed003 commit a4d9976

File tree

20 files changed

+530
-54
lines changed

20 files changed

+530
-54
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Category/Collection/Factory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Category\Collection;
77

8+
/**
9+
* Category collection factory.
10+
*/
811
class Factory
912
{
1013
/**

app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Magento\Catalog\Model\ResourceModel\Category;
77

88
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
9+
use Magento\Catalog\Model\ResourceModel\Category\Flat\CollectionFactory as CategoryFlatCollectionFactory;
10+
use Magento\Framework\App\ObjectManager;
911

1012
/**
1113
* Category flat model
@@ -68,6 +70,7 @@ class Flat extends \Magento\Indexer\Model\ResourceModel\AbstractResource
6870
* Category collection factory
6971
*
7072
* @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
73+
* @deprecated
7174
*/
7275
protected $_categoryCollectionFactory;
7376

@@ -78,6 +81,11 @@ class Flat extends \Magento\Indexer\Model\ResourceModel\AbstractResource
7881
*/
7982
protected $_categoryFactory;
8083

84+
/**
85+
* @var CategoryFlatCollectionFactory
86+
*/
87+
private $categoryFlatCollectionFactory;
88+
8189
/**
8290
* Class constructor
8391
*
@@ -399,7 +407,7 @@ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $as
399407
);
400408
$parentPath = $this->getConnection()->fetchOne($select);
401409

402-
$collection = $this->_categoryCollectionFactory
410+
$collection = $this->getCategoryFlatCollectionFactory()
403411
->create()
404412
->addNameToResult()
405413
->addUrlRewriteToResult()
@@ -690,4 +698,19 @@ public function getProductsPosition($category)
690698

691699
return $this->getConnection()->fetchPairs($select, $bind);
692700
}
701+
702+
/**
703+
* Get instance of CategoryFlatCollectionFactory
704+
*
705+
* @return CategoryFlatCollectionFactory
706+
*/
707+
private function getCategoryFlatCollectionFactory()
708+
{
709+
if (!$this->categoryFlatCollectionFactory instanceof CategoryFlatCollectionFactory) {
710+
$this->categoryFlatCollectionFactory = ObjectManager::getInstance()
711+
->get(CategoryFlatCollectionFactory::class);
712+
}
713+
714+
return $this->categoryFlatCollectionFactory;
715+
}
693716
}
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Category;
8+
9+
use Magento\Catalog\Model\ResourceModel\Category\Flat\CollectionFactory;
10+
use Magento\Catalog\Model\ResourceModel\Category\Flat\Collection;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\Catalog\Model\ResourceModel\Category\Flat;
13+
use Magento\Framework\DB\Select;
14+
use Magento\Framework\DB\Adapter\AdapterInterface as Adapter;
15+
use Magento\Framework\App\ResourceConnection;
16+
use Magento\Framework\Model\ResourceModel\Db\Context;
17+
use Magento\Store\Model\Store;
18+
use Magento\Store\Model\StoreManagerInterface;
19+
20+
/**
21+
* Category flat model test
22+
*
23+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24+
*/
25+
class FlatTest extends \PHPUnit_Framework_TestCase
26+
{
27+
const STORE_ID = 1;
28+
const TABLE_NAME = 'test_table';
29+
const PARENT_PATH = '1';
30+
const SORTED = false;
31+
const PARENT = 1;
32+
const RECURSION_LEVEL = 0;
33+
34+
/**
35+
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
private $categoryCollectionFactoryMock;
38+
39+
/**
40+
* @var Collection|\PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
private $categoryCollectionMock;
43+
44+
/**
45+
* @var Flat
46+
*/
47+
private $model;
48+
49+
/**
50+
* @var ObjectManager
51+
*/
52+
private $objectManager;
53+
54+
/**
55+
* @var Select|\PHPUnit_Framework_MockObject_MockObject
56+
*/
57+
private $selectMock;
58+
59+
/**
60+
* @var Adapter|\PHPUnit_Framework_MockObject_MockObject
61+
*/
62+
private $connectionMock;
63+
64+
/**
65+
* @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
66+
*/
67+
private $resourceMock;
68+
69+
/**
70+
* @var Context|\PHPUnit_Framework_MockObject_MockObject
71+
*/
72+
private $contextMock;
73+
74+
/**
75+
* @var Store|\PHPUnit_Framework_MockObject_MockObject
76+
*/
77+
private $storeMock;
78+
79+
/**
80+
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
81+
*/
82+
private $storeManagerMock;
83+
84+
/**
85+
* {@inheritdoc}
86+
*/
87+
protected function setUp()
88+
{
89+
$this->objectManager = new ObjectManager($this);
90+
91+
$this->selectMock = $this->getMockBuilder(Select::class)
92+
->disableOriginalConstructor()
93+
->setMethods(['where', 'from'])
94+
->getMock();
95+
$this->selectMock->expects($this->once())
96+
->method('where')
97+
->willReturn($this->selectMock);
98+
$this->selectMock->expects($this->once())
99+
->method('from')
100+
->willReturn($this->selectMock);
101+
$this->connectionMock = $this->getMockBuilder(Adapter::class)
102+
->getMockForAbstractClass();
103+
$this->connectionMock->expects($this->once())
104+
->method('select')
105+
->willReturn($this->selectMock);
106+
$this->connectionMock->expects($this->once())
107+
->method('fetchOne')
108+
->with($this->selectMock)
109+
->willReturn(self::PARENT_PATH);
110+
$this->resourceMock = $this->getMockBuilder(ResourceConnection::class)
111+
->disableOriginalConstructor()
112+
->setMethods(['getConnection', 'getTableName'])
113+
->getMock();
114+
$this->resourceMock->expects($this->any())
115+
->method('getConnection')
116+
->willReturn($this->connectionMock);
117+
$this->resourceMock->expects($this->any())
118+
->method('getTableName')
119+
->willReturn(self::TABLE_NAME);
120+
$this->contextMock = $this->getMockBuilder(Context::class)
121+
->disableOriginalConstructor()
122+
->setMethods(['getResources'])
123+
->getMock();
124+
$this->contextMock->expects($this->any())
125+
->method('getResources')
126+
->willReturn($this->resourceMock);
127+
128+
$this->storeMock = $this->getMockBuilder(Store::class)
129+
->disableOriginalConstructor()
130+
->setMethods(['getId'])
131+
->getMock();
132+
$this->storeMock->expects($this->any())
133+
->method('getId')
134+
->willReturn(self::STORE_ID);
135+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
136+
->getMockForAbstractClass();
137+
$this->storeManagerMock->expects($this->any())
138+
->method('getStore')
139+
->willReturn($this->storeMock);
140+
}
141+
142+
public function testGetCategories()
143+
{
144+
$this->categoryCollectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
145+
->disableOriginalConstructor()
146+
->setMethods(['create'])
147+
->getMock();
148+
$this->categoryCollectionMock = $this->getMockBuilder(Collection::class)
149+
->disableOriginalConstructor()
150+
->setMethods(
151+
[
152+
'addNameToResult',
153+
'addUrlRewriteToResult',
154+
'addParentPathFilter',
155+
'addStoreFilter',
156+
'addIsActiveFilter',
157+
'addAttributeToFilter',
158+
'addSortedField',
159+
'load'
160+
]
161+
)
162+
->getMock();
163+
$this->categoryCollectionMock->expects($this->once())
164+
->method('addNameToResult')
165+
->willReturn($this->categoryCollectionMock);
166+
$this->categoryCollectionMock->expects($this->once())
167+
->method('addUrlRewriteToResult')
168+
->willReturn($this->categoryCollectionMock);
169+
$this->categoryCollectionMock->expects($this->once())
170+
->method('addParentPathFilter')
171+
->with(self::PARENT_PATH)
172+
->willReturn($this->categoryCollectionMock);
173+
$this->categoryCollectionMock->expects($this->once())
174+
->method('addStoreFilter')
175+
->willReturn($this->categoryCollectionMock);
176+
$this->categoryCollectionMock->expects($this->once())
177+
->method('addIsActiveFilter')
178+
->willReturn($this->categoryCollectionMock);
179+
$this->categoryCollectionMock->expects($this->once())
180+
->method('addSortedField')
181+
->with(self::SORTED)
182+
->willReturn($this->categoryCollectionMock);
183+
$this->categoryCollectionMock->expects($this->once())
184+
->method('addAttributeToFilter')
185+
->with('include_in_menu', 1)
186+
->willReturn($this->categoryCollectionMock);
187+
$this->categoryCollectionMock->expects($this->once())
188+
->method('load')
189+
->willReturn($this->categoryCollectionMock);
190+
$this->categoryCollectionFactoryMock->expects($this->once())
191+
->method('create')
192+
->willReturn($this->categoryCollectionMock);
193+
194+
$this->model = $this->objectManager->getObject(
195+
Flat::class,
196+
[
197+
'context' => $this->contextMock,
198+
'storeManager' => $this->storeManagerMock,
199+
]
200+
);
201+
202+
$reflection = new \ReflectionClass(get_class($this->model));
203+
$reflectionProperty = $reflection->getProperty('categoryFlatCollectionFactory');
204+
$reflectionProperty->setAccessible(true);
205+
$reflectionProperty->setValue($this->model, $this->categoryCollectionFactoryMock);
206+
207+
$this->assertEquals(
208+
$this->model->getCategories(self::PARENT, self::RECURSION_LEVEL, self::SORTED, true),
209+
$this->categoryCollectionMock
210+
);
211+
}
212+
}

dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Category/Edit/CategoryForm.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,19 @@ public function fill(FixtureInterface $fixture, SimpleElement $element = null)
5959

6060
return $this->fillTabs($tabs, $element);
6161
}
62+
63+
/**
64+
* Return category Id.
65+
*
66+
* @return string
67+
*/
68+
public function getCategoryId()
69+
{
70+
$categoryId = '';
71+
if (preg_match('/\/id\/(?<id>\d+)(?:\/)?/', $this->browser->getUrl(), $matches)) {
72+
$categoryId = $matches['id'];
73+
}
74+
75+
return $categoryId;
76+
}
6277
}

dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Category/Edit/CategoryForm.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
<strategy>css selector</strategy>
1313
<wrapper>general</wrapper>
1414
<fields>
15+
<use_default_name>
16+
<selector>#group_4name_default</selector>
17+
<input>checkbox</input>
18+
</use_default_name>
1519
<is_active>
1620
<input>select</input>
1721
</is_active>

dev/tests/functional/tests/app/Magento/Catalog/Test/Constraint/AssertCategoryForm.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class AssertCategoryForm extends AbstractAssertForm
2626
* @var array
2727
*/
2828
protected $skippedFixtureFields = [
29-
'parent_id'
29+
'parent_id',
30+
'id'
3031
];
3132

3233
/**

0 commit comments

Comments
 (0)