Skip to content

Commit 9de4a7e

Browse files
authored
Merge pull request #5068 from magento-tsg/2.3-develop-com-pr7
[TSG-Commerce] Tests for 2.3 (pr7) (2.3-develop)
2 parents c7ca62e + da284ab commit 9de4a7e

File tree

15 files changed

+1124
-1
lines changed

15 files changed

+1124
-1
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\Product\Attribute\Save;
9+
10+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Eav\Api\AttributeRepositoryInterface;
14+
use Magento\Eav\Model\Entity\Attribute\Exception;
15+
use Magento\Framework\ObjectManagerInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Base class for text product attributes
21+
*/
22+
abstract class AbstractAttributeTest extends TestCase
23+
{
24+
/** @var ObjectManagerInterface */
25+
protected $objectManager;
26+
27+
/** @var AttributeRepositoryInterface */
28+
protected $attributeRepository;
29+
30+
/** @var ProductRepositoryInterface */
31+
protected $productRepository;
32+
33+
/** @var ProductAttributeInterface */
34+
protected $attribute;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function setUp()
40+
{
41+
parent::setUp();
42+
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
45+
$this->attributeRepository = $this->objectManager->create(AttributeRepositoryInterface::class);
46+
}
47+
48+
/**
49+
* @dataProvider productProvider
50+
* @param $productSku
51+
* @return void
52+
*/
53+
public function testSaveAttribute(string $productSku): void
54+
{
55+
$product = $this->setAttributeValueAndValidate($productSku, $this->getDefaultAttributeValue());
56+
$product = $this->productRepository->save($product);
57+
$this->assertEquals($this->getDefaultAttributeValue(), $product->getData($this->getAttributeCode()));
58+
}
59+
60+
/**
61+
* @dataProvider productProvider
62+
* @param string $productSku
63+
* @return void
64+
*/
65+
public function testRequiredAttribute(string $productSku): void
66+
{
67+
$this->expectException(Exception::class);
68+
$messageFormat = 'The "%s" attribute value is empty. Set the attribute and try again.';
69+
$this->expectExceptionMessage(
70+
(string)__(sprintf($messageFormat, $this->getAttribute()->getDefaultFrontendLabel()))
71+
);
72+
$this->prepareAttribute(['is_required' => true]);
73+
$this->unsetAttributeValueAndValidate($productSku);
74+
}
75+
76+
/**
77+
* @dataProvider productProvider
78+
* @param string $productSku
79+
* @return void
80+
*/
81+
public function testDefaultValue(string $productSku): void
82+
{
83+
$this->prepareAttribute(['default_value' => $this->getDefaultAttributeValue()]);
84+
$product = $this->unsetAttributeValueAndValidate($productSku);
85+
$product = $this->productRepository->save($product);
86+
$this->assertEquals($this->getDefaultAttributeValue(), $product->getData($this->getAttributeCode()));
87+
}
88+
89+
/**
90+
* @dataProvider uniqueAttributeValueProvider
91+
* @param string $firstSku
92+
* @param string $secondSku
93+
* @return void
94+
*/
95+
public function testUniqueAttribute(string $firstSku, string $secondSku): void
96+
{
97+
$this->expectException(Exception::class);
98+
$messageFormat = 'The value of the "%s" attribute isn\'t unique. Set a unique value and try again.';
99+
$this->expectExceptionMessage(
100+
(string)__(sprintf($messageFormat, $this->getAttribute()->getDefaultFrontendLabel()))
101+
);
102+
$this->prepareAttribute(['is_unique' => 1]);
103+
$product = $this->setAttributeValueAndValidate($firstSku, $this->getDefaultAttributeValue());
104+
$this->productRepository->save($product);
105+
$this->setAttributeValueAndValidate($secondSku, $this->getDefaultAttributeValue());
106+
}
107+
108+
/**
109+
* Get attribute
110+
*
111+
* @return ProductAttributeInterface
112+
*/
113+
protected function getAttribute(): ProductAttributeInterface
114+
{
115+
if ($this->attribute === null) {
116+
$this->attribute = $this->attributeRepository->get(
117+
ProductAttributeInterface::ENTITY_TYPE_CODE,
118+
$this->getAttributeCode()
119+
);
120+
}
121+
122+
return $this->attribute;
123+
}
124+
125+
/**
126+
* Set attribute value to product and validate the product
127+
*
128+
* @param string $attributeValue
129+
* @param string $productSku
130+
* @return ProductInterface
131+
*/
132+
protected function setAttributeValueAndValidate(string $productSku, string $attributeValue): ProductInterface
133+
{
134+
$product = $this->productRepository->get($productSku);
135+
$product->addData([$this->getAttributeCode() => $attributeValue]);
136+
$product->validate();
137+
138+
return $product;
139+
}
140+
141+
/**
142+
* Unset attribute value of the product and validate the product
143+
*
144+
* @param string $productSku
145+
* @return ProductInterface
146+
*/
147+
private function unsetAttributeValueAndValidate(string $productSku): ProductInterface
148+
{
149+
$product = $this->productRepository->get($productSku);
150+
$product->unsetData($this->getAttributeCode());
151+
$product->validate();
152+
153+
return $product;
154+
}
155+
156+
/**
157+
* Prepare attribute to test
158+
*
159+
* @param array $data
160+
* @return void
161+
*/
162+
private function prepareAttribute(array $data): void
163+
{
164+
$attribute = $this->getAttribute();
165+
$attribute->addData($data);
166+
$this->attributeRepository->save($attribute);
167+
}
168+
169+
/**
170+
* Returns attribute code for current test
171+
*
172+
* @return string
173+
*/
174+
abstract protected function getAttributeCode(): string;
175+
176+
/**
177+
* Get default value for current attribute
178+
*
179+
* @return string
180+
*/
181+
abstract protected function getDefaultAttributeValue(): string;
182+
183+
/**
184+
* Products provider for tests
185+
*
186+
* @return array
187+
*/
188+
abstract public function productProvider(): array;
189+
190+
/**
191+
* Provider for unique attribute tests
192+
*
193+
* @return array
194+
*/
195+
abstract public function uniqueAttributeValueProvider(): array;
196+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\Product\Attribute\Save;
9+
10+
/**
11+
* @magentoDbIsolation enabled
12+
* @magentoDataFixture Magento/Catalog/_files/product_date_attribute.php
13+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
14+
*/
15+
class AttributeDateTest extends AbstractAttributeTest
16+
{
17+
/**
18+
* @dataProvider productProvider
19+
* @param string $productSku
20+
*/
21+
public function testDefaultValue(string $productSku): void
22+
{
23+
$this->markTestSkipped('Test is blocked by issue MC-28950');
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
protected function getAttributeCode(): string
30+
{
31+
return 'date_attribute';
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
protected function getDefaultAttributeValue(): string
38+
{
39+
return $this->getAttribute()->getBackend()->formatDate('11/20/19');
40+
}
41+
42+
/**
43+
* @magentoDataFixture Magento/Catalog/_files/product_date_attribute.php
44+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
45+
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
46+
* @dataProvider uniqueAttributeValueProvider
47+
* phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod
48+
* @inheritdoc
49+
*/
50+
public function testUniqueAttribute(string $firstSku, string $secondSku): void
51+
{
52+
parent::testUniqueAttribute($firstSku, $secondSku);
53+
}
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
public function productProvider(): array
59+
{
60+
return [
61+
[
62+
'product_sku' => 'simple2',
63+
],
64+
];
65+
}
66+
67+
/**
68+
* @inheritdoc
69+
*/
70+
public function uniqueAttributeValueProvider(): array
71+
{
72+
return [
73+
[
74+
'first_product_sku' => 'simple2',
75+
'second_product_sku' => 'simple-out-of-stock',
76+
],
77+
];
78+
}
79+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\Product\Attribute\Save;
9+
10+
/**
11+
* @magentoDbIsolation enabled
12+
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
13+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
14+
*/
15+
class AttributeDropdownTest extends AbstractAttributeTest
16+
{
17+
/**
18+
* @inheritdoc
19+
*/
20+
protected function getAttributeCode(): string
21+
{
22+
return 'dropdown_attribute';
23+
}
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
protected function getDefaultAttributeValue(): string
29+
{
30+
return $this->getAttribute()->getSource()->getOptionId('Option 1');
31+
}
32+
33+
/**
34+
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
35+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
36+
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
37+
* @dataProvider uniqueAttributeValueProvider
38+
* phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod
39+
* @inheritdoc
40+
*/
41+
public function testUniqueAttribute(string $firstSku, string $secondSku): void
42+
{
43+
parent::testUniqueAttribute($firstSku, $secondSku);
44+
}
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
public function productProvider(): array
50+
{
51+
return [
52+
[
53+
'product_sku' => 'simple2',
54+
],
55+
];
56+
}
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
public function uniqueAttributeValueProvider(): array
62+
{
63+
return [
64+
[
65+
'first_product_sku' => 'simple2',
66+
'second_product_sku' => 'simple-out-of-stock',
67+
],
68+
];
69+
}
70+
}

0 commit comments

Comments
 (0)