Skip to content

Commit 047e509

Browse files
committed
B2B-2255: "Currency" GraphQl query has no cache identity
1 parent 9a2bb63 commit 047e509

File tree

4 files changed

+86
-12
lines changed

4 files changed

+86
-12
lines changed

app/code/Magento/DirectoryGraphQl/Model/Cache/Tag/Strategy/CurrencyConfig.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\DirectoryGraphQl\Model\Resolver\Currency\Identity;
1111
use Magento\Framework\App\Cache\Tag\StrategyInterface;
1212
use Magento\Framework\App\Config\ValueInterface;
13+
use Magento\Store\Model\ScopeInterface;
14+
use Magento\Store\Model\StoreManagerInterface;
1315

1416
/**
1517
* Produce cache tags for currency config.
@@ -26,6 +28,20 @@ class CurrencyConfig implements StrategyInterface
2628
'currency/options/customsymbol'
2729
];
2830

31+
/**
32+
* @var StoreManagerInterface
33+
*/
34+
private $storeManager;
35+
36+
/**
37+
* @param StoreManagerInterface $storeManager
38+
*/
39+
public function __construct(
40+
StoreManagerInterface $storeManager
41+
) {
42+
$this->storeManager = $storeManager;
43+
}
44+
2945
/**
3046
* @inheritdoc
3147
*/
@@ -39,7 +55,19 @@ public function getTags($object): array
3955
&& in_array($object->getPath(), $this->currencyConfigPaths)
4056
&& $object->isValueChanged()
4157
) {
42-
return [Identity::CACHE_TAG];
58+
if ($object->getScope() == ScopeInterface::SCOPE_WEBSITES) {
59+
$website = $this->storeManager->getWebsite($object->getScopeId());
60+
$storeIds = $website->getStoreIds();
61+
} elseif ($object->getScope() == ScopeInterface::SCOPE_STORES) {
62+
$storeIds = [$object->getScopeId()];
63+
} else {
64+
$storeIds = array_keys($this->storeManager->getStores());
65+
}
66+
$ids = [];
67+
foreach ($storeIds as $storeId) {
68+
$ids[] = sprintf('%s_%s', Identity::CACHE_TAG, $storeId);
69+
}
70+
return $ids;
4371
}
4472

4573
return [];

app/code/Magento/DirectoryGraphQl/Model/Resolver/Currency/Identity.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\DirectoryGraphQl\Model\Resolver\Currency;
99

1010
use Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
1112

1213
class Identity implements IdentityInterface
1314
{
@@ -16,12 +17,26 @@ class Identity implements IdentityInterface
1617
*/
1718
public const CACHE_TAG = 'gql_currency';
1819

20+
/**
21+
* @var StoreManagerInterface
22+
*/
23+
private $storeManager;
24+
25+
/**
26+
* @param StoreManagerInterface $storeManager
27+
*/
28+
public function __construct(StoreManagerInterface $storeManager)
29+
{
30+
$this->storeManager = $storeManager;
31+
}
32+
1933
/**
2034
* @inheritdoc
2135
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
2236
*/
2337
public function getIdentities(array $resolvedData): array
2438
{
25-
return [self::CACHE_TAG];
39+
$storeId = $this->storeManager->getStore()->getId();
40+
return [self::CACHE_TAG, sprintf('%s_%s', self::CACHE_TAG, $storeId)];
2641
}
2742
}

app/code/Magento/DirectoryGraphQl/Plugin/Currency.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(ManagerInterface $eventManager)
4040
*/
4141
public function afterSaveRates(CurrencyModel $subject, CurrencyModel $result): CurrencyModel
4242
{
43-
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this]);
43+
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $result]);
4444
return $result;
4545
}
4646
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Directory/CurrencyTest.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,46 @@
1414
*/
1515
class CurrencyTest extends GraphQlAbstract
1616
{
17+
/**
18+
* Test stores set up:
19+
* STORE - WEBSITE - STORE GROUP
20+
* default - base - main_website_store
21+
* test - base - main_website_store
22+
*
23+
* @magentoApiDataFixture Magento/Store/_files/store.php
24+
* @magentoConfigFixture test_store currency/options/base USD
25+
* @magentoConfigFixture test_store currency/options/default CNY
26+
* @magentoConfigFixture test_store currency/options/allow CNY,USD
27+
* @magentoDataFixture Magento/Directory/_files/usd_cny_rate.php
28+
*/
1729
public function testGetCurrency()
30+
{
31+
$result = $this->graphQlQuery($this->getQuery());
32+
$this->assertArrayHasKey('currency', $result);
33+
$this->assertEquals('USD', $result['currency']['base_currency_code']);
34+
$this->assertEquals('USD', $result['currency']['default_display_currency_code']);
35+
$this->assertEquals(['USD'], $result['currency']['available_currency_codes']);
36+
$this->assertEquals('USD', $result['currency']['exchange_rates'][0]['currency_to']);
37+
$this->assertEquals(1, $result['currency']['exchange_rates'][0]['rate']);
38+
39+
$result = $this->graphQlQuery(
40+
$this->getQuery(),
41+
[],
42+
'',
43+
['Store' => 'test']
44+
);
45+
$this->assertArrayHasKey('currency', $result);
46+
$this->assertEquals('USD', $result['currency']['base_currency_code']);
47+
$this->assertEquals('CNY', $result['currency']['default_display_currency_code']);
48+
$this->assertEquals(['CNY','USD'], $result['currency']['available_currency_codes']);
49+
}
50+
51+
/**
52+
* Get query
53+
*
54+
* @return string
55+
*/
56+
private function getQuery(): string
1857
{
1958
$query = <<<QUERY
2059
query {
@@ -31,14 +70,6 @@ public function testGetCurrency()
3170
}
3271
}
3372
QUERY;
34-
35-
$result = $this->graphQlQuery($query);
36-
$this->assertArrayHasKey('currency', $result);
37-
$this->assertArrayHasKey('base_currency_code', $result['currency']);
38-
$this->assertArrayHasKey('base_currency_symbol', $result['currency']);
39-
$this->assertArrayHasKey('default_display_currency_code', $result['currency']);
40-
$this->assertArrayHasKey('default_display_currency_symbol', $result['currency']);
41-
$this->assertArrayHasKey('available_currency_codes', $result['currency']);
42-
$this->assertArrayHasKey('exchange_rates', $result['currency']);
73+
return $query;
4374
}
4475
}

0 commit comments

Comments
 (0)