This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,61 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Test class for \Magento\Eav\Block\Adminhtml\Attribute\Edit\Main\AbstractMain
*/
namespace Magento\Eav\Block\Adminhtml\Attribute\Edit\Main;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AbstractMainTest extends \PHPUnit\Framework\TestCase
{
/**
* @magentoAppIsolation enabled
*/
public function testPrepareForm()
{
/** @var $objectManager \Magento\TestFramework\ObjectManager */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
\Magento\TestFramework\Helper\Bootstrap::getInstance()
->loadArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
$objectManager->get(\Magento\Framework\View\DesignInterface::class)
->setDefaultDesignTheme();
$entityType = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class)
->getEntityType('customer');
$model = $objectManager->create(\Magento\Customer\Model\Attribute::class);
$model->setEntityTypeId($entityType->getId());
$objectManager->get(\Magento\Framework\Registry::class)->register('entity_attribute', $model);
$block = $this->getMockForAbstractClass(
\Magento\Eav\Block\Adminhtml\Attribute\Edit\Main\AbstractMain::class,
[
$objectManager->get(\Magento\Backend\Block\Template\Context::class),
$objectManager->get(\Magento\Framework\Registry::class),
$objectManager->get(\Magento\Framework\Data\FormFactory::class),
$objectManager->get(\Magento\Eav\Helper\Data::class),
$objectManager->get(\Magento\Config\Model\Config\Source\YesnoFactory::class),
$objectManager->get(\Magento\Eav\Model\Adminhtml\System\Config\Source\InputtypeFactory::class),
$objectManager->get(\Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker::class)
]
)->setLayout(
$objectManager->create(\Magento\Framework\View\Layout::class)
);
$method = new \ReflectionMethod(
\Magento\Eav\Block\Adminhtml\Attribute\Edit\Main\AbstractMain::class,
'_prepareForm'
);
$method->setAccessible(true);
$method->invoke($block);
$element = $block->getForm()->getElement('default_value_date');
$this->assertNotNull($element);
$this->assertNotEmpty($element->getDateFormat());
}
}
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Save\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save\AbstractSaveAttributeTest;
/**
* Test cases related to create attribute with input type date.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class DateTest extends AbstractSaveAttributeTest
{
/**
* Test create attribute and compare attribute data and input data.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\Date::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\Eav\Model\Attribute\DataProvider\Date::getAttributeDataWithErrorMessage
*
* @param array $attributePostData
* @param string $errorMessage
* @return void
*/
public function testCreateAttributeWithError(array $attributePostData, string $errorMessage): void
{
$this->createAttributeUsingDataWithErrorAndAssert($attributePostData, $errorMessage);
}
}
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Save\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save\AbstractSaveAttributeTest;
/**
* Test cases related to create attribute with input type datetime.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class DateTimeTest extends AbstractSaveAttributeTest
{
/**
* Test create attribute and compare attribute data and input data.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DateTime::getAttributeDataWithCheckArray
* @magentoConfigFixture default/general/locale/timezone UTC
*
* @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\Eav\Model\Attribute\DataProvider\DateTime::getAttributeDataWithErrorMessage
*
* @param array $attributePostData
* @param string $errorMessage
* @return void
*/
public function testCreateAttributeWithError(array $attributePostData, string $errorMessage): void
{
$this->createAttributeUsingDataWithErrorAndAssert($attributePostData, $errorMessage);
}
}
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Save\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save\AbstractSaveAttributeTest;
/**
* Test cases related to create attribute with input type dropdown.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class DropDownTest extends AbstractSaveAttributeTest
{
/**
* Test create attribute and compare attribute data and input data.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DropDown::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\Eav\Model\Attribute\DataProvider\DropDown::getAttributeDataWithErrorMessage
*
* @param array $attributePostData
* @param string $errorMessage
* @return void
*/
public function testCreateAttributeWithError(array $attributePostData, string $errorMessage): void
{
$this->createAttributeUsingDataWithErrorAndAssert($attributePostData, $errorMessage);
}
}
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Save\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save\AbstractSaveAttributeTest;
/**
* Test cases related to create attribute with input type multiselect.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class MultipleSelectTest extends AbstractSaveAttributeTest
{
/**
* Test create attribute and compare attribute data and input data.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\MultipleSelect::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\Eav\Model\Attribute\DataProvider\MultipleSelect::getAttributeDataWithErrorMessage
*
* @param array $attributePostData
* @param string $errorMessage
* @return void
*/
public function testCreateAttributeWithError(array $attributePostData, string $errorMessage): void
{
$this->createAttributeUsingDataWithErrorAndAssert($attributePostData, $errorMessage);
}
}
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Save\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save\AbstractSaveAttributeTest;
/**
* Test cases related to create attribute with input type text_area.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class TextAreaTest extends AbstractSaveAttributeTest
{
/**
* Test create attribute and compare attribute data and input data.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\TextArea::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\Eav\Model\Attribute\DataProvider\TextArea::getAttributeDataWithErrorMessage
*
* @param array $attributePostData
* @param string $errorMessage
* @return void
*/
public function testCreateAttributeWithError(array $attributePostData, string $errorMessage): void
{
$this->createAttributeUsingDataWithErrorAndAssert($attributePostData, $errorMessage);
}
}
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Save\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save\AbstractSaveAttributeTest;
/**
* Test cases related to create attribute with input type text_editor.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class TextEditorTest extends AbstractSaveAttributeTest
{
/**
* Test create attribute and compare attribute data and input data.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\TextEditor::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\Eav\Model\Attribute\DataProvider\TextEditor::getAttributeDataWithErrorMessage
*
* @param array $attributePostData
* @param string $errorMessage
* @return void
*/
public function testCreateAttributeWithError(array $attributePostData, string $errorMessage): void
{
$this->createAttributeUsingDataWithErrorAndAssert($attributePostData, $errorMessage);
}
}
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Save\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save\AbstractSaveAttributeTest;
/**
* Test cases related to create attribute with input type text.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class TextTest extends AbstractSaveAttributeTest
{
/**
* Test create attribute and compare attribute data and input data.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\Text::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\Eav\Model\Attribute\DataProvider\Text::getAttributeDataWithErrorMessage
*
* @param array $attributePostData
* @param string $errorMessage
* @return void
*/
public function testCreateAttributeWithError(array $attributePostData, string $errorMessage): void
{
$this->createAttributeUsingDataWithErrorAndAssert($attributePostData, $errorMessage);
}
}
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Save\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save\AbstractSaveAttributeTest;
/**
* Test cases related to create attribute with yes/no input type.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class YesNoTest extends AbstractSaveAttributeTest
{
/**
* Test create attribute and compare attribute data and input data.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\YesNo::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\Eav\Model\Attribute\DataProvider\YesNo::getAttributeDataWithErrorMessage
*
* @param array $attributePostData
* @param string $errorMessage
* @return void
*/
public function testCreateAttributeWithError(array $attributePostData, string $errorMessage): void
{
$this->createAttributeUsingDataWithErrorAndAssert($attributePostData, $errorMessage);
}
}
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Update\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Update\AbstractUpdateAttributeTest;
/**
* Test cases related to update attribute with input type date.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class DateTest extends AbstractUpdateAttributeTest
{
/**
* Test update attribute.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\Date::getUpdateProvider
* @magentoDataFixture Magento/Catalog/_files/product_date_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateAttribute(array $postData, array $expectedData): void
{
$this->updateAttributeUsingData('date_attribute', $postData);
$this->assertUpdateAttributeProcess('date_attribute', $postData, $expectedData);
}
/**
* Test update attribute with error.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\Date::getUpdateProviderWithErrorMessage
* @magentoDataFixture Magento/Catalog/_files/product_date_attribute.php
*
* @param array $postData
* @param string $errorMessage
* @return void
*/
public function testUpdateAttributeWithError(array $postData, string $errorMessage): void
{
$this->updateAttributeUsingData('date_attribute', $postData);
$this->assertErrorSessionMessages($errorMessage);
}
/**
* Test update attribute frontend labels on stores.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\Date::getUpdateFrontendLabelsProvider
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/Catalog/_files/product_date_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateFrontendLabelOnStores(array $postData, array $expectedData): void
{
$this->processUpdateFrontendLabelOnStores('date_attribute', $postData, $expectedData);
}
}
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Update\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Update\AbstractUpdateAttributeTest;
/**
* Test cases related to update attribute with input type date.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class DateTimeTest extends AbstractUpdateAttributeTest
{
/**
* Test update attribute.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DateTime::getUpdateProvider
* @magentoConfigFixture default/general/locale/timezone UTC
* @magentoDataFixture Magento/Catalog/_files/product_datetime_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateAttribute(array $postData, array $expectedData): void
{
$this->updateAttributeUsingData('datetime_attribute', $postData);
$this->assertUpdateAttributeProcess('datetime_attribute', $postData, $expectedData);
}
/**
* Test update attribute with error.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DateTime::getUpdateProviderWithErrorMessage
* @magentoDataFixture Magento/Catalog/_files/product_datetime_attribute.php
*
* @param array $postData
* @param string $errorMessage
* @return void
*/
public function testUpdateAttributeWithError(array $postData, string $errorMessage): void
{
$this->updateAttributeUsingData('datetime_attribute', $postData);
$this->assertErrorSessionMessages($errorMessage);
}
/**
* Test update attribute frontend labels on stores.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DateTime::getUpdateFrontendLabelsProvider
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/Catalog/_files/product_datetime_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateFrontendLabelOnStores(array $postData, array $expectedData): void
{
$this->processUpdateFrontendLabelOnStores('datetime_attribute', $postData, $expectedData);
}
}
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Update\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Update\AbstractUpdateAttributeTest;
/**
* Test cases related to update attribute with input type dropdown.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class DropDownTest extends AbstractUpdateAttributeTest
{
/**
* Test update attribute.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DropDown::getUpdateProvider
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateAttribute(array $postData, array $expectedData): void
{
$this->updateAttributeUsingData('dropdown_attribute', $postData);
$this->assertUpdateAttributeProcess('dropdown_attribute', $postData, $expectedData);
}
/**
* Test update attribute with error.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DropDown::getUpdateProviderWithErrorMessage
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
*
* @param array $postData
* @param string $errorMessage
* @return void
*/
public function testUpdateAttributeWithError(array $postData, string $errorMessage): void
{
$this->updateAttributeUsingData('dropdown_attribute', $postData);
$this->assertErrorSessionMessages($errorMessage);
}
/**
* Test update attribute frontend labels on stores.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DropDown::getUpdateFrontendLabelsProvider
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateFrontendLabelOnStores(array $postData, array $expectedData): void
{
$this->processUpdateFrontendLabelOnStores('dropdown_attribute', $postData, $expectedData);
}
/**
* Test update attribute options on stores.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\DropDown::getUpdateOptionsProvider
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
*
* @param array $postData
* @return void
*/
public function testUpdateOptionsOnStores(array $postData): void
{
$this->processUpdateOptionsOnStores('dropdown_attribute', $postData);
}
}
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Update\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Update\AbstractUpdateAttributeTest;
/**
* Test cases related to update attribute with input type multiselect.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class MultipleSelectTest extends AbstractUpdateAttributeTest
{
/**
* Test update attribute.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\MultipleSelect::getUpdateProvider
* @magentoDataFixture Magento/Catalog/_files/multiselect_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateAttribute(array $postData, array $expectedData): void
{
$this->updateAttributeUsingData('multiselect_attribute', $postData);
$this->assertUpdateAttributeProcess('multiselect_attribute', $postData, $expectedData);
}
/**
* Test update attribute with error.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\MultipleSelect::getUpdateProviderWithErrorMessage
* @magentoDataFixture Magento/Catalog/_files/multiselect_attribute.php
*
* @param array $postData
* @param string $errorMessage
* @return void
*/
public function testUpdateAttributeWithError(array $postData, string $errorMessage): void
{
$this->updateAttributeUsingData('multiselect_attribute', $postData);
$this->assertErrorSessionMessages($errorMessage);
}
/**
* Test update attribute frontend labels on stores.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\MultipleSelect::getUpdateFrontendLabelsProvider
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/Catalog/_files/multiselect_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateFrontendLabelOnStores(array $postData, array $expectedData): void
{
$this->processUpdateFrontendLabelOnStores('multiselect_attribute', $postData, $expectedData);
}
/**
* Test update attribute options on stores.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\MultipleSelect::getUpdateOptionsProvider
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/Catalog/_files/multiselect_attribute.php
*
* @param array $postData
* @return void
*/
public function testUpdateOptionsOnStores(array $postData): void
{
$this->processUpdateOptionsOnStores('multiselect_attribute', $postData);
}
}
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Update\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Update\AbstractUpdateAttributeTest;
/**
* Test cases related to update attribute with input type text_area.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class TextAreaTest extends AbstractUpdateAttributeTest
{
/**
* Test update attribute.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\TextArea::getUpdateProvider
* @magentoDataFixture Magento/Catalog/_files/product_text_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateAttribute(array $postData, array $expectedData): void
{
$this->updateAttributeUsingData('text_attribute', $postData);
$this->assertUpdateAttributeProcess('text_attribute', $postData, $expectedData);
}
/**
* Test update attribute with error.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\TextArea::getUpdateProviderWithErrorMessage
* @magentoDataFixture Magento/Catalog/_files/product_text_attribute.php
*
* @param array $postData
* @param string $errorMessage
* @return void
*/
public function testUpdateAttributeWithError(array $postData, string $errorMessage): void
{
$this->updateAttributeUsingData('text_attribute', $postData);
$this->assertErrorSessionMessages($errorMessage);
}
/**
* Test update attribute frontend labels on stores.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\TextArea::getUpdateFrontendLabelsProvider
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/Catalog/_files/product_text_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateFrontendLabelOnStores(array $postData, array $expectedData): void
{
$this->processUpdateFrontendLabelOnStores('text_attribute', $postData, $expectedData);
}
}
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Update\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Update\AbstractUpdateAttributeTest;
/**
* Test cases related to update attribute with input type text_editor.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class TextEditorTest extends AbstractUpdateAttributeTest
{
/**
* Test update attribute.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\TextEditor::getUpdateProvider
* @magentoDataFixture Magento/Catalog/_files/product_text_editor_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateAttribute(array $postData, array $expectedData): void
{
$this->updateAttributeUsingData('text_editor_attribute', $postData);
$this->assertUpdateAttributeProcess('text_editor_attribute', $postData, $expectedData);
}
/**
* Test update attribute with error.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\TextEditor::getUpdateProviderWithErrorMessage
* @magentoDataFixture Magento/Catalog/_files/product_text_editor_attribute.php
*
* @param array $postData
* @param string $errorMessage
* @return void
*/
public function testUpdateAttributeWithError(array $postData, string $errorMessage): void
{
$this->updateAttributeUsingData('text_editor_attribute', $postData);
$this->assertErrorSessionMessages($errorMessage);
}
/**
* Test update attribute frontend labels on stores.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\TextEditor::getUpdateFrontendLabelsProvider
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/Catalog/_files/product_text_editor_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateFrontendLabelOnStores(array $postData, array $expectedData): void
{
$this->processUpdateFrontendLabelOnStores('text_editor_attribute', $postData, $expectedData);
}
}
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Update\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Update\AbstractUpdateAttributeTest;
/**
* Test cases related to update attribute with input type text.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class TextTest extends AbstractUpdateAttributeTest
{
/**
* Test update attribute.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\Text::getUpdateProvider
* @magentoDataFixture Magento/Catalog/_files/product_varchar_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateAttribute(array $postData, array $expectedData): void
{
$this->updateAttributeUsingData('varchar_attribute', $postData);
$this->assertUpdateAttributeProcess('varchar_attribute', $postData, $expectedData);
}
/**
* Test update attribute with error.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\Text::getUpdateProviderWithErrorMessage
* @magentoDataFixture Magento/Catalog/_files/product_varchar_attribute.php
*
* @param array $postData
* @param string $errorMessage
* @return void
*/
public function testUpdateAttributeWithError(array $postData, string $errorMessage): void
{
$this->updateAttributeUsingData('varchar_attribute', $postData);
$this->assertErrorSessionMessages($errorMessage);
}
/**
* Test update attribute frontend labels on stores.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\Text::getUpdateFrontendLabelsProvider
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/Catalog/_files/product_varchar_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateFrontendLabelOnStores(array $postData, array $expectedData): void
{
$this->processUpdateFrontendLabelOnStores('varchar_attribute', $postData, $expectedData);
}
}
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Controller\Adminhtml\Product\Attribute\Update\InputType;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Update\AbstractUpdateAttributeTest;
/**
* Test cases related to update attribute with yes/no input type.
*
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
class YesNoTest extends AbstractUpdateAttributeTest
{
/**
* Test update attribute.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\YesNo::getUpdateProvider
* @magentoDataFixture Magento/Catalog/_files/product_boolean_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateAttribute(array $postData, array $expectedData): void
{
$this->updateAttributeUsingData('boolean_attribute', $postData);
$this->assertUpdateAttributeProcess('boolean_attribute', $postData, $expectedData);
}
/**
* Test update attribute with error.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\YesNo::getUpdateProviderWithErrorMessage
* @magentoDataFixture Magento/Catalog/_files/product_boolean_attribute.php
*
* @param array $postData
* @param string $errorMessage
* @return void
*/
public function testUpdateAttributeWithError(array $postData, string $errorMessage): void
{
$this->updateAttributeUsingData('boolean_attribute', $postData);
$this->assertErrorSessionMessages($errorMessage);
}
/**
* Test update attribute frontend labels on stores.
*
* @dataProvider \Magento\TestFramework\Eav\Model\Attribute\DataProvider\YesNo::getUpdateFrontendLabelsProvider
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/Catalog/_files/product_boolean_attribute.php
*
* @param array $postData
* @param array $expectedData
* @return void
*/
public function testUpdateFrontendLabelOnStores(array $postData, array $expectedData): void
{
$this->processUpdateFrontendLabelOnStores('boolean_attribute', $postData, $expectedData);
}
}
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model\Attribute;
use Magento\Eav\Api\AttributeGroupRepositoryInterface;
use Magento\Eav\Model\Entity\Attribute\Set;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\TestFramework\Helper\Bootstrap;
class GroupRepositoryTest extends \PHPUnit\Framework\TestCase
{
/**
* @var AttributeGroupRepositoryInterface
*/
private $repository;
protected function setUp(): void
{
$this->repository = Bootstrap::getObjectManager()->create(AttributeGroupRepositoryInterface::class);
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_group_for_search.php
*/
public function testGetList()
{
/** @var Set $attributeSet */
$attributeSet = Bootstrap::getObjectManager()->create(Set::class)
->load('attribute_set_1_for_search', 'attribute_set_name');
/** @var FilterBuilder $filterBuilder */
$filterBuilder = Bootstrap::getObjectManager()->create(FilterBuilder::class);
$filter1 = $filterBuilder->setField('attribute_set_id')
->setValue($attributeSet->getId())
->create();
$filter2 = $filterBuilder->setField('default_id')
->setValue(0)
->setConditionType('eq')
->create();
$filter3 = $filterBuilder->setField('sort_order')
->setValue(10)
->setConditionType('gteq')
->create();
$filter4 = $filterBuilder->setField('sort_order')
->setValue(30)
->setConditionType('lteq')
->create();
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = Bootstrap::getObjectManager()->create(SearchCriteriaBuilder::class);
$searchCriteriaBuilder->addFilters([$filter1, $filter2]);
$searchCriteriaBuilder->addFilters([$filter3, $filter4]);
/** @var SortOrderBuilder $sortOrderBuilder */
$sortOrderBuilder = Bootstrap::getObjectManager()->create(SortOrderBuilder::class);
/** @var SortOrder $sortOrder */
$sortOrder = $sortOrderBuilder->setField('attribute_group_code')
->setDirection(SortOrder::SORT_ASC)
->create();
$searchCriteriaBuilder->setSortOrders([$sortOrder]);
$searchCriteriaBuilder->setPageSize(1);
$searchCriteriaBuilder->setCurrentPage(1);
$searchCriteria = $searchCriteriaBuilder->create();
$searchResult = $this->repository->getList($searchCriteria);
$this->assertEquals(2, $searchResult->getTotalCount());
$items = array_values($searchResult->getItems());
$this->assertCount(1, $items);
$this->assertEquals('attribute_group_3_for_search', $items[0]['attribute_group_code']);
}
}
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model;
class AttributeManagementTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Eav\Api\AttributeManagementInterface
*/
private $model;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->model = $this->objectManager->create(\Magento\Eav\Api\AttributeManagementInterface::class);
}
/**
* Verify that collection in service used correctly
*/
public function testGetList()
{
$productAttributeSetId = $this->getAttributeSetId(
\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE
);
$productAttributes = $this->model->getAttributes(
\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
$productAttributeSetId
);
// Verify that result contains only product attributes
$this->verifyAttributeSetIds($productAttributes, $productAttributeSetId);
$categoryAttributeSetId = $this->getAttributeSetId(
\Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE
);
$categoryAttributes = $this->model->getAttributes(
\Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE,
$categoryAttributeSetId
);
// Verify that result contains only category attributes
$this->verifyAttributeSetIds($categoryAttributes, $categoryAttributeSetId);
}
/**
* @param string $entityTypeCode
* @return int
*/
private function getAttributeSetId($entityTypeCode)
{
/** @var \Magento\Eav\Model\Config $eavConfig */
$eavConfig = $this->objectManager->create(\Magento\Eav\Model\Config::class);
return $eavConfig->getEntityType($entityTypeCode)->getDefaultAttributeSetId();
}
/**
* @param array $items
* @param string $attributeSetId
* @return void
*/
private function verifyAttributeSetIds(array $items, $attributeSetId)
{
/** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $item */
foreach ($items as $item) {
$this->assertEquals($attributeSetId, $item->getAttributeSetId());
}
}
}
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\TestFramework\Helper\Bootstrap;
class AttributeRepositoryTest extends \PHPUnit\Framework\TestCase
{
/**
* @var AttributeRepositoryInterface
*/
private $repository;
protected function setUp(): void
{
$this->repository = Bootstrap::getObjectManager()->create(AttributeRepositoryInterface::class);
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
*/
public function testGetList()
{
/** @var FilterBuilder $filterBuilder */
$filterBuilder = Bootstrap::getObjectManager()->create(FilterBuilder::class);
$filter1 = $filterBuilder->setField('backend_type')
->setValue('varchar')
->create();
$filter2 = $filterBuilder->setField('is_user_defined')
->setValue(true)
->create();
$filter3 = $filterBuilder->setField('is_required')
->setValue(true)
->create();
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = Bootstrap::getObjectManager()->create(SearchCriteriaBuilder::class);
$searchCriteriaBuilder->addFilters([$filter1, $filter2]);
$searchCriteriaBuilder->addFilters([$filter3]);
/** @var SortOrderBuilder $sortOrderBuilder */
$sortOrderBuilder = Bootstrap::getObjectManager()->create(SortOrderBuilder::class);
/** @var SortOrder $sortOrder */
$sortOrder = $sortOrderBuilder->setField('attribute_code')
->setDirection(SortOrder::SORT_ASC)
->create();
$searchCriteriaBuilder->setSortOrders([$sortOrder]);
$searchCriteriaBuilder->setPageSize(2);
$searchCriteriaBuilder->setCurrentPage(2);
$searchCriteria = $searchCriteriaBuilder->create();
$searchResult = $this->repository->getList('test', $searchCriteria);
$this->assertEquals(3, $searchResult->getTotalCount());
$items = array_values($searchResult->getItems());
$this->assertCount(1, $items);
$this->assertEquals('attribute_for_search_3', $items[0]['attribute_code']);
}
}
@@ -0,0 +1,207 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model;
use Magento\Framework\App\Config\MutableScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\CacheCleaner;
/**
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
*/
class ConfigTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Config
*/
private $config;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->config = $objectManager->get(Config::class);
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
*/
public function testGetEntityAttributeCodes()
{
$entityType = 'test';
$entityAttributeCodes1 = $this->config->getEntityAttributeCodes($entityType);
$this->assertEquals(
[
'attribute_for_search_1',
'attribute_for_search_2',
'attribute_for_search_3',
'attribute_for_search_4',
'attribute_for_search_5',
],
$entityAttributeCodes1
);
$entityAttributeCodes2 = $this->config->getEntityAttributeCodes($entityType);
$this->assertEquals($entityAttributeCodes1, $entityAttributeCodes2);
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
*/
public function testGetEntityAttributeCodesWithObject()
{
$entityType = 'test';
/** @var \Magento\Eav\Model\Entity\Type $testEntityType */
$testEntityType = Bootstrap::getObjectManager()->create(\Magento\Eav\Model\Entity\Type::class)
->loadByCode('test');
$attributeSetId = $testEntityType->getDefaultAttributeSetId();
$object = new DataObject(
[
'attribute_set_id' => $attributeSetId,
'store_id' => 0
]
);
$entityAttributeCodes1 = $this->config->getEntityAttributeCodes($entityType, $object);
$this->assertEquals(
[
'attribute_for_search_1',
'attribute_for_search_2',
],
$entityAttributeCodes1
);
$entityAttributeCodes2 = $this->config->getEntityAttributeCodes($entityType, $object);
$this->assertEquals($entityAttributeCodes1, $entityAttributeCodes2);
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
*/
public function testGetAttributes()
{
$entityType = 'test';
$attributes1 = $this->config->getAttributes($entityType);
$expectedAttributeCodes = [
'attribute_for_search_1',
'attribute_for_search_2',
'attribute_for_search_3',
'attribute_for_search_4',
'attribute_for_search_5',
];
$this->assertEquals(count($expectedAttributeCodes), count($attributes1));
$attributeCodes = [];
foreach ($attributes1 as $attribute) {
$attributeCodes[] = $attribute->getAttributeCode();
}
$this->assertEquals($expectedAttributeCodes, $attributeCodes);
$attributes2 = $this->config->getAttributes($entityType);
$this->assertEquals($attributes1, $attributes2);
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
*/
public function testGetAttribute()
{
$entityType = 'test';
$attribute1 = $this->config->getAttribute($entityType, 'attribute_for_search_1');
$this->assertEquals('attribute_for_search_1', $attribute1->getAttributeCode());
$this->assertEquals('varchar', $attribute1->getBackendType());
$this->assertEquals(1, $attribute1->getIsRequired());
$this->assertEquals(1, $attribute1->getIsUserDefined());
$this->assertEquals(0, $attribute1->getIsUnique());
$attribute2 = $this->config->getAttribute($entityType, 'attribute_for_search_1');
$this->assertEquals($attribute1, $attribute2);
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_for_caching.php
*/
public function testGetAttributeWithCacheUserDefinedAttribute()
{
/** @var MutableScopeConfigInterface $mutableScopeConfig */
$mutableScopeConfig = Bootstrap::getObjectManager()->get(MutableScopeConfigInterface::class);
$mutableScopeConfig->setValue('dev/caching/cache_user_defined_attributes', 1);
$entityType = 'catalog_product';
$attribute = $this->config->getAttribute($entityType, 'foo');
$this->assertEquals('foo', $attribute->getAttributeCode());
$this->assertEquals('foo', $attribute->getFrontendLabel());
$this->assertEquals('varchar', $attribute->getBackendType());
$this->assertEquals(1, $attribute->getIsRequired());
$this->assertEquals(1, $attribute->getIsUserDefined());
$this->assertEquals(0, $attribute->getIsUnique());
// Update attribute
$eavSetupFactory = Bootstrap::getObjectManager()->create(\Magento\Eav\Setup\EavSetupFactory::class);
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
$eavSetup = $eavSetupFactory->create();
$eavSetup->updateAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'foo',
[
'frontend_label' => 'bar',
]
);
// Check that attribute data has not changed
$config = Bootstrap::getObjectManager()->create(\Magento\Eav\Model\Config::class);
$updatedAttribute = $config->getAttribute($entityType, 'foo');
$this->assertEquals('foo', $updatedAttribute->getFrontendLabel());
CacheCleaner::clean(['eav']);
$config = Bootstrap::getObjectManager()->create(\Magento\Eav\Model\Config::class);
// Check that attribute data has changed
$updatedAttributeAfterCacheClean = $config->getAttribute($entityType, 'foo');
$this->assertEquals('bar', $updatedAttributeAfterCacheClean->getFrontendLabel());
$mutableScopeConfig->setValue('dev/caching/cache_user_defined_attributes', 0);
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_for_caching.php
*/
public function testGetAttributeWithInitUserDefinedAttribute()
{
/** @var MutableScopeConfigInterface $mutableScopeConfig */
$mutableScopeConfig = Bootstrap::getObjectManager()->get(MutableScopeConfigInterface::class);
$mutableScopeConfig->setValue('dev/caching/cache_user_defined_attributes', 0);
$entityType = 'catalog_product';
$attribute = $this->config->getAttribute($entityType, 'foo');
$this->assertEquals('foo', $attribute->getAttributeCode());
$this->assertEquals('foo', $attribute->getFrontendLabel());
$this->assertEquals('varchar', $attribute->getBackendType());
$this->assertEquals(1, $attribute->getIsRequired());
$this->assertEquals(1, $attribute->getIsUserDefined());
$this->assertEquals(0, $attribute->getIsUnique());
// Update attribute
$eavSetupFactory = Bootstrap::getObjectManager()->create(\Magento\Eav\Setup\EavSetupFactory::class);
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
$eavSetup = $eavSetupFactory->create();
$eavSetup->updateAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'foo',
[
'frontend_label' => 'bar',
]
);
// Check that attribute data has changed
$config = Bootstrap::getObjectManager()->create(\Magento\Eav\Model\Config::class);
$updatedAttributeAfterCacheClean = $config->getAttribute($entityType, 'foo');
$this->assertEquals('bar', $updatedAttributeAfterCacheClean->getFrontendLabel());
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
$reflection = new \ReflectionObject($this);
foreach ($reflection->getProperties() as $property) {
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
$property->setAccessible(true);
$property->setValue($this, null);
}
}
}
}
@@ -0,0 +1,119 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Model\Entity\Attribute\Frontend;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Store\ExecuteInStoreContext;
use PHPUnit\Framework\TestCase;
/**
* Checks Datetime attribute's frontend model
*
* @magentoAppArea frontend
*
* @see \Magento\Eav\Model\Entity\Attribute\Frontend\Datetime
*/
class DatetimeTest extends TestCase
{
/**
* @var int
*/
private const ONE_HOUR_IN_MILLISECONDS = 3600;
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var ProductAttributeRepositoryInterface
*/
private $attributeRepository;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var DateTime
*/
private $dateTime;
/**
* @var ExecuteInStoreContext
*/
private $executeInStoreContext;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->attributeRepository = $this->objectManager->get(ProductAttributeRepositoryInterface::class);
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$this->productRepository->cleanCache();
$this->dateTime = $this->objectManager->create(DateTime::class);
$this->executeInStoreContext = $this->objectManager->get(ExecuteInStoreContext::class);
}
/**
* @magentoDbIsolation disabled
*
* @magentoDataFixture Magento/Catalog/_files/product_two_websites.php
* @magentoDataFixture Magento/Catalog/_files/product_datetime_attribute.php
*
* @magentoConfigFixture default_store general/locale/timezone Asia/Tokyo
* @magentoConfigFixture fixture_second_store_store general/locale/timezone Asia/Shanghai
*
* @return void
*/
public function testFrontendValueOnDifferentWebsites(): void
{
$attribute = $this->attributeRepository->get('datetime_attribute');
$product = $this->productRepository->get('simple-on-two-websites');
$product->setDatetimeAttribute($this->dateTime->date('Y-m-d H:i:s'));
$firstWebsiteValue = $this->executeInStoreContext->execute(
'default',
[$attribute->getFrontend(), 'getValue'],
$product
);
$secondWebsiteValue = $this->executeInStoreContext->execute(
'fixture_second_store',
[$attribute->getFrontend(), 'getValue'],
$product
);
$this->assertEquals(
self::ONE_HOUR_IN_MILLISECONDS,
$this->dateTime->gmtTimestamp($firstWebsiteValue) - $this->dateTime->gmtTimestamp($secondWebsiteValue),
'The difference between values per different timezones is incorrect'
);
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
$reflection = new \ReflectionObject($this);
foreach ($reflection->getProperties() as $property) {
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
$property->setAccessible(true);
$property->setValue($this, null);
}
}
}
}
@@ -0,0 +1,119 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model\Entity\Attribute\Frontend;
use Magento\Catalog\Model\Product;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\CacheCleaner;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\App\CacheInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Serialize\Serializer\Json as Serializer;
use Magento\Eav\Model\Entity\Attribute;
/**
* @magentoAppIsolation enabled
*/
class DefaultFrontendTest extends \PHPUnit\Framework\TestCase
{
/**
* @var DefaultFrontend
*/
private $defaultFrontend;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;
/**
* @var AbstractAttribute
*/
private $attribute;
/**
* @var array
*/
private $options;
/**
* @var CacheInterface
*/
private $cache;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var Serializer
*/
private $serializer;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->defaultFrontend = $this->objectManager->get(DefaultFrontend::class);
$this->cache = $this->objectManager->get(CacheInterface::class);
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
$this->serializer = $this->objectManager->get(Serializer::class);
$this->attribute = $this->objectManager->get(Attribute::class);
$this->attribute->setAttributeCode('store_id');
$this->options = $this->attribute->getSource()->getAllOptions();
$this->defaultFrontend->setAttribute($this->attribute);
}
public function testGetSelectOptions()
{
$this->assertSame($this->options, $this->defaultFrontend->getSelectOptions());
$this->assertSame(
$this->serializer->serialize($this->options),
$this->cache->load($this->getCacheKey())
);
}
/**
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
*/
public function testAttributeEntityValueNotSet()
{
$entity = $this->objectManager->create(Product::class);
$entity->setStoreId(0);
$entity->load(1);
$frontEnd = $this->attribute->loadByCode('catalog_product', 'dropdown_attribute');
$value = $frontEnd->getFrontend()->getValue($entity);
$this->assertFalse($value);
}
/**
* Cache key generation
* @return string
*/
private function getCacheKey()
{
return 'attribute-navigation-option-' .
$this->defaultFrontend->getAttribute()->getAttributeCode() . '-' .
$this->storeManager->getStore()->getId();
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
$reflection = new \ReflectionObject($this);
foreach ($reflection->getProperties() as $property) {
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
$property->setAccessible(true);
$property->setValue($this, null);
}
}
}
}
@@ -0,0 +1,127 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model\Entity;
use Magento\Framework\DataObject;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\CacheCleaner;
/**
* @magentoAppIsolation enabled
* @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
*/
class AttributeLoaderTest extends \PHPUnit\Framework\TestCase
{
/**
* @var AttributeLoader
*/
private $attributeLoader;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;
/**
* @var \Magento\Eav\Model\Entity\AbstractEntity
*/
private $resource;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->attributeLoader = $this->objectManager->get(AttributeLoader::class);
$entityType = $this->objectManager->create(\Magento\Eav\Model\Entity\Type::class)
->loadByCode('test');
$context = $this->objectManager->get(\Magento\Eav\Model\Entity\Context::class);
$this->resource = $this->getMockBuilder(\Magento\Eav\Model\Entity\AbstractEntity::class)
->setConstructorArgs([$context])
->setMethods(['getEntityType', 'getLinkField'])
->getMock();
$this->resource->method('getEntityType')
->willReturn($entityType);
$this->resource->method('getLinkField')
->willReturn('link_field');
}
/**
* @param int $expectedNumOfAttributesByCode
* @param int $expectedNumOfAttributesByTable
* @param DataObject|null $object
* @dataProvider loadAllAttributesDataProvider
*/
public function testLoadAllAttributesTheFirstTime(
$expectedNumOfAttributesByCode,
$expectedNumOfAttributesByTable,
$object
) {
// Before load all attributes
$attributesByCode = $this->resource->getAttributesByCode();
$attributesByTable = $this->resource->getAttributesByTable();
$this->assertCount(0, $attributesByCode);
$this->assertCount(0, $attributesByTable);
// Load all attributes
$resource2 = $this->attributeLoader->loadAllAttributes(
$this->resource,
$object
);
$attributesByCode2 = $resource2->getAttributesByCode();
$attributesByTable2 = $resource2->getAttributesByTable();
$this->assertEquals($expectedNumOfAttributesByCode, count($attributesByCode2));
$this->assertEquals($expectedNumOfAttributesByTable, count($attributesByTable2));
}
public function loadAllAttributesDataProvider()
{
/** @var \Magento\Eav\Model\Entity\Type $entityType */
$entityType = Bootstrap::getObjectManager()->create(\Magento\Eav\Model\Entity\Type::class)
->loadByCode('order');
$attributeSetId = $entityType->getDefaultAttributeSetId();
return [
[
13,
2,
null
],
[
10,
1,
new DataObject(
[
'attribute_set_id' => $attributeSetId,
'store_id' => 0
]
),
],
[
10,
1,
new DataObject(
[
'attribute_set_id' => $attributeSetId,
'store_id' => 10
]
),
],
];
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
$reflection = new \ReflectionObject($this);
foreach ($reflection->getProperties() as $property) {
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
$property->setAccessible(true);
$property->setValue($this, null);
}
}
}
}
@@ -0,0 +1,140 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Model\Entity;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Class to test EAV Entity attribute model
*/
class AttributeTest extends TestCase
{
/**
* @var Attribute
*/
private $attribute;
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var ResolverInterface
*/
private $localeResolver;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->attribute = $this->objectManager->get(Attribute::class);
$this->localeResolver = $this->objectManager->get(ResolverInterface::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->attribute = null;
$this->objectManager = null;
$this->localeResolver = null;
$reflection = new \ReflectionObject($this);
foreach ($reflection->getProperties() as $property) {
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
$property->setAccessible(true);
$property->setValue($this, null);
}
}
}
/**
* @param string $defaultValue
* @param string $backendType
* @param string $locale
* @param string $expected
* @dataProvider beforeSaveDataProvider
* @throws
*/
public function testBeforeSave(
string $defaultValue,
string $backendType,
string $frontendInput,
string $locale,
string $expected
) {
$this->attribute->setDefaultValue($defaultValue);
$this->attribute->setBackendType($backendType);
$this->attribute->setFrontendInput($frontendInput);
$this->localeResolver->setLocale($locale);
$this->attribute->beforeSave();
$this->assertEquals($expected, $this->attribute->getDefaultValue());
}
/**
* Data provider for beforeSaveData.
*
* @return array
*/
public function beforeSaveDataProvider()
{
return [
['21/07/18', 'datetime', 'date', 'en_AU', '2018-07-21 00:00:00'],
['07/21/18', 'datetime', 'date', 'en_US', '2018-07-21 00:00:00'],
['21/07/18', 'datetime', 'date', 'fr_FR', '2018-07-21 00:00:00'],
['21/07/18', 'datetime', 'date', 'de_DE', '2018-07-21 00:00:00'],
['21/07/18', 'datetime', 'date', 'uk_UA', '2018-07-21 00:00:00'],
['100.50', 'decimal', 'decimal', 'en_US', '100.50'],
['100,50', 'decimal', 'decimal', 'uk_UA', '100.5'],
['07/21/2019 2:30 PM', 'datetime', 'datetime', 'en_US', '2019-07-21 21:30:00'],
['21.07.2019 14:30', 'datetime', 'datetime', 'uk_UA', '2019-07-21 21:30:00'],
];
}
/**
* @param string $defaultValue
* @param string $backendType
* @param string $locale
* @param string $expected
* @dataProvider beforeSaveErrorDataDataProvider
*/
public function testBeforeSaveErrorData($defaultValue, $backendType, $locale, $expected)
{
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
$this->attribute->setDefaultValue($defaultValue);
$this->attribute->setBackendType($backendType);
$this->localeResolver->setLocale($locale);
$this->attribute->beforeSave();
$this->expectExceptionMessage($expected);
}
/**
* Data provider for beforeSaveData with error result.
*
* @return array
*/
public function beforeSaveErrorDataDataProvider()
{
return [
'wrong date for Australia' => ['32/38', 'datetime', 'en_AU', 'Invalid default date'],
'wrong date for States' => ['32/38', 'datetime', 'en_US', 'Invalid default date'],
'wrong decimal separator for US' => ['100,50', 'decimal', 'en_US', 'Invalid default decimal value'],
'wrong decimal separator for UA' => ['100.50', 'decimal', 'uk_UA', 'Invalid default decimal value'],
];
}
}
@@ -0,0 +1,99 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model\ResourceModel;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\TestFramework\Helper\Bootstrap;
/**
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
* @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
*/
class AttributeLoaderTest extends \Magento\TestFramework\Indexer\TestCase
{
/**
* @var AttributeLoader
*/
private $attributeLoader;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$metadataPool = $objectManager->create(
MetadataPool::class,
[
'metadata' => [
'Test\Entity\Type' => [
'entityTableName' => 'test_entity',
'eavEntityType' => 'test',
'identifierField' => 'entity_id',
]
]
]
);
$this->attributeLoader = $objectManager->create(AttributeLoader::class, ['metadataPool' => $metadataPool]);
}
/**
* @param string[] $expectedAttributeCodes
* @param int|null $attributeSetId
* @dataProvider getAttributesDataProvider
*/
public function testGetAttributes($expectedAttributeCodes, $attributeSetId = null)
{
$attributes = $this->attributeLoader->getAttributes('Test\Entity\Type', $attributeSetId);
$this->assertEquals(count($expectedAttributeCodes), count($attributes));
$attributeCodes = [];
foreach ($attributes as $attribute) {
$attributeCodes[] = $attribute->getAttributeCode();
}
$this->assertEquals($expectedAttributeCodes, $attributeCodes);
$attributes2 = $this->attributeLoader->getAttributes('Test\Entity\Type', $attributeSetId);
$this->assertEquals($attributes, $attributes2);
}
public function getAttributesDataProvider()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$entityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)
->loadByCode('order');
$attributeSetId = $entityType->getDefaultAttributeSetId();
return [
'all' => [
[
'attribute_for_search_1',
'attribute_for_search_2',
'attribute_for_search_3',
]
],
"$attributeSetId" => [
[
'attribute_for_search_1',
'attribute_for_search_2',
],
$attributeSetId,
]
];
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
$reflection = new \ReflectionObject($this);
foreach ($reflection->getProperties() as $property) {
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
$property->setAccessible(true);
$property->setValue($this, null);
}
}
}
}
@@ -0,0 +1,111 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model\ResourceModel\Entity\Attribute;
class CollectionTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
*/
protected $_model;
protected function setUp(): void
{
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class
);
}
/**
* Returns array of set ids, present in collection attributes
*
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $collection
* @return array
*/
protected function _getSets($collection)
{
$collection->addSetInfo();
$sets = [];
foreach ($collection as $attribute) {
foreach (array_keys($attribute->getAttributeSetInfo()) as $setId) {
$sets[$setId] = $setId;
}
}
return array_values($sets);
}
public function testSetAttributeGroupFilter()
{
$collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class
);
$groupsPresent = $this->_getGroups($collection);
$includeGroupId = current($groupsPresent);
$this->_model->setAttributeGroupFilter($includeGroupId);
$groups = $this->_getGroups($this->_model);
$this->assertEquals([$includeGroupId], $groups);
}
/**
* Test if getAllIds method return results after using setInAllAttributeSetsFilter method
*
* @covers \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::setInAllAttributeSetsFilter()
* @covers \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::getAllIds()
*/
public function testSetInAllAttributeSetsFilterWithGetAllIds()
{
$sets = [1];
$this->_model->setInAllAttributeSetsFilter($sets);
$attributeIds = $this->_model->getAllIds();
$this->assertGreaterThan(0, count($attributeIds));
}
/**
* Returns array of group ids, present in collection attributes
*
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $collection
* @return array
*/
protected function _getGroups($collection)
{
$collection->addSetInfo();
$groups = [];
foreach ($collection as $attribute) {
foreach ($attribute->getAttributeSetInfo() as $setInfo) {
$groupId = $setInfo['group_id'];
$groups[$groupId] = $groupId;
}
}
return array_values($groups);
}
public function testAddAttributeGrouping()
{
$select = $this->_model->getSelect();
$this->assertEmpty($select->getPart(\Magento\Framework\DB\Select::GROUP));
$this->_model->addAttributeGrouping();
$this->assertEquals(['main_table.attribute_id'], $select->getPart(\Magento\Framework\DB\Select::GROUP));
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
$reflection = new \ReflectionObject($this);
foreach ($reflection->getProperties() as $property) {
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
$property->setAccessible(true);
$property->setValue($this, null);
}
}
}
}
@@ -0,0 +1,80 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model\ResourceModel\UpdateHandler;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\ResourceModel\UpdateHandler;
use Magento\Eav\Model\ResourceModel\UpdateHandlerAbstract;
use Magento\TestFramework\Helper\Bootstrap;
/**
* @magentoAppArea adminhtml
* @magentoAppIsolation enabled
*/
class ExecuteProcessForAllStoresTest extends UpdateHandlerAbstract
{
/**
* @covers \Magento\Eav\Model\ResourceModel\UpdateHandler::execute
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @dataProvider getAllStoresDataProvider
* @param $code
* @param $snapshotValue
* @param $newValue
* @param $expected
* @magentoDbIsolation disabled
*/
public function testExecuteProcessForAllStores($code, $snapshotValue, $newValue, $expected)
{
if ($snapshotValue !== '-') {
$entity = Bootstrap::getObjectManager()->create(Product::class);
$entity->setStoreId(0);
$entity->load(1);
$entity->setData($code, $snapshotValue);
$entity->save();
}
$entity = Bootstrap::getObjectManager()->create(Product::class);
$entity->setStoreId(0);
$entity->load(1);
$updateHandler = Bootstrap::getObjectManager()->create(UpdateHandler::class);
$entityData = array_merge($entity->getData(), [$code => $newValue]);
$updateHandler->execute(\Magento\Catalog\Api\Data\ProductInterface::class, $entityData);
$resultEntity = Bootstrap::getObjectManager()->create(Product::class);
$resultEntity->setStoreId(0);
$resultEntity->load(1);
$this->assertSame($expected, $resultEntity->getData($code));
}
/**
* @return array
*/
public function getAllStoresDataProvider()
{
return [
['description', '', 'not_empty_value', 'not_empty_value'], //0
['description', '', '', null], //1
['description', '', null, null], //2
['description', '', false, null], //3
['description', 'not_empty_value', 'not_empty_value2', 'not_empty_value2'], //4
['description', 'not_empty_value', '', null], //5
['description', 'not_empty_value', null, null], //6
['description', 'not_empty_value', false, null], //7
['description', null, 'not_empty_value', 'not_empty_value'], //8
['description', null, '', null], //9
['description', null, false, null], //10
['description', false, 'not_empty_value', 'not_empty_value'], //11
['description', false, '', null], //12
['description', false, null, null], //13
];
}
}
@@ -0,0 +1,133 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model\ResourceModel\UpdateHandler;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Eav\Model\ResourceModel\UpdateHandlerAbstract;
use Magento\Store\Model\Store;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Eav\Model\ResourceModel\UpdateHandler;
/**
* @magentoAppArea adminhtml
* @magentoAppIsolation enabled
*/
class ExecuteProcessForCustomAttributeInCustomStoreTest extends UpdateHandlerAbstract
{
/**
* @covers \Magento\Eav\Model\ResourceModel\UpdateHandler::execute
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
* @magentoDataFixture Magento/Store/_files/second_store.php
* @dataProvider getCustomAttributeDataProvider
* @param $code
* @param $defaultStoreValue
* @param $snapshotValue
* @param $newValue
* @param $expected
* @magentoDbIsolation disabled
*/
public function testExecuteProcessForCustomAttributeInCustomStore(
$code,
$defaultStoreValue,
$snapshotValue,
$newValue,
$expected
) {
$store = Bootstrap::getObjectManager()->create(Store::class);
$store->load('fixture_second_store', 'code');
$this->reindexAll();
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
Attribute::class
);
$attribute->loadByCode(4, $code);
$options = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class
);
$options->setAttributeFilter($attribute->getId());
$optionIds = $options->getAllIds();
$entity = Bootstrap::getObjectManager()->create(Product::class);
$entity->setStoreId(0);
$entity->load(1);
$entity->setData($code, $optionIds[$defaultStoreValue]);
$entity->save();
if ($snapshotValue !== '-') {
/** @var $options \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection */
$entity = Bootstrap::getObjectManager()->create(Product::class);
$entity->setStoreId($store->getId());
$entity->load(1);
if ($snapshotValue) {
$snapshotValue = $optionIds[$snapshotValue];
}
$entity->setData($code, $snapshotValue);
$entity->save();
}
$entity = Bootstrap::getObjectManager()->create(Product::class);
$entity->setStoreId($store->getId());
$entity->load(1);
$updateHandler = Bootstrap::getObjectManager()->create(UpdateHandler::class);
if ($newValue) {
$newValue = $optionIds[$newValue];
}
$entityData = array_merge($entity->getData(), [$code => $newValue]);
$updateHandler->execute(\Magento\Catalog\Api\Data\ProductInterface::class, $entityData);
$resultEntity = Bootstrap::getObjectManager()->create(Product::class);
$resultEntity->setStoreId($store->getId());
$resultEntity->load(1);
if ($expected !== null) {
$expected = $optionIds[$expected];
}
$this->assertSame($expected, $resultEntity->getData($code));
}
/**
* @return array
*/
public function getCustomAttributeDataProvider()
{
return [
['dropdown_attribute', 0, '', 1, 1], //0
['dropdown_attribute', 0, '', '', null], //1
['dropdown_attribute', 0, '', null, 0], //2
['dropdown_attribute', 0, '', false, 0], //3
['dropdown_attribute', 0, 1, 2, 2], //4
['dropdown_attribute', 0, 1, '', null], //5
['dropdown_attribute', 0, 1, null, 0], //6
['dropdown_attribute', 0, 1, false, 0], //7
['dropdown_attribute', 0, null, 1, 1], //8
['dropdown_attribute', 0, null, '', null], //9
['dropdown_attribute', 0, null, false, 0], //10
['dropdown_attribute', 0, false, 1, 1], //11
['dropdown_attribute', 0, false, '', null], //12
['dropdown_attribute', 0, false, null, 0], //13
['dropdown_attribute', 0, '-', 1, 1], //14
['dropdown_attribute', 0, '-', '', null], //15
['dropdown_attribute', 0, '-', null, 0], //16
['dropdown_attribute', 0, '-', false, 0], //17
];
}
}
@@ -0,0 +1,88 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model\ResourceModel\UpdateHandler;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\ResourceModel\UpdateHandlerAbstract;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Eav\Model\ResourceModel\UpdateHandler;
/**
* @magentoAppArea adminhtml
* @magentoAppIsolation enabled
*/
class ExecuteProcessForCustomStoreTest extends UpdateHandlerAbstract
{
/**
* @covers \Magento\Eav\Model\ResourceModel\UpdateHandler::execute
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDataFixture Magento/Store/_files/second_store.php
* @dataProvider getCustomStoreDataProvider
* @param $code
* @param $snapshotValue
* @param $newValue
* @param $expected
* @magentoDbIsolation disabled
*/
public function testExecuteProcessForCustomStore($code, $snapshotValue, $newValue, $expected)
{
$store = Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class);
$store->load('fixture_second_store', 'code');
$this->reindexAll();
if ($snapshotValue !== '-') {
$entity = Bootstrap::getObjectManager()->create(Product::class);
$entity->setStoreId($store->getId());
$entity->load(1);
$entity->setData($code, $snapshotValue);
$entity->save();
}
$entity = Bootstrap::getObjectManager()->create(Product::class);
$entity->setStoreId($store->getId());
$entity->load(1);
$updateHandler = Bootstrap::getObjectManager()->create(UpdateHandler::class);
$entityData = array_merge($entity->getData(), [$code => $newValue]);
$updateHandler->execute(ProductInterface::class, $entityData);
$resultEntity = Bootstrap::getObjectManager()->create(Product::class);
$resultEntity->setStoreId($store->getId());
$resultEntity->load(1);
$this->assertSame($expected, $resultEntity->getData($code));
}
/**
* @return array
*/
public function getCustomStoreDataProvider()
{
return [
['description', '', 'not_empty_value', 'not_empty_value'], //0
['description', '', '', null], //1
['description', '', null, 'Description with <b>html tag</b>'], //2
['description', '', false, 'Description with <b>html tag</b>'], //3
['description', 'not_empty_value', 'not_empty_value2', 'not_empty_value2'], //4
['description', 'not_empty_value', '', null], //5
['description', 'not_empty_value', null, 'Description with <b>html tag</b>'], //6
['description', 'not_empty_value', false, 'Description with <b>html tag</b>'], //7
['description', null, 'not_empty_value', 'not_empty_value'], //8
['description', null, '', null], //9
['description', null, false, 'Description with <b>html tag</b>'], //10
['description', false, 'not_empty_value', 'not_empty_value'], //11
['description', false, '', null], //12
['description', false, null, 'Description with <b>html tag</b>'], //13
];
}
}
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model\ResourceModel;
use Magento\CatalogSearch\Model\Indexer\Fulltext\Processor;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Indexer\TestCase;
/**
* @magentoAppArea adminhtml
* @magentoAppIsolation enabled
*/
class UpdateHandlerAbstract extends TestCase
{
protected function reindexAll(): void
{
Bootstrap::getObjectManager()
->get(Processor::class)
->reindexAll();
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
$reflection = new \ReflectionObject($this);
foreach ($reflection->getProperties() as $property) {
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
$property->setAccessible(true);
$property->setValue($this, null);
}
}
}
}
@@ -0,0 +1,222 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model;
use Magento\TestFramework\Helper\Bootstrap;
class TypeLocatorTest extends \PHPUnit\Framework\TestCase
{
/**
* @var TypeLocator
*/
private $attributeTypeLocator;
protected function setUp(): void
{
$this->attributeTypeLocator = Bootstrap::getObjectManager()->get(TypeLocator::class);
}
/**
* @param string $entityType
* @param string[] $attributeList
* @dataProvider getExpectedAttributeTypesProvider
*/
public function testGetType(
$entityType,
array $attributeList
) {
foreach ($attributeList as $attributeCode => $expectedType) {
$this->assertEquals(
$expectedType,
$this->attributeTypeLocator->getType($attributeCode, $entityType),
"Expected type of '{$attributeCode}' product attribute was '{$expectedType}"
);
}
}
/**
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function getExpectedAttributeTypesProvider(): array
{
return [
'product' => [
'catalog_product',
[
'name' => 'string',
'sku' => 'string',
'description' => 'string',
'short_description' => 'string',
'price' => 'double',
'special_price' => 'double',
'special_from_date' => 'string',
'special_to_date' => 'string',
'cost' => 'double',
'weight' => 'double',
'manufacturer' => 'int',
'meta_title' => 'string',
'meta_keyword' => 'string',
'meta_description' => 'string',
'image' => 'string',
'small_image' => 'string',
'thumbnail' => 'string',
'old_id' => 'int',
'tier_price' => 'double',
'color' => 'int',
'news_from_date' => 'string',
'news_to_date' => 'string',
'gallery' => 'string',
'status' => 'int',
'minimal_price' => 'double',
'visibility' => 'int',
'custom_design' => 'string',
'custom_design_from' => 'string',
'custom_design_to' => 'string',
'custom_layout_update' => 'string',
'page_layout' => 'string',
'category_ids' => 'int[]',
'options_container' => 'string',
'required_options' => 'string',
'has_options' => 'string',
'image_label' => 'string',
'small_image_label' => 'string',
'thumbnail_label' => 'string',
'created_at' => 'string',
'updated_at' => 'string',
'country_of_manufacture' => 'string',
'quantity_and_stock_status' => \Magento\CatalogInventory\Api\Data\StockItemInterface::class . '[]',
'custom_layout' => 'string',
'url_key' => 'string',
'url_path' => 'string',
'msrp' => 'double',
'msrp_display_actual_price_type' => 'string',
'price_type' => 'int',
'sku_type' => 'int',
'weight_type' => 'int',
'price_view' => 'int',
'shipment_type' => 'int',
'links_purchased_separately' => 'int',
'samples_title' => 'string',
'links_title' => 'string',
'links_exist' => 'int',
'gift_message_available' => 'string',
'swatch_image' => 'string',
'tax_class_id' => 'int'
]
],
'customer'=> [
'customer',
[
'confirmation' => 'string',
'created_at' => 'string',
'website_id' => 'int',
'store_id' => 'int',
'created_in' => 'string',
'group_id' => 'string',
'disable_auto_group_change' => 'boolean',
'prefix' => 'string',
'firstname' => 'string',
'middlename' => 'string',
'lastname' => 'string',
'suffix' => 'string',
'email' => 'string',
'password_hash' => 'string',
'default_billing' => \Magento\Customer\Api\Data\AddressInterface::class,
'default_shipping' => \Magento\Customer\Api\Data\AddressInterface::class,
'updated_at' => 'string',
'dob' => 'string',
'taxvat' => 'string',
'failures_num' => 'string',
'gender' => 'string',
'first_failure' => 'string',
'rp_token' => 'string',
'rp_token_created_at' => 'string',
'lock_expires' => 'string',
]
],
'customer address' => [
'customer_address',
[
'prefix' => 'string',
'firstname' => 'string',
'middlename' => 'string',
'lastname' => 'string',
'suffix' => 'string',
'company' => 'string',
'street' => 'string',
'city' => 'string',
'country_id' => 'string',
'region' => \Magento\Customer\Api\Data\RegionInterface::class,
'region_id' => 'string',
'postcode' => 'string',
'telephone' => 'string',
'fax' => 'string',
'vat_is_valid' => 'string',
'vat_request_id' => 'string',
'vat_request_date' => 'string',
'vat_request_success' => 'string',
'vat_id' => 'string',
]
],
'category' => [
'catalog_category',
[
'name' => 'string',
'is_active' => 'int',
'url_key' => 'string',
'description' => 'string',
'image' => 'string',
'custom_use_parent_settings' => 'int',
'meta_title' => 'string',
'custom_apply_to_products' => 'int',
'meta_keywords' => 'string',
'meta_description' => 'string',
'display_mode' => 'string',
'custom_design' => 'string',
'include_in_menu' => 'int',
'path' => 'string',
'position' => 'string',
'all_children' => 'string',
'path_in_store' => 'string',
'children' => 'string',
'url_path' => 'string',
'landing_page' => 'int',
'level' => 'string',
'children_count' => 'string',
'is_anchor' => 'int',
'custom_design_from' => 'string',
'custom_design_to' => 'string',
'available_sort_by' => 'string[]',
'page_layout' => 'string',
'default_sort_by' => 'string[]',
'filter_price_range' => 'double',
'custom_layout_update' => 'string',
]
],
'undefined attributes' => [
'catalog_product',
['media_gallery' => 'anyType', 'undefine_attribute' => 'anyType']
]
];
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
$reflection = new \ReflectionObject($this);
foreach ($reflection->getProperties() as $property) {
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
$property->setAccessible(true);
$property->setValue($this, null);
}
}
}
}
@@ -0,0 +1,65 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Test for \Magento\Eav\Model\Validator\Attribute\Backend
*/
namespace Magento\Eav\Model\Validator\Attribute;
class BackendTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Eav\Model\Validator\Attribute\Backend
*/
protected $_model;
protected function setUp(): void
{
$this->_model = new \Magento\Eav\Model\Validator\Attribute\Backend();
}
/**
* Test method for \Magento\Eav\Model\Validator\Attribute\Backend::isValid
*
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
public function testIsValid()
{
/** @var $entity \Magento\Customer\Model\Customer */
$entity = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Customer\Model\Customer::class
)->load(
1
);
$this->assertTrue($this->_model->isValid($entity));
$this->assertEmpty($this->_model->getMessages());
$entity->setData('email', null);
$this->assertFalse($this->_model->isValid($entity));
$this->assertArrayHasKey('email', $this->_model->getMessages());
$entity->setData('firstname', null);
$this->assertFalse($this->_model->isValid($entity));
$this->assertArrayHasKey('email', $this->_model->getMessages());
$this->assertArrayHasKey('firstname', $this->_model->getMessages());
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
$reflection = new \ReflectionObject($this);
foreach ($reflection->getProperties() as $property) {
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
$property->setAccessible(true);
$property->setValue($this, null);
}
}
}
}
@@ -0,0 +1,243 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Eav\Setup;
use Magento\Catalog\Model\Product;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Eav\Api\Data\AttributeOptionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use PHPUnit\Framework\TestCase;
/**
* Tests coverage for \Magento\Eav\Setup\AddOptionToAttribute
*/
class AddOptionToAttributeTest extends TestCase
{
/**
* @var AddOptionToAttribute
*/
private $operation;
/**
* @var int
*/
private $attributeId;
/**
* @var EavSetup
*/
private $eavSetup;
/**
* @var AttributeRepositoryInterface
*/
private $attrRepo;
/**
* @var ModuleDataSetupInterface
*/
private $setup;
protected function setUp(): void
{
$objectManager = ObjectManager::getInstance();
$this->operation = $objectManager->get(AddOptionToAttribute::class);
/** @var ModuleDataSetupInterface $setup */
$this->setup = $objectManager->get(ModuleDataSetupInterface::class);
/** @var AttributeRepositoryInterface attrRepo */
$this->attrRepo = $objectManager->get(AttributeRepositoryInterface::class);
/** @var EavSetup $eavSetup */
$this->eavSetup = $objectManager->get(EavSetupFactory::class)
->create(['setup' => $this->setup]);
$this->attributeId = $this->eavSetup->getAttributeId(Product::ENTITY, 'zzz');
}
/**
* @param bool $fetchPairs
*
* @return array
*/
private function getAttributeOptions($fetchPairs = true): array
{
$optionTable = $this->setup->getTable('eav_attribute_option');
$optionValueTable = $this->setup->getTable('eav_attribute_option_value');
$select = $this->setup
->getConnection()
->select()
->from(['o' => $optionTable])
->reset('columns')
->columns('sort_order')
->join(['ov' => $optionValueTable], 'o.option_id = ov.option_id', 'value')
->where(AttributeInterface::ATTRIBUTE_ID . ' = ?', $this->attributeId)
->where('store_id = 0');
return $fetchPairs
? $this->setup->getConnection()->fetchPairs($select)
: $this->setup->getConnection()->fetchAll($select);
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
*/
public function testAddNewOptions()
{
$optionsBefore = $this->getAttributeOptions(false);
$this->operation->execute(
[
'values' => ['new1', 'new2'],
'attribute_id' => $this->attributeId
]
);
$optionsAfter = $this->getAttributeOptions(false);
$this->assertEquals(count($optionsBefore) + 2, count($optionsAfter));
foreach ($optionsBefore as $option) {
$this->assertContainsEquals($option, $optionsAfter);
}
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
*/
public function testAddExistingOptionsWithTheSameSortOrder()
{
$optionsBefore = $this->getAttributeOptions();
$this->operation->execute(
[
'values' => ['Black', 'White'],
'attribute_id' => $this->attributeId
]
);
$optionsAfter = $this->getAttributeOptions();
$this->assertEquals(count($optionsBefore), count($optionsAfter));
foreach ($optionsBefore as $option) {
$this->assertContainsEquals($option, $optionsAfter);
}
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
*/
public function testAddExistingOptionsWithDifferentSortOrder()
{
$optionsBefore = $this->getAttributeOptions();
$this->operation->execute(
[
'values' => [666 => 'White', 777 => 'Black'],
'attribute_id' => $this->attributeId
]
);
$optionsAfter = $this->getAttributeOptions();
$this->assertSameSize($optionsBefore, array_intersect($optionsBefore, $optionsAfter));
$this->assertEquals($optionsAfter[777], $optionsBefore[0]);
$this->assertEquals($optionsAfter[666], $optionsBefore[1]);
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
*/
public function testAddMixedOptions()
{
$sizeBefore = count($this->getAttributeOptions());
$this->operation->execute(
[
'values' => [666 => 'Black', 'NewOption'],
'attribute_id' => $this->attributeId
]
);
$updatedOptions = $this->getAttributeOptions();
$this->assertEquals(count($updatedOptions), $sizeBefore + 1);
$this->assertEquals($updatedOptions[666], 'Black');
$this->assertEquals($updatedOptions[667], 'NewOption');
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
*/
public function testAddNewOption()
{
$sizeBefore = count($this->getAttributeOptions());
$this->operation->execute(
[
'attribute_id' => $this->attributeId,
'order' => [0 => 13],
'value' => [
[
0 => 'NewOption',
],
],
]
);
$updatedOptions = $this->getAttributeOptions();
$this->assertEquals(count($updatedOptions), $sizeBefore + 1);
$this->assertEquals($updatedOptions[13], 'NewOption');
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
*/
public function testDeleteOption()
{
$optionsBefore = $this->getAttributeOptions();
$options = $this->attrRepo->get(Product::ENTITY, $this->attributeId)->getOptions();
/** @var AttributeOptionInterface $optionToDelete */
$optionToDelete = end($options);
$this->operation->execute(
[
'attribute_id' => $this->attributeId,
'delete' => [$optionToDelete->getValue() => true],
'value' => [
$optionToDelete->getValue() => null,
],
]
);
$updatedOptions = $this->getAttributeOptions();
$this->assertEquals(count($updatedOptions), count($optionsBefore) - 1);
foreach ($updatedOptions as $option) {
$this->assertContainsEquals($option, $optionsBefore);
}
}
/**
* @magentoDataFixture Magento/Eav/_files/attribute_with_options.php
*/
public function testUpdateOption()
{
$optionsBefore = $this->getAttributeOptions();
$this->operation->execute(
[
'attribute_id' => $this->attributeId,
'value' => [
0 => ['updatedValue'],
],
]
);
$optionsAfter = $this->getAttributeOptions();
$this->assertEquals($optionsAfter[0], 'updatedValue');
$this->assertSame(array_slice($optionsBefore, 1), array_slice($optionsAfter, 1));
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
$reflection = new \ReflectionObject($this);
foreach ($reflection->getProperties() as $property) {
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
$property->setAccessible(true);
$property->setValue($this, null);
}
}
}
}
@@ -0,0 +1,165 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Setup;
/**
* Test class for Magento\Eav\Setup\EavSetup.
* @magentoDbIsolation enabled
*/
class EavSetupTest extends \PHPUnit\Framework\TestCase
{
/**
* Eav setup.
*
* @var \Magento\Eav\Setup\EavSetup
*/
private $eavSetup;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->eavSetup = $objectManager->create(\Magento\Eav\Setup\EavSetup::class);
}
/**
* Verify that add attribute work correct attribute_code.
*
* @param string $attributeCode
*
* @dataProvider addAttributeDataProvider
*/
public function testAddAttribute($attributeCode)
{
$attributeData = $this->getAttributeData();
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, $attributeData);
$attribute = $this->eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode);
$this->assertEmpty(array_diff($attributeData, $attribute));
}
/**
* Data provider for testAddAttributeThrowException().
*
* @return array
*/
public function addAttributeDataProvider()
{
return [
['eav_setup_test'],
['characters_59_characters_59_characters_59_characters_59_59_'],
];
}
/**
* Verify that add attribute throw exception if attribute_code is not valid.
*
* @param string|null $attributeCode
*
* @dataProvider addAttributeThrowExceptionDataProvider
*/
public function testAddAttributeThrowException($attributeCode)
{
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
$this->expectExceptionMessage('An attribute code must not be less than 1 and more than 60 characters.');
$attributeData = $this->getAttributeData();
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, $attributeData);
}
/**
* Data provider for testAddAttributeThrowException().
*
* @return array
*/
public function addAttributeThrowExceptionDataProvider()
{
return [
[null],
[''],
[' '],
['more_than_60_characters_more_than_more_than_60_characters_more'],
];
}
/**
* Verify that add attribute throw exception if attribute_code is not valid.
*
* @param string|null $attributeCode
*
* @dataProvider addInvalidAttributeThrowExceptionDataProvider
*/
public function testAddInvalidAttributeThrowException($attributeCode)
{
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
$this->expectExceptionMessage('Please use only letters (a-z or A-Z), numbers (0-9) or underscore (_) in this field,');
$attributeData = $this->getAttributeData();
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, $attributeData);
}
/**
* Data provider for testAddInvalidAttributeThrowException().
*
* @return array
*/
public function addInvalidAttributeThrowExceptionDataProvider()
{
return [
['1first_character_is_not_letter'],
['attribute.with.dots'],
];
}
/**
* Get simple attribute data.
*/
private function getAttributeData()
{
$attributeData = [
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Eav Setup Test',
'input' => 'text',
'class' => '',
'source' => '',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
'visible' => 0,
'required' => 0,
'user_defined' => 1,
'default' => 'none',
'searchable' => 0,
'filterable' => 0,
'comparable' => 0,
'visible_on_front' => 0,
'unique' => 0,
'apply_to' => 'category',
];
return $attributeData;
}
/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
$reflection = new \ReflectionObject($this);
foreach ($reflection->getProperties() as $property) {
if (!$property->isStatic() && 0 !== strpos($property->getDeclaringClass()->getName(), 'PHPUnit')) {
$property->setAccessible(true);
$property->setValue($this, null);
}
}
}
}
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Eav\Model\Entity\Type $entityType */
$entityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)
->loadByCode('catalog_product');
$data = $entityType->getData();
$entityTypeId = $entityType->getId();
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
$attributeSet->setData(
[
'attribute_set_name' => 'test_attribute_set',
'entity_type_id' => $entityTypeId,
'sort_order' => 100
]
);
$attributeSet->validate();
$attributeSet->save();
$attributeData = [
[
'attribute_code' => 'foo',
'entity_type_id' => $entityTypeId,
'backend_type' => 'varchar',
'is_required' => 1,
'is_user_defined' => 1,
'is_unique' => 0,
'frontend_label' => ['foo'],
'attribute_set_id' => $entityType->getDefaultAttributeSetId()
]
];
foreach ($attributeData as $data) {
/** @var \Magento\Eav\Model\Entity\Attribute $attribute */
$attribute = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class);
$attribute->setData($data);
$attribute->save();
}
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Eav\Model\Entity\Attribute\Set;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\Registry;
$objectManager = Bootstrap::getObjectManager();
$registry = $objectManager->get(Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var Attribute $attribute */
$attribute = $objectManager->create(Attribute::class);
$attribute->loadByCode(4, 'foo');
if ($attribute->getId()) {
$attribute->delete();
}
/** @var Set $attributeSet */
$attributeSet = $objectManager->create(Set::class)->load('test_attribute_set', 'attribute_set_name');
if ($attributeSet->getId()) {
$attributeSet->delete();
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
@@ -0,0 +1,97 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Eav\Model\Entity\Type $entityType */
$entityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)
->loadByCode('order');
$data = $entityType->getData();
$data['entity_type_code'] = 'test';
unset($data['entity_type_id']);
/** @var \Magento\Eav\Model\Entity\Type $testEntityType */
$testEntityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)
->setData($data)
->save();
$entityTypeId = $testEntityType->getId();
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
$attributeSet->setData([
'attribute_set_name' => 'test_attribute_set',
'entity_type_id' => $entityTypeId,
'sort_order' => 100,
]);
$attributeSet->validate();
$attributeSet->save();
/** @var \Magento\Eav\Model\Entity\Attribute\Group $attributeGroup */
$attributeGroup = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Group::class);
$attributeGroup->setData(
[
'attribute_set_id' => $attributeSet->getAttributeSetId(),
'sort_order' => 30,
'attribute_group_code' => 'test_attribute_group',
'default_id' => 0,
]
);
$attributeGroup->save();
$attributeData = [
[
'attribute_code' => 'attribute_for_search_1',
'entity_type_id' => $entityTypeId,
'backend_type' => 'varchar',
'is_required' => 1,
'is_user_defined' => 1,
'is_unique' => 0,
'attribute_set_id' => $testEntityType->getDefaultAttributeSetId(),
'attribute_group_id' => $attributeSet->getDefaultGroupId($testEntityType->getDefaultAttributeSetId()),
],
[
'attribute_code' => 'attribute_for_search_2',
'entity_type_id' => $entityTypeId,
'backend_type' => 'varchar',
'is_required' => 1,
'is_user_defined' => 1,
'is_unique' => 0,
'attribute_set_id' => $testEntityType->getDefaultAttributeSetId(),
'attribute_group_id' => $attributeSet->getDefaultGroupId($testEntityType->getDefaultAttributeSetId()),
],
[
'attribute_code' => 'attribute_for_search_3',
'entity_type_id' => $entityTypeId,
'backend_type' => 'varchar',
'is_required' => 1,
'is_user_defined' => 1,
'is_unique' => 0,
'attribute_set_id' => $attributeSet->getAttributeSetId(),
'attribute_group_id' => $attributeGroup->getAttributeGroupId(),
],
[
'attribute_code' => 'attribute_for_search_4',
'entity_type_id' => $entityTypeId,
'backend_type' => 'int',
'is_required' => 0,
'is_user_defined' => 1,
'is_unique' => 0,
],
[
'attribute_code' => 'attribute_for_search_5',
'entity_type_id' => $entityTypeId,
'backend_type' => 'varchar',
'is_required' => 0,
'is_user_defined' => 1,
'is_unique' => 0,
],
];
foreach ($attributeData as $data) {
/** @var \Magento\Eav\Model\Entity\Attribute $attribute */
$attribute = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class);
$attribute->setData($data);
$attribute->save();
}
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/**
* Create attribute set
*/
$entityTypeId = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)
->loadByCode('catalog_product')
->getId();
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
$attributeSet->setData([
'attribute_set_name' => 'attribute_set_1_for_search',
'entity_type_id' => $entityTypeId,
'sort_order' => 100,
]);
$attributeSet->validate();
$attributeSet->save();
/**
* Create attribute groups
*/
$attributeGroupData = [
[
'attribute_set_id' => $attributeSet->getAttributeSetId(),
'sort_order' => 10,
'attribute_group_code' => 'attribute_group_1_for_search',
'default_id' => 1,
],
[
'attribute_set_id' => $attributeSet->getAttributeSetId(),
'sort_order' => 20,
'attribute_group_code' => 'attribute_group_2_for_search',
'default_id' => 0,
],
[
'attribute_set_id' => $attributeSet->getAttributeSetId(),
'sort_order' => 30,
'attribute_group_code' => 'attribute_group_3_for_search',
'default_id' => 0,
],
];
foreach ($attributeGroupData as $data) {
/** @var \Magento\Eav\Model\Entity\Attribute\Group $attributeGroup */
$attributeGroup = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Group::class);
$attributeGroup->setData($data);
$attributeGroup->save();
}
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$entityTypeId = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)
->loadByCode('catalog_product')
->getId();
$attributeSetData = [
[
'attribute_set_name' => 'attribute_set_1_for_search',
'entity_type_id' => $entityTypeId,
'sort_order' => 100,
],
[
'attribute_set_name' => 'attribute_set_2_for_search',
'entity_type_id' => $entityTypeId,
'sort_order' => 200,
],
[
'attribute_set_name' => 'attribute_set_3_for_search',
'entity_type_id' => $entityTypeId,
'sort_order' => 300,
],
[
'attribute_set_name' => 'attribute_set_4_for_search',
'entity_type_id' => $entityTypeId,
'sort_order' => 400,
],
];
foreach ($attributeSetData as $data) {
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
$attributeSet->setData($data);
$attributeSet->validate();
$attributeSet->save();
}
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$attributeSetData = [
'attribute_set_1_for_search',
'attribute_set_2_for_search',
'attribute_set_3_for_search',
'attribute_set_4_for_search',
];
foreach ($attributeSetData as $attributeSetName) {
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class)
->load($attributeSetName, 'attribute_set_name');
if ($attributeSet->getId()) {
$attributeSet->delete();
}
}
@@ -0,0 +1,46 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\TestFramework\Helper\Bootstrap;
$objectManager = Bootstrap::getObjectManager();
$setup = $objectManager->get(ModuleDataSetupInterface::class);
/** @var EavSetup $eavSetup */
$eavSetup = $objectManager->get(EavSetupFactory::class)
->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'zzz',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'zzz',
'input' => 'select',
'class' => '',
'source' => '',
'global' => 1,
'visible' => true,
'required' => true,
'user_defined' => true,
'default' => null,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => true,
'apply_to' => '',
'system' => 1,
'group' => 'General',
'option' => ['values' => ["Black", "White", "Red", "Brown", "zzz", "Metallic"]]
]
);
@@ -0,0 +1,20 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Model\Product;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\TestFramework\Helper\Bootstrap;
$objectManager = Bootstrap::getObjectManager();
$setup = $objectManager->get(ModuleDataSetupInterface::class);
/** @var EavSetup $eavSetup */
$eavSetup = $objectManager->get(EavSetupFactory::class)
->create(['setup' => $setup]);
$eavSetup->removeAttribute(Product::ENTITY, 'zzz');
@@ -0,0 +1,16 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
$entityTypeId = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)->loadByCode('catalog_product')->getId();
$attributeSet->setData([
'attribute_set_name' => 'empty_attribute_set',
'entity_type_id' => $entityTypeId,
'sort_order' => 200,
]);
$attributeSet->validate();
$attributeSet->save();
@@ -0,0 +1,12 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class)
->load('empty_attribute_set', 'attribute_set_name');
if ($attributeSet->getId()) {
$attributeSet->delete();
}
@@ -0,0 +1,51 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
use Magento\Eav\Setup\EavSetup;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Catalog\Model\Product\Attribute\Frontend\Inputtype\Presentation;
$objectManager = Bootstrap::getObjectManager();
/** @var AttributeFactory $attributeFactory */
$attributeFactory = $objectManager->get(AttributeFactory::class);
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
$productAttributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class);
$attribute = $attributeFactory->create()->loadByCode(Product::ENTITY, 'text_editor_attribute');
if (!$attribute->getId()) {
/** @var Presentation $presentation */
$presentation = $objectManager->get(Presentation::class);
/** @var EavSetup $installer */
$installer = $objectManager->create(EavSetup::class);
$attributeData = [
'attribute_code' => 'text_editor_attribute',
'is_global' => 1,
'is_user_defined' => 1,
'frontend_input' => 'texteditor',
'is_unique' => 0,
'is_required' => 0,
'is_searchable' => 0,
'is_visible_in_advanced_search' => 0,
'is_comparable' => 0,
'is_filterable' => 0,
'is_filterable_in_search' => 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,
'frontend_label' => ['Text editor attribute'],
'backend_type' => 'text',
];
$attribute->setData($presentation->convertPresentationDataToInputType($attributeData));
$productAttributeRepository->save($attribute);
$attribute = $productAttributeRepository->get('text_editor_attribute');
/* Assign attribute to attribute set */
$installer->addAttributeToGroup(Product::ENTITY, 'Default', 'Attributes', $attribute->getId());
}
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Magento\TestFramework\Helper\Bootstrap;
$objectManager = Bootstrap::getObjectManager();
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
$productAttributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class);
/** @var Registry $registry */
$registry = $objectManager->get(Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
try {
$attribute = $productAttributeRepository->get('text_editor_attribute');
$productAttributeRepository->delete($attribute);
} catch (NoSuchEntityException $e) {
//Attribute already deleted.
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);