ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Swatches\Model;
|
||||
|
||||
use Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory;
|
||||
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
|
||||
use Magento\Eav\Api\Data\AttributeOptionInterfaceFactory;
|
||||
|
||||
/**
|
||||
* Test save of swatch attribute
|
||||
*
|
||||
*/
|
||||
class AttributeCreateTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testSetScopeDefault()
|
||||
{
|
||||
$om = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
|
||||
$data = [
|
||||
'is_required' => 1,
|
||||
'is_visible_on_front' => 1,
|
||||
'is_visible_in_advanced_search' => 0,
|
||||
'attribute_code' => 'color_swatch',
|
||||
'backend_type' => '',
|
||||
'is_searchable' => 0,
|
||||
'is_filterable' => 0,
|
||||
'is_filterable_in_search' => 0,
|
||||
'frontend_label' => 'Attribute ',
|
||||
];
|
||||
$optionsPerAttribute = 3;
|
||||
|
||||
$data['frontend_input'] = 'swatch_visual';
|
||||
$data['swatch_input_type'] = 'visual';
|
||||
$data['swatchvisual']['value'] = array_reduce(
|
||||
range(1, $optionsPerAttribute),
|
||||
function ($values, $index) use ($optionsPerAttribute) {
|
||||
$values['option_' . $index] = '#'
|
||||
. str_repeat(
|
||||
dechex(255 * $index / $optionsPerAttribute),
|
||||
3
|
||||
);
|
||||
return $values;
|
||||
},
|
||||
[]
|
||||
);
|
||||
$data['optionvisual']['value'] = array_reduce(
|
||||
range(1, $optionsPerAttribute),
|
||||
function ($values, $index) {
|
||||
$values['option_' . $index] = ['option ' . $index];
|
||||
return $values;
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
$data['options']['option'] = array_reduce(
|
||||
range(1, $optionsPerAttribute),
|
||||
function ($values, $index) {
|
||||
$values[] = [
|
||||
'label' => 'option ' . $index,
|
||||
'value' => 'option_' . $index
|
||||
];
|
||||
return $values;
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
$options = [];
|
||||
foreach ($data['options']['option'] as $optionData) {
|
||||
$options[] = $om->get(AttributeOptionInterfaceFactory::class)->create(['data' => $optionData]);
|
||||
}
|
||||
|
||||
$attribute = $om->get(ProductAttributeInterfaceFactory::class)
|
||||
->create(['data' => $data]);
|
||||
$attribute->setOptions($options);
|
||||
$attribute->setNote('auto');
|
||||
|
||||
$attribute = $om->get(ProductAttributeRepositoryInterface::class)->save($attribute);
|
||||
$this->assertNotEmpty($attribute->getId());
|
||||
$this->assertEquals('swatch_visual', $attribute->getFrontendInput());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Swatches\Model;
|
||||
|
||||
use Magento\Catalog\Model\Product\Attribute\Save\AbstractAttributeTest;
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_text_swatch_attribute.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*/
|
||||
class AttributeTextSwatchTest extends AbstractAttributeTest
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAttributeCode(): string
|
||||
{
|
||||
return 'text_swatch_attribute';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getDefaultAttributeValue(): string
|
||||
{
|
||||
return $this->getAttribute()->getSource()->getOptionId('Option 2');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_text_swatch_attribute.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
|
||||
* @dataProvider uniqueAttributeValueProvider
|
||||
* phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function testUniqueAttribute(string $firstSku, string $secondSku): void
|
||||
{
|
||||
parent::testUniqueAttribute($firstSku, $secondSku);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function productProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'product_sku' => 'simple2',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function uniqueAttributeValueProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'first_product_sku' => 'simple2',
|
||||
'second_product_sku' => 'simple-out-of-stock',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Swatches\Model;
|
||||
|
||||
use Magento\Catalog\Model\Product\Attribute\Save\AbstractAttributeTest;
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_visual_swatch_attribute.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*/
|
||||
class AttributeVisualSwatchTest extends AbstractAttributeTest
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAttributeCode(): string
|
||||
{
|
||||
return 'visual_swatch_attribute';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getDefaultAttributeValue(): string
|
||||
{
|
||||
return $this->getAttribute()->getSource()->getOptionId('option 2');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Swatches/_files/product_visual_swatch_attribute.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
|
||||
* @dataProvider uniqueAttributeValueProvider
|
||||
* phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function testUniqueAttribute(string $firstSku, string $secondSku): void
|
||||
{
|
||||
parent::testUniqueAttribute($firstSku, $secondSku);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function productProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'product_sku' => 'simple2',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function uniqueAttributeValueProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'first_product_sku' => 'simple2',
|
||||
'second_product_sku' => 'simple-out-of-stock',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Swatches\Model\Plugin;
|
||||
|
||||
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
|
||||
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Swatches\Model\ResourceModel\Swatch as SwatchResource;
|
||||
use Magento\Swatches\Model\Swatch;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Interception\PluginList;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Checks swatches attribute save behaviour
|
||||
*
|
||||
* @see \Magento\Swatches\Model\Plugin\EavAttribute
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDbIsolation disabled
|
||||
*/
|
||||
class EavAttributeTest extends TestCase
|
||||
{
|
||||
/** @var ObjectManagerInterface */
|
||||
private $objectManager;
|
||||
|
||||
/** @var ProductAttributeRepositoryInterface */
|
||||
private $productAttributeRepository;
|
||||
|
||||
/** @var SwatchResource */
|
||||
private $swatchResource;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->productAttributeRepository = $this->objectManager->get(ProductAttributeRepositoryInterface::class);
|
||||
$this->swatchResource = $this->objectManager->get(SwatchResource::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testEavAttributePluginIsRegistered(): void
|
||||
{
|
||||
$pluginInfo = $this->objectManager->get(PluginList::class)->get(Attribute::class);
|
||||
$this->assertSame(EavAttribute::class, $pluginInfo['save_swatches_option_params']['instance']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Swatches/_files/text_swatch_attribute.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testChangeAttributeToDropdown(): void
|
||||
{
|
||||
$attribute = $this->productAttributeRepository->get('test_configurable');
|
||||
$options = $attribute->getOptions();
|
||||
unset($options[0]);
|
||||
$optionsIds = $this->collectOptionsIds($options);
|
||||
$attribute->addData($this->prepareOptions($options));
|
||||
$attribute->setData(Swatch::SWATCH_INPUT_TYPE_KEY, Swatch::SWATCH_INPUT_TYPE_DROPDOWN);
|
||||
$attribute->beforeSave();
|
||||
$this->assertEmpty($this->fetchSwatchOptions($optionsIds), 'Swatch options were not deleted');
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare options
|
||||
*
|
||||
* @param array $options
|
||||
* @return array
|
||||
*/
|
||||
private function prepareOptions(array $options): array
|
||||
{
|
||||
foreach ($options as $key => $option) {
|
||||
$preparedOptions['option']['order'][$option->getValue()] = $key;
|
||||
$preparedOptions['option']['value'][$option->getValue()] = [$option->getLabel()];
|
||||
$preparedOptions['option']['delete'][$option->getValue()] = '';
|
||||
}
|
||||
|
||||
return $preparedOptions ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect options ids
|
||||
*
|
||||
* @param array $options
|
||||
* @return array
|
||||
*/
|
||||
private function collectOptionsIds(array $options): array
|
||||
{
|
||||
foreach ($options as $option) {
|
||||
$optionsIds[] = $option->getValue();
|
||||
}
|
||||
|
||||
return $optionsIds ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch related to provided ids records from eav_attribute_option_swatch table
|
||||
*
|
||||
* @param $optionsIds
|
||||
* @return array
|
||||
*/
|
||||
private function fetchSwatchOptions(array $optionsIds): array
|
||||
{
|
||||
$connection = $this->swatchResource->getConnection();
|
||||
$select = $connection->select()->from(['main_table' => $this->swatchResource->getMainTable()])
|
||||
->where('main_table.option_id IN (?)', $optionsIds);
|
||||
|
||||
return $connection->fetchAll($select);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Swatches\Model;
|
||||
|
||||
class SwatchAttributeCodesTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/** @var \Magento\Swatches\Model\SwatchAttributeCodes */
|
||||
private $swatchAttributeCodes;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->swatchAttributeCodes = $this->objectManager->create(
|
||||
\Magento\Swatches\Model\SwatchAttributeCodes::class
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoDataFixture Magento/Swatches/_files/swatch_attribute.php
|
||||
*/
|
||||
public function testGetCodes()
|
||||
{
|
||||
$attribute = $this->objectManager
|
||||
->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
|
||||
->load('color_swatch', 'attribute_code');
|
||||
$expected = [
|
||||
$attribute->getAttributeId() => $attribute->getAttributeCode()
|
||||
];
|
||||
$swatchAttributeCodes = $this->swatchAttributeCodes->getCodes();
|
||||
|
||||
$this->assertEquals($expected, $swatchAttributeCodes);
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Swatches\Model;
|
||||
|
||||
use Magento\Catalog\Api\ProductAttributeOptionManagementInterface;
|
||||
use Magento\Eav\Api\Data\AttributeOptionInterface;
|
||||
use Magento\Eav\Api\Data\AttributeOptionInterfaceFactory;
|
||||
|
||||
/**
|
||||
* Test add option of swatch attribute
|
||||
*
|
||||
*/
|
||||
class SwatchAttributeOptionAddTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoDataFixture Magento/Swatches/_files/swatch_attribute.php
|
||||
*/
|
||||
public function testSwatchOptionAdd()
|
||||
{
|
||||
/** @var \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute */
|
||||
$attribute = $this->objectManager
|
||||
->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
|
||||
->load('color_swatch', 'attribute_code');
|
||||
$optionsPerAttribute = 4;
|
||||
|
||||
$data['options']['option'] = array_reduce(
|
||||
range(10, $optionsPerAttribute),
|
||||
function ($values, $index) {
|
||||
$values[] = [
|
||||
'label' => 'option ' . $index,
|
||||
'value' => 'option_' . $index
|
||||
];
|
||||
return $values;
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
/** @var AttributeOptionInterface[] $options */
|
||||
$options = [];
|
||||
foreach ($data['options']['option'] as $optionData) {
|
||||
$options[] = $this->objectManager
|
||||
->get(AttributeOptionInterfaceFactory::class)
|
||||
->create(['data' => $optionData]);
|
||||
}
|
||||
|
||||
/** @var ProductAttributeOptionManagementInterface $optionManagement */
|
||||
$optionManagement = $this->objectManager->get(ProductAttributeOptionManagementInterface::class);
|
||||
foreach ($options as $option) {
|
||||
$optionManagement->add(
|
||||
$attribute->getAttributeCode(),
|
||||
$option
|
||||
);
|
||||
}
|
||||
|
||||
$items = $optionManagement->getItems($attribute->getAttributeCode());
|
||||
array_walk(
|
||||
$items,
|
||||
function (&$item) {
|
||||
/** @var AttributeOptionInterface $item */
|
||||
$item = $item->getLabel();
|
||||
}
|
||||
);
|
||||
foreach ($options as $option) {
|
||||
$this->assertTrue(in_array($option->getLabel(), $items));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user