Skip to content

Commit 089c8d9

Browse files
author
Oleksandr Iegorov
committed
Merge branch 'MAGETWO-94424' of github.com:magento-tango/magento2ce into PR-1911
2 parents b1b82fe + a1611d4 commit 089c8d9

File tree

2 files changed

+103
-15
lines changed

2 files changed

+103
-15
lines changed

app/code/Magento/Store/Model/Store.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -902,23 +902,17 @@ public function setCurrentCurrencyCode($code)
902902
*/
903903
public function getCurrentCurrencyCode()
904904
{
905+
$availableCurrencyCodes = \array_values($this->getAvailableCurrencyCodes(true));
905906
// try to get currently set code among allowed
906-
$code = $this->_httpContext->getValue(Context::CONTEXT_CURRENCY);
907-
$code = $code === null ? $this->_getSession()->getCurrencyCode() : $code;
908-
if (empty($code)) {
907+
$code = $this->_httpContext->getValue(Context::CONTEXT_CURRENCY) ?? $this->_getSession()->getCurrencyCode();
908+
if (empty($code) || !\in_array($code, $availableCurrencyCodes)) {
909909
$code = $this->getDefaultCurrencyCode();
910-
}
911-
if (in_array($code, $this->getAvailableCurrencyCodes(true))) {
912-
return $code;
910+
if (!\in_array($code, $availableCurrencyCodes) && !empty($availableCurrencyCodes)) {
911+
$code = $availableCurrencyCodes[0];
912+
}
913913
}
914914

915-
// take first one of allowed codes
916-
$codes = array_values($this->getAvailableCurrencyCodes(true));
917-
if (empty($codes)) {
918-
// return default code, if no codes specified at all
919-
return $this->getDefaultCurrencyCode();
920-
}
921-
return array_shift($codes);
915+
return $code;
922916
}
923917

924918
/**

app/code/Magento/Store/Test/Unit/Model/StoreTest.php

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Store\Test\Unit\Model;
87

98
use Magento\Framework\App\Config\ReinitableConfigInterface;
109
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\Session\SessionManagerInterface;
1111
use Magento\Store\Model\ScopeInterface;
1212
use Magento\Store\Model\Store;
1313

@@ -38,6 +38,16 @@ class StoreTest extends \PHPUnit\Framework\TestCase
3838
*/
3939
protected $filesystemMock;
4040

41+
/**
42+
* @var ReinitableConfigInterface|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
private $configMock;
45+
46+
/**
47+
* @var SessionManagerInterface|\PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
private $sessionMock;
50+
4151
/**
4252
* @var \Magento\Framework\Url\ModifierInterface|\PHPUnit_Framework_MockObject_MockObject
4353
*/
@@ -61,12 +71,22 @@ protected function setUp()
6171
'isSecure',
6272
'getServer',
6373
]);
74+
6475
$this->filesystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
6576
->disableOriginalConstructor()
6677
->getMock();
78+
$this->configMock = $this->getMockBuilder(ReinitableConfigInterface::class)
79+
->getMock();
80+
$this->sessionMock = $this->getMockBuilder(SessionManagerInterface::class)
81+
->setMethods(['getCurrencyCode'])
82+
->getMockForAbstractClass();
6783
$this->store = $this->objectManagerHelper->getObject(
6884
\Magento\Store\Model\Store::class,
69-
['filesystem' => $this->filesystemMock]
85+
[
86+
'filesystem' => $this->filesystemMock,
87+
'config' => $this->configMock,
88+
'session' => $this->sessionMock,
89+
]
7090
);
7191

7292
$this->urlModifierMock = $this->createMock(\Magento\Framework\Url\ModifierInterface::class);
@@ -694,6 +714,80 @@ public function testGetScopeTypeName()
694714
$this->assertEquals('Store View', $this->store->getScopeTypeName());
695715
}
696716

717+
/**
718+
* @param array $availableCodes
719+
* @param string $currencyCode
720+
* @param string $defaultCode
721+
* @param string $expectedCode
722+
* @return void
723+
* @dataProvider currencyCodeDataProvider
724+
*/
725+
public function testGetCurrentCurrencyCode(
726+
array $availableCodes,
727+
string $currencyCode,
728+
string $defaultCode,
729+
string $expectedCode
730+
): void {
731+
$this->store->setData('available_currency_codes', $availableCodes);
732+
$this->sessionMock->method('getCurrencyCode')
733+
->willReturn($currencyCode);
734+
$this->configMock->method('getValue')
735+
->with(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_DEFAULT)
736+
->willReturn($defaultCode);
737+
738+
$code = $this->store->getCurrentCurrencyCode();
739+
$this->assertEquals($expectedCode, $code);
740+
}
741+
742+
/**
743+
* @return array
744+
*/
745+
public function currencyCodeDataProvider(): array
746+
{
747+
return [
748+
[
749+
[
750+
'USD',
751+
],
752+
'USD',
753+
'USD',
754+
'USD',
755+
],
756+
[
757+
[
758+
'USD',
759+
'EUR',
760+
],
761+
'EUR',
762+
'USD',
763+
'EUR',
764+
],
765+
[
766+
[
767+
'EUR',
768+
'USD',
769+
],
770+
'GBP',
771+
'USD',
772+
'USD',
773+
],
774+
[
775+
[
776+
'USD',
777+
],
778+
'GBP',
779+
'EUR',
780+
'USD',
781+
],
782+
[
783+
[],
784+
'GBP',
785+
'EUR',
786+
'EUR',
787+
],
788+
];
789+
}
790+
697791
/**
698792
* @param \Magento\Store\Model\Store $model
699793
*/

0 commit comments

Comments
 (0)