annotate core/modules/rest/src/Tests/RESTTestBase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\rest\Tests;
Chris@0 4
Chris@18 5 @trigger_error(__NAMESPACE__ . '\RESTTestBase is deprecated in Drupal 8.3.x-dev and will be removed before Drupal 9.0.0. Use \Drupal\Tests\rest\Functional\ResourceTestBase and \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase instead. Only retained for contributed module tests that may be using this base class.', E_USER_DEPRECATED);
Chris@18 6
Chris@0 7 use Drupal\Component\Utility\NestedArray;
Chris@0 8 use Drupal\Core\Config\Entity\ConfigEntityType;
Chris@0 9 use Drupal\node\NodeInterface;
Chris@0 10 use Drupal\rest\RestResourceConfigInterface;
Chris@0 11 use Drupal\simpletest\WebTestBase;
Chris@0 12 use GuzzleHttp\Cookie\FileCookieJar;
Chris@0 13 use GuzzleHttp\Cookie\SetCookie;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Test helper class that provides a REST client method to send HTTP requests.
Chris@0 17 *
Chris@18 18 * @deprecated in Drupal 8.3.x-dev and will be removed before Drupal 9.0.0. Use
Chris@18 19 * \Drupal\Tests\rest\Functional\ResourceTestBase and
Chris@18 20 * \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase
Chris@18 21 * instead. Only retained for contributed module tests that may be using this
Chris@18 22 * base class.
Chris@0 23 */
Chris@0 24 abstract class RESTTestBase extends WebTestBase {
Chris@0 25
Chris@0 26 /**
Chris@0 27 * The REST resource config storage.
Chris@0 28 *
Chris@0 29 * @var \Drupal\Core\Entity\EntityStorageInterface
Chris@0 30 */
Chris@0 31 protected $resourceConfigStorage;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * The default serialization format to use for testing REST operations.
Chris@0 35 *
Chris@0 36 * @var string
Chris@0 37 */
Chris@0 38 protected $defaultFormat;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * The default MIME type to use for testing REST operations.
Chris@0 42 *
Chris@0 43 * @var string
Chris@0 44 */
Chris@0 45 protected $defaultMimeType;
Chris@0 46
Chris@0 47 /**
Chris@0 48 * The entity type to use for testing.
Chris@0 49 *
Chris@0 50 * @var string
Chris@0 51 */
Chris@0 52 protected $testEntityType = 'entity_test';
Chris@0 53
Chris@0 54 /**
Chris@0 55 * The default authentication provider to use for testing REST operations.
Chris@0 56 *
Chris@0 57 * @var array
Chris@0 58 */
Chris@0 59 protected $defaultAuth;
Chris@0 60
Chris@0 61
Chris@0 62 /**
Chris@0 63 * The raw response body from http request operations.
Chris@0 64 *
Chris@0 65 * @var array
Chris@0 66 */
Chris@0 67 protected $responseBody;
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Modules to install.
Chris@0 71 *
Chris@0 72 * @var array
Chris@0 73 */
Chris@0 74 public static $modules = ['rest', 'entity_test'];
Chris@0 75
Chris@0 76 /**
Chris@0 77 * The last response.
Chris@0 78 *
Chris@0 79 * @var \Psr\Http\Message\ResponseInterface
Chris@0 80 */
Chris@0 81 protected $response;
Chris@0 82
Chris@0 83 protected function setUp() {
Chris@0 84 parent::setUp();
Chris@0 85 $this->defaultFormat = 'hal_json';
Chris@0 86 $this->defaultMimeType = 'application/hal+json';
Chris@0 87 $this->defaultAuth = ['cookie'];
Chris@0 88 $this->resourceConfigStorage = $this->container->get('entity_type.manager')->getStorage('rest_resource_config');
Chris@0 89 // Create a test content type for node testing.
Chris@0 90 if (in_array('node', static::$modules)) {
Chris@0 91 $this->drupalCreateContentType(['name' => 'resttest', 'type' => 'resttest']);
Chris@0 92 }
Chris@0 93
Chris@0 94 $this->cookieFile = $this->publicFilesDirectory . '/cookie.jar';
Chris@0 95 }
Chris@0 96
Chris@0 97 /**
Chris@0 98 * Calculates cookies used by guzzle later.
Chris@0 99 *
Chris@0 100 * @return \GuzzleHttp\Cookie\CookieJarInterface
Chris@0 101 * The used CURL options in guzzle.
Chris@0 102 */
Chris@0 103 protected function cookies() {
Chris@0 104 $cookies = [];
Chris@0 105
Chris@0 106 foreach ($this->cookies as $key => $cookie) {
Chris@0 107 $cookies[$key][] = $cookie['value'];
Chris@0 108 }
Chris@0 109
Chris@0 110 $request = \Drupal::request();
Chris@0 111 $cookies = NestedArray::mergeDeep($cookies, $this->extractCookiesFromRequest($request));
Chris@0 112
Chris@0 113 $cookie_jar = new FileCookieJar($this->cookieFile);
Chris@0 114 foreach ($cookies as $key => $cookie_values) {
Chris@0 115 foreach ($cookie_values as $cookie_value) {
Chris@0 116 // setcookie() sets the value of a cookie to be deleted, when its gonna
Chris@0 117 // be removed.
Chris@0 118 if ($cookie_value !== 'deleted') {
Chris@0 119 $cookie_jar->setCookie(new SetCookie(['Name' => $key, 'Value' => $cookie_value, 'Domain' => $request->getHost()]));
Chris@0 120 }
Chris@0 121 }
Chris@0 122 }
Chris@0 123
Chris@0 124 return $cookie_jar;
Chris@0 125 }
Chris@0 126
Chris@0 127 /**
Chris@0 128 * Helper function to issue a HTTP request with simpletest's cURL.
Chris@0 129 *
Chris@0 130 * @param string|\Drupal\Core\Url $url
Chris@0 131 * A Url object or system path.
Chris@0 132 * @param string $method
Chris@0 133 * HTTP method, one of GET, POST, PUT or DELETE.
Chris@0 134 * @param string $body
Chris@0 135 * The body for POST and PUT.
Chris@0 136 * @param string $mime_type
Chris@0 137 * The MIME type of the transmitted content.
Chris@0 138 * @param bool $csrf_token
Chris@0 139 * If NULL, a CSRF token will be retrieved and used. If FALSE, omit the
Chris@0 140 * X-CSRF-Token request header (to simulate developer error). Otherwise, the
Chris@0 141 * passed in value will be used as the value for the X-CSRF-Token request
Chris@0 142 * header (to simulate developer error, by sending an invalid CSRF token).
Chris@0 143 *
Chris@0 144 * @return string
Chris@0 145 * The content returned from the request.
Chris@0 146 */
Chris@0 147 protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL, $csrf_token = NULL) {
Chris@0 148 if (!isset($mime_type)) {
Chris@0 149 $mime_type = $this->defaultMimeType;
Chris@0 150 }
Chris@0 151 if (!in_array($method, ['GET', 'HEAD', 'OPTIONS', 'TRACE'])) {
Chris@0 152 // GET the CSRF token first for writing requests.
Chris@0 153 $requested_token = $this->drupalGet('session/token');
Chris@0 154 }
Chris@0 155
Chris@0 156 $client = \Drupal::httpClient();
Chris@0 157 $url = $this->buildUrl($url);
Chris@0 158
Chris@0 159 $options = [
Chris@0 160 'http_errors' => FALSE,
Chris@0 161 'cookies' => $this->cookies(),
Chris@0 162 'curl' => [
Chris@0 163 CURLOPT_HEADERFUNCTION => [&$this, 'curlHeaderCallback'],
Chris@0 164 ],
Chris@0 165 ];
Chris@0 166 switch ($method) {
Chris@0 167 case 'GET':
Chris@0 168 $options += [
Chris@0 169 'headers' => [
Chris@0 170 'Accept' => $mime_type,
Chris@0 171 ],
Chris@0 172 ];
Chris@0 173 $response = $client->get($url, $options);
Chris@0 174 break;
Chris@0 175
Chris@0 176 case 'HEAD':
Chris@0 177 $response = $client->head($url, $options);
Chris@0 178 break;
Chris@0 179
Chris@0 180 case 'POST':
Chris@0 181 $options += [
Chris@0 182 'headers' => $csrf_token !== FALSE ? [
Chris@0 183 'Content-Type' => $mime_type,
Chris@0 184 'X-CSRF-Token' => ($csrf_token === NULL ? $requested_token : $csrf_token),
Chris@0 185 ] : [
Chris@0 186 'Content-Type' => $mime_type,
Chris@0 187 ],
Chris@0 188 'body' => $body,
Chris@0 189 ];
Chris@0 190 $response = $client->post($url, $options);
Chris@0 191 break;
Chris@0 192
Chris@0 193 case 'PUT':
Chris@0 194 $options += [
Chris@0 195 'headers' => $csrf_token !== FALSE ? [
Chris@0 196 'Content-Type' => $mime_type,
Chris@0 197 'X-CSRF-Token' => ($csrf_token === NULL ? $requested_token : $csrf_token),
Chris@0 198 ] : [
Chris@0 199 'Content-Type' => $mime_type,
Chris@0 200 ],
Chris@0 201 'body' => $body,
Chris@0 202 ];
Chris@0 203 $response = $client->put($url, $options);
Chris@0 204 break;
Chris@0 205
Chris@0 206 case 'PATCH':
Chris@0 207 $options += [
Chris@0 208 'headers' => $csrf_token !== FALSE ? [
Chris@0 209 'Content-Type' => $mime_type,
Chris@0 210 'X-CSRF-Token' => ($csrf_token === NULL ? $requested_token : $csrf_token),
Chris@0 211 ] : [
Chris@0 212 'Content-Type' => $mime_type,
Chris@0 213 ],
Chris@0 214 'body' => $body,
Chris@0 215 ];
Chris@0 216 $response = $client->patch($url, $options);
Chris@0 217 break;
Chris@0 218
Chris@0 219 case 'DELETE':
Chris@0 220 $options += [
Chris@0 221 'headers' => $csrf_token !== FALSE ? [
Chris@0 222 'Content-Type' => $mime_type,
Chris@0 223 'X-CSRF-Token' => ($csrf_token === NULL ? $requested_token : $csrf_token),
Chris@0 224 ] : [],
Chris@0 225 ];
Chris@0 226 $response = $client->delete($url, $options);
Chris@0 227 break;
Chris@0 228 }
Chris@0 229
Chris@0 230 $this->response = $response;
Chris@0 231 $this->responseBody = (string) $response->getBody();
Chris@0 232 $this->setRawContent($this->responseBody);
Chris@0 233
Chris@0 234 // Ensure that any changes to variables in the other thread are picked up.
Chris@0 235 $this->refreshVariables();
Chris@0 236
Chris@0 237 $this->verbose($method . ' request to: ' . $url .
Chris@0 238 '<hr />Code: ' . $this->response->getStatusCode() .
Chris@0 239 (isset($options['headers']) ? '<hr />Request headers: ' . nl2br(print_r($options['headers'], TRUE)) : '') .
Chris@0 240 (isset($options['body']) ? '<hr />Request body: ' . nl2br(print_r($options['body'], TRUE)) : '') .
Chris@0 241 '<hr />Response headers: ' . nl2br(print_r($response->getHeaders(), TRUE)) .
Chris@0 242 '<hr />Response body: ' . $this->responseBody);
Chris@0 243
Chris@0 244 return $this->responseBody;
Chris@0 245 }
Chris@0 246
Chris@0 247 /**
Chris@0 248 * {@inheritdoc}
Chris@0 249 */
Chris@0 250 protected function assertResponse($code, $message = '', $group = 'Browser') {
Chris@0 251 if (!isset($this->response)) {
Chris@0 252 return parent::assertResponse($code, $message, $group);
Chris@0 253 }
Chris@0 254 return $this->assertEqual($code, $this->response->getStatusCode(), $message ? $message : "HTTP response expected $code, actual {$this->response->getStatusCode()}", $group);
Chris@0 255 }
Chris@0 256
Chris@0 257 /**
Chris@0 258 * {@inheritdoc}
Chris@0 259 */
Chris@0 260 protected function drupalGetHeaders($all_requests = FALSE) {
Chris@0 261 if (!isset($this->response)) {
Chris@0 262 return parent::drupalGetHeaders($all_requests);
Chris@0 263 }
Chris@0 264 $lowercased_keys = array_map('strtolower', array_keys($this->response->getHeaders()));
Chris@0 265 return array_map(function (array $header) {
Chris@0 266 return implode(', ', $header);
Chris@0 267 }, array_combine($lowercased_keys, array_values($this->response->getHeaders())));
Chris@0 268 }
Chris@0 269
Chris@0 270 /**
Chris@0 271 * {@inheritdoc}
Chris@0 272 */
Chris@0 273 protected function drupalGetHeader($name, $all_requests = FALSE) {
Chris@0 274 if (!isset($this->response)) {
Chris@0 275 return parent::drupalGetHeader($name, $all_requests);
Chris@0 276 }
Chris@0 277 if ($header = $this->response->getHeader($name)) {
Chris@0 278 return implode(', ', $header);
Chris@0 279 }
Chris@0 280 }
Chris@0 281
Chris@0 282 /**
Chris@0 283 * Creates entity objects based on their types.
Chris@0 284 *
Chris@0 285 * @param string $entity_type
Chris@0 286 * The type of the entity that should be created.
Chris@0 287 *
Chris@0 288 * @return \Drupal\Core\Entity\EntityInterface
Chris@0 289 * The new entity object.
Chris@0 290 */
Chris@0 291 protected function entityCreate($entity_type) {
Chris@0 292 return $this->container->get('entity_type.manager')
Chris@0 293 ->getStorage($entity_type)
Chris@0 294 ->create($this->entityValues($entity_type));
Chris@0 295 }
Chris@0 296
Chris@0 297 /**
Chris@0 298 * Provides an array of suitable property values for an entity type.
Chris@0 299 *
Chris@0 300 * Required properties differ from entity type to entity type, so we keep a
Chris@0 301 * minimum mapping here.
Chris@0 302 *
Chris@0 303 * @param string $entity_type_id
Chris@0 304 * The ID of the type of entity that should be created.
Chris@0 305 *
Chris@0 306 * @return array
Chris@0 307 * An array of values keyed by property name.
Chris@0 308 */
Chris@0 309 protected function entityValues($entity_type_id) {
Chris@0 310 switch ($entity_type_id) {
Chris@0 311 case 'entity_test':
Chris@0 312 return [
Chris@0 313 'name' => $this->randomMachineName(),
Chris@0 314 'user_id' => 1,
Chris@0 315 'field_test_text' => [
Chris@0 316 0 => [
Chris@0 317 'value' => $this->randomString(),
Chris@0 318 'format' => 'plain_text',
Chris@0 319 ],
Chris@0 320 ],
Chris@0 321 ];
Chris@0 322 case 'config_test':
Chris@0 323 return [
Chris@0 324 'id' => $this->randomMachineName(),
Chris@0 325 'label' => 'Test label',
Chris@0 326 ];
Chris@0 327 case 'node':
Chris@0 328 return ['title' => $this->randomString(), 'type' => 'resttest'];
Chris@0 329 case 'node_type':
Chris@0 330 return [
Chris@0 331 'type' => 'article',
Chris@0 332 'name' => $this->randomMachineName(),
Chris@0 333 ];
Chris@0 334 case 'user':
Chris@0 335 return ['name' => $this->randomMachineName()];
Chris@0 336
Chris@0 337 case 'comment':
Chris@0 338 return [
Chris@0 339 'subject' => $this->randomMachineName(),
Chris@0 340 'entity_type' => 'node',
Chris@0 341 'comment_type' => 'comment',
Chris@0 342 'comment_body' => $this->randomString(),
Chris@0 343 'entity_id' => 'invalid',
Chris@0 344 'field_name' => 'comment',
Chris@0 345 ];
Chris@0 346 case 'taxonomy_vocabulary':
Chris@0 347 return [
Chris@0 348 'vid' => 'tags',
Chris@0 349 'name' => $this->randomMachineName(),
Chris@0 350 ];
Chris@0 351 case 'block':
Chris@0 352 // Block placements depend on themes, ensure Bartik is installed.
Chris@0 353 $this->container->get('theme_installer')->install(['bartik']);
Chris@0 354 return [
Chris@0 355 'id' => strtolower($this->randomMachineName(8)),
Chris@0 356 'plugin' => 'system_powered_by_block',
Chris@0 357 'theme' => 'bartik',
Chris@0 358 'region' => 'header',
Chris@0 359 ];
Chris@0 360 default:
Chris@0 361 if ($this->isConfigEntity($entity_type_id)) {
Chris@0 362 return $this->configEntityValues($entity_type_id);
Chris@0 363 }
Chris@0 364 return [];
Chris@0 365 }
Chris@0 366 }
Chris@0 367
Chris@0 368 /**
Chris@0 369 * Enables the REST service interface for a specific entity type.
Chris@0 370 *
Chris@0 371 * @param string|false $resource_type
Chris@0 372 * The resource type that should get REST API enabled or FALSE to disable all
Chris@0 373 * resource types.
Chris@0 374 * @param string $method
Chris@0 375 * The HTTP method to enable, e.g. GET, POST etc.
Chris@0 376 * @param string|array $format
Chris@0 377 * (Optional) The serialization format, e.g. hal_json, or a list of formats.
Chris@0 378 * @param array $auth
Chris@0 379 * (Optional) The list of valid authentication methods.
Chris@0 380 */
Chris@0 381 protected function enableService($resource_type, $method = 'GET', $format = NULL, array $auth = []) {
Chris@0 382 if ($resource_type) {
Chris@0 383 // Enable REST API for this entity type.
Chris@0 384 $resource_config_id = str_replace(':', '.', $resource_type);
Chris@0 385 // get entity by id
Chris@0 386 /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */
Chris@0 387 $resource_config = $this->resourceConfigStorage->load($resource_config_id);
Chris@0 388 if (!$resource_config) {
Chris@0 389 $resource_config = $this->resourceConfigStorage->create([
Chris@0 390 'id' => $resource_config_id,
Chris@0 391 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
Chris@17 392 'configuration' => [],
Chris@0 393 ]);
Chris@0 394 }
Chris@0 395 $configuration = $resource_config->get('configuration');
Chris@0 396
Chris@0 397 if (is_array($format)) {
Chris@0 398 for ($i = 0; $i < count($format); $i++) {
Chris@0 399 $configuration[$method]['supported_formats'][] = $format[$i];
Chris@0 400 }
Chris@0 401 }
Chris@0 402 else {
Chris@0 403 if ($format == NULL) {
Chris@0 404 $format = $this->defaultFormat;
Chris@0 405 }
Chris@0 406 $configuration[$method]['supported_formats'][] = $format;
Chris@0 407 }
Chris@0 408
Chris@0 409 if (!is_array($auth) || empty($auth)) {
Chris@0 410 $auth = $this->defaultAuth;
Chris@0 411 }
Chris@0 412 foreach ($auth as $auth_provider) {
Chris@0 413 $configuration[$method]['supported_auth'][] = $auth_provider;
Chris@0 414 }
Chris@0 415
Chris@0 416 $resource_config->set('configuration', $configuration);
Chris@0 417 $resource_config->save();
Chris@0 418 }
Chris@0 419 else {
Chris@0 420 foreach ($this->resourceConfigStorage->loadMultiple() as $resource_config) {
Chris@0 421 $resource_config->delete();
Chris@0 422 }
Chris@0 423 }
Chris@0 424 $this->rebuildCache();
Chris@0 425 }
Chris@0 426
Chris@0 427 /**
Chris@0 428 * Rebuilds routing caches.
Chris@0 429 */
Chris@0 430 protected function rebuildCache() {
Chris@0 431 $this->container->get('router.builder')->rebuildIfNeeded();
Chris@0 432 }
Chris@0 433
Chris@0 434 /**
Chris@0 435 * {@inheritdoc}
Chris@0 436 *
Chris@0 437 * This method is overridden to deal with a cURL quirk: the usage of
Chris@0 438 * CURLOPT_CUSTOMREQUEST cannot be unset on the cURL handle, so we need to
Chris@0 439 * override it every time it is omitted.
Chris@0 440 */
Chris@0 441 protected function curlExec($curl_options, $redirect = FALSE) {
Chris@0 442 unset($this->response);
Chris@0 443
Chris@0 444 if (!isset($curl_options[CURLOPT_CUSTOMREQUEST])) {
Chris@0 445 if (!empty($curl_options[CURLOPT_HTTPGET])) {
Chris@0 446 $curl_options[CURLOPT_CUSTOMREQUEST] = 'GET';
Chris@0 447 }
Chris@0 448 if (!empty($curl_options[CURLOPT_POST])) {
Chris@0 449 $curl_options[CURLOPT_CUSTOMREQUEST] = 'POST';
Chris@0 450 }
Chris@0 451 }
Chris@0 452 return parent::curlExec($curl_options, $redirect);
Chris@0 453 }
Chris@0 454
Chris@0 455 /**
Chris@0 456 * Provides the necessary user permissions for entity operations.
Chris@0 457 *
Chris@0 458 * @param string $entity_type_id
Chris@0 459 * The entity type.
Chris@0 460 * @param string $operation
Chris@0 461 * The operation, one of 'view', 'create', 'update' or 'delete'.
Chris@0 462 *
Chris@0 463 * @return array
Chris@0 464 * The set of user permission strings.
Chris@0 465 */
Chris@0 466 protected function entityPermissions($entity_type_id, $operation) {
Chris@0 467 switch ($entity_type_id) {
Chris@0 468 case 'entity_test':
Chris@0 469 switch ($operation) {
Chris@0 470 case 'view':
Chris@0 471 return ['view test entity'];
Chris@0 472 case 'create':
Chris@0 473 case 'update':
Chris@0 474 case 'delete':
Chris@0 475 return ['administer entity_test content'];
Chris@0 476 }
Chris@0 477 case 'node':
Chris@0 478 switch ($operation) {
Chris@0 479 case 'view':
Chris@0 480 return ['access content'];
Chris@0 481 case 'create':
Chris@0 482 return ['create resttest content'];
Chris@0 483 case 'update':
Chris@0 484 return ['edit any resttest content'];
Chris@0 485 case 'delete':
Chris@0 486 return ['delete any resttest content'];
Chris@0 487 }
Chris@0 488
Chris@0 489 case 'comment':
Chris@0 490 switch ($operation) {
Chris@0 491 case 'view':
Chris@0 492 return ['access comments'];
Chris@0 493
Chris@0 494 case 'create':
Chris@0 495 return ['post comments', 'skip comment approval'];
Chris@0 496
Chris@0 497 case 'update':
Chris@0 498 return ['edit own comments'];
Chris@0 499
Chris@0 500 case 'delete':
Chris@0 501 return ['administer comments'];
Chris@0 502 }
Chris@0 503 break;
Chris@0 504
Chris@0 505 case 'user':
Chris@0 506 switch ($operation) {
Chris@0 507 case 'view':
Chris@0 508 return ['access user profiles'];
Chris@0 509
Chris@0 510 default:
Chris@0 511 return ['administer users'];
Chris@0 512 }
Chris@0 513
Chris@0 514 default:
Chris@0 515 if ($this->isConfigEntity($entity_type_id)) {
Chris@0 516 $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
Chris@0 517 if ($admin_permission = $entity_type->getAdminPermission()) {
Chris@0 518 return [$admin_permission];
Chris@0 519 }
Chris@0 520 }
Chris@0 521 }
Chris@0 522 return [];
Chris@0 523 }
Chris@0 524
Chris@0 525 /**
Chris@0 526 * Loads an entity based on the location URL returned in the location header.
Chris@0 527 *
Chris@0 528 * @param string $location_url
Chris@0 529 * The URL returned in the Location header.
Chris@0 530 *
Chris@0 531 * @return \Drupal\Core\Entity\Entity|false
Chris@0 532 * The entity or FALSE if there is no matching entity.
Chris@0 533 */
Chris@0 534 protected function loadEntityFromLocationHeader($location_url) {
Chris@0 535 $url_parts = explode('/', $location_url);
Chris@0 536 $id = end($url_parts);
Chris@0 537 return $this->container->get('entity_type.manager')
Chris@0 538 ->getStorage($this->testEntityType)->load($id);
Chris@0 539 }
Chris@0 540
Chris@0 541 /**
Chris@0 542 * Remove node fields that can only be written by an admin user.
Chris@0 543 *
Chris@0 544 * @param \Drupal\node\NodeInterface $node
Chris@0 545 * The node to remove fields where non-administrative users cannot write.
Chris@0 546 *
Chris@0 547 * @return \Drupal\node\NodeInterface
Chris@0 548 * The node with removed fields.
Chris@0 549 */
Chris@0 550 protected function removeNodeFieldsForNonAdminUsers(NodeInterface $node) {
Chris@0 551 $node->set('status', NULL);
Chris@0 552 $node->set('created', NULL);
Chris@0 553 $node->set('changed', NULL);
Chris@0 554 $node->set('promote', NULL);
Chris@0 555 $node->set('sticky', NULL);
Chris@0 556 $node->set('revision_timestamp', NULL);
Chris@0 557 $node->set('revision_log', NULL);
Chris@0 558 $node->set('uid', NULL);
Chris@0 559
Chris@0 560 return $node;
Chris@0 561 }
Chris@0 562
Chris@0 563 /**
Chris@0 564 * Check to see if the HTTP request response body is identical to the expected
Chris@0 565 * value.
Chris@0 566 *
Chris@0 567 * @param $expected
Chris@0 568 * The first value to check.
Chris@0 569 * @param $message
Chris@0 570 * (optional) A message to display with the assertion. Do not translate
Chris@17 571 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
Chris@0 572 * variables in the message text, not t(). If left blank, a default message
Chris@0 573 * will be displayed.
Chris@0 574 * @param $group
Chris@0 575 * (optional) The group this message is in, which is displayed in a column
Chris@0 576 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 577 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 578 * this default.
Chris@0 579 *
Chris@0 580 * @return bool
Chris@0 581 * TRUE if the assertion succeeded, FALSE otherwise.
Chris@0 582 */
Chris@0 583 protected function assertResponseBody($expected, $message = '', $group = 'REST Response') {
Chris@0 584 return $this->assertIdentical($expected, $this->responseBody, $message ? $message : strtr('Response body @expected (expected) is equal to @response (actual).', ['@expected' => var_export($expected, TRUE), '@response' => var_export($this->responseBody, TRUE)]), $group);
Chris@0 585 }
Chris@0 586
Chris@0 587 /**
Chris@0 588 * Checks if an entity type id is for a Config Entity.
Chris@0 589 *
Chris@0 590 * @param string $entity_type_id
Chris@0 591 * The entity type ID to check.
Chris@0 592 *
Chris@0 593 * @return bool
Chris@0 594 * TRUE if the entity is a Config Entity, FALSE otherwise.
Chris@0 595 */
Chris@0 596 protected function isConfigEntity($entity_type_id) {
Chris@0 597 return \Drupal::entityTypeManager()->getDefinition($entity_type_id) instanceof ConfigEntityType;
Chris@0 598 }
Chris@0 599
Chris@0 600 /**
Chris@0 601 * Provides an array of suitable property values for a config entity type.
Chris@0 602 *
Chris@0 603 * Config entities have some common keys that need to be created. Required
Chris@0 604 * properties differ among config entity types, so we keep a minimum mapping
Chris@0 605 * here.
Chris@0 606 *
Chris@0 607 * @param string $entity_type_id
Chris@0 608 * The ID of the type of entity that should be created.
Chris@0 609 *
Chris@0 610 * @return array
Chris@0 611 * An array of values keyed by property name.
Chris@0 612 */
Chris@0 613 protected function configEntityValues($entity_type_id) {
Chris@0 614 $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
Chris@0 615 $keys = $entity_type->getKeys();
Chris@0 616 $values = [];
Chris@0 617 // Fill out known key values that are shared across entity types.
Chris@0 618 foreach ($keys as $key) {
Chris@0 619 if ($key === 'id' || $key === 'label') {
Chris@0 620 $values[$key] = $this->randomMachineName();
Chris@0 621 }
Chris@0 622 }
Chris@0 623 // Add extra values for particular entity types.
Chris@0 624 switch ($entity_type_id) {
Chris@0 625 case 'block':
Chris@0 626 $values['plugin'] = 'system_powered_by_block';
Chris@0 627 break;
Chris@0 628 }
Chris@0 629 return $values;
Chris@0 630 }
Chris@0 631
Chris@0 632 }