Skip to content

Commit e3158f1

Browse files
author
Mariana Lashch
committed
Merge branch '2.2-develop' of github.com:magento/magento2ce into mpi-PR-2008
2 parents 9b76a32 + eb84c53 commit e3158f1

File tree

6 files changed

+205
-14
lines changed

6 files changed

+205
-14
lines changed

app/code/Magento/Catalog/Plugin/Model/Attribute/Backend/AttributeValidation.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class AttributeValidation
1414
*/
1515
private $storeManager;
1616

17+
/**
18+
* @var array
19+
*/
20+
private $allowedEntityTypes;
21+
1722
/**
1823
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
1924
* @param array $allowedEntityTypes
@@ -30,6 +35,7 @@ public function __construct(
3035
* @param \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject
3136
* @param \Closure $proceed
3237
* @param \Magento\Framework\DataObject $entity
38+
* @throws \Magento\Framework\Exception\NoSuchEntityException
3339
* @return bool
3440
*/
3541
public function aroundValidate(
@@ -41,7 +47,7 @@ public function aroundValidate(
4147
return $entity instanceof $allowedEntity;
4248
}, $this->allowedEntityTypes)));
4349

44-
if ($isAllowedType && $this->storeManager->getStore()->getId() !== Store::DEFAULT_STORE_ID) {
50+
if ($isAllowedType && (int) $this->storeManager->getStore()->getId() !== Store::DEFAULT_STORE_ID) {
4551
$attrCode = $subject->getAttribute()->getAttributeCode();
4652
// Null is meaning "no value" which should be overridden by value from default scope
4753
if (array_key_exists($attrCode, $entity->getData()) && $entity->getData($attrCode) === null) {
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\Plugin\Model\Attribute\Backend;
8+
9+
use Magento\Catalog\Plugin\Model\Attribute\Backend\AttributeValidation;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Magento\Store\Api\Data\StoreInterface;
13+
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend;
14+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
15+
use Magento\Framework\DataObject;
16+
17+
class AttributeValidationTest extends \PHPUnit\Framework\TestCase
18+
{
19+
/**
20+
* @var AttributeValidation
21+
*/
22+
private $attributeValidation;
23+
24+
/**
25+
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
private $storeManagerMock;
28+
29+
/**
30+
* @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
private $storeMock;
33+
34+
/**
35+
* @var array
36+
*/
37+
private $allowedEntityTypes;
38+
39+
/**
40+
* @var \Callable
41+
*/
42+
private $proceedMock;
43+
44+
/**
45+
* @var bool
46+
*/
47+
private $isProceedMockCalled = false;
48+
49+
/**
50+
* @var AbstractBackend|\PHPUnit_Framework_MockObject_MockObject
51+
*/
52+
private $subjectMock;
53+
54+
/**
55+
* @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject
56+
*/
57+
private $attributeMock;
58+
59+
/**
60+
* @var DataObject|\PHPUnit_Framework_MockObject_MockObject
61+
*/
62+
private $entityMock;
63+
64+
protected function setUp()
65+
{
66+
$objectManager = new ObjectManager($this);
67+
68+
$this->attributeMock = $this->getMockBuilder(AbstractBackend::class)
69+
->setMethods(['getAttributeCode'])
70+
->getMockForAbstractClass();
71+
$this->subjectMock = $this->getMockBuilder(AbstractBackend::class)
72+
->setMethods(['getAttribute'])
73+
->getMockForAbstractClass();
74+
$this->subjectMock->expects($this->any())
75+
->method('getAttribute')
76+
->willReturn($this->attributeMock);
77+
78+
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
79+
->setMethods(['getId'])
80+
->getMockForAbstractClass();
81+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
82+
->setMethods(['getStore'])
83+
->getMockForAbstractClass();
84+
$this->storeManagerMock->expects($this->any())
85+
->method('getStore')
86+
->willReturn($this->storeMock);
87+
88+
$this->entityMock = $this->getMockBuilder(DataObject::class)
89+
->setMethods(['getData'])
90+
->getMock();
91+
92+
$this->allowedEntityTypes = [$this->entityMock];
93+
94+
$this->proceedMock = function () {
95+
$this->isProceedMockCalled = true;
96+
};
97+
98+
$this->attributeValidation = $objectManager->getObject(
99+
AttributeValidation::class,
100+
[
101+
'storeManager' => $this->storeManagerMock,
102+
'allowedEntityTypes' => $this->allowedEntityTypes,
103+
]
104+
);
105+
}
106+
107+
/**
108+
* @param bool $shouldProceedRun
109+
* @param bool $defaultStoreUsed
110+
* @param null|int|string $storeId
111+
* @dataProvider aroundValidateDataProvider
112+
* @throws \Magento\Framework\Exception\NoSuchEntityException
113+
* @return void
114+
*/
115+
public function testAroundValidate(bool $shouldProceedRun, bool $defaultStoreUsed, $storeId)
116+
{
117+
$this->isProceedMockCalled = false;
118+
$attributeCode = 'code';
119+
120+
$this->storeMock->expects($this->once())
121+
->method('getId')
122+
->willReturn($storeId);
123+
if ($defaultStoreUsed) {
124+
$this->attributeMock->expects($this->once())
125+
->method('getAttributeCode')
126+
->willReturn($attributeCode);
127+
$this->entityMock->expects($this->at(0))
128+
->method('getData')
129+
->willReturn([$attributeCode => null]);
130+
$this->entityMock->expects($this->at(1))
131+
->method('getData')
132+
->with($attributeCode)
133+
->willReturn(null);
134+
}
135+
136+
$this->attributeValidation->aroundValidate($this->subjectMock, $this->proceedMock, $this->entityMock);
137+
$this->assertSame($shouldProceedRun, $this->isProceedMockCalled);
138+
}
139+
140+
/**
141+
* Data provider for testAroundValidate
142+
* @return array
143+
*/
144+
public function aroundValidateDataProvider(): array
145+
{
146+
return [
147+
[true, false, '0'],
148+
[true, false, 0],
149+
[true, false, null],
150+
[false, true, 1],
151+
];
152+
}
153+
}

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,22 @@ public function addToCollection($collection)
124124
{
125125
$attribute = $this->getAttributeObject();
126126

127-
if ($attribute->getUsedInProductListing() && $collection->isEnabledFlat()) {
128-
$alias = array_keys($collection->getSelect()->getPart('from'))[0];
129-
$this->joinedAttributes[$attribute->getAttributeCode()] = $alias . '.' . $attribute->getAttributeCode();
127+
if ($collection->isEnabledFlat()) {
128+
if ($this->isEnabledInFlat($attribute)) {
129+
$alias = array_keys($collection->getSelect()->getPart('from'))[0];
130+
$this->joinedAttributes[$attribute->getAttributeCode()] = $alias . '.' . $attribute->getAttributeCode();
131+
} else {
132+
$alias = 'at_' . $attribute->getAttributeCode();
133+
if (!in_array($alias, array_keys($collection->getSelect()->getPart('from')))) {
134+
$collection->joinAttribute(
135+
$attribute->getAttributeCode(),
136+
'catalog_product/'.$attribute->getAttributeCode(),
137+
'entity_id'
138+
);
139+
}
140+
141+
$this->joinedAttributes[$attribute->getAttributeCode()] = $alias . '.value';
142+
}
130143
return $this;
131144
}
132145

@@ -245,4 +258,15 @@ public function collectValidatedAttributes($productCollection)
245258
{
246259
return $this->addToCollection($productCollection);
247260
}
261+
262+
/**
263+
* @param \Magento\Framework\DataObject $attribute
264+
* @return bool
265+
*/
266+
private function isEnabledInFlat(\Magento\Framework\DataObject $attribute): bool
267+
{
268+
return $attribute->getData('backend_type') === 'static'
269+
|| (int) $attribute->getData('used_in_product_listing') === 1
270+
|| (int) $attribute->getData('used_for_sort_by') === 1;
271+
}
248272
}

app/code/Magento/ImportExport/Helper/Report.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public function __construct(
5252
*/
5353
public function getExecutionTime($time)
5454
{
55-
$reportTime = $this->timeZone->date($time, $this->timeZone->getConfigTimezone());
55+
$reportTime = $this->timeZone->date($time);
5656
$timeDiff = $reportTime->diff($this->timeZone->date());
57-
return $timeDiff->format('%H:%M:%S');
57+
return $timeDiff->format('%H:%I:%S');
5858
}
5959

6060
/**

app/code/Magento/ImportExport/Test/Unit/Helper/ReportTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,17 @@ protected function setUp()
8989
*/
9090
public function testGetExecutionTime()
9191
{
92-
$time = '01:02:03';
93-
$this->timezone->expects($this->any())->method('date')->willReturnSelf();
94-
$this->timezone->expects($this->any())->method('getConfigTimezone')->willReturn('America/Los_Angeles');
95-
$this->timezone->expects($this->any())->method('diff')->willReturnSelf();
96-
$this->timezone->expects($this->any())->method('format')->willReturn($time);
97-
$this->assertEquals($time, $this->report->getExecutionTime($time));
92+
$startDate = '2000-01-01 01:01:01';
93+
$endDate = '2000-01-01 02:03:04';
94+
$executionTime = '01:02:03';
95+
96+
$startDateMock = $this->createTestProxy(\DateTime::class, ['time' => $startDate]);
97+
$endDateMock = $this->createTestProxy(\DateTime::class, ['time' => $endDate]);
98+
$this->timezone->method('date')
99+
->withConsecutive([$startDate], [])
100+
->willReturnOnConsecutiveCalls($startDateMock, $endDateMock);
101+
102+
$this->assertEquals($executionTime, $this->report->getExecutionTime($startDate));
98103
}
99104

100105
/**

dev/tests/integration/testsuite/Magento/CatalogWidget/Block/Product/ProductListTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ public function testCreateCollectionWithMultipleSkuCondition()
8989
}
9090

9191
/**
92-
* Test product list widget can process condition with dropdown type of attribute which has Store Scope
92+
* Test product list widget can process condition with dropdown type of attribute
9393
*
9494
* @magentoDbIsolation disabled
9595
* @magentoDataFixture Magento/Catalog/_files/products_with_dropdown_attribute.php
9696
*/
97-
public function testCreateCollectionWithDropdownAttributeStoreScope()
97+
public function testCreateCollectionWithDropdownAttribute()
9898
{
9999
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
100100
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
@@ -118,6 +118,9 @@ public function testCreateCollectionWithDropdownAttributeStoreScope()
118118
$attribute->setUsedInProductListing(0);
119119
$attribute->save();
120120
$this->performAssertions(2);
121+
$attribute->setIsGlobal(1);
122+
$attribute->save();
123+
$this->performAssertions(2);
121124
}
122125

123126
/**

0 commit comments

Comments
 (0)