-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAGETWO-83329: 8003: Using System Value for Base Currency Results in …
…Config Error. #11809 - Merge Pull Request #11809 from nmalevanec/magento2:Github-8003 - Merged commits: 1. 38735ac 2. b63ceb9 3. 3e0c785
- Loading branch information
Showing
5 changed files
with
443 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Directory\Model; | ||
|
||
use Magento\Framework\App\Area; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\App\State; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
/** | ||
* Provide config values for allowed, base and default currencies. | ||
*/ | ||
class CurrencyConfig | ||
{ | ||
/** | ||
* @var State | ||
*/ | ||
private $appState; | ||
|
||
/** | ||
* @var ScopeConfigInterface | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* CurrencyConfig constructor. | ||
* | ||
* @param State $appState | ||
* @param ScopeConfigInterface $config | ||
* @param StoreManagerInterface $storeManager | ||
*/ | ||
public function __construct( | ||
State $appState, | ||
ScopeConfigInterface $config, | ||
StoreManagerInterface $storeManager | ||
) { | ||
$this->appState = $appState; | ||
$this->config = $config; | ||
$this->storeManager = $storeManager; | ||
} | ||
|
||
/** | ||
* Retrieve config currency data by config path. | ||
* | ||
* @param string $path | ||
* @return array | ||
*/ | ||
public function getConfigCurrencies(string $path) | ||
{ | ||
$result = $this->appState->getAreaCode() === Area::AREA_ADMINHTML | ||
? $this->getConfigForAllStores($path) | ||
: $this->getConfigForCurrentStore($path); | ||
sort($result); | ||
|
||
return array_unique($result); | ||
} | ||
|
||
/** | ||
* Get allowed, base and default currency codes for all stores. | ||
* | ||
* @param string $path | ||
* @return array | ||
*/ | ||
private function getConfigForAllStores(string $path) | ||
{ | ||
$storesResult = [[]]; | ||
foreach ($this->storeManager->getStores() as $store) { | ||
$storesResult[] = explode( | ||
',', | ||
$this->config->getValue($path, ScopeInterface::SCOPE_STORE, $store->getCode()) | ||
); | ||
} | ||
|
||
return array_merge(...$storesResult); | ||
} | ||
|
||
/** | ||
* Get allowed, base and default currency codes for current store. | ||
* | ||
* @param string $path | ||
* @return mixed | ||
*/ | ||
private function getConfigForCurrentStore(string $path) | ||
{ | ||
$store = $this->storeManager->getStore(); | ||
|
||
return explode(',', $this->config->getValue($path, ScopeInterface::SCOPE_STORE, $store->getCode())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
app/code/Magento/Directory/Test/Unit/Model/CurrencyConfigTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Directory\Test\Unit\Model; | ||
|
||
use Magento\Config\App\Config\Type\System; | ||
use Magento\Directory\Model\CurrencyConfig; | ||
use Magento\Framework\App\Area; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\App\State; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\Store\Api\Data\StoreInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Provide tests for CurrencyConfig model. | ||
*/ | ||
class CurrencyConfigTest extends TestCase | ||
{ | ||
/** | ||
* @var CurrencyConfig | ||
*/ | ||
private $testSubject; | ||
|
||
/** | ||
* @var System|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* @var State|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $appState; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->config = $this->getMockBuilder(ScopeConfigInterface::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->storeManager = $this->getMockBuilder(StoreManagerInterface::class) | ||
->setMethods(['getStores', 'getWebsites']) | ||
->disableOriginalConstructor() | ||
->getMockForAbstractClass(); | ||
$this->appState = $this->getMockBuilder(State::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$objectManager = new ObjectManager($this); | ||
$this->testSubject = $objectManager->getObject( | ||
CurrencyConfig::class, | ||
[ | ||
'storeManager' => $this->storeManager, | ||
'appState' => $this->appState, | ||
'config' => $this->config, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Test get currency config for admin and storefront areas. | ||
* | ||
* @dataProvider getConfigCurrenciesDataProvider | ||
* @return void | ||
*/ | ||
public function testGetConfigCurrencies(string $areCode) | ||
{ | ||
$path = 'test/path'; | ||
$expected = ['ARS', 'AUD', 'BZD']; | ||
|
||
$this->appState->expects(self::once()) | ||
->method('getAreaCode') | ||
->willReturn($areCode); | ||
|
||
/** @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject $store */ | ||
$store = $this->getMockBuilder(StoreInterface::class) | ||
->setMethods(['getCode']) | ||
->disableOriginalConstructor() | ||
->getMockForAbstractClass(); | ||
$store->expects(self::once()) | ||
->method('getCode') | ||
->willReturn('testCode'); | ||
|
||
if ($areCode === Area::AREA_ADMINHTML) { | ||
$this->storeManager->expects(self::once()) | ||
->method('getStores') | ||
->willReturn([$store]); | ||
} else { | ||
$this->storeManager->expects(self::once()) | ||
->method('getStore') | ||
->willReturn($store); | ||
} | ||
|
||
$this->config->expects(self::once()) | ||
->method('getValue') | ||
->with( | ||
self::identicalTo($path) | ||
)->willReturn('ARS,AUD,BZD'); | ||
|
||
$result = $this->testSubject->getConfigCurrencies($path); | ||
|
||
self::assertEquals($expected, $result); | ||
} | ||
|
||
/** | ||
* Provide test data for getConfigCurrencies test. | ||
* | ||
* @return array | ||
*/ | ||
public function getConfigCurrenciesDataProvider() | ||
{ | ||
return [ | ||
['areaCode' => Area::AREA_ADMINHTML], | ||
['areaCode' => Area::AREA_FRONTEND], | ||
]; | ||
} | ||
} |
Oops, something went wrong.