|
| 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\ConfigurableProduct\Controller\Adminhtml\Product\Attribute; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 12 | +use Magento\Framework\Serialize\SerializerInterface; |
| 13 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test for creates options for product attributes. |
| 17 | + * |
| 18 | + * @see \Magento\ConfigurableProduct\Controller\Adminhtml\Product\Attribute\CreateOptions |
| 19 | + * @magentoAppArea adminhtml |
| 20 | + * @magentoDbIsolation enabled |
| 21 | + */ |
| 22 | +class CreateOptionsTest extends AbstractBackendController |
| 23 | +{ |
| 24 | + /** @var ProductRepositoryInterface */ |
| 25 | + private $productRepository; |
| 26 | + |
| 27 | + /** @var SerializerInterface */ |
| 28 | + private $json; |
| 29 | + |
| 30 | + /** |
| 31 | + * @inheritdoc |
| 32 | + */ |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + parent::setUp(); |
| 36 | + |
| 37 | + $this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class); |
| 38 | + $this->json = $this->_objectManager->get(SerializerInterface::class); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php |
| 43 | + * |
| 44 | + * @return void |
| 45 | + */ |
| 46 | + public function testAddOptionWithUniqueValidationOneMoreTime(): void |
| 47 | + { |
| 48 | + $this->getRequest()->setMethod(HttpRequest::METHOD_POST); |
| 49 | + $this->getRequest()->setParams([ |
| 50 | + 'options' => [0 => [ |
| 51 | + 'label' => 'Option 1', |
| 52 | + 'is_new' => true, |
| 53 | + 'attribute_id' => $this->getFirstAttributeId() |
| 54 | + ] |
| 55 | + ] |
| 56 | + ]); |
| 57 | + $this->dispatch('backend/catalog/product_attribute/createOptions'); |
| 58 | + $responseBody = $this->json->unserialize($this->getResponse()->getBody()); |
| 59 | + $this->assertNotEmpty($responseBody['message']); |
| 60 | + $this->assertStringContainsString( |
| 61 | + (string)__('The value of attribute ""test_configurable"" must be unique'), |
| 62 | + $responseBody['message'] |
| 63 | + ); |
| 64 | + } |
| 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 | +} |
0 commit comments