Skip to content

Commit

Permalink
Merge branch '2.4-develop' of https://github.com/mage-os/mirror-magento2
Browse files Browse the repository at this point in the history
 into 2.4-develop
  • Loading branch information
mage-os-ci committed Jun 22, 2023
2 parents d4b4a3c + 35e8e43 commit dbf991c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,62 @@
*/
namespace Magento\Catalog\Model\Product\Attribute\Source;

use Magento\Directory\Model\CountryFactory;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
use Magento\Framework\App\Cache\Type\Config;
use Magento\Framework\Data\OptionSourceInterface;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Store\Model\StoreManagerInterface;

class Countryofmanufacture extends AbstractSource implements OptionSourceInterface
{
/**
* @var \Magento\Framework\App\Cache\Type\Config
* @var Config
*/
protected $_configCacheType;

/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
* @var StoreManagerInterface
*/
protected $_storeManager;

/**
* Country factory
*
* @var \Magento\Directory\Model\CountryFactory
* @var CountryFactory
*/
protected $_countryFactory;

/**
* @var \Magento\Framework\Serialize\SerializerInterface
* @var SerializerInterface
*/
private $serializer;

/**
* @var ResolverInterface
*/
private $localeResolver;

/**
* Construct
*
* @param \Magento\Directory\Model\CountryFactory $countryFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\Cache\Type\Config $configCacheType
* @param CountryFactory $countryFactory
* @param StoreManagerInterface $storeManager
* @param Config $configCacheType
* @param ResolverInterface $localeResolver
* @param SerializerInterface $serializer
*/
public function __construct(
\Magento\Directory\Model\CountryFactory $countryFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\Cache\Type\Config $configCacheType
CountryFactory $countryFactory,
StoreManagerInterface $storeManager,
Config $configCacheType,
ResolverInterface $localeResolver,
SerializerInterface $serializer
) {
$this->_countryFactory = $countryFactory;
$this->_storeManager = $storeManager;
$this->_configCacheType = $configCacheType;
$this->localeResolver = $localeResolver;
$this->serializer = $serializer;
}

/**
Expand All @@ -64,32 +76,20 @@ public function __construct(
*/
public function getAllOptions()
{
$cacheKey = 'COUNTRYOFMANUFACTURE_SELECT_STORE_' . $this->_storeManager->getStore()->getCode();
$storeCode = $this->_storeManager->getStore()->getCode();
$locale = $this->localeResolver->getLocale();

$cacheKey = 'COUNTRYOFMANUFACTURE_SELECT_STORE_' . $storeCode . '_LOCALE_' . $locale;
if ($cache = $this->_configCacheType->load($cacheKey)) {
$options = $this->getSerializer()->unserialize($cache);
$options = $this->serializer->unserialize($cache);
} else {
/** @var \Magento\Directory\Model\Country $country */
$country = $this->_countryFactory->create();
/** @var \Magento\Directory\Model\ResourceModel\Country\Collection $collection */
$collection = $country->getResourceCollection();
$options = $collection->load()->toOptionArray();
$this->_configCacheType->save($this->getSerializer()->serialize($options), $cacheKey);
$this->_configCacheType->save($this->serializer->serialize($options), $cacheKey);
}
return $options;
}

/**
* Get serializer
*
* @return \Magento\Framework\Serialize\SerializerInterface
* @deprecated 102.0.0
*/
private function getSerializer()
{
if ($this->serializer === null) {
$this->serializer = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\SerializerInterface::class);
}
return $this->serializer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Catalog\Model\Product\Attribute\Source\Countryofmanufacture;
use Magento\Framework\App\Cache\Type\Config;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\Store;
Expand Down Expand Up @@ -46,17 +47,24 @@ class CountryofmanufactureTest extends TestCase
*/
private $serializerMock;

/**
* @var ResolverInterface
*/
private $localeResolverMock;

protected function setUp(): void
{
$this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class);
$this->storeMock = $this->createMock(Store::class);
$this->cacheConfig = $this->createMock(Config::class);
$this->localeResolverMock = $this->getMockForAbstractClass(ResolverInterface::class);
$this->objectManagerHelper = new ObjectManager($this);
$this->countryOfManufacture = $this->objectManagerHelper->getObject(
Countryofmanufacture::class,
[
'storeManager' => $this->storeManagerMock,
'configCacheType' => $this->cacheConfig,
'localeResolver' => $this->localeResolverMock,
]
);

Expand All @@ -80,9 +88,10 @@ public function testGetAllOptions($cachedDataSrl, $cachedDataUnsrl)
{
$this->storeMock->expects($this->once())->method('getCode')->willReturn('store_code');
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
$this->localeResolverMock->expects($this->once())->method('getLocale')->willReturn('en_US');
$this->cacheConfig->expects($this->once())
->method('load')
->with($this->equalTo('COUNTRYOFMANUFACTURE_SELECT_STORE_store_code'))
->with($this->equalTo('COUNTRYOFMANUFACTURE_SELECT_STORE_store_code_LOCALE_en_US'))
->willReturn($cachedDataSrl);
$this->serializerMock->expects($this->once())
->method('unserialize')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,8 @@ define([
if (productId && !images.file) {
images = product.images;
}
productDataFromGrid = _.pick(
productDataFromGrid,
'sku',
'name',
'weight',
'status',
'price',
'qty'
);
productDataFromGrid = this.prepareProductDataFromGrid(productDataFromGrid);

if (productDataFromGrid.hasOwnProperty('qty')) {
productDataFromGrid[this.quantityFieldName] = productDataFromGrid.qty;
}
delete productDataFromGrid.qty;
product = _.pick(
product || {},
'sku',
Expand Down Expand Up @@ -288,6 +276,32 @@ define([
* Back.
*/
back: function () {
},

/**
* Prepare product data from grid to have all the current fields values
*
* @param {Object} productDataFromGrid
* @return {Object}
*/
prepareProductDataFromGrid: function (productDataFromGrid) {
productDataFromGrid = _.pick(
productDataFromGrid,
'sku',
'name',
'weight',
'status',
'price',
'qty'
);

if (productDataFromGrid.hasOwnProperty('qty')) {
productDataFromGrid[this.quantityFieldName] = productDataFromGrid.qty;
}

delete productDataFromGrid.qty;

return productDataFromGrid;
}
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/


/* eslint max-nested-callbacks: 0 */
/* jscs:disable jsDoc*/

define([
'Magento_ConfigurableProduct/js/variations/steps/summary'
], function (Summary) {
'use strict';

describe('Magento_ConfigurableProduct/js/variations/steps/summary', function () {
let model, quantityFieldName, productDataFromGrid, productDataFromGridExpected;

beforeEach(function () {
quantityFieldName = 'quantity123';
model = new Summary({quantityFieldName: quantityFieldName});

productDataFromGrid = {
sku: 'testSku',
name: 'test name',
weight: 12.12312,
status: 1,
price: 333.333,
someField: 'someValue',
quantity: 10
};

productDataFromGrid[quantityFieldName] = 12;

productDataFromGridExpected = {
sku: 'testSku',
name: 'test name',
weight: 12.12312,
status: 1,
price: 333.333
};
});

describe('Check prepareProductDataFromGrid', function () {

it('Check call to prepareProductDataFromGrid method with qty', function () {
productDataFromGrid.qty = 3;
productDataFromGridExpected[quantityFieldName] = 3;
const result = model.prepareProductDataFromGrid(productDataFromGrid);

expect(result).toEqual(productDataFromGridExpected);
});


it('Check call to prepareProductDataFromGrid method without qty', function () {
const result = model.prepareProductDataFromGrid(productDataFromGrid);

expect(result).toEqual(productDataFromGridExpected);
});
});
});
});

0 comments on commit dbf991c

Please sign in to comment.