CurrencyConfigTest.php 6.4 KB

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