Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\rest\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Behat\Mink\Driver\BrowserKitDriver;
|
Chris@0
|
6 use Drupal\Core\Url;
|
Chris@0
|
7 use Drupal\rest\RestResourceConfigInterface;
|
Chris@0
|
8 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
9 use Drupal\user\Entity\Role;
|
Chris@0
|
10 use Drupal\user\RoleInterface;
|
Chris@0
|
11 use GuzzleHttp\RequestOptions;
|
Chris@0
|
12 use Psr\Http\Message\ResponseInterface;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Subclass this for every REST resource, every format and every auth provider.
|
Chris@0
|
16 *
|
Chris@0
|
17 * For more guidance see
|
Chris@0
|
18 * \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase
|
Chris@0
|
19 * which has recommendations for testing the
|
Chris@0
|
20 * \Drupal\rest\Plugin\rest\resource\EntityResource REST resource for every
|
Chris@0
|
21 * format and every auth provider. It's a special case (because that single REST
|
Chris@0
|
22 * resource generates supports not just one thing, but many things — multiple
|
Chris@0
|
23 * entity types), but the same principles apply.
|
Chris@0
|
24 */
|
Chris@0
|
25 abstract class ResourceTestBase extends BrowserTestBase {
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The format to use in this test.
|
Chris@0
|
29 *
|
Chris@0
|
30 * A format is the combination of a certain normalizer and a certain
|
Chris@0
|
31 * serializer.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @see https://www.drupal.org/developing/api/8/serialization
|
Chris@0
|
34 *
|
Chris@0
|
35 * (The default is 'json' because that doesn't depend on any module.)
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var string
|
Chris@0
|
38 */
|
Chris@0
|
39 protected static $format = 'json';
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * The MIME type that corresponds to $format.
|
Chris@0
|
43 *
|
Chris@0
|
44 * (Sadly this cannot be computed automatically yet.)
|
Chris@0
|
45 *
|
Chris@0
|
46 * @var string
|
Chris@0
|
47 */
|
Chris@0
|
48 protected static $mimeType = 'application/json';
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * The authentication mechanism to use in this test.
|
Chris@0
|
52 *
|
Chris@0
|
53 * (The default is 'cookie' because that doesn't depend on any module.)
|
Chris@0
|
54 *
|
Chris@0
|
55 * @var string
|
Chris@0
|
56 */
|
Chris@0
|
57 protected static $auth = FALSE;
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * The REST Resource Config entity ID under test (i.e. a resource type).
|
Chris@0
|
61 *
|
Chris@0
|
62 * The REST Resource plugin ID can be calculated from this.
|
Chris@0
|
63 *
|
Chris@0
|
64 * @var string
|
Chris@0
|
65 *
|
Chris@0
|
66 * @see \Drupal\rest\Entity\RestResourceConfig::__construct()
|
Chris@0
|
67 */
|
Chris@0
|
68 protected static $resourceConfigId = NULL;
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * The account to use for authentication, if any.
|
Chris@0
|
72 *
|
Chris@0
|
73 * @var null|\Drupal\Core\Session\AccountInterface
|
Chris@0
|
74 */
|
Chris@0
|
75 protected $account = NULL;
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * The REST resource config entity storage.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @var \Drupal\Core\Entity\EntityStorageInterface
|
Chris@0
|
81 */
|
Chris@0
|
82 protected $resourceConfigStorage;
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * The serializer service.
|
Chris@0
|
86 *
|
Chris@0
|
87 * @var \Symfony\Component\Serializer\Serializer
|
Chris@0
|
88 */
|
Chris@0
|
89 protected $serializer;
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Modules to install.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @var array
|
Chris@0
|
95 */
|
Chris@0
|
96 public static $modules = ['rest'];
|
Chris@0
|
97
|
Chris@0
|
98 /**
|
Chris@0
|
99 * {@inheritdoc}
|
Chris@0
|
100 */
|
Chris@0
|
101 public function setUp() {
|
Chris@0
|
102 parent::setUp();
|
Chris@0
|
103
|
Chris@0
|
104 $this->serializer = $this->container->get('serializer');
|
Chris@0
|
105
|
Chris@0
|
106 // Ensure the anonymous user role has no permissions at all.
|
Chris@0
|
107 $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
|
Chris@0
|
108 foreach ($user_role->getPermissions() as $permission) {
|
Chris@0
|
109 $user_role->revokePermission($permission);
|
Chris@0
|
110 }
|
Chris@0
|
111 $user_role->save();
|
Chris@14
|
112 assert([] === $user_role->getPermissions(), 'The anonymous user role has no permissions at all.');
|
Chris@0
|
113
|
Chris@0
|
114 if (static::$auth !== FALSE) {
|
Chris@0
|
115 // Ensure the authenticated user role has no permissions at all.
|
Chris@0
|
116 $user_role = Role::load(RoleInterface::AUTHENTICATED_ID);
|
Chris@0
|
117 foreach ($user_role->getPermissions() as $permission) {
|
Chris@0
|
118 $user_role->revokePermission($permission);
|
Chris@0
|
119 }
|
Chris@0
|
120 $user_role->save();
|
Chris@14
|
121 assert([] === $user_role->getPermissions(), 'The authenticated user role has no permissions at all.');
|
Chris@0
|
122
|
Chris@0
|
123 // Create an account.
|
Chris@0
|
124 $this->account = $this->createUser();
|
Chris@0
|
125 }
|
Chris@0
|
126 else {
|
Chris@0
|
127 // Otherwise, also create an account, so that any test involving User
|
Chris@0
|
128 // entities will have the same user IDs regardless of authentication.
|
Chris@0
|
129 $this->createUser();
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 $this->resourceConfigStorage = $this->container->get('entity_type.manager')->getStorage('rest_resource_config');
|
Chris@0
|
133
|
Chris@0
|
134 // Ensure there's a clean slate: delete all REST resource config entities.
|
Chris@0
|
135 $this->resourceConfigStorage->delete($this->resourceConfigStorage->loadMultiple());
|
Chris@0
|
136 $this->refreshTestStateAfterRestConfigChange();
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@0
|
140 * Provisions the REST resource under test.
|
Chris@0
|
141 *
|
Chris@0
|
142 * @param string[] $formats
|
Chris@0
|
143 * The allowed formats for this resource.
|
Chris@0
|
144 * @param string[] $authentication
|
Chris@0
|
145 * The allowed authentication providers for this resource.
|
Chris@0
|
146 */
|
Chris@17
|
147 protected function provisionResource($formats = [], $authentication = [], array $methods = ['GET', 'POST', 'PATCH', 'DELETE']) {
|
Chris@0
|
148 $this->resourceConfigStorage->create([
|
Chris@0
|
149 'id' => static::$resourceConfigId,
|
Chris@0
|
150 'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
|
Chris@0
|
151 'configuration' => [
|
Chris@17
|
152 'methods' => $methods,
|
Chris@0
|
153 'formats' => $formats,
|
Chris@0
|
154 'authentication' => $authentication,
|
Chris@0
|
155 ],
|
Chris@0
|
156 'status' => TRUE,
|
Chris@0
|
157 ])->save();
|
Chris@0
|
158 $this->refreshTestStateAfterRestConfigChange();
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 /**
|
Chris@0
|
162 * Refreshes the state of the tester to be in sync with the testee.
|
Chris@0
|
163 *
|
Chris@0
|
164 * Should be called after every change made to:
|
Chris@0
|
165 * - RestResourceConfig entities
|
Chris@0
|
166 * - the 'rest.settings' simple configuration
|
Chris@0
|
167 */
|
Chris@0
|
168 protected function refreshTestStateAfterRestConfigChange() {
|
Chris@0
|
169 // Ensure that the cache tags invalidator has its internal values reset.
|
Chris@0
|
170 // Otherwise the http_response cache tag invalidation won't work.
|
Chris@0
|
171 $this->refreshVariables();
|
Chris@0
|
172
|
Chris@0
|
173 // Tests using this base class may trigger route rebuilds due to changes to
|
Chris@0
|
174 // RestResourceConfig entities or 'rest.settings'. Ensure the test generates
|
Chris@0
|
175 // routes using an up-to-date router.
|
Chris@0
|
176 \Drupal::service('router.builder')->rebuildIfNeeded();
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 /**
|
Chris@0
|
180 * Return the expected error message.
|
Chris@0
|
181 *
|
Chris@0
|
182 * @param string $method
|
Chris@0
|
183 * The HTTP method (GET, POST, PATCH, DELETE).
|
Chris@0
|
184 *
|
Chris@0
|
185 * @return string
|
Chris@0
|
186 * The error string.
|
Chris@0
|
187 */
|
Chris@0
|
188 protected function getExpectedUnauthorizedAccessMessage($method) {
|
Chris@0
|
189 $resource_plugin_id = str_replace('.', ':', static::$resourceConfigId);
|
Chris@0
|
190 $permission = 'restful ' . strtolower($method) . ' ' . $resource_plugin_id;
|
Chris@0
|
191 return "The '$permission' permission is required.";
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 /**
|
Chris@0
|
195 * Sets up the necessary authorization.
|
Chris@0
|
196 *
|
Chris@0
|
197 * In case of a test verifying publicly accessible REST resources: grant
|
Chris@0
|
198 * permissions to the anonymous user role.
|
Chris@0
|
199 *
|
Chris@0
|
200 * In case of a test verifying behavior when using a particular authentication
|
Chris@0
|
201 * provider: create a user with a particular set of permissions.
|
Chris@0
|
202 *
|
Chris@0
|
203 * Because of the $method parameter, it's possible to first set up
|
Chris@0
|
204 * authentication for only GET, then add POST, et cetera. This then also
|
Chris@0
|
205 * allows for verifying a 403 in case of missing authorization.
|
Chris@0
|
206 *
|
Chris@0
|
207 * @param string $method
|
Chris@0
|
208 * The HTTP method for which to set up authentication.
|
Chris@0
|
209 *
|
Chris@0
|
210 * @see ::grantPermissionsToAnonymousRole()
|
Chris@0
|
211 * @see ::grantPermissionsToAuthenticatedRole()
|
Chris@0
|
212 */
|
Chris@0
|
213 abstract protected function setUpAuthorization($method);
|
Chris@0
|
214
|
Chris@0
|
215 /**
|
Chris@0
|
216 * Verifies the error response in case of missing authentication.
|
Chris@14
|
217 *
|
Chris@14
|
218 * @param string $method
|
Chris@14
|
219 * HTTP method.
|
Chris@14
|
220 * @param \Psr\Http\Message\ResponseInterface $response
|
Chris@14
|
221 * The response to assert.
|
Chris@0
|
222 */
|
Chris@14
|
223 abstract protected function assertResponseWhenMissingAuthentication($method, ResponseInterface $response);
|
Chris@0
|
224
|
Chris@0
|
225 /**
|
Chris@0
|
226 * Asserts normalization-specific edge cases.
|
Chris@0
|
227 *
|
Chris@0
|
228 * (Should be called before sending a well-formed request.)
|
Chris@0
|
229 *
|
Chris@0
|
230 * @see \GuzzleHttp\ClientInterface::request()
|
Chris@0
|
231 *
|
Chris@0
|
232 * @param string $method
|
Chris@0
|
233 * HTTP method.
|
Chris@0
|
234 * @param \Drupal\Core\Url $url
|
Chris@0
|
235 * URL to request.
|
Chris@0
|
236 * @param array $request_options
|
Chris@0
|
237 * Request options to apply.
|
Chris@0
|
238 */
|
Chris@0
|
239 abstract protected function assertNormalizationEdgeCases($method, Url $url, array $request_options);
|
Chris@0
|
240
|
Chris@0
|
241 /**
|
Chris@0
|
242 * Asserts authentication provider-specific edge cases.
|
Chris@0
|
243 *
|
Chris@0
|
244 * (Should be called before sending a well-formed request.)
|
Chris@0
|
245 *
|
Chris@0
|
246 * @see \GuzzleHttp\ClientInterface::request()
|
Chris@0
|
247 *
|
Chris@0
|
248 * @param string $method
|
Chris@0
|
249 * HTTP method.
|
Chris@0
|
250 * @param \Drupal\Core\Url $url
|
Chris@0
|
251 * URL to request.
|
Chris@0
|
252 * @param array $request_options
|
Chris@0
|
253 * Request options to apply.
|
Chris@0
|
254 */
|
Chris@0
|
255 abstract protected function assertAuthenticationEdgeCases($method, Url $url, array $request_options);
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@14
|
258 * Returns the expected cacheability of an unauthorized access response.
|
Chris@14
|
259 *
|
Chris@14
|
260 * @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface
|
Chris@14
|
261 * The expected cacheability.
|
Chris@14
|
262 */
|
Chris@14
|
263 abstract protected function getExpectedUnauthorizedAccessCacheability();
|
Chris@14
|
264
|
Chris@14
|
265 /**
|
Chris@0
|
266 * Initializes authentication.
|
Chris@0
|
267 *
|
Chris@0
|
268 * E.g. for cookie authentication, we first need to get a cookie.
|
Chris@0
|
269 */
|
Chris@0
|
270 protected function initAuthentication() {}
|
Chris@0
|
271
|
Chris@0
|
272 /**
|
Chris@0
|
273 * Returns Guzzle request options for authentication.
|
Chris@0
|
274 *
|
Chris@0
|
275 * @param string $method
|
Chris@0
|
276 * The HTTP method for this authenticated request.
|
Chris@0
|
277 *
|
Chris@0
|
278 * @return array
|
Chris@0
|
279 * Guzzle request options to use for authentication.
|
Chris@0
|
280 *
|
Chris@0
|
281 * @see \GuzzleHttp\ClientInterface::request()
|
Chris@0
|
282 */
|
Chris@0
|
283 protected function getAuthenticationRequestOptions($method) {
|
Chris@0
|
284 return [];
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 /**
|
Chris@0
|
288 * Grants permissions to the anonymous role.
|
Chris@0
|
289 *
|
Chris@0
|
290 * @param string[] $permissions
|
Chris@0
|
291 * Permissions to grant.
|
Chris@0
|
292 */
|
Chris@0
|
293 protected function grantPermissionsToAnonymousRole(array $permissions) {
|
Chris@0
|
294 $this->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), $permissions);
|
Chris@0
|
295 }
|
Chris@0
|
296
|
Chris@0
|
297 /**
|
Chris@0
|
298 * Grants permissions to the authenticated role.
|
Chris@0
|
299 *
|
Chris@0
|
300 * @param string[] $permissions
|
Chris@0
|
301 * Permissions to grant.
|
Chris@0
|
302 */
|
Chris@0
|
303 protected function grantPermissionsToAuthenticatedRole(array $permissions) {
|
Chris@0
|
304 $this->grantPermissions(Role::load(RoleInterface::AUTHENTICATED_ID), $permissions);
|
Chris@0
|
305 }
|
Chris@0
|
306
|
Chris@0
|
307 /**
|
Chris@0
|
308 * Grants permissions to the tested role: anonymous or authenticated.
|
Chris@0
|
309 *
|
Chris@0
|
310 * @param string[] $permissions
|
Chris@0
|
311 * Permissions to grant.
|
Chris@0
|
312 *
|
Chris@0
|
313 * @see ::grantPermissionsToAuthenticatedRole()
|
Chris@0
|
314 * @see ::grantPermissionsToAnonymousRole()
|
Chris@0
|
315 */
|
Chris@0
|
316 protected function grantPermissionsToTestedRole(array $permissions) {
|
Chris@0
|
317 if (static::$auth) {
|
Chris@0
|
318 $this->grantPermissionsToAuthenticatedRole($permissions);
|
Chris@0
|
319 }
|
Chris@0
|
320 else {
|
Chris@0
|
321 $this->grantPermissionsToAnonymousRole($permissions);
|
Chris@0
|
322 }
|
Chris@0
|
323 }
|
Chris@0
|
324
|
Chris@0
|
325 /**
|
Chris@0
|
326 * Performs a HTTP request. Wraps the Guzzle HTTP client.
|
Chris@0
|
327 *
|
Chris@0
|
328 * Why wrap the Guzzle HTTP client? Because we want to keep the actual test
|
Chris@0
|
329 * code as simple as possible, and hence not require them to specify the
|
Chris@0
|
330 * 'http_errors = FALSE' request option, nor do we want them to have to
|
Chris@0
|
331 * convert Drupal Url objects to strings.
|
Chris@0
|
332 *
|
Chris@0
|
333 * We also don't want to follow redirects automatically, to ensure these tests
|
Chris@0
|
334 * are able to detect when redirects are added or removed.
|
Chris@0
|
335 *
|
Chris@0
|
336 * @see \GuzzleHttp\ClientInterface::request()
|
Chris@0
|
337 *
|
Chris@0
|
338 * @param string $method
|
Chris@0
|
339 * HTTP method.
|
Chris@0
|
340 * @param \Drupal\Core\Url $url
|
Chris@0
|
341 * URL to request.
|
Chris@0
|
342 * @param array $request_options
|
Chris@0
|
343 * Request options to apply.
|
Chris@0
|
344 *
|
Chris@0
|
345 * @return \Psr\Http\Message\ResponseInterface
|
Chris@0
|
346 */
|
Chris@0
|
347 protected function request($method, Url $url, array $request_options) {
|
Chris@0
|
348 $request_options[RequestOptions::HTTP_ERRORS] = FALSE;
|
Chris@0
|
349 $request_options[RequestOptions::ALLOW_REDIRECTS] = FALSE;
|
Chris@0
|
350 $request_options = $this->decorateWithXdebugCookie($request_options);
|
Chris@16
|
351 $client = $this->getHttpClient();
|
Chris@0
|
352 return $client->request($method, $url->setAbsolute(TRUE)->toString(), $request_options);
|
Chris@0
|
353 }
|
Chris@0
|
354
|
Chris@0
|
355 /**
|
Chris@0
|
356 * Asserts that a resource response has the given status code and body.
|
Chris@0
|
357 *
|
Chris@0
|
358 * @param int $expected_status_code
|
Chris@0
|
359 * The expected response status.
|
Chris@0
|
360 * @param string|false $expected_body
|
Chris@0
|
361 * The expected response body. FALSE in case this should not be asserted.
|
Chris@0
|
362 * @param \Psr\Http\Message\ResponseInterface $response
|
Chris@0
|
363 * The response to assert.
|
Chris@14
|
364 * @param string[]|false $expected_cache_tags
|
Chris@14
|
365 * (optional) The expected cache tags in the X-Drupal-Cache-Tags response
|
Chris@14
|
366 * header, or FALSE if that header should be absent. Defaults to FALSE.
|
Chris@14
|
367 * @param string[]|false $expected_cache_contexts
|
Chris@14
|
368 * (optional) The expected cache contexts in the X-Drupal-Cache-Contexts
|
Chris@14
|
369 * response header, or FALSE if that header should be absent. Defaults to
|
Chris@14
|
370 * FALSE.
|
Chris@14
|
371 * @param string|false $expected_page_cache_header_value
|
Chris@14
|
372 * (optional) The expected X-Drupal-Cache response header value, or FALSE if
|
Chris@14
|
373 * that header should be absent. Possible strings: 'MISS', 'HIT'. Defaults
|
Chris@14
|
374 * to FALSE.
|
Chris@14
|
375 * @param string|false $expected_dynamic_page_cache_header_value
|
Chris@14
|
376 * (optional) The expected X-Drupal-Dynamic-Cache response header value, or
|
Chris@14
|
377 * FALSE if that header should be absent. Possible strings: 'MISS', 'HIT'.
|
Chris@14
|
378 * Defaults to FALSE.
|
Chris@0
|
379 */
|
Chris@14
|
380 protected function assertResourceResponse($expected_status_code, $expected_body, ResponseInterface $response, $expected_cache_tags = FALSE, $expected_cache_contexts = FALSE, $expected_page_cache_header_value = FALSE, $expected_dynamic_page_cache_header_value = FALSE) {
|
Chris@0
|
381 $this->assertSame($expected_status_code, $response->getStatusCode());
|
Chris@14
|
382 if ($expected_status_code === 204) {
|
Chris@14
|
383 // DELETE responses should not include a Content-Type header. But Apache
|
Chris@14
|
384 // sets it to 'text/html' by default. We also cannot detect the presence
|
Chris@14
|
385 // of Apache either here in the CLI. For now having this documented here
|
Chris@14
|
386 // is all we can do.
|
Chris@14
|
387 // $this->assertSame(FALSE, $response->hasHeader('Content-Type'));
|
Chris@14
|
388 $this->assertSame('', (string) $response->getBody());
|
Chris@14
|
389 }
|
Chris@14
|
390 else {
|
Chris@14
|
391 $this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
|
Chris@14
|
392 if ($expected_body !== FALSE) {
|
Chris@14
|
393 $this->assertSame($expected_body, (string) $response->getBody());
|
Chris@14
|
394 }
|
Chris@14
|
395 }
|
Chris@14
|
396
|
Chris@14
|
397 // Expected cache tags: X-Drupal-Cache-Tags header.
|
Chris@14
|
398 $this->assertSame($expected_cache_tags !== FALSE, $response->hasHeader('X-Drupal-Cache-Tags'));
|
Chris@14
|
399 if (is_array($expected_cache_tags)) {
|
Chris@14
|
400 $this->assertSame($expected_cache_tags, explode(' ', $response->getHeader('X-Drupal-Cache-Tags')[0]));
|
Chris@14
|
401 }
|
Chris@14
|
402
|
Chris@14
|
403 // Expected cache contexts: X-Drupal-Cache-Contexts header.
|
Chris@14
|
404 $this->assertSame($expected_cache_contexts !== FALSE, $response->hasHeader('X-Drupal-Cache-Contexts'));
|
Chris@14
|
405 if (is_array($expected_cache_contexts)) {
|
Chris@14
|
406 $this->assertSame($expected_cache_contexts, explode(' ', $response->getHeader('X-Drupal-Cache-Contexts')[0]));
|
Chris@14
|
407 }
|
Chris@14
|
408
|
Chris@14
|
409 // Expected Page Cache header value: X-Drupal-Cache header.
|
Chris@14
|
410 if ($expected_page_cache_header_value !== FALSE) {
|
Chris@14
|
411 $this->assertTrue($response->hasHeader('X-Drupal-Cache'));
|
Chris@14
|
412 $this->assertSame($expected_page_cache_header_value, $response->getHeader('X-Drupal-Cache')[0]);
|
Chris@14
|
413 }
|
Chris@14
|
414 else {
|
Chris@14
|
415 $this->assertFalse($response->hasHeader('X-Drupal-Cache'));
|
Chris@14
|
416 }
|
Chris@14
|
417
|
Chris@14
|
418 // Expected Dynamic Page Cache header value: X-Drupal-Dynamic-Cache header.
|
Chris@14
|
419 if ($expected_dynamic_page_cache_header_value !== FALSE) {
|
Chris@14
|
420 $this->assertTrue($response->hasHeader('X-Drupal-Dynamic-Cache'));
|
Chris@14
|
421 $this->assertSame($expected_dynamic_page_cache_header_value, $response->getHeader('X-Drupal-Dynamic-Cache')[0]);
|
Chris@14
|
422 }
|
Chris@14
|
423 else {
|
Chris@14
|
424 $this->assertFalse($response->hasHeader('X-Drupal-Dynamic-Cache'));
|
Chris@0
|
425 }
|
Chris@0
|
426 }
|
Chris@0
|
427
|
Chris@0
|
428 /**
|
Chris@0
|
429 * Asserts that a resource error response has the given message.
|
Chris@0
|
430 *
|
Chris@0
|
431 * @param int $expected_status_code
|
Chris@0
|
432 * The expected response status.
|
Chris@0
|
433 * @param string $expected_message
|
Chris@0
|
434 * The expected error message.
|
Chris@0
|
435 * @param \Psr\Http\Message\ResponseInterface $response
|
Chris@0
|
436 * The error response to assert.
|
Chris@14
|
437 * @param string[]|false $expected_cache_tags
|
Chris@14
|
438 * (optional) The expected cache tags in the X-Drupal-Cache-Tags response
|
Chris@14
|
439 * header, or FALSE if that header should be absent. Defaults to FALSE.
|
Chris@14
|
440 * @param string[]|false $expected_cache_contexts
|
Chris@14
|
441 * (optional) The expected cache contexts in the X-Drupal-Cache-Contexts
|
Chris@14
|
442 * response header, or FALSE if that header should be absent. Defaults to
|
Chris@14
|
443 * FALSE.
|
Chris@14
|
444 * @param string|false $expected_page_cache_header_value
|
Chris@14
|
445 * (optional) The expected X-Drupal-Cache response header value, or FALSE if
|
Chris@14
|
446 * that header should be absent. Possible strings: 'MISS', 'HIT'. Defaults
|
Chris@14
|
447 * to FALSE.
|
Chris@14
|
448 * @param string|false $expected_dynamic_page_cache_header_value
|
Chris@14
|
449 * (optional) The expected X-Drupal-Dynamic-Cache response header value, or
|
Chris@14
|
450 * FALSE if that header should be absent. Possible strings: 'MISS', 'HIT'.
|
Chris@14
|
451 * Defaults to FALSE.
|
Chris@0
|
452 */
|
Chris@14
|
453 protected function assertResourceErrorResponse($expected_status_code, $expected_message, ResponseInterface $response, $expected_cache_tags = FALSE, $expected_cache_contexts = FALSE, $expected_page_cache_header_value = FALSE, $expected_dynamic_page_cache_header_value = FALSE) {
|
Chris@0
|
454 $expected_body = ($expected_message !== FALSE) ? $this->serializer->encode(['message' => $expected_message], static::$format) : FALSE;
|
Chris@14
|
455 $this->assertResourceResponse($expected_status_code, $expected_body, $response, $expected_cache_tags, $expected_cache_contexts, $expected_page_cache_header_value, $expected_dynamic_page_cache_header_value);
|
Chris@0
|
456 }
|
Chris@0
|
457
|
Chris@0
|
458 /**
|
Chris@0
|
459 * Adds the Xdebug cookie to the request options.
|
Chris@0
|
460 *
|
Chris@0
|
461 * @param array $request_options
|
Chris@0
|
462 * The request options.
|
Chris@0
|
463 *
|
Chris@0
|
464 * @return array
|
Chris@0
|
465 * Request options updated with the Xdebug cookie if present.
|
Chris@0
|
466 */
|
Chris@0
|
467 protected function decorateWithXdebugCookie(array $request_options) {
|
Chris@0
|
468 $session = $this->getSession();
|
Chris@0
|
469 $driver = $session->getDriver();
|
Chris@0
|
470 if ($driver instanceof BrowserKitDriver) {
|
Chris@0
|
471 $client = $driver->getClient();
|
Chris@0
|
472 foreach ($client->getCookieJar()->all() as $cookie) {
|
Chris@0
|
473 if (isset($request_options[RequestOptions::HEADERS]['Cookie'])) {
|
Chris@0
|
474 $request_options[RequestOptions::HEADERS]['Cookie'] .= '; ' . $cookie->getName() . '=' . $cookie->getValue();
|
Chris@0
|
475 }
|
Chris@0
|
476 else {
|
Chris@0
|
477 $request_options[RequestOptions::HEADERS]['Cookie'] = $cookie->getName() . '=' . $cookie->getValue();
|
Chris@0
|
478 }
|
Chris@0
|
479 }
|
Chris@0
|
480 }
|
Chris@0
|
481 return $request_options;
|
Chris@0
|
482 }
|
Chris@0
|
483
|
Chris@17
|
484 /**
|
Chris@17
|
485 * Recursively sorts an array by key.
|
Chris@17
|
486 *
|
Chris@17
|
487 * @param array $array
|
Chris@17
|
488 * An array to sort.
|
Chris@17
|
489 *
|
Chris@17
|
490 * @return array
|
Chris@17
|
491 * The sorted array.
|
Chris@17
|
492 */
|
Chris@17
|
493 protected static function recursiveKSort(array &$array) {
|
Chris@17
|
494 // First, sort the main array.
|
Chris@17
|
495 ksort($array);
|
Chris@17
|
496
|
Chris@17
|
497 // Then check for child arrays.
|
Chris@17
|
498 foreach ($array as $key => &$value) {
|
Chris@17
|
499 if (is_array($value)) {
|
Chris@17
|
500 static::recursiveKSort($value);
|
Chris@17
|
501 }
|
Chris@17
|
502 }
|
Chris@17
|
503 }
|
Chris@17
|
504
|
Chris@0
|
505 }
|