|
12 | 12 | use Magento\Customer\Model\Customer;
|
13 | 13 | use Magento\Customer\Test\Fixture\Customer as CustomerFixture;
|
14 | 14 | use Magento\CustomerGraphQl\Model\Resolver\Customer as CustomerResolver;
|
| 15 | +use Magento\CustomerGraphQl\Model\Resolver\IsSubscribed; |
15 | 16 | use Magento\Framework\ObjectManagerInterface;
|
16 | 17 | use Magento\Framework\Registry;
|
17 | 18 | use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\Calculator\ProviderInterface;
|
@@ -131,6 +132,91 @@ public function testCustomerResolverCacheAndInvalidation(callable $invalidationM
|
131 | 132 | $this->assertCurrentCustomerCacheRecordDoesNotExist();
|
132 | 133 | }
|
133 | 134 |
|
| 135 | + /** |
| 136 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 137 | + * @magentoApiDataFixture Magento/Customer/_files/customer_address.php |
| 138 | + * @magentoApiDataFixture Magento/Store/_files/second_store.php |
| 139 | + * @magentoConfigFixture default/system/full_page_cache/caching_application 2 |
| 140 | + * @dataProvider invalidationMechanismProvider |
| 141 | + */ |
| 142 | + public function testCustomerIsSubscribedResolverCacheAndInvalidation() |
| 143 | + { |
| 144 | + $customer = $this-> customerRepository-> get( '[email protected]'); |
| 145 | + |
| 146 | + $query = $this->getCustomerQuery(); |
| 147 | + |
| 148 | + $token = $this->generateCustomerToken($customer->getEmail(), 'password'); |
| 149 | + |
| 150 | + $this->mockCustomerUserInfoContext($customer); |
| 151 | + $response = $this->graphQlQueryWithResponseHeaders( |
| 152 | + $query, |
| 153 | + [], |
| 154 | + '', |
| 155 | + ['Authorization' => 'Bearer ' . $token] |
| 156 | + ); |
| 157 | + |
| 158 | + $this->assertCurrentCustomerCacheRecordExists($customer); |
| 159 | + $this->assertIsSubscribedRecordExists($customer); |
| 160 | + |
| 161 | + // call query again to ensure no errors are thrown |
| 162 | + $this->graphQlQueryWithResponseHeaders( |
| 163 | + $query, |
| 164 | + [], |
| 165 | + '', |
| 166 | + ['Authorization' => 'Bearer ' . $token] |
| 167 | + ); |
| 168 | + |
| 169 | + // change customer subscription |
| 170 | + |
| 171 | + // assert that cache entry is invalidated |
| 172 | +// $this->assertCurrentCustomerCacheRecordDoesNotExist(); |
| 173 | + } |
| 174 | + |
| 175 | + private function getCacheKeyForIsSubscribedResolver(CustomerInterface $customer): string |
| 176 | + { |
| 177 | + $resolverMock = $this->getMockBuilder(IsSubscribed::class) |
| 178 | + ->disableOriginalConstructor() |
| 179 | + ->getMock(); |
| 180 | + |
| 181 | + /** @var ProviderInterface $cacheKeyCalculatorProvider */ |
| 182 | + $cacheKeyCalculatorProvider = Bootstrap::getObjectManager()->get(ProviderInterface::class); |
| 183 | + |
| 184 | + $cacheKeyFactor = $cacheKeyCalculatorProvider |
| 185 | + ->getKeyCalculatorForResolver($resolverMock) |
| 186 | + ->calculateCacheKey( |
| 187 | + ['model' => $customer] |
| 188 | + ); |
| 189 | + |
| 190 | + $cacheKeyQueryPayloadMetadata = IsSubscribed::class . '\Interceptor[]'; |
| 191 | + |
| 192 | + $cacheKeyParts = [ |
| 193 | + GraphQlResolverCache::CACHE_TAG, |
| 194 | + $cacheKeyFactor, |
| 195 | + sha1($cacheKeyQueryPayloadMetadata) |
| 196 | + ]; |
| 197 | + |
| 198 | + // strtoupper is called in \Magento\Framework\Cache\Frontend\Adapter\Zend::_unifyId |
| 199 | + return strtoupper(implode('_', $cacheKeyParts)); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Assert that cache record exists for the given customer. |
| 204 | + * |
| 205 | + * @param CustomerInterface $customer |
| 206 | + * @return void |
| 207 | + */ |
| 208 | + private function assertIsSubscribedRecordExists(CustomerInterface $customer) |
| 209 | + { |
| 210 | + $cacheKey = $this->getCacheKeyForIsSubscribedResolver($customer); |
| 211 | + $cacheEntry = Bootstrap::getObjectManager()->get(GraphQlResolverCache::class)->load($cacheKey); |
| 212 | + $cacheEntryDecoded = json_decode($cacheEntry, true); |
| 213 | + |
| 214 | + $this->assertEquals( |
| 215 | + $customer->getEmail(), |
| 216 | + $cacheEntryDecoded['email'] |
| 217 | + ); |
| 218 | + } |
| 219 | + |
134 | 220 | /**
|
135 | 221 | * @magentoApiDataFixture Magento/Customer/_files/customer.php
|
136 | 222 | * @magentoApiDataFixture Magento/Store/_files/second_store.php
|
|
0 commit comments