Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport] MAGETWO-95819: Customer registration fields not translated #21081

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\App\Cache\StateInterface;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Cache for attribute metadata
Expand Down Expand Up @@ -53,24 +54,33 @@ class AttributeMetadataCache
*/
private $serializer;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* Constructor
*
* @param CacheInterface $cache
* @param StateInterface $state
* @param SerializerInterface $serializer
* @param AttributeMetadataHydrator $attributeMetadataHydrator
* @param StoreManagerInterface|null $storeManager
*/
public function __construct(
CacheInterface $cache,
StateInterface $state,
SerializerInterface $serializer,
AttributeMetadataHydrator $attributeMetadataHydrator
AttributeMetadataHydrator $attributeMetadataHydrator,
StoreManagerInterface $storeManager = null
) {
$this->cache = $cache;
$this->state = $state;
$this->serializer = $serializer;
$this->attributeMetadataHydrator = $attributeMetadataHydrator;
$this->storeManager = $storeManager ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(StoreManagerInterface::class);
}

/**
Expand All @@ -82,19 +92,20 @@ public function __construct(
*/
public function load($entityType, $suffix = '')
{
if (isset($this->attributes[$entityType . $suffix])) {
return $this->attributes[$entityType . $suffix];
$storeId = $this->storeManager->getStore()->getId();
if (isset($this->attributes[$entityType . $suffix . $storeId])) {
return $this->attributes[$entityType . $suffix . $storeId];
}
if ($this->isEnabled()) {
$cacheKey = self::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix;
$cacheKey = self::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix . $storeId;
$serializedData = $this->cache->load($cacheKey);
if ($serializedData) {
$attributesData = $this->serializer->unserialize($serializedData);
$attributes = [];
foreach ($attributesData as $key => $attributeData) {
$attributes[$key] = $this->attributeMetadataHydrator->hydrate($attributeData);
}
$this->attributes[$entityType . $suffix] = $attributes;
$this->attributes[$entityType . $suffix . $storeId] = $attributes;
return $attributes;
}
}
Expand All @@ -111,9 +122,10 @@ public function load($entityType, $suffix = '')
*/
public function save($entityType, array $attributes, $suffix = '')
{
$this->attributes[$entityType . $suffix] = $attributes;
$storeId = $this->storeManager->getStore()->getId();
$this->attributes[$entityType . $suffix . $storeId] = $attributes;
if ($this->isEnabled()) {
$cacheKey = self::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix;
$cacheKey = self::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix . $storeId;
$attributesData = [];
foreach ($attributes as $key => $attribute) {
$attributesData[$key] = $this->attributeMetadataHydrator->extract($attribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class AttributeMetadataCache Test
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AttributeMetadataCacheTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -43,20 +50,35 @@ class AttributeMetadataCacheTest extends \PHPUnit\Framework\TestCase
*/
private $attributeMetadataCache;

/**
* @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeMock;

/**
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeManagerMock;

protected function setUp()
{
$objectManager = new ObjectManager($this);
$this->cacheMock = $this->createMock(CacheInterface::class);
$this->stateMock = $this->createMock(StateInterface::class);
$this->serializerMock = $this->createMock(SerializerInterface::class);
$this->attributeMetadataHydratorMock = $this->createMock(AttributeMetadataHydrator::class);
$this->storeMock = $this->createMock(StoreInterface::class);
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
$this->storeManagerMock->method('getStore')->willReturn($this->storeMock);
$this->storeMock->method('getId')->willReturn(1);
$this->attributeMetadataCache = $objectManager->getObject(
AttributeMetadataCache::class,
[
'cache' => $this->cacheMock,
'state' => $this->stateMock,
'serializer' => $this->serializerMock,
'attributeMetadataHydrator' => $this->attributeMetadataHydratorMock
'attributeMetadataHydrator' => $this->attributeMetadataHydratorMock,
'storeManager' => $this->storeManagerMock
]
);
}
Expand All @@ -80,7 +102,8 @@ public function testLoadNoCache()
{
$entityType = 'EntityType';
$suffix = 'none';
$cacheKey = AttributeMetadataCache::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix;
$storeId = 1;
$cacheKey = AttributeMetadataCache::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix . $storeId;
$this->stateMock->expects($this->once())
->method('isEnabled')
->with(Type::TYPE_IDENTIFIER)
Expand All @@ -96,7 +119,8 @@ public function testLoad()
{
$entityType = 'EntityType';
$suffix = 'none';
$cacheKey = AttributeMetadataCache::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix;
$storeId = 1;
$cacheKey = AttributeMetadataCache::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix . $storeId;
$serializedString = 'serialized string';
$attributeMetadataOneData = [
'attribute_code' => 'attribute_code',
Expand Down Expand Up @@ -156,7 +180,8 @@ public function testSave()
{
$entityType = 'EntityType';
$suffix = 'none';
$cacheKey = AttributeMetadataCache::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix;
$storeId = 1;
$cacheKey = AttributeMetadataCache::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix . $storeId;
$serializedString = 'serialized string';
$attributeMetadataOneData = [
'attribute_code' => 'attribute_code',
Expand Down