7
7
8
8
namespace Magento \ConfigurableProduct \Controller \Adminhtml \Product \Attribute ;
9
9
10
+ use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
10
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
12
use Magento \Framework \App \Request \Http as HttpRequest ;
12
13
use Magento \Framework \Serialize \SerializerInterface ;
13
14
use Magento \TestFramework \TestCase \AbstractBackendController ;
14
15
15
16
/**
16
- * Test for creates options for product attributes .
17
+ * Cheks creating attribute options process .
17
18
*
18
19
* @see \Magento\ConfigurableProduct\Controller\Adminhtml\Product\Attribute\CreateOptions
19
20
* @magentoAppArea adminhtml
@@ -27,6 +28,9 @@ class CreateOptionsTest extends AbstractBackendController
27
28
/** @var SerializerInterface */
28
29
private $ json ;
29
30
31
+ /** @var ProductAttributeRepositoryInterface */
32
+ private $ attributeRepository ;
33
+
30
34
/**
31
35
* @inheritdoc
32
36
*/
@@ -35,47 +39,35 @@ protected function setUp(): void
35
39
parent ::setUp ();
36
40
37
41
$ this ->productRepository = $ this ->_objectManager ->get (ProductRepositoryInterface::class);
42
+ $ this ->productRepository ->cleanCache ();
38
43
$ this ->json = $ this ->_objectManager ->get (SerializerInterface::class);
44
+ $ this ->attributeRepository = $ this ->_objectManager ->get (ProductAttributeRepositoryInterface::class);
39
45
}
40
46
41
47
/**
42
48
* @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
43
49
*
44
50
* @return void
45
51
*/
46
- public function testAddOptionWithUniqueValidationOneMoreTime (): void
52
+ public function testAddAlreadyAddedOption (): void
47
53
{
48
54
$ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
55
+ $ attr = $ this ->attributeRepository ->get ('test_configurable ' );
49
56
$ this ->getRequest ()->setParams ([
50
- 'options ' => [0 => [
57
+ 'options ' => [
58
+ [
51
59
'label ' => 'Option 1 ' ,
52
60
'is_new ' => true ,
53
- 'attribute_id ' => $ this -> getFirstAttributeId ()
54
- ]
55
- ]
56
- ]);
61
+ 'attribute_id ' => ( int ) $ attr -> getAttributeId (),
62
+ ],
63
+ ],
64
+ ]);
57
65
$ this ->dispatch ('backend/catalog/product_attribute/createOptions ' );
58
66
$ responseBody = $ this ->json ->unserialize ($ this ->getResponse ()->getBody ());
59
- $ this ->assertNotEmpty ($ responseBody[ ' message ' ] );
67
+ $ this ->assertNotEmpty ($ responseBody );
60
68
$ this ->assertStringContainsString (
61
- (string )__ ('The value of attribute ""test_configurable "" must be unique ' ),
69
+ (string )__ ('The value of attribute ""%1 "" must be unique ' , $ attr -> getAttributeCode () ),
62
70
$ responseBody ['message ' ]
63
71
);
64
72
}
65
-
66
- /**
67
- * Get first attribute id
68
- *
69
- * @return int
70
- */
71
- private function getFirstAttributeId (): int
72
- {
73
- $ configurableProduct = $ this ->productRepository ->get ('configurable ' );
74
- $ options = $ configurableProduct ->getExtensionAttributes ()->getConfigurableProductOptions ();
75
- foreach ($ options as $ option ) {
76
- $ attributeIds [] = $ option ->getAttributeId ();
77
- }
78
-
79
- return (int )array_shift ($ attributeIds );
80
- }
81
73
}
0 commit comments