diff --git a/app/code/Magento/Directory/Api/Data/AvailableCurrencyInterface.php b/app/code/Magento/Directory/Api/Data/AvailableCurrencyInterface.php new file mode 100644 index 0000000000000..72cac86595c30 --- /dev/null +++ b/app/code/Magento/Directory/Api/Data/AvailableCurrencyInterface.php @@ -0,0 +1,75 @@ +currencyInformationFactory = $currencyInformationFactory; $this->exchangeRateFactory = $exchangeRateFactory; $this->storeManager = $storeManager; + $this->localeCurrency = $localeCurrency; + $this->availableCurrencyFactory = $availableCurrencyFactory; } /** @@ -45,6 +61,7 @@ public function __construct( */ public function getCurrencyInfo() { + /** @var \Magento\Directory\Model\Data\CurrencyInformation $currencyInfo */ $currencyInfo = $this->currencyInformationFactory->create(); /** @var \Magento\Store\Model\Store $store */ @@ -59,13 +76,28 @@ public function getCurrencyInfo() $currencyInfo->setAvailableCurrencyCodes($store->getAvailableCurrencyCodes(true)); $exchangeRates = []; + $availableCurrencies = []; foreach ($store->getAvailableCurrencyCodes(true) as $currencyCode) { + $currency = $this->localeCurrency->getCurrency($currencyCode); + + if ($currency instanceof \Magento\Framework\Currency) { + /** @var \Magento\Directory\Model\Data\AvailableCurrency $availableCurrency */ + $availableCurrency = $this->availableCurrencyFactory->create(); + $availableCurrency->setSymbol($currency->getSymbol()); + $availableCurrency->setName($currency->getName()); + $availableCurrency->setValue($currency->getValue()); + $availableCurrency->setCode($currencyCode); + $availableCurrencies[] = $availableCurrency; + } + + /** @var \Magento\Directory\Model\Data\ExchangeRate $exchangeRate */ $exchangeRate = $this->exchangeRateFactory->create(); $exchangeRate->setRate($store->getBaseCurrency()->getRate($currencyCode)); $exchangeRate->setCurrencyTo($currencyCode); $exchangeRates[] = $exchangeRate; } $currencyInfo->setExchangeRates($exchangeRates); + $currencyInfo->setAvailableCurrencies($availableCurrencies); return $currencyInfo; } diff --git a/app/code/Magento/Directory/Model/Data/AvailableCurrency.php b/app/code/Magento/Directory/Model/Data/AvailableCurrency.php new file mode 100644 index 0000000000000..cc1edde142bec --- /dev/null +++ b/app/code/Magento/Directory/Model/Data/AvailableCurrency.php @@ -0,0 +1,106 @@ +_get(self::KEY_CODE); + } + + /** + * @inheritDoc + */ + public function setCode($code) + { + return $this->setData(self::KEY_CODE, $code); + } + + /** + * @inheritDoc + */ + public function getValue() + { + return $this->_get(self::KEY_VALUE); + } + + /** + * @inheritDoc + */ + public function setValue($value) + { + return $this->setData(self::KEY_VALUE, $value); + } + + /** + * @inheritDoc + */ + public function getName() + { + return $this->_get(self::KEY_NAME); + } + + /** + * @inheritDoc + */ + public function setName($name) + { + return $this->setData(self::KEY_NAME, $name); + } + + /** + * @inheritDoc + */ + public function getSymbol() + { + return $this->_get(self::KEY_SYMBOL); + } + + /** + * @inheritDoc + */ + public function setSymbol($symbol) + { + return $this->setData(self::KEY_SYMBOL, $symbol); + } + + /** + * @inheritDoc + */ + public function getExtensionAttributes() + { + return $this->_getExtensionAttributes(); + } + + /** + * @inheritDoc + */ + public function setExtensionAttributes( + \Magento\Directory\Api\Data\ExchangeRateExtensionInterface $extensionAttributes + ) { + return $this->_setExtensionAttributes($extensionAttributes); + } +} diff --git a/app/code/Magento/Directory/Model/Data/CurrencyInformation.php b/app/code/Magento/Directory/Model/Data/CurrencyInformation.php index 8bbbdc1164b7f..ff4ee28441b4c 100644 --- a/app/code/Magento/Directory/Model/Data/CurrencyInformation.php +++ b/app/code/Magento/Directory/Model/Data/CurrencyInformation.php @@ -20,6 +20,7 @@ class CurrencyInformation extends \Magento\Framework\Api\AbstractExtensibleObjec const KEY_DEFAULT_DISPLAY_CURRENCY_CODE = 'default_display_currency_code'; const KEY_DEFAULT_DISPLAY_CURRENCY_SYMBOL = 'default_display_currency_symbol'; const KEY_AVAILABLE_CURRENCY_CODES = 'available_currency_codes'; + const KEY_AVAILABLE_CURRENCIES = 'available_currencies'; const KEY_EXCHANGE_RATES = 'exchange_rates'; /** @@ -102,6 +103,22 @@ public function setAvailableCurrencyCodes(array $codes = null) return $this->setData(self::KEY_AVAILABLE_CURRENCY_CODES, $codes); } + /** + * @inheritDoc + */ + public function getAvailableCurrencies() + { + return $this->_get(self::KEY_AVAILABLE_CURRENCIES); + } + + /** + * @inheritDoc + */ + public function setAvailableCurrencies(array $availableCurrencies = null) + { + return $this->setData(self::KEY_AVAILABLE_CURRENCIES, $availableCurrencies); + } + /** * @inheritDoc */ diff --git a/app/code/Magento/Directory/Test/Unit/Model/CurrencyInformationAcquirerTest.php b/app/code/Magento/Directory/Test/Unit/Model/CurrencyInformationAcquirerTest.php index e302108588b35..1f9c13e363779 100644 --- a/app/code/Magento/Directory/Test/Unit/Model/CurrencyInformationAcquirerTest.php +++ b/app/code/Magento/Directory/Test/Unit/Model/CurrencyInformationAcquirerTest.php @@ -10,6 +10,8 @@ use Magento\Directory\Model\Currency; use Magento\Directory\Model\CurrencyInformationAcquirer; +use Magento\Directory\Model\Data\AvailableCurrency; +use Magento\Directory\Model\Data\AvailableCurrencyFactory; use Magento\Directory\Model\Data\CurrencyInformation; use Magento\Directory\Model\Data\CurrencyInformationFactory; use Magento\Directory\Model\Data\ExchangeRate; @@ -37,6 +39,11 @@ class CurrencyInformationAcquirerTest extends TestCase */ protected $exchangeRateFactory; + /** + * @var MockObject + */ + protected $availableCurrencyFactory; + /** * @var MockObject */ @@ -64,6 +71,11 @@ protected function setUp(): void ->onlyMethods(['create']) ->getMock(); + $this->availableCurrencyFactory = $this->getMockBuilder(AvailableCurrencyFactory::class) + ->disableOriginalConstructor() + ->onlyMethods(['create']) + ->getMock(); + $this->storeManager = $this->getMockBuilder(StoreManager::class) ->disableOriginalConstructor() ->onlyMethods(['getStore']) @@ -74,6 +86,7 @@ protected function setUp(): void [ 'currencyInformationFactory' => $this->currencyInformationFactory, 'exchangeRateFactory' => $this->exchangeRateFactory, + 'availableCurrencyFactory' => $this->availableCurrencyFactory, 'storeManager' => $this->storeManager, ] ); @@ -93,6 +106,15 @@ public function testGetCurrencyInfo() $exchangeRate->expects($this->any())->method('load')->willReturnSelf(); $this->exchangeRateFactory->expects($this->any())->method('create')->willReturn($exchangeRate); + /** @var AvailableCurrency $availableCurrency */ + $availableCurrency = $this->getMockBuilder(AvailableCurrency::class) + ->addMethods(['load']) + ->disableOriginalConstructor() + ->getMock(); + + $availableCurrency->expects($this->any())->method('load')->willReturnSelf(); + $this->availableCurrencyFactory->expects($this->any())->method('create')->willReturn($availableCurrency); + /** @var CurrencyInformation $currencyInformation */ $currencyInformation = $this->getMockBuilder(CurrencyInformation::class) ->addMethods(['load']) @@ -132,5 +154,11 @@ public function testGetCurrencyInfo() $this->assertEquals([$exchangeRate], $result->getExchangeRates()); $this->assertEquals('0.80', $result->getExchangeRates()[0]->getRate()); $this->assertEquals('AUD', $result->getExchangeRates()[0]->getCurrencyTo()); + $this->assertIsArray($result->getAvailableCurrencies()); + $this->assertEquals([$availableCurrency], $result->getAvailableCurrencies()); + $this->assertEquals('AUD', $result->getAvailableCurrencies()[0]->getCode()); + $this->assertEquals('Australian Dollar', $result->getAvailableCurrencies()[0]->getName()); + $this->assertEquals('A$', $result->getAvailableCurrencies()[0]->getSymbol()); + $this->assertEquals('0', $result->getAvailableCurrencies()[0]->getValue()); } } diff --git a/app/code/Magento/DirectoryGraphQl/etc/schema.graphqls b/app/code/Magento/DirectoryGraphQl/etc/schema.graphqls index 06d7d4817ee02..ab62f696efb42 100644 --- a/app/code/Magento/DirectoryGraphQl/etc/schema.graphqls +++ b/app/code/Magento/DirectoryGraphQl/etc/schema.graphqls @@ -15,9 +15,17 @@ type Currency { default_display_currecy_symbol: String @deprecated(reason: "Symbol was missed. Use `default_display_currency_symbol`.") default_display_currency_symbol: String available_currency_codes: [String] + available_currencies: [CurrencyOutput] @doc(description: "The list of allowed currencies for the store.") exchange_rates: [ExchangeRate] } +type CurrencyOutput { + code: String + value: String + name: String + symbol: String +} + type ExchangeRate { currency_to: String rate: Float diff --git a/dev/tests/api-functional/testsuite/Magento/Directory/Api/CurrencyInformationAcquirerTest.php b/dev/tests/api-functional/testsuite/Magento/Directory/Api/CurrencyInformationAcquirerTest.php index 67f80bca297bb..f0056f28f2bdc 100644 --- a/dev/tests/api-functional/testsuite/Magento/Directory/Api/CurrencyInformationAcquirerTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Directory/Api/CurrencyInformationAcquirerTest.php @@ -34,6 +34,7 @@ public function testGet() $this->assertArrayHasKey('default_display_currency_symbol', $result); $this->assertArrayHasKey('available_currency_codes', $result); $this->assertArrayHasKey('exchange_rates', $result); + $this->assertArrayHasKey('available_currencies', $result); $this->assertTrue( in_array($result['base_currency_code'], $result['available_currency_codes']), diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Directory/CurrencyTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Directory/CurrencyTest.php index ad5d71cb08605..15c01f711f2fb 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Directory/CurrencyTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Directory/CurrencyTest.php @@ -24,6 +24,12 @@ public function testGetCurrency() default_display_currency_code default_display_currency_symbol available_currency_codes + available_currencies { + code + value + name + symbol + } exchange_rates { currency_to rate @@ -40,5 +46,12 @@ public function testGetCurrency() $this->assertArrayHasKey('default_display_currency_symbol', $result['currency']); $this->assertArrayHasKey('available_currency_codes', $result['currency']); $this->assertArrayHasKey('exchange_rates', $result['currency']); + $this->assertArrayHasKey('available_currencies', $result['currency']); + $this->assertNotEmpty($result['currency']['available_currencies']); + $available_currency = $result['currency']['available_currencies'][0]; + $this->assertEquals('AUD', $available_currency['code']); + $this->assertEquals('Australian Dollar', $available_currency['name']); + $this->assertEquals('A$', $available_currency['symbol']); + $this->assertEquals('0', $available_currency['value']); } }