17
17
use Magento \Framework \Registry ;
18
18
use Magento \GraphQlResolverCache \Model \Resolver \Result \CacheKey \Calculator \ProviderInterface ;
19
19
use Magento \GraphQlResolverCache \Model \Resolver \Result \Type as GraphQlResolverCache ;
20
+ use Magento \Newsletter \Model \SubscriptionManagerInterface ;
20
21
use Magento \Store \Api \WebsiteRepositoryInterface ;
21
22
use Magento \Store \Model \StoreManagerInterface ;
22
23
use Magento \Store \Test \Fixture \Group as StoreGroupFixture ;
@@ -137,11 +138,14 @@ public function testCustomerResolverCacheAndInvalidation(callable $invalidationM
137
138
* @magentoApiDataFixture Magento/Customer/_files/customer_address.php
138
139
* @magentoApiDataFixture Magento/Store/_files/second_store.php
139
140
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
140
- * @dataProvider invalidationMechanismProvider
141
141
*/
142
142
public function testCustomerIsSubscribedResolverCacheAndInvalidation ()
143
143
{
144
+ /** @var SubscriptionManagerInterface $subscriptionManager */
145
+ $ subscriptionManager = $ this ->objectManager ->get (SubscriptionManagerInterface::class);
144
146
$ customer =
$ this ->
customerRepository ->
get (
'[email protected] ' );
147
+ // unsubscribe customer to initialize state
148
+ $ subscriptionManager ->unsubscribeCustomer ((int )$ customer ->getId (), (int )$ customer ->getStoreId ());
145
149
146
150
$ query = $ this ->getCustomerQuery ();
147
151
@@ -154,69 +158,99 @@ public function testCustomerIsSubscribedResolverCacheAndInvalidation()
154
158
'' ,
155
159
['Authorization ' => 'Bearer ' . $ token ]
156
160
);
157
-
161
+ $ this -> assertFalse ( $ response [ ' body ' ][ ' customer ' ][ ' is_subscribed ' ]);
158
162
$ this ->assertCurrentCustomerCacheRecordExists ($ customer );
159
- $ this ->assertIsSubscribedRecordExists ($ customer );
163
+ $ this ->assertIsSubscribedRecordExists ($ customer, false );
160
164
161
165
// call query again to ensure no errors are thrown
162
- $ this ->graphQlQueryWithResponseHeaders (
166
+ $ response = $ this ->graphQlQueryWithResponseHeaders (
163
167
$ query ,
164
168
[],
165
169
'' ,
166
170
['Authorization ' => 'Bearer ' . $ token ]
167
171
);
168
172
173
+ $ this ->assertFalse ($ response ['body ' ]['customer ' ]['is_subscribed ' ]);
174
+
169
175
// change customer subscription
176
+ $ subscriptionManager ->subscribeCustomer ((int )$ customer ->getId (), (int )$ customer ->getStoreId ());
177
+ $ this ->assertIsSubscribedRecordNotExists ($ customer );
178
+ $ this ->assertCurrentCustomerCacheRecordExists ($ customer );
170
179
171
- // assert that cache entry is invalidated
172
- // $this->assertCurrentCustomerCacheRecordDoesNotExist();
180
+ // query customer again so that subscription record
181
+ $ response = $ this ->graphQlQueryWithResponseHeaders (
182
+ $ query ,
183
+ [],
184
+ '' ,
185
+ ['Authorization ' => 'Bearer ' . $ token ]
186
+ );
187
+ $ this ->assertTrue ($ response ['body ' ]['customer ' ]['is_subscribed ' ]);
188
+
189
+ $ this ->assertIsSubscribedRecordExists ($ customer , true );
190
+ // unsubscribe customer to restore original state
191
+ $ subscriptionManager ->unsubscribeCustomer ((int )$ customer ->getId (), (int )$ customer ->getStoreId ());
192
+ $ this ->assertIsSubscribedRecordNotExists ($ customer );
173
193
}
174
194
195
+ /**
196
+ * Prepare cache key for subscription flag cache record.
197
+ *
198
+ * @param CustomerInterface $customer
199
+ * @return string
200
+ */
175
201
private function getCacheKeyForIsSubscribedResolver (CustomerInterface $ customer ): string
176
202
{
177
- $ resolverMock = $ this ->getMockBuilder (IsSubscribed::class)
178
- ->disableOriginalConstructor ()
179
- ->getMock ();
180
-
203
+ $ resolverMock = $ this ->getMockBuilder (IsSubscribed::class)->disableOriginalConstructor ()->getMock ();
181
204
/** @var ProviderInterface $cacheKeyCalculatorProvider */
182
205
$ cacheKeyCalculatorProvider = Bootstrap::getObjectManager ()->get (ProviderInterface::class);
183
-
184
206
$ cacheKeyFactor = $ cacheKeyCalculatorProvider
185
207
->getKeyCalculatorForResolver ($ resolverMock )
186
208
->calculateCacheKey (
187
209
['model ' => $ customer ]
188
210
);
189
-
190
211
$ cacheKeyQueryPayloadMetadata = IsSubscribed::class . '\Interceptor[] ' ;
191
-
192
212
$ cacheKeyParts = [
193
213
GraphQlResolverCache::CACHE_TAG ,
194
214
$ cacheKeyFactor ,
195
215
sha1 ($ cacheKeyQueryPayloadMetadata )
196
216
];
197
-
198
217
// strtoupper is called in \Magento\Framework\Cache\Frontend\Adapter\Zend::_unifyId
199
218
return strtoupper (implode ('_ ' , $ cacheKeyParts ));
200
219
}
201
220
202
221
/**
203
- * Assert that cache record exists for the given customer.
222
+ * Assert subscription cache record exists for the given customer.
204
223
*
205
224
* @param CustomerInterface $customer
225
+ * @param bool $expectedValue
206
226
* @return void
207
227
*/
208
- private function assertIsSubscribedRecordExists (CustomerInterface $ customer )
228
+ private function assertIsSubscribedRecordExists (CustomerInterface $ customer, bool $ expectedValue )
209
229
{
210
230
$ cacheKey = $ this ->getCacheKeyForIsSubscribedResolver ($ customer );
211
231
$ cacheEntry = Bootstrap::getObjectManager ()->get (GraphQlResolverCache::class)->load ($ cacheKey );
232
+ $ this ->assertIsString ($ cacheEntry );
212
233
$ cacheEntryDecoded = json_decode ($ cacheEntry , true );
213
234
214
235
$ this ->assertEquals (
215
- $ customer -> getEmail () ,
216
- $ cacheEntryDecoded[ ' email ' ]
236
+ $ expectedValue ,
237
+ $ cacheEntryDecoded
217
238
);
218
239
}
219
240
241
+ /**
242
+ * Assert subscription cache record does not exist for the given customer.
243
+ *
244
+ * @param CustomerInterface $customer
245
+ * @return void
246
+ */
247
+ private function assertIsSubscribedRecordNotExists (CustomerInterface $ customer )
248
+ {
249
+ $ cacheKey = $ this ->getCacheKeyForIsSubscribedResolver ($ customer );
250
+ $ cacheEntry = Bootstrap::getObjectManager ()->get (GraphQlResolverCache::class)->load ($ cacheKey );
251
+ $ this ->assertFalse ($ cacheEntry );
252
+ }
253
+
220
254
/**
221
255
* @magentoApiDataFixture Magento/Customer/_files/customer.php
222
256
* @magentoApiDataFixture Magento/Store/_files/second_store.php
0 commit comments