1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Directory\Model;
use Magento\Directory\Model\Currency as CurrencyModel;
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\App\State;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Provide tests for CurrencyConfig model.
*/
class CurrencyConfigTest extends TestCase
{
/**
* @var string
*/
private $baseCurrencyPath = 'currency/options/base';
/**
* @var string
*/
private $defaultCurrencyPath = 'currency/options/default';
/**
* @var string
*/
private $allowedCurrenciesPath = 'currency/options/allow';
/**
* @var ConfigInterface
*/
private $config;
/**
* @var CurrencyModel
*/
private $currency;
/**
* @inheritdoc
*/
protected function setUp()
{
$this->currency = Bootstrap::getObjectManager()->get(CurrencyModel::class);
$this->config = Bootstrap::getObjectManager()->get(ConfigInterface::class);
}
/**
* Test get currency config for admin, crontab and storefront areas.
*
* @dataProvider getConfigCurrenciesDataProvider
* @magentoDataFixture Magento/Store/_files/store.php
* @magentoDbIsolation disabled
* @param string $areaCode
* @param array $expected
* @return void
*/
public function testGetConfigCurrencies(string $areaCode, array $expected)
{
/** @var State $appState */
$appState = Bootstrap::getObjectManager()->get(State::class);
$appState->setAreaCode($areaCode);
$store = Bootstrap::getObjectManager()->get(Store::class);
$store->load('test', 'code');
$this->clearCurrencyConfig();
$this->setStoreConfig($store->getId());
$storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
$storeManager->setCurrentStore($store->getId());
if (in_array($areaCode, [Area::AREA_ADMINHTML, Area::AREA_CRONTAB])) {
self::assertEquals($expected['allowed'], $this->currency->getConfigAllowCurrencies());
self::assertEquals($expected['base'], $this->currency->getConfigBaseCurrencies());
self::assertEquals($expected['default'], $this->currency->getConfigDefaultCurrencies());
} else {
/** @var StoreManagerInterface $storeManager */
$storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
foreach ($storeManager->getStores() as $store) {
$storeManager->setCurrentStore($store->getId());
self::assertEquals(
$expected[$store->getCode()]['allowed'],
$this->currency->getConfigAllowCurrencies()
);
self::assertEquals(
$expected[$store->getCode()]['base'],
$this->currency->getConfigBaseCurrencies()
);
self::assertEquals(
$expected[$store->getCode()]['default'],
$this->currency->getConfigDefaultCurrencies()
);
}
}
}
/**
* Provide test data for getConfigCurrencies test.
*
* @return array
*/
public function getConfigCurrenciesDataProvider()
{
return [
[
'areaCode' => Area::AREA_ADMINHTML,
'expected' => [
'allowed' => ['BDT', 'BNS', 'BTD', 'EUR', 'USD'],
'base' => ['BDT', 'USD'],
'default' => ['BDT', 'USD'],
],
],
[
'areaCode' => Area::AREA_CRONTAB,
'expected' => [
'allowed' => ['BDT', 'BNS', 'BTD', 'EUR', 'USD'],
'base' => ['BDT', 'USD'],
'default' => ['BDT', 'USD'],
],
],
[
'areaCode' => Area::AREA_FRONTEND,
'expected' => [
'default' => [
'allowed' => ['EUR', 'USD'],
'base' => ['USD'],
'default' => ['USD'],
],
'test' => [
'allowed' => ['BDT', 'BNS', 'BTD', 'USD'],
'base' => ['BDT'],
'default' => ['BDT'],
],
],
],
];
}
/**
* Remove currency config form Db.
*
* @return void
*/
private function clearCurrencyConfig()
{
$storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
foreach ($storeManager->getStores() as $store) {
$this->config->deleteConfig(
$this->allowedCurrenciesPath,
'stores',
$store->getId()
);
$this->config->deleteConfig(
$this->baseCurrencyPath,
'stores',
$store->getId()
);
$this->config->deleteConfig(
$this->defaultCurrencyPath,
'stores',
$store->getId()
);
}
}
/**
* Set allowed, base and default currency config values for given store.
*
* @param string $storeId
* @return void
*/
private function setStoreConfig(string $storeId)
{
$allowedCurrencies = 'BDT,BNS,BTD';
$baseCurrency = 'BDT';
$this->config->saveConfig(
$this->baseCurrencyPath,
$baseCurrency,
'stores',
$storeId
);
$this->config->saveConfig(
$this->defaultCurrencyPath,
$baseCurrency,
'stores',
$storeId
);
$this->config->saveConfig(
$this->allowedCurrenciesPath,
$allowedCurrencies,
'stores',
$storeId
);
Bootstrap::getObjectManager()->get(ReinitableConfigInterface::class)->reinit();
Bootstrap::getObjectManager()->create(StoreManagerInterface::class)->reinitStores();
}
protected function tearDown()
{
$this->clearCurrencyConfig();
}
}