ContentHubSubscriberTestBase.php in Acquia Content Hub 8
File
acquia_contenthub_subscriber/tests/src/Functional/ContentHubSubscriberTestBase.phpView source
<?php
namespace Drupal\Tests\acquia_contenthub_subscriber\Functional;
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\User;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\RequestOptions;
/**
* Provides the base class for web tests for Content Hub Subscribers.
*/
abstract class ContentHubSubscriberTestBase extends BrowserTestBase {
use StringTranslationTrait;
/**
* Modules to enable for this test.
*
* @var string[]
*/
protected static $modules = [
'system',
'acquia_contenthub',
'acquia_contenthub_subscriber',
];
/**
* An admin user used for this test.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $adminUser;
/**
* The permissions of the admin user.
*
* @var string[]
*/
protected $adminUserPermissions = [
'administer acquia content hub',
'access administration pages',
'restful get contenthub_filter',
'restful post contenthub_filter',
'restful patch contenthub_filter',
'restful delete contenthub_filter',
];
/**
* A user without Acquia Content Hub admin permission.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $unauthorizedUser;
/**
* The anonymous user used for this test.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $anonymousUser;
/**
* The URL generator.
*
* @var \Drupal\Core\Routing\UrlGeneratorInterface
*/
protected $urlGenerator;
/**
* The Content Hub Filter config storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $entityConfigStorage;
/**
* The default serialization format to use for testing REST operations.
*
* @var string
*/
protected $defaultFormat;
/**
* The default MIME type to use for testing REST operations.
*
* @var string
*/
protected $defaultMimeType;
/**
* The entity type to use for testing.
*
* @var string
*/
protected $testEntityType = 'contenthub_filter';
/**
* The default authentication provider to use for testing REST operations.
*
* @var array
*/
protected $defaultAuth;
/**
* The raw response body from http request operations.
*
* @var array
*/
protected $responseBody;
/**
* Default theme for test.
*
* @var string
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
public function setUp() : void {
parent::setUp();
$this->defaultFormat = 'json';
$this->defaultMimeType = 'application/json';
$this->defaultAuth = [
'cookie',
];
$this->entityConfigStorage = $this->container
->get('entity_type.manager')
->getStorage('contenthub_filter');
// Create the users us ed for the tests.
$this->unauthorizedUser = $this
->createUser([
'access administration pages',
]);
$this->adminUser = $this
->createUser($this->adminUserPermissions, NULL, TRUE);
$this->anonymousUser = User::load(0);
// Get the URL generator.
$this->urlGenerator = $this->container
->get('url_generator');
}
/**
* Helper function to issue a HTTP request with simpletest's cURL.
*
* @param string|\Drupal\Core\Url $url
* A Url object or system path.
* @param string $method
* HTTP method, one of GET, POST, PUT or DELETE.
* @param string $body
* The body for POST and PUT.
* @param string $mime_type
* The MIME type of the transmitted content.
* @param bool $csrf_token
* If NULL, a CSRF token will be retrieved and used. If FALSE, omit the
* X-CSRF-Token request header (to simulate developer error). Otherwise, the
* passed in value will be used as the value for the X-CSRF-Token request
* header (to simulate developer error, by sending an invalid CSRF token).
*
* @return string
* The content returned from the request.
*/
protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL, $csrf_token = NULL) {
if (!isset($mime_type)) {
$mime_type = $this->defaultMimeType;
}
if (!in_array($method, [
'GET',
'HEAD',
'OPTIONS',
'TRACE',
])) {
// GET the CSRF token first for writing requests.
$requested_token = $this
->drupalGet('/session/token');
}
$url = $this
->buildUrl($url) . '?_format=json';
$request_options = [];
$client = \Drupal::httpClient();
switch ($method) {
case 'GET':
$request_options = [
'headers' => [
'Accept' => $mime_type,
],
'curl' => [
CURLOPT_CUSTOMREQUEST => 'GET',
],
];
break;
case 'POST':
$headers = [];
$headers['X-CSRF-Token'] = $csrf_token !== FALSE ? $csrf_token === NULL ? $requested_token : $csrf_token : NULL;
$headers['Content-Type'] = $mime_type !== 'none' ? $mime_type : NULL;
$request_options = [
'timeout' => 10,
'body' => $body,
'headers' => $headers,
'curl' => [
CURLOPT_CUSTOMREQUEST => 'POST',
],
];
break;
case 'PATCH':
$headers = [];
$headers['X-CSRF-Token'] = $csrf_token !== FALSE ? $csrf_token === NULL ? $requested_token : $csrf_token : NULL;
$headers['Content-Type'] = $mime_type !== 'none' ? $mime_type : NULL;
$request_options = [
'timeout' => 10,
'body' => $body,
'headers' => $headers,
'curl' => [
CURLOPT_CUSTOMREQUEST => 'PATCH',
],
];
break;
case 'DELETE':
$headers = [];
$headers['X-CSRF-Token'] = $csrf_token !== FALSE ? $csrf_token === NULL ? $requested_token : $csrf_token : NULL;
$request_options = [
'timeout' => 10,
'headers' => $headers,
'curl' => [
CURLOPT_CUSTOMREQUEST => 'DELETE',
],
];
break;
}
$request_options[RequestOptions::ALLOW_REDIRECTS] = TRUE;
try {
$response = $client
->request($method, $url, $request_options);
$this->responseBody = $response
->getBody();
} catch (RequestException $e) {
dump($e
->getCode() . ": " . $e
->getMessage());
}
// Ensure that any changes to variables in the other thread are picked up.
$this
->refreshVariables();
$headers = $this
->getSession()
->getResponseHeaders();
dump($method . ' request to: ' . $url . '<hr />Code: ' . $response
->getStatusCode() . (isset($request_options['headers']) ? '<hr />Request headers: ' . nl2br(print_r($request_options['headers'], TRUE)) : '') . (isset($request_options['body']) ? '<hr />Request body: ' . nl2br(print_r($request_options['body'], TRUE)) : '') . '<hr />Response headers: ' . nl2br(print_r($headers, TRUE)) . '<hr />Response body: ' . $this->responseBody);
return $this->responseBody;
}
/**
* Check if the HTTP request response body is identical to the expected value.
*
* @param string $expected
* The first value to check.
* @param string $message
* (optional) A message to display with the assertion. Do not translate
* messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
* variables in the message text, not t(). If left blank, a default message
* will be displayed.
* @param string $group
* (optional) The group this message is in, which is displayed in a column
* in test output. Use 'Debug' to indicate this is debugging output. Do not
* translate this string. Defaults to 'Other'; most tests do not override
* this default.
*
* @return bool
* TRUE if the assertion succeeded, FALSE otherwise.
*/
protected function assertResponseBody($expected, $message = '', $group = 'REST Response') {
return $this
->assertSame($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);
}
/**
* Provides an array of suitable property values for an entity type.
*
* Required properties differ from entity type to entity type, so we keep a
* minimum mapping here.
*
* @param string $entity_type_id
* The ID of the type of entity that should be created.
*
* @return array
* An array of values keyed by property name.
*/
protected function entityValues($entity_type_id) {
switch ($entity_type_id) {
case 'node':
return [
'title' => $this
->randomString(),
'type' => 'resttest',
];
case 'node_type':
return [
'type' => 'article',
'name' => $this
->randomMachineName(),
];
case 'user':
return [
'name' => $this
->randomMachineName(),
];
case 'taxonomy_vocabulary':
return [
'vid' => 'tags',
'name' => $this
->randomMachineName(),
];
default:
if ($this
->isConfigEntity($entity_type_id)) {
return $this
->configEntityValues($entity_type_id);
}
return [];
}
}
/**
* Checks if an entity type id is for a Config Entity.
*
* @param string $entity_type_id
* The entity type ID to check.
*
* @return bool
* TRUE if the entity is a Config Entity, FALSE otherwise.
*/
protected function isConfigEntity($entity_type_id) {
return \Drupal::entityTypeManager()
->getDefinition($entity_type_id) instanceof ConfigEntityType;
}
/**
* Creates entity objects based on their types.
*
* @param string $entity_type
* The type of the entity that should be created.
* @param \Drupal\Core\Session\AccountInterface $account
* The account that will own this entity.
*
* @return \Drupal\Core\Entity\EntityInterface|\Drupal\acquia_contenthub_subscriber\Entity\ContentHubFilter
* The new entity object.
*/
protected function entityCreate($entity_type, AccountInterface $account = NULL) {
return $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create($this
->configEntityValues($entity_type, $account));
}
/**
* Provides an array of suitable property values for a config entity type.
*
* Config entities have some common keys that need to be created. Required
* properties differ among config entity types, so we keep a minimum mapping
* here.
*
* @param string $entity_type_id
* The ID of the type of entity that should be created.
* @param \Drupal\Core\Session\AccountInterface $account
* The account that will own this entity.
*
* @return array
* An array of values keyed by property name.
*/
protected function configEntityValues($entity_type_id, AccountInterface $account = NULL) {
$entity_type = \Drupal::entityTypeManager()
->getDefinition($entity_type_id);
$keys = $entity_type
->getKeys();
$values = [];
// Fill out known key values that are shared across entity types.
foreach ($keys as $key) {
if ($key === 'id' || $key === 'label') {
$values[$key] = $this
->randomMachineName();
}
}
// Add extra values for particular entity types.
switch ($entity_type_id) {
case 'contenthub_filter':
$publish_settings = [
'none',
'import',
'publish',
];
$values['name'] = 'Name for ' . $values['id'];
$values['publish_setting'] = $publish_settings[random_int(0, 2)];
$values['search_term'] = $this
->randomMachineName();
$author = $account !== NULL ? $account
->id() : $this->adminUser
->id();
$values['author'] = intval($author);
$values['entity_types'] = [
'node',
];
$values['bundles'] = [
'article',
'page',
];
break;
}
return $values;
}
}
Classes
Name | Description |
---|---|
ContentHubSubscriberTestBase | Provides the base class for web tests for Content Hub Subscribers. |