77
88namespace Magento \ConfigurableProduct \Controller \Adminhtml \Product \Attribute ;
99
10+ use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
1011use Magento \Catalog \Api \ProductRepositoryInterface ;
1112use Magento \Framework \App \Request \Http as HttpRequest ;
1213use Magento \Framework \Serialize \SerializerInterface ;
1314use Magento \TestFramework \TestCase \AbstractBackendController ;
1415
1516/**
16- * Test for creates options for product attributes .
17+ * Cheks creating attribute options process .
1718 *
1819 * @see \Magento\ConfigurableProduct\Controller\Adminhtml\Product\Attribute\CreateOptions
1920 * @magentoAppArea adminhtml
@@ -27,6 +28,9 @@ class CreateOptionsTest extends AbstractBackendController
2728 /** @var SerializerInterface */
2829 private $ json ;
2930
31+ /** @var ProductAttributeRepositoryInterface */
32+ private $ attributeRepository ;
33+
3034 /**
3135 * @inheritdoc
3236 */
@@ -35,47 +39,35 @@ protected function setUp(): void
3539 parent ::setUp ();
3640
3741 $ this ->productRepository = $ this ->_objectManager ->get (ProductRepositoryInterface::class);
42+ $ this ->productRepository ->cleanCache ();
3843 $ this ->json = $ this ->_objectManager ->get (SerializerInterface::class);
44+ $ this ->attributeRepository = $ this ->_objectManager ->get (ProductAttributeRepositoryInterface::class);
3945 }
4046
4147 /**
4248 * @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
4349 *
4450 * @return void
4551 */
46- public function testAddOptionWithUniqueValidationOneMoreTime (): void
52+ public function testAddAlreadyAddedOption (): void
4753 {
4854 $ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
55+ $ attr = $ this ->attributeRepository ->get ('test_configurable ' );
4956 $ this ->getRequest ()->setParams ([
50- 'options ' => [0 => [
57+ 'options ' => [
58+ [
5159 'label ' => 'Option 1 ' ,
5260 'is_new ' => true ,
53- 'attribute_id ' => $ this -> getFirstAttributeId ()
54- ]
55- ]
56- ]);
61+ 'attribute_id ' => ( int ) $ attr -> getAttributeId (),
62+ ],
63+ ],
64+ ]);
5765 $ this ->dispatch ('backend/catalog/product_attribute/createOptions ' );
5866 $ responseBody = $ this ->json ->unserialize ($ this ->getResponse ()->getBody ());
59- $ this ->assertNotEmpty ($ responseBody[ ' message ' ] );
67+ $ this ->assertNotEmpty ($ responseBody );
6068 $ 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 () ),
6270 $ responseBody ['message ' ]
6371 );
6472 }
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- }
8173}
0 commit comments