init
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Swatches\Controller\Adminhtml\Iframe;
|
||||
|
||||
use Magento\TestFramework\Bootstrap;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class ShowTest extends \Magento\TestFramework\TestCase\AbstractBackendController
|
||||
{
|
||||
/**
|
||||
* Check Swatch Acl Access
|
||||
*/
|
||||
public function testAclAccess()
|
||||
{
|
||||
/** @var $acl \Magento\Framework\Acl */
|
||||
$acl = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->get(\Magento\Framework\Acl\Builder::class)
|
||||
->getAcl();
|
||||
|
||||
$acl->allow(
|
||||
Bootstrap::ADMIN_ROLE_ID,
|
||||
\Magento\Swatches\Controller\Adminhtml\Iframe\Show::ADMIN_RESOURCE
|
||||
);
|
||||
|
||||
$this->dispatch('backend/swatches/iframe/show/');
|
||||
|
||||
$this->assertEquals(200, $this->getResponse()->getHttpResponseCode());
|
||||
$this->assertStringNotContainsString(
|
||||
'Sorry, you need permissions to view this content.',
|
||||
$this->getResponse()->getBody()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check Swatch Acl Access Denied
|
||||
*/
|
||||
public function testAclAccessDenied()
|
||||
{
|
||||
/** @var $acl \Magento\Framework\Acl */
|
||||
$acl = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->get(\Magento\Framework\Acl\Builder::class)
|
||||
->getAcl();
|
||||
|
||||
$acl->deny(
|
||||
Bootstrap::ADMIN_ROLE_ID,
|
||||
\Magento\Swatches\Controller\Adminhtml\Iframe\Show::ADMIN_RESOURCE
|
||||
);
|
||||
|
||||
$this->dispatch('backend/swatches/iframe/show/');
|
||||
|
||||
$this->assertEquals(403, $this->getResponse()->getHttpResponseCode());
|
||||
$this->assertStringContainsString(
|
||||
'Sorry, you need permissions to view this content.',
|
||||
$this->getResponse()->getBody()
|
||||
);
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Swatches\Controller\Adminhtml\Product\Attribute\Delete;
|
||||
|
||||
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Delete\AbstractDeleteAttributeControllerTest;
|
||||
|
||||
/**
|
||||
* Delete catalog product attributes with input types like "swatch_text" and "swatch_visual".
|
||||
* Attributes from Magento_Swatches module.
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
class SwatchesAttributesControllerTest extends AbstractDeleteAttributeControllerTest
|
||||
{
|
||||
/**
|
||||
* Assert that attribute with input type "swatch_text" will be deleted
|
||||
* after dispatch delete product attribute action.
|
||||
*
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_text_swatch_attribute.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDeleteSwatchTextAttribute(): void
|
||||
{
|
||||
$this->dispatchDeleteAttribute('text_swatch_attribute');
|
||||
$this->assertAttributeIsDeleted('text_swatch_attribute');
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that attribute with input type "swatch_visual" will be deleted
|
||||
* after dispatch delete product attribute action.
|
||||
*
|
||||
* @magentoDataFixture Magento/Swatches/_files/swatch_attribute.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDeleteSwatchVisualAttribute(): void
|
||||
{
|
||||
$this->dispatchDeleteAttribute('color_swatch');
|
||||
$this->assertAttributeIsDeleted('color_swatch');
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Swatches\Controller\Adminhtml\Product\Attribute\Save\InputType;
|
||||
|
||||
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save\AbstractSaveAttributeTest;
|
||||
use Magento\Eav\Api\Data\AttributeInterface;
|
||||
use Magento\Eav\Model\Entity\Attribute\Source\Table;
|
||||
|
||||
/**
|
||||
* Test cases related to create attribute with input type text swatch.
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class TextSwatchTest extends AbstractSaveAttributeTest
|
||||
{
|
||||
/**
|
||||
* Test create attribute and compare attribute data and input data.
|
||||
*
|
||||
* @dataProvider \Magento\TestFramework\Swatches\Model\Attribute\DataProvider\TextSwatch::getAttributeDataWithCheckArray
|
||||
*
|
||||
* @param array $attributePostData
|
||||
* @param array $checkArray
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateAttribute(array $attributePostData, array $checkArray): void
|
||||
{
|
||||
$this->createAttributeUsingDataAndAssert($attributePostData, $checkArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create attribute with error.
|
||||
*
|
||||
* @dataProvider \Magento\TestFramework\Swatches\Model\Attribute\DataProvider\TextSwatch::getAttributeDataWithErrorMessage
|
||||
*
|
||||
* @param array $attributePostData
|
||||
* @param string $errorMessage
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateAttributeWithError(array $attributePostData, string $errorMessage): void
|
||||
{
|
||||
$this->createAttributeUsingDataWithErrorAndAssert($attributePostData, $errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function assertAttributeOptions(AttributeInterface $attribute, array $optionsData): void
|
||||
{
|
||||
/** @var Table $attributeSource */
|
||||
$attributeSource = $attribute->getSource();
|
||||
$swatchOptions = $attributeSource->getAllOptions(true, true);
|
||||
foreach ($optionsData as $optionData) {
|
||||
$optionVisualValueArr = $optionData['optiontext']['value'];
|
||||
$optionVisualValue = reset($optionVisualValueArr)[0];
|
||||
$optionFounded = false;
|
||||
foreach ($swatchOptions as $attributeOption) {
|
||||
if ($attributeOption['label'] === $optionVisualValue) {
|
||||
$optionFounded = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->assertTrue($optionFounded);
|
||||
}
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Swatches\Controller\Adminhtml\Product\Attribute\Save\InputType;
|
||||
|
||||
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save\AbstractSaveAttributeTest;
|
||||
use Magento\Eav\Api\Data\AttributeInterface;
|
||||
use Magento\Eav\Model\Entity\Attribute\Source\Table;
|
||||
|
||||
/**
|
||||
* Test cases related to create attribute with input type visual swatch.
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class VisualSwatchTest extends AbstractSaveAttributeTest
|
||||
{
|
||||
/**
|
||||
* Test create attribute and compare attribute data and input data.
|
||||
*
|
||||
* @dataProvider \Magento\TestFramework\Swatches\Model\Attribute\DataProvider\VisualSwatch::getAttributeDataWithCheckArray
|
||||
*
|
||||
* @param array $attributePostData
|
||||
* @param array $checkArray
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateAttribute(array $attributePostData, array $checkArray): void
|
||||
{
|
||||
$this->createAttributeUsingDataAndAssert($attributePostData, $checkArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create attribute with error.
|
||||
*
|
||||
* @dataProvider \Magento\TestFramework\Swatches\Model\Attribute\DataProvider\VisualSwatch::getAttributeDataWithErrorMessage
|
||||
*
|
||||
* @param array $attributePostData
|
||||
* @param string $errorMessage
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateAttributeWithError(array $attributePostData, string $errorMessage): void
|
||||
{
|
||||
$this->createAttributeUsingDataWithErrorAndAssert($attributePostData, $errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function assertAttributeOptions(AttributeInterface $attribute, array $optionsData): void
|
||||
{
|
||||
/** @var Table $attributeSource */
|
||||
$attributeSource = $attribute->getSource();
|
||||
$swatchOptions = $attributeSource->getAllOptions(true, true);
|
||||
foreach ($optionsData as $optionData) {
|
||||
$optionVisualValueArr = $optionData['optionvisual']['value'];
|
||||
$optionVisualValue = reset($optionVisualValueArr)[0];
|
||||
$optionFounded = false;
|
||||
foreach ($swatchOptions as $attributeOption) {
|
||||
if ($attributeOption['label'] === $optionVisualValue) {
|
||||
$optionFounded = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->assertTrue($optionFounded);
|
||||
}
|
||||
}
|
||||
}
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Swatches\Controller\Adminhtml\Product\Attribute\Update;
|
||||
|
||||
use Magento\Catalog\Api\Data\ProductAttributeInterface;
|
||||
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Update\AbstractUpdateAttributeTest;
|
||||
use Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory as SwatchCollectionFactory;
|
||||
use Magento\Swatches\Model\Swatch;
|
||||
use Magento\Swatches\Model\SwatchAttributeType;
|
||||
|
||||
/**
|
||||
* Base update and assert swatch attribute data.
|
||||
*/
|
||||
abstract class AbstractUpdateSwatchAttributeTest extends AbstractUpdateAttributeTest
|
||||
{
|
||||
/** @var SwatchAttributeType */
|
||||
private $swatchAttributeType;
|
||||
|
||||
/** @var SwatchCollectionFactory */
|
||||
private $swatchCollectionFactory;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->swatchAttributeType = $this->_objectManager->get(SwatchAttributeType::class);
|
||||
$this->swatchCollectionFactory = $this->_objectManager->get(SwatchCollectionFactory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function replaceStoreCodeWithId(array $optionsArray): array
|
||||
{
|
||||
$optionsArray = parent::replaceStoreCodeWithId($optionsArray);
|
||||
foreach ($optionsArray as $key => $option) {
|
||||
if (isset($option['swatch'])) {
|
||||
$optionsArray[$key]['swatch'] = $this->prepareStoresData($option['swatch']);
|
||||
}
|
||||
}
|
||||
|
||||
return $optionsArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getActualOptionsData(string $attributeId): array
|
||||
{
|
||||
$actualOptionsData = parent::getActualOptionsData($attributeId);
|
||||
foreach (array_keys($actualOptionsData) as $optionId) {
|
||||
$actualOptionsData[$optionId]['swatch'] = $this->getAttributeOptionSwatchValues($optionId);
|
||||
}
|
||||
|
||||
return $actualOptionsData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function prepareStoreOptionsPostData(array $optionsData): array
|
||||
{
|
||||
$optionsPostData = parent::prepareStoreOptionsPostData($optionsData);
|
||||
$swatchType = $this->getSwatchType();
|
||||
$swatchOptionsPostData = [];
|
||||
|
||||
foreach ($optionsData as $optionId => $option) {
|
||||
$data = [];
|
||||
$data['option' . $swatchType] = $optionsPostData[$optionId]['option'];
|
||||
$optionSwatch = $swatchType == Swatch::SWATCH_INPUT_TYPE_VISUAL ? $option['swatch'][0] : $option['swatch'];
|
||||
|
||||
$data['swatch' . $swatchType] = [
|
||||
'value' => [
|
||||
$optionId => $optionSwatch,
|
||||
],
|
||||
];
|
||||
if (isset($optionsPostData[$optionId]['default'])) {
|
||||
$data['default' . $swatchType] = $optionsPostData[$optionId]['default'];
|
||||
}
|
||||
$swatchOptionsPostData[] = $data;
|
||||
}
|
||||
|
||||
return $swatchOptionsPostData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function prepareStoreOptionsExpectedData(array $optionsData): array
|
||||
{
|
||||
$optionsExpectedData = parent::prepareStoreOptionsExpectedData($optionsData);
|
||||
$optionsArray = $optionsExpectedData['options_array'];
|
||||
foreach (array_keys($optionsArray) as $optionId) {
|
||||
$optionsArray[$optionId]['swatch'] = $optionsData[$optionId]['swatch'];
|
||||
}
|
||||
|
||||
return [
|
||||
'options_array' => $optionsArray,
|
||||
'default_value' => $optionsExpectedData['default_value'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function assertUpdateAttributeData(
|
||||
ProductAttributeInterface $attribute,
|
||||
array $expectedData
|
||||
): void {
|
||||
$this->swatchAttributeType->isSwatchAttribute($attribute);
|
||||
parent::assertUpdateAttributeData($attribute, $expectedData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute option swatch values by option id.
|
||||
*
|
||||
* @param int $optionId
|
||||
* @return array
|
||||
*/
|
||||
private function getAttributeOptionSwatchValues(int $optionId): array
|
||||
{
|
||||
$swatchValues = [];
|
||||
$collection = $this->swatchCollectionFactory->create();
|
||||
$collection->addFieldToFilter('option_id', $optionId);
|
||||
|
||||
foreach ($collection as $item) {
|
||||
$swatchValues[$item->getData('store_id')] = $item->getData('value');
|
||||
}
|
||||
|
||||
return $swatchValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get swatch type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function getSwatchType(): string;
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Swatches\Controller\Adminhtml\Product\Attribute\Update\InputType;
|
||||
|
||||
use Magento\Swatches\Controller\Adminhtml\Product\Attribute\Update\AbstractUpdateSwatchAttributeTest;
|
||||
use Magento\Swatches\Model\Swatch;
|
||||
|
||||
/**
|
||||
* Test cases related to update attribute with input type text swatch.
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class TextSwatchTest extends AbstractUpdateSwatchAttributeTest
|
||||
{
|
||||
/**
|
||||
* Test update attribute.
|
||||
*
|
||||
* @dataProvider \Magento\TestFramework\Swatches\Model\Attribute\DataProvider\TextSwatch::getUpdateProvider
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_text_swatch_attribute.php
|
||||
*
|
||||
* @param array $postData
|
||||
* @param array $expectedData
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdateAttribute(array $postData, array $expectedData): void
|
||||
{
|
||||
$this->updateAttributeUsingData('text_swatch_attribute', $postData);
|
||||
$this->assertUpdateAttributeProcess('text_swatch_attribute', $postData, $expectedData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update attribute with error.
|
||||
*
|
||||
* @dataProvider \Magento\TestFramework\Swatches\Model\Attribute\DataProvider\TextSwatch::getUpdateProviderWithErrorMessage
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_text_swatch_attribute.php
|
||||
*
|
||||
* @param array $postData
|
||||
* @param string $errorMessage
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdateAttributeWithError(array $postData, string $errorMessage): void
|
||||
{
|
||||
$this->updateAttributeUsingData('text_swatch_attribute', $postData);
|
||||
$this->assertErrorSessionMessages($errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update attribute frontend labels on stores.
|
||||
*
|
||||
* @dataProvider \Magento\TestFramework\Swatches\Model\Attribute\DataProvider\TextSwatch::getUpdateFrontendLabelsProvider
|
||||
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_text_swatch_attribute.php
|
||||
*
|
||||
* @param array $postData
|
||||
* @param array $expectedData
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdateFrontendLabelOnStores(array $postData, array $expectedData): void
|
||||
{
|
||||
$this->processUpdateFrontendLabelOnStores('text_swatch_attribute', $postData, $expectedData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update attribute options on stores.
|
||||
*
|
||||
* @dataProvider \Magento\TestFramework\Swatches\Model\Attribute\DataProvider\TextSwatch::getUpdateOptionsProvider
|
||||
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_text_swatch_attribute.php
|
||||
*
|
||||
* @param array $postData
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdateOptionsOnStores(array $postData): void
|
||||
{
|
||||
$this->processUpdateOptionsOnStores('text_swatch_attribute', $postData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getSwatchType(): string
|
||||
{
|
||||
return Swatch::SWATCH_INPUT_TYPE_TEXT;
|
||||
}
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Swatches\Controller\Adminhtml\Product\Attribute\Update\InputType;
|
||||
|
||||
use Magento\Swatches\Controller\Adminhtml\Product\Attribute\Update\AbstractUpdateSwatchAttributeTest;
|
||||
use Magento\Swatches\Model\Swatch;
|
||||
|
||||
/**
|
||||
* Test cases related to update attribute with input type visual swatch.
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class VisualSwatchTest extends AbstractUpdateSwatchAttributeTest
|
||||
{
|
||||
/**
|
||||
* Test update attribute.
|
||||
*
|
||||
* @dataProvider \Magento\TestFramework\Swatches\Model\Attribute\DataProvider\VisualSwatch::getUpdateProvider
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_visual_swatch_attribute.php
|
||||
*
|
||||
* @param array $postData
|
||||
* @param array $expectedData
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdateAttribute(array $postData, array $expectedData): void
|
||||
{
|
||||
$this->updateAttributeUsingData('visual_swatch_attribute', $postData);
|
||||
$this->assertUpdateAttributeProcess('visual_swatch_attribute', $postData, $expectedData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update attribute with error.
|
||||
*
|
||||
* @dataProvider \Magento\TestFramework\Swatches\Model\Attribute\DataProvider\VisualSwatch::getUpdateProviderWithErrorMessage
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_visual_swatch_attribute.php
|
||||
*
|
||||
* @param array $postData
|
||||
* @param string $errorMessage
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdateAttributeWithError(array $postData, string $errorMessage): void
|
||||
{
|
||||
$this->updateAttributeUsingData('visual_swatch_attribute', $postData);
|
||||
$this->assertErrorSessionMessages($errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update attribute frontend labels on stores.
|
||||
*
|
||||
* @dataProvider \Magento\TestFramework\Swatches\Model\Attribute\DataProvider\VisualSwatch::getUpdateFrontendLabelsProvider
|
||||
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_visual_swatch_attribute.php
|
||||
*
|
||||
* @param array $postData
|
||||
* @param array $expectedData
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdateFrontendLabelOnStores(array $postData, array $expectedData): void
|
||||
{
|
||||
$this->processUpdateFrontendLabelOnStores('visual_swatch_attribute', $postData, $expectedData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update attribute options on stores.
|
||||
*
|
||||
* @dataProvider \Magento\TestFramework\Swatches\Model\Attribute\DataProvider\VisualSwatch::getUpdateOptionsProvider
|
||||
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_visual_swatch_attribute.php
|
||||
*
|
||||
* @param array $postData
|
||||
* @return void
|
||||
*/
|
||||
public function testUpdateOptionsOnStores(array $postData): void
|
||||
{
|
||||
$this->processUpdateOptionsOnStores('visual_swatch_attribute', $postData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getSwatchType(): string
|
||||
{
|
||||
return Swatch::SWATCH_INPUT_TYPE_VISUAL;
|
||||
}
|
||||
}
|
||||
+236
@@ -0,0 +1,236 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Swatches\Controller\Adminhtml\Product;
|
||||
|
||||
use Magento\Framework\App\Request\Http as HttpRequest;
|
||||
use Magento\Framework\Data\Form\FormKey;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
|
||||
/**
|
||||
* Test for product attribute save controller.
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
class AttributeTest extends \Magento\TestFramework\TestCase\AbstractBackendController
|
||||
{
|
||||
/**
|
||||
* @var FormKey
|
||||
*/
|
||||
private $formKey;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->formKey = $this->_objectManager->get(FormKey::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate random hex color.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getRandomColor() : string
|
||||
{
|
||||
return '#' . str_pad(dechex(random_int(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get visual swatches data set.
|
||||
*
|
||||
* @param int $optionsCount
|
||||
* @return array
|
||||
*/
|
||||
private function getSwatchVisualDataSet(int $optionsCount) : array
|
||||
{
|
||||
$optionsData = [];
|
||||
$expectedOptionsLabels = [];
|
||||
for ($i = 0; $i < $optionsCount; $i++) {
|
||||
$expectedOptionLabelOnStoreView = 'value_' . $i .'_store_1';
|
||||
$expectedOptionsLabels[$i+1] = $expectedOptionLabelOnStoreView;
|
||||
$optionId = 'option_' .$i;
|
||||
$optionRowData = [];
|
||||
$optionRowData['optionvisual']['order'][$optionId] = $i + 1;
|
||||
$optionRowData['defaultvisual'][] = $optionId;
|
||||
$optionRowData['swatchvisual']['value'][$optionId] = $this->getRandomColor();
|
||||
$optionRowData['optionvisual']['value'][$optionId][0] = 'value_' . $i .'_admin';
|
||||
$optionRowData['optionvisual']['value'][$optionId][1] = $expectedOptionLabelOnStoreView;
|
||||
$optionRowData['optionvisual']['delete'][$optionId] = '';
|
||||
$optionsData[] = http_build_query($optionRowData);
|
||||
}
|
||||
return [
|
||||
'attribute_data' => array_merge_recursive(
|
||||
[
|
||||
'serialized_options' => json_encode($optionsData),
|
||||
],
|
||||
[
|
||||
'visual_swatch_validation' => '',
|
||||
'visual_swatch_validation_unique' => '',
|
||||
],
|
||||
$this->getAttributePreset(),
|
||||
[
|
||||
'frontend_input' => 'swatch_visual'
|
||||
]
|
||||
),
|
||||
'expected_options_count' => $optionsCount + 1,
|
||||
'expected_store_labels' => $expectedOptionsLabels
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get text swatches data set.
|
||||
*
|
||||
* @param int $optionsCount
|
||||
* @return array
|
||||
*/
|
||||
private function getSwatchTextDataSet(int $optionsCount) : array
|
||||
{
|
||||
$optionsData = [];
|
||||
$expectedOptionsLabels = [];
|
||||
for ($i = 0; $i < $optionsCount; $i++) {
|
||||
$expectedOptionLabelOnStoreView = 'value_' . $i . '_store_1';
|
||||
$expectedOptionsLabels[$i+1] = $expectedOptionLabelOnStoreView;
|
||||
$optionId = 'option_' . $i;
|
||||
$optionRowData = [];
|
||||
$optionRowData['optiontext']['order'][$optionId] = $i + 1;
|
||||
$optionRowData['defaulttext'][] = $optionId;
|
||||
$optionRowData['swatchtext']['value'][$optionId][] = 'x' . $i ;
|
||||
$optionRowData['optiontext']['value'][$optionId][0] = 'value_' . $i . '_admin';
|
||||
$optionRowData['optiontext']['value'][$optionId][1]= $expectedOptionLabelOnStoreView;
|
||||
$optionRowData['optiontext']['delete'][$optionId]='';
|
||||
$optionsData[] = http_build_query($optionRowData);
|
||||
}
|
||||
return [
|
||||
'attribute_data' => array_merge_recursive(
|
||||
[
|
||||
'serialized_options' => json_encode($optionsData),
|
||||
],
|
||||
[
|
||||
'text_swatch_validation' => '',
|
||||
'text_swatch_validation_unique' => '',
|
||||
],
|
||||
$this->getAttributePreset(),
|
||||
[
|
||||
'frontend_input' => 'swatch_text'
|
||||
]
|
||||
),
|
||||
'expected_options_count' => $optionsCount + 1,
|
||||
'expected_store_labels' => $expectedOptionsLabels
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data preset for new attribute.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getAttributePreset() : array
|
||||
{
|
||||
return [
|
||||
'form_key' => 'XxtpPYjm2YPYUlAt',
|
||||
'frontend_label' => [
|
||||
0 => 'asdasd',
|
||||
1 => '',
|
||||
2 => '',
|
||||
],
|
||||
'is_required' => '0',
|
||||
'update_product_preview_image' => '0',
|
||||
'use_product_image_for_swatch' => '0',
|
||||
'is_global' => '0',
|
||||
'default_value_text' => '512',
|
||||
'default_value_yesno' => '1',
|
||||
'default_value_date' => '1/1/70',
|
||||
'default_value_textarea' => '512',
|
||||
'is_unique' => '0',
|
||||
'is_used_in_grid' => '1',
|
||||
'is_visible_in_grid' => '1',
|
||||
'is_filterable_in_grid' => '1',
|
||||
'is_searchable' => '0',
|
||||
'is_comparable' => '0',
|
||||
'is_filterable' => '0',
|
||||
'is_filterable_in_search' => '0',
|
||||
'position' => '0',
|
||||
'is_used_for_promo_rules' => '0',
|
||||
'is_html_allowed_on_front' => '1',
|
||||
'is_visible_on_front' => '0',
|
||||
'used_in_product_listing' => '0',
|
||||
'used_for_sort_by' => '0',
|
||||
'attribute_code' => 'test_many_swatches',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for large swatches amount test.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getLargeSwatchesAmountAttributeData() : array
|
||||
{
|
||||
$swatchVisualOptionsCount = 2000;
|
||||
$swatchTextOptionsCount = 2000;
|
||||
return [
|
||||
'visual swatches' => $this->getSwatchVisualDataSet($swatchVisualOptionsCount),
|
||||
'text swatches' => $this->getSwatchTextDataSet($swatchTextOptionsCount)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test attribute saving with large amount of options exceeding maximum allowed by max_input_vars limit.
|
||||
* @dataProvider getLargeSwatchesAmountAttributeData()
|
||||
* @param array $attributeData
|
||||
* @param int $expectedOptionsCount
|
||||
* @param array $expectedLabels
|
||||
* @return void
|
||||
*/
|
||||
public function testLargeOptionsDataSet(
|
||||
array $attributeData,
|
||||
int $expectedOptionsCount,
|
||||
array $expectedLabels
|
||||
) : void {
|
||||
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
|
||||
$this->getRequest()->setPostValue($attributeData);
|
||||
$this->getRequest()->setPostValue('form_key', $this->formKey->getFormKey());
|
||||
$this->dispatch('backend/catalog/product_attribute/save');
|
||||
$entityTypeId = $this->_objectManager->create(
|
||||
\Magento\Eav\Model\Entity::class
|
||||
)->setType(
|
||||
\Magento\Catalog\Model\Product::ENTITY
|
||||
)->getTypeId();
|
||||
|
||||
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
|
||||
$attribute = $this->_objectManager->create(
|
||||
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
|
||||
)->setEntityTypeId(
|
||||
$entityTypeId
|
||||
);
|
||||
try {
|
||||
$attribute->loadByCode($entityTypeId, 'test_many_swatches');
|
||||
$options = $attribute->getOptions();
|
||||
// assert that all options are saved without truncation
|
||||
$this->assertEquals(
|
||||
$expectedOptionsCount,
|
||||
count($options),
|
||||
'Expected options count does not match (regarding first empty option for non-required attribute)'
|
||||
);
|
||||
|
||||
foreach ($expectedLabels as $optionOrderNum => $label) {
|
||||
$this->assertEquals(
|
||||
$label,
|
||||
$options[$optionOrderNum]->getLabel(),
|
||||
"Label for option #{$optionOrderNum} does not match expected."
|
||||
);
|
||||
}
|
||||
} catch (LocalizedException $e) {
|
||||
$this->fail('Test failed with exception on attribute model load: ' . $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user