init
This commit is contained in:
+119
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+119
@@ -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'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user