Skip to content

Commit d3ed5a1

Browse files
committed
Merge remote-tracking branch 'tango/MAGETWO-93047-2' into TANGO-PR-22
2 parents b75955f + 74c533f commit d3ed5a1

File tree

2 files changed

+160
-1
lines changed

2 files changed

+160
-1
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+
}

0 commit comments

Comments
 (0)