3 namespace Drupal\Tests\rest\Functional;
5 use Behat\Mink\Driver\BrowserKitDriver;
7 use Drupal\rest\RestResourceConfigInterface;
8 use Drupal\Tests\BrowserTestBase;
9 use Drupal\user\Entity\Role;
10 use Drupal\user\RoleInterface;
11 use GuzzleHttp\RequestOptions;
12 use Psr\Http\Message\ResponseInterface;
15 * Subclass this for every REST resource, every format and every auth provider.
17 * For more guidance see
18 * \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase
19 * which has recommendations for testing the
20 * \Drupal\rest\Plugin\rest\resource\EntityResource REST resource for every
21 * format and every auth provider. It's a special case (because that single REST
22 * resource generates supports not just one thing, but many things — multiple
23 * entity types), but the same principles apply.
25 abstract class ResourceTestBase extends BrowserTestBase {
28 * The format to use in this test.
30 * A format is the combination of a certain normalizer and a certain
33 * @see https://www.drupal.org/developing/api/8/serialization
35 * (The default is 'json' because that doesn't depend on any module.)
39 protected static $format = 'json';
42 * The MIME type that corresponds to $format.
44 * (Sadly this cannot be computed automatically yet.)
48 protected static $mimeType = 'application/json';
51 * The authentication mechanism to use in this test.
53 * (The default is 'cookie' because that doesn't depend on any module.)
57 protected static $auth = FALSE;
60 * The REST Resource Config entity ID under test (i.e. a resource type).
62 * The REST Resource plugin ID can be calculated from this.
66 * @see \Drupal\rest\Entity\RestResourceConfig::__construct()
68 protected static $resourceConfigId = NULL;
71 * The account to use for authentication, if any.
73 * @var null|\Drupal\Core\Session\AccountInterface
75 protected $account = NULL;
78 * The REST resource config entity storage.
80 * @var \Drupal\Core\Entity\EntityStorageInterface
82 protected $resourceConfigStorage;
85 * The serializer service.
87 * @var \Symfony\Component\Serializer\Serializer
89 protected $serializer;
96 public static $modules = ['rest'];
101 public function setUp() {
104 $this->serializer = $this->container->get('serializer');
106 // Ensure the anonymous user role has no permissions at all.
107 $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
108 foreach ($user_role->getPermissions() as $permission) {
109 $user_role->revokePermission($permission);
112 assert('[] === $user_role->getPermissions()', 'The anonymous user role has no permissions at all.');
114 if (static::$auth !== FALSE) {
115 // Ensure the authenticated user role has no permissions at all.
116 $user_role = Role::load(RoleInterface::AUTHENTICATED_ID);
117 foreach ($user_role->getPermissions() as $permission) {
118 $user_role->revokePermission($permission);
121 assert('[] === $user_role->getPermissions()', 'The authenticated user role has no permissions at all.');
123 // Create an account.
124 $this->account = $this->createUser();
127 // Otherwise, also create an account, so that any test involving User
128 // entities will have the same user IDs regardless of authentication.
132 $this->resourceConfigStorage = $this->container->get('entity_type.manager')->getStorage('rest_resource_config');
134 // Ensure there's a clean slate: delete all REST resource config entities.
135 $this->resourceConfigStorage->delete($this->resourceConfigStorage->loadMultiple());
136 $this->refreshTestStateAfterRestConfigChange();
140 * Provisions the REST resource under test.
142 * @param string[] $formats
143 * The allowed formats for this resource.
144 * @param string[] $authentication
145 * The allowed authentication providers for this resource.
147 protected function provisionResource($formats = [], $authentication = []) {
148 $this->resourceConfigStorage->create([
149 'id' => static::$resourceConfigId,
150 'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
152 'methods' => ['GET', 'POST', 'PATCH', 'DELETE'],
153 'formats' => $formats,
154 'authentication' => $authentication,
158 $this->refreshTestStateAfterRestConfigChange();
162 * Refreshes the state of the tester to be in sync with the testee.
164 * Should be called after every change made to:
165 * - RestResourceConfig entities
166 * - the 'rest.settings' simple configuration
168 protected function refreshTestStateAfterRestConfigChange() {
169 // Ensure that the cache tags invalidator has its internal values reset.
170 // Otherwise the http_response cache tag invalidation won't work.
171 $this->refreshVariables();
173 // Tests using this base class may trigger route rebuilds due to changes to
174 // RestResourceConfig entities or 'rest.settings'. Ensure the test generates
175 // routes using an up-to-date router.
176 \Drupal::service('router.builder')->rebuildIfNeeded();
180 * Return the expected error message.
182 * @param string $method
183 * The HTTP method (GET, POST, PATCH, DELETE).
188 protected function getExpectedUnauthorizedAccessMessage($method) {
189 $resource_plugin_id = str_replace('.', ':', static::$resourceConfigId);
190 $permission = 'restful ' . strtolower($method) . ' ' . $resource_plugin_id;
191 return "The '$permission' permission is required.";
195 * Sets up the necessary authorization.
197 * In case of a test verifying publicly accessible REST resources: grant
198 * permissions to the anonymous user role.
200 * In case of a test verifying behavior when using a particular authentication
201 * provider: create a user with a particular set of permissions.
203 * Because of the $method parameter, it's possible to first set up
204 * authentication for only GET, then add POST, et cetera. This then also
205 * allows for verifying a 403 in case of missing authorization.
207 * @param string $method
208 * The HTTP method for which to set up authentication.
210 * @see ::grantPermissionsToAnonymousRole()
211 * @see ::grantPermissionsToAuthenticatedRole()
213 abstract protected function setUpAuthorization($method);
216 * Verifies the error response in case of missing authentication.
218 abstract protected function assertResponseWhenMissingAuthentication(ResponseInterface $response);
221 * Asserts normalization-specific edge cases.
223 * (Should be called before sending a well-formed request.)
225 * @see \GuzzleHttp\ClientInterface::request()
227 * @param string $method
229 * @param \Drupal\Core\Url $url
231 * @param array $request_options
232 * Request options to apply.
234 abstract protected function assertNormalizationEdgeCases($method, Url $url, array $request_options);
237 * Asserts authentication provider-specific edge cases.
239 * (Should be called before sending a well-formed request.)
241 * @see \GuzzleHttp\ClientInterface::request()
243 * @param string $method
245 * @param \Drupal\Core\Url $url
247 * @param array $request_options
248 * Request options to apply.
250 abstract protected function assertAuthenticationEdgeCases($method, Url $url, array $request_options);
253 * Initializes authentication.
255 * E.g. for cookie authentication, we first need to get a cookie.
257 protected function initAuthentication() {}
260 * Returns Guzzle request options for authentication.
262 * @param string $method
263 * The HTTP method for this authenticated request.
266 * Guzzle request options to use for authentication.
268 * @see \GuzzleHttp\ClientInterface::request()
270 protected function getAuthenticationRequestOptions($method) {
275 * Grants permissions to the anonymous role.
277 * @param string[] $permissions
278 * Permissions to grant.
280 protected function grantPermissionsToAnonymousRole(array $permissions) {
281 $this->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), $permissions);
285 * Grants permissions to the authenticated role.
287 * @param string[] $permissions
288 * Permissions to grant.
290 protected function grantPermissionsToAuthenticatedRole(array $permissions) {
291 $this->grantPermissions(Role::load(RoleInterface::AUTHENTICATED_ID), $permissions);
295 * Grants permissions to the tested role: anonymous or authenticated.
297 * @param string[] $permissions
298 * Permissions to grant.
300 * @see ::grantPermissionsToAuthenticatedRole()
301 * @see ::grantPermissionsToAnonymousRole()
303 protected function grantPermissionsToTestedRole(array $permissions) {
305 $this->grantPermissionsToAuthenticatedRole($permissions);
308 $this->grantPermissionsToAnonymousRole($permissions);
313 * Performs a HTTP request. Wraps the Guzzle HTTP client.
315 * Why wrap the Guzzle HTTP client? Because we want to keep the actual test
316 * code as simple as possible, and hence not require them to specify the
317 * 'http_errors = FALSE' request option, nor do we want them to have to
318 * convert Drupal Url objects to strings.
320 * We also don't want to follow redirects automatically, to ensure these tests
321 * are able to detect when redirects are added or removed.
323 * @see \GuzzleHttp\ClientInterface::request()
325 * @param string $method
327 * @param \Drupal\Core\Url $url
329 * @param array $request_options
330 * Request options to apply.
332 * @return \Psr\Http\Message\ResponseInterface
334 protected function request($method, Url $url, array $request_options) {
335 $request_options[RequestOptions::HTTP_ERRORS] = FALSE;
336 $request_options[RequestOptions::ALLOW_REDIRECTS] = FALSE;
337 $request_options = $this->decorateWithXdebugCookie($request_options);
338 $client = $this->getSession()->getDriver()->getClient()->getClient();
339 return $client->request($method, $url->setAbsolute(TRUE)->toString(), $request_options);
343 * Asserts that a resource response has the given status code and body.
345 * @param int $expected_status_code
346 * The expected response status.
347 * @param string|false $expected_body
348 * The expected response body. FALSE in case this should not be asserted.
349 * @param \Psr\Http\Message\ResponseInterface $response
350 * The response to assert.
352 protected function assertResourceResponse($expected_status_code, $expected_body, ResponseInterface $response) {
353 $this->assertSame($expected_status_code, $response->getStatusCode());
354 $this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
355 if ($expected_body !== FALSE) {
356 $this->assertSame($expected_body, (string) $response->getBody());
361 * Asserts that a resource error response has the given message.
363 * @param int $expected_status_code
364 * The expected response status.
365 * @param string $expected_message
366 * The expected error message.
367 * @param \Psr\Http\Message\ResponseInterface $response
368 * The error response to assert.
370 protected function assertResourceErrorResponse($expected_status_code, $expected_message, ResponseInterface $response) {
371 $expected_body = ($expected_message !== FALSE) ? $this->serializer->encode(['message' => $expected_message], static::$format) : FALSE;
372 $this->assertResourceResponse($expected_status_code, $expected_body, $response);
376 * Adds the Xdebug cookie to the request options.
378 * @param array $request_options
379 * The request options.
382 * Request options updated with the Xdebug cookie if present.
384 protected function decorateWithXdebugCookie(array $request_options) {
385 $session = $this->getSession();
386 $driver = $session->getDriver();
387 if ($driver instanceof BrowserKitDriver) {
388 $client = $driver->getClient();
389 foreach ($client->getCookieJar()->all() as $cookie) {
390 if (isset($request_options[RequestOptions::HEADERS]['Cookie'])) {
391 $request_options[RequestOptions::HEADERS]['Cookie'] .= '; ' . $cookie->getName() . '=' . $cookie->getValue();
394 $request_options[RequestOptions::HEADERS]['Cookie'] = $cookie->getName() . '=' . $cookie->getValue();
398 return $request_options;