Skip to content

Commit ae2416b

Browse files
committed
MC-23203: Admin: Simple product with all custom attributes
1 parent e62c675 commit ae2416b

File tree

12 files changed

+1014
-0
lines changed

12 files changed

+1014
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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\Catalog\Model\ResourceModel\Eav\Attribute;
14+
use Magento\Eav\Api\AttributeRepositoryInterface;
15+
use Magento\Eav\Model\Entity\Attribute\Exception;
16+
use Magento\Framework\ObjectManagerInterface;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Base class for text product attributes
22+
*/
23+
abstract class AbstractAttributeTest extends TestCase
24+
{
25+
/** @var ObjectManagerInterface */
26+
protected $objectManager;
27+
28+
/** @var AttributeRepositoryInterface */
29+
protected $attributeRepository;
30+
31+
/** @var ProductRepositoryInterface */
32+
protected $productRepository;
33+
34+
/** @var Attribute */
35+
protected $attribute;
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
protected function setUp()
41+
{
42+
parent::setUp();
43+
44+
$this->objectManager = Bootstrap::getObjectManager();
45+
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
46+
$this->attributeRepository = $this->objectManager->create(AttributeRepositoryInterface::class);
47+
$this->attribute = $this->attributeRepository->get(
48+
ProductAttributeInterface::ENTITY_TYPE_CODE,
49+
$this->getAttributeCode()
50+
);
51+
}
52+
53+
/**
54+
* @dataProvider productProvider
55+
* @param $productSku
56+
* @return void
57+
*/
58+
public function testSaveText(string $productSku): void
59+
{
60+
$product = $this->setAttributeValueAndValidate($productSku, $this->getDefaultAttributeValue());
61+
$product = $this->productRepository->save($product);
62+
$this->assertEquals($this->getDefaultAttributeValue(), $product->getData($this->getAttributeCode()));
63+
}
64+
65+
/**
66+
* @dataProvider productProvider
67+
* @param string $productSku
68+
* @return void
69+
*/
70+
public function testRequiredAttribute(string $productSku): void
71+
{
72+
$this->expectException(Exception::class);
73+
$messageFormat = 'The "%s" attribute value is empty. Set the attribute and try again.';
74+
$this->expectExceptionMessage((string)__(sprintf($messageFormat, $this->attribute->getDefaultFrontendLabel())));
75+
$this->prepareAttribute(['is_required' => true]);
76+
$this->unsetAttributeValueAndValidate($productSku);
77+
}
78+
79+
/**
80+
* @dataProvider productProvider
81+
* @param string $productSku
82+
* @return void
83+
*/
84+
public function testDefaultValue(string $productSku): void
85+
{
86+
$this->prepareAttribute(['default_value' => $this->getDefaultAttributeValue()]);
87+
$product = $this->unsetAttributeValueAndValidate($productSku);
88+
$product = $this->productRepository->save($product);
89+
$this->assertEquals($this->getDefaultAttributeValue(), $product->getData($this->getAttributeCode()));
90+
}
91+
92+
/**
93+
* @dataProvider uniqueTestProvider
94+
* @param string $firstSku
95+
* @param string $secondSku
96+
* @return void
97+
*/
98+
public function testUniqueAttribute(string $firstSku, string $secondSku): void
99+
{
100+
$this->expectException(Exception::class);
101+
$messageFormat = 'The value of the "%s" attribute isn\'t unique. Set a unique value and try again.';
102+
$this->expectExceptionMessage((string)__(sprintf($messageFormat, $this->attribute->getDefaultFrontendLabel())));
103+
$this->prepareAttribute(['is_unique' => 1]);
104+
$product = $this->setAttributeValueAndValidate($firstSku, $this->getDefaultAttributeValue());
105+
$this->productRepository->save($product);
106+
$this->setAttributeValueAndValidate($secondSku, $this->getDefaultAttributeValue());
107+
}
108+
109+
/**
110+
* Set attribute value to product and validate the product
111+
*
112+
* @param string $attributeValue
113+
* @param string $productSku
114+
* @return ProductInterface
115+
*/
116+
protected function setAttributeValueAndValidate(string $productSku, string $attributeValue): ProductInterface
117+
{
118+
$product = $this->productRepository->get($productSku);
119+
$product->addData([$this->getAttributeCode() => $attributeValue]);
120+
$product->validate();
121+
122+
return $product;
123+
}
124+
125+
/**
126+
* Unset attribute value of the product and validate the product
127+
*
128+
* @param string $productSku
129+
* @return ProductInterface
130+
*/
131+
private function unsetAttributeValueAndValidate(string $productSku): ProductInterface
132+
{
133+
$product = $this->productRepository->get($productSku);
134+
$product->unsetData($this->getAttributeCode());
135+
$product->validate();
136+
137+
return $product;
138+
}
139+
140+
/**
141+
* Prepare attribute to test
142+
*
143+
* @param array $data
144+
* @return void
145+
*/
146+
private function prepareAttribute(array $data): void
147+
{
148+
$attribute = $this->attributeRepository->get(
149+
ProductAttributeInterface::ENTITY_TYPE_CODE,
150+
$this->getAttributeCode()
151+
);
152+
$attribute->addData($data);
153+
$this->attributeRepository->save($attribute);
154+
}
155+
156+
/**
157+
* Returns attribute code for current test
158+
*
159+
* @return string
160+
*/
161+
abstract protected function getAttributeCode(): string;
162+
163+
/**
164+
* Get default value for current attribute
165+
*
166+
* @return string
167+
*/
168+
abstract protected function getDefaultAttributeValue(): string;
169+
170+
/**
171+
* Products provider for tests
172+
*
173+
* @return array
174+
*/
175+
abstract public function productProvider(): array;
176+
177+
/**
178+
* Provider for unique attribute tests
179+
*
180+
* @return array
181+
*/
182+
abstract public function uniqueTestProvider(): array;
183+
}
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->attribute->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 uniqueTestProvider
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 uniqueTestProvider(): 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->attribute->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 uniqueTestProvider
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 uniqueTestProvider(): 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)