Skip to content

Commit f377e18

Browse files
committed
Merge branch 'MAGETWO-85781-2' into 2.1.13-PR-0.5
2 parents c87720d + da1c45b commit f377e18

File tree

7 files changed

+288
-77
lines changed

7 files changed

+288
-77
lines changed

app/code/Magento/CatalogRule/Model/Rule/Condition/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function validate(\Magento\Framework\Model\AbstractModel $model)
2424
{
2525
$attrCode = $this->getAttribute();
2626
if ('category_ids' == $attrCode) {
27-
return $this->validateAttribute($model->getAvailableInCategories());
27+
return parent::validate($model);
2828
}
2929

3030
$oldAttrValue = $model->getData($attrCode);

app/code/Magento/CatalogRule/Test/Unit/Model/Rule/Condition/ProductTest.php

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,51 +28,41 @@ class ProductTest extends \PHPUnit_Framework_TestCase
2828
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute|\PHPUnit_Framework_MockObject_MockObject */
2929
protected $eavAttributeResource;
3030

31+
/** @var \Magento\Catalog\Model\ResourceModel\Category|\PHPUnit_Framework_MockObject_MockObject */
32+
private $category;
33+
34+
/** @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */
35+
private $connection;
36+
37+
/** @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject */
38+
private $dbSelect;
39+
3140
protected function setUp()
3241
{
33-
$this->config = $this->getMock('Magento\Eav\Model\Config', ['getAttribute'], [], '', false);
34-
$this->productModel = $this->getMock(
35-
'Magento\Catalog\Model\Product',
36-
[
37-
'__wakeup',
38-
'getAvailableInCategories',
39-
'hasData',
40-
'getData',
41-
'getId',
42-
'getStoreId',
43-
'getResource',
44-
'addAttributeToSelect',
45-
],
46-
[],
47-
'',
48-
false
49-
);
50-
$this->productResource = $this->getMock(
51-
'Magento\Catalog\Model\ResourceModel\Product',
52-
['loadAllAttributes',
53-
'getAttributesByCode',
54-
'getAttribute'
55-
],
56-
[],
57-
'',
58-
false
59-
);
60-
$this->eavAttributeResource = $this->getMock(
61-
'\Magento\Catalog\Model\ResourceModel\Eav\Attribute',
62-
[
63-
'__wakeup',
64-
'isAllowedForRuleCondition',
65-
'getDataUsingMethod',
66-
'getAttributeCode',
67-
'getFrontendLabel',
68-
'isScopeGlobal',
69-
'getBackendType',
70-
'getFrontendInput'
71-
],
72-
[],
73-
'',
74-
false
75-
);
42+
$this->config = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
43+
->disableOriginalConstructor()
44+
->setMethods(['getAttribute'])
45+
->getMock();
46+
$this->productModel = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
47+
->disableOriginalConstructor()
48+
->getMock();
49+
$this->category = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Category::class)
50+
->disableOriginalConstructor()
51+
->getMock();
52+
$this->productResource = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product::class)
53+
->setMethods(['loadAllAttributes', 'getAttributesByCode', 'getAttribute', 'getConnection', 'getTable'])
54+
->disableOriginalConstructor()
55+
->getMock();
56+
$this->connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
57+
->disableOriginalConstructor()
58+
->getMockForAbstractClass();
59+
$this->dbSelect = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
60+
->disableOriginalConstructor()
61+
->setMethods(['from', 'where'])
62+
->getMock();
63+
$this->eavAttributeResource = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
64+
->disableOriginalConstructor()
65+
->getMock();
7666

7767
$this->productResource->expects($this->any())->method('loadAllAttributes')
7868
->will($this->returnSelf());
@@ -89,11 +79,12 @@ protected function setUp()
8979

9080
$this->objectManagerHelper = new ObjectManagerHelper($this);
9181
$this->product = $this->objectManagerHelper->getObject(
92-
'Magento\CatalogRule\Model\Rule\Condition\Product',
82+
\Magento\CatalogRule\Model\Rule\Condition\Product::class,
9383
[
9484
'config' => $this->config,
9585
'product' => $this->productModel,
96-
'productResource' => $this->productResource
86+
'productResource' => $this->productResource,
87+
'category' => $this->category
9788
]
9889
);
9990
}
@@ -103,12 +94,27 @@ protected function setUp()
10394
*/
10495
public function testValidateMeetsCategory()
10596
{
97+
$categoryIdList = [1, 2, 3];
98+
99+
$this->productResource->expects($this->atLeastOnce())
100+
->method('getConnection')
101+
->willReturn($this->connection);
102+
$this->connection->expects($this->atLeastOnce())
103+
->method('select')
104+
->willReturn($this->dbSelect);
105+
$this->dbSelect->expects($this->atLeastOnce())
106+
->method('from')
107+
->willReturnSelf();
108+
$this->dbSelect->expects($this->atLeastOnce())
109+
->method('where')
110+
->willReturnSelf();
111+
$this->connection->expects($this->once())
112+
->method('fetchCol')
113+
->willReturn($categoryIdList);
106114
$this->product->setData('attribute', 'category_ids');
107115
$this->product->setData('value_parsed', '1');
108-
$this->product->setData('operator', '>=');
116+
$this->product->setData('operator', '{}');
109117

110-
$this->productModel->expects($this->once())->method('getAvailableInCategories')
111-
->will($this->returnValue('2'));
112118
$this->assertTrue($this->product->validate($this->productModel));
113119
}
114120

app/code/Magento/CatalogWidget/Model/Rule/Condition/Product.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
87
/**
98
* CatalogWidget Rule Product Condition data model
109
*/
1110
namespace Magento\CatalogWidget\Model\Rule\Condition;
1211

12+
use \Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
13+
use \Magento\Catalog\Model\ResourceModel\Eav\Attribute as EavAttribute;
14+
use \Magento\Store\Model\StoreManagerInterface;
15+
1316
/**
1417
* Class Product
18+
*
19+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1520
*/
1621
class Product extends \Magento\Rule\Model\Condition\Product\AbstractProduct
1722
{
@@ -28,7 +33,7 @@ class Product extends \Magento\Rule\Model\Condition\Product\AbstractProduct
2833
/**
2934
* Store manager
3035
*
31-
* @var \Magento\Store\Model\StoreManagerInterface
36+
* @var StoreManagerInterface
3237
*/
3338
protected $storeManager;
3439

@@ -41,8 +46,9 @@ class Product extends \Magento\Rule\Model\Condition\Product\AbstractProduct
4146
* @param \Magento\Catalog\Model\ResourceModel\Product $productResource
4247
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection $attrSetCollection
4348
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
44-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
49+
* @param StoreManagerInterface $storeManager
4550
* @param array $data
51+
* @param \Magento\Catalog\Model\ResourceModel\Category $category
4652
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
4753
*/
4854
public function __construct(
@@ -54,8 +60,9 @@ public function __construct(
5460
\Magento\Catalog\Model\ResourceModel\Product $productResource,
5561
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection $attrSetCollection,
5662
\Magento\Framework\Locale\FormatInterface $localeFormat,
57-
\Magento\Store\Model\StoreManagerInterface $storeManager,
58-
array $data = []
63+
StoreManagerInterface $storeManager,
64+
array $data = [],
65+
\Magento\Catalog\Model\ResourceModel\Category $category = null
5966
) {
6067
$this->storeManager = $storeManager;
6168
parent::__construct(
@@ -67,7 +74,8 @@ public function __construct(
6774
$productResource,
6875
$attrSetCollection,
6976
$localeFormat,
70-
$data
77+
$data,
78+
$category
7179
);
7280
}
7381

@@ -106,7 +114,7 @@ protected function _addSpecialAttributes(array &$attributes)
106114
/**
107115
* Add condition to collection
108116
*
109-
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
117+
* @param ProductCollection $collection
110118
* @return $this
111119
*/
112120
public function addToCollection($collection)
@@ -137,14 +145,12 @@ public function addToCollection($collection)
137145
}
138146

139147
/**
140-
* @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute
141-
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
148+
* @param EavAttribute $attribute
149+
* @param ProductCollection $collection
142150
* @return $this
143151
*/
144-
protected function addGlobalAttribute(
145-
\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute,
146-
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
147-
) {
152+
protected function addGlobalAttribute(EavAttribute $attribute, ProductCollection $collection)
153+
{
148154
$storeId = $this->storeManager->getStore()->getId();
149155

150156
switch ($attribute->getBackendType()) {
@@ -170,14 +176,12 @@ protected function addGlobalAttribute(
170176
}
171177

172178
/**
173-
* @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute
174-
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
179+
* @param EavAttribute $attribute
180+
* @param ProductCollection $collection
175181
* @return $this
176182
*/
177-
protected function addNotGlobalAttribute(
178-
\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute,
179-
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
180-
) {
183+
protected function addNotGlobalAttribute(EavAttribute $attribute, ProductCollection $collection)
184+
{
181185
$storeId = $this->storeManager->getStore()->getId();
182186
$values = $collection->getAllAttributeValues($attribute);
183187
$validEntities = [];

app/code/Magento/Rule/Model/Condition/Product/AbstractProduct.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
*/
1212
namespace Magento\Rule\Model\Condition\Product;
1313

14+
use Magento\Catalog\Model\Indexer\Category\Product\AbstractAction;
15+
use Magento\Framework\DB\Select;
16+
use Magento\Framework\DB\Sql\UnionExpression;
17+
1418
/**
1519
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
1620
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -79,6 +83,16 @@ abstract class AbstractProduct extends \Magento\Rule\Model\Condition\AbstractCon
7983
*/
8084
protected $_localeFormat;
8185

86+
/**
87+
* @var \Magento\Catalog\Model\ResourceModel\Category
88+
*/
89+
private $category;
90+
91+
/**
92+
* @var array
93+
*/
94+
private $categoryIdList = [];
95+
8296
/**
8397
* @param \Magento\Rule\Model\Condition\Context $context
8498
* @param \Magento\Backend\Helper\Data $backendData
@@ -89,6 +103,9 @@ abstract class AbstractProduct extends \Magento\Rule\Model\Condition\AbstractCon
89103
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection $attrSetCollection
90104
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
91105
* @param array $data
106+
* @param \Magento\Catalog\Model\ResourceModel\Category|null $category
107+
*
108+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
92109
*/
93110
public function __construct(
94111
\Magento\Rule\Model\Condition\Context $context,
@@ -99,7 +116,8 @@ public function __construct(
99116
\Magento\Catalog\Model\ResourceModel\Product $productResource,
100117
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection $attrSetCollection,
101118
\Magento\Framework\Locale\FormatInterface $localeFormat,
102-
array $data = []
119+
array $data = [],
120+
\Magento\Catalog\Model\ResourceModel\Category $category = null
103121
) {
104122
$this->_backendData = $backendData;
105123
$this->_config = $config;
@@ -108,6 +126,8 @@ public function __construct(
108126
$this->_productResource = $productResource;
109127
$this->_attrSetCollection = $attrSetCollection;
110128
$this->_localeFormat = $localeFormat;
129+
$this->category = $category ?: \Magento\Framework\App\ObjectManager::getInstance()
130+
->get(\Magento\Catalog\Model\ResourceModel\Category::class);
111131
parent::__construct($context, $data);
112132
}
113133

@@ -520,7 +540,8 @@ public function validate(\Magento\Framework\Model\AbstractModel $model)
520540
$attrCode = $this->getAttribute();
521541

522542
if ('category_ids' == $attrCode) {
523-
return $this->validateAttribute($model->getAvailableInCategories());
543+
$productId = (int)$model->getEntityId();
544+
return $this->validateAttribute($this->getCategoryIds($productId));
524545
} elseif (!isset($this->_entityAttributeValues[$model->getId()])) {
525546
if (!$model->getResource()) {
526547
return false;
@@ -721,4 +742,39 @@ protected function getEavAttributeTableAlias()
721742

722743
return 'at_' . $attribute->getAttributeCode();
723744
}
745+
746+
/**
747+
* Retrieve category id list where product is present.
748+
*
749+
* @param int $productId
750+
* @return array
751+
*/
752+
private function getCategoryIds($productId)
753+
{
754+
if (!isset($this->categoryIdList[$productId])) {
755+
$this->categoryIdList[$productId] = $this->_productResource->getConnection()->fetchCol(
756+
$this->getCategorySelect($productId, $this->category->getCategoryProductTable())
757+
);
758+
}
759+
760+
return $this->categoryIdList[$productId];
761+
}
762+
763+
/**
764+
* Returns DB select.
765+
*
766+
* @param int $productId
767+
* @param string $tableName
768+
* @return Select
769+
*/
770+
private function getCategorySelect($productId, $tableName)
771+
{
772+
return $this->_productResource->getConnection()->select()->from(
773+
$tableName,
774+
['category_id']
775+
)->where(
776+
'product_id = ?',
777+
$productId
778+
);
779+
}
724780
}

0 commit comments

Comments
 (0)