.../EntityResource/HalEntityNormalizationTrait.php | 14 ++++++-------- .../EntityResource/Block/BlockResourceTestBase.php | 6 +++++- .../Comment/CommentResourceTestBase.php | 6 +++++- .../ConfigTest/ConfigTestResourceTestBase.php | 6 +++++- .../EntityTest/EntityTestResourceTestBase.php | 6 +++++- .../EntityResource/Node/NodeResourceTestBase.php | 6 +++++- .../EntityResource/Role/RoleResourceTestBase.php | 6 +++++- .../EntityResource/Term/TermResourceTestBase.php | 6 +++++- .../EntityResource/User/UserResourceTestBase.php | 6 +++++- .../Vocabulary/VocabularyResourceTestBase.php | 6 +++++- .../rest/tests/src/Functional/ResourceTestBase.php | 19 +++++++------------ 11 files changed, 58 insertions(+), 29 deletions(-) diff --git a/core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php b/core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php index 49ac07d..ade5239 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php @@ -27,10 +27,7 @@ protected function applyHalFieldNormalization(array $normalization) { // bundle field. $bundle_key = $this->entity->getEntityType()->getKey('bundle'); $reference_fields = array_keys(array_filter($this->entity->getFields(), function (FieldItemListInterface $field) use ($bundle_key) { - if ($field->getName() === $bundle_key) { - return FALSE; - } - return $field instanceof EntityReferenceFieldItemListInterface; + return ($field->getName() === $bundle_key) ? FALSE : $field instanceof EntityReferenceFieldItemListInterface; })); foreach ($reference_fields as $field_name) { unset($normalization[$field_name]); @@ -44,10 +41,11 @@ protected function applyHalFieldNormalization(array $normalization) { } // In the HAL normalization, empty fields are omitted. - foreach ($normalization as $field_name => $data) { - if ($this->entity->$field_name->isEmpty()) { - unset($normalization[$field_name]); - } + $empty_fields = array_keys(array_filter($this->entity->getFields(), function (FieldItemListInterface $field) { + return $field->isEmpty(); + })); + foreach ($empty_fields as $field_name) { + unset($normalization[$field_name]); } return $normalization; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockResourceTestBase.php index 268f690..c569c20 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockResourceTestBase.php @@ -18,6 +18,11 @@ protected static $entityType = 'block'; /** + * @var \Drupal\block\BlockInterface + */ + protected $entity; + + /** * {@inheritdoc} */ protected function setUpAuthorization($method) { @@ -69,7 +74,6 @@ protected function createEntity() { * {@inheritdoc} */ protected function getExpectedNormalizedEntity() { - assert('$this->entity instanceof \Drupal\Core\Entity\EntityInterface', 'Entity must already have been created.'); $normalization = [ 'uuid' => $this->entity->uuid(), 'id' => 'llama', diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php index d602524..ef37d1b 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php @@ -25,6 +25,11 @@ protected static $entityType = 'comment'; /** + * @var \Drupal\comment\CommentInterface + */ + protected $entity; + + /** * {@inheritdoc} */ protected function setUpAuthorization($method) { @@ -83,7 +88,6 @@ protected function createEntity() { * {@inheritdoc} */ protected function getExpectedNormalizedEntity() { - assert('$this->entity instanceof \Drupal\Core\Entity\EntityInterface', 'Entity must already have been created.'); $author = User::load(0); return [ 'cid' => [ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestResourceTestBase.php index 9024eae..50c6ec9 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/ConfigTest/ConfigTestResourceTestBase.php @@ -17,6 +17,11 @@ protected static $entityType = 'config_test'; /** + * @var \Drupal\config_test\ConfigTestInterface + */ + protected $entity; + + /** * {@inheritdoc} */ protected function setUpAuthorization($method) { @@ -40,7 +45,6 @@ protected function createEntity() { * {@inheritdoc} */ protected function getExpectedNormalizedEntity() { - assert('$this->entity instanceof \Drupal\Core\Entity\EntityInterface', 'Entity must already have been created.'); $normalization = [ 'uuid' => $this->entity->uuid(), 'id' => 'llama', diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestResourceTestBase.php index 5dff569..ab375f9 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestResourceTestBase.php @@ -19,6 +19,11 @@ protected static $entityType = 'entity_test'; /** + * @var \Drupal\entity_test\Entity\EntityTest + */ + protected $entity; + + /** * {@inheritdoc} */ protected function setUpAuthorization($method) { @@ -54,7 +59,6 @@ protected function createEntity() { * {@inheritdoc} */ protected function getExpectedNormalizedEntity() { - assert('$this->entity instanceof \Drupal\Core\Entity\EntityInterface', 'Entity must already have been created.'); $author = User::load(0); $normalization = [ 'uuid' => [ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php index ba1ec18..e77467a 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php @@ -20,6 +20,11 @@ protected static $entityType = 'node'; /** + * @var \Drupal\node\NodeInterface + */ + protected $entity; + + /** * {@inheritdoc} */ protected function setUpAuthorization($method) { @@ -67,7 +72,6 @@ protected function createEntity() { * {@inheritdoc} */ protected function getExpectedNormalizedEntity() { - assert('$this->entity instanceof \Drupal\Core\Entity\EntityInterface', 'Entity must already have been created.'); $author = User::load(0); return [ 'nid' => [ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleResourceTestBase.php index 12f329c..4cc247c 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleResourceTestBase.php @@ -19,6 +19,11 @@ protected static $entityType = 'user_role'; /** + * @var \Drupal\user\RoleInterface + */ + protected $entity; + + /** * {@inheritdoc} */ protected function setUpAuthorization($method) { @@ -42,7 +47,6 @@ protected function createEntity() { * {@inheritdoc} */ protected function getExpectedNormalizedEntity() { - assert('$this->entity instanceof \Drupal\Core\Entity\EntityInterface', 'Entity must already have been created.'); return [ 'uuid' => $this->entity->uuid(), 'weight' => 2, diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php index 7c9f0f2..dd56380 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php @@ -19,6 +19,11 @@ protected static $entityType = 'taxonomy_term'; /** + * @var \Drupal\taxonomy\TermInterface + */ + protected $entity; + + /** * {@inheritdoc} */ protected function setUpAuthorization($method) { @@ -62,7 +67,6 @@ protected function createEntity() { * {@inheritdoc} */ protected function getExpectedNormalizedEntity() { - assert('$this->entity instanceof \Drupal\Core\Entity\EntityInterface', 'Entity must already have been created.'); return [ 'tid' => [ ['value' => 1], diff --git a/core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php index fa6f32c..478c442 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php @@ -18,6 +18,11 @@ protected static $entityType = 'user'; /** + * @var \Drupal\user\UserInterface + */ + protected $entity; + + /** * {@inheritdoc} */ protected static $labelField = 'name'; @@ -66,7 +71,6 @@ protected function createEntity() { * {@inheritdoc} */ protected function getExpectedNormalizedEntity() { - assert('$this->entity instanceof \Drupal\Core\Entity\EntityInterface', 'Entity must already have been created.'); return [ 'uid' => [ ['value' => '3'], diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyResourceTestBase.php index 241eb17..e812fbb 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Vocabulary/VocabularyResourceTestBase.php @@ -21,6 +21,11 @@ protected static $entityType = 'taxonomy_vocabulary'; /** + * @var \Drupal\taxonomy\VocabularyInterface + */ + protected $entity; + + /** * {@inheritdoc} */ protected function setUpAuthorization($method) { @@ -44,7 +49,6 @@ protected function createEntity() { * {@inheritdoc} */ protected function getExpectedNormalizedEntity() { - assert('$this->entity instanceof \Drupal\Core\Entity\EntityInterface', 'Entity must already have been created.'); return [ 'uuid' => $this->entity->uuid(), 'vid' => 'llama', diff --git a/core/modules/rest/tests/src/Functional/ResourceTestBase.php b/core/modules/rest/tests/src/Functional/ResourceTestBase.php index 07cf862..549ebad 100644 --- a/core/modules/rest/tests/src/Functional/ResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/ResourceTestBase.php @@ -9,6 +9,7 @@ use Drupal\user\RoleInterface; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ServerException; +use GuzzleHttp\RequestOptions; use Psr\Http\Message\ResponseInterface; /** @@ -261,8 +262,10 @@ protected function grantPermissionsToTestedRole(array $permissions) { /** * Performs a HTTP request. Wraps the Guzzle HTTP client. * - * Why wrap the Guzzle HTTP client? Because any error response is returned via - * an exception, which would make the tests unnecessarily complex to read. + * Why wrap the Guzzle HTTP client? Because we want to keep the actual test + * code as simple as possible, and hence not require them to specify the + * 'http_errors = FALSE' request option, nor do we want them to have to + * convert Drupal Url objects to strings. * * @see \GuzzleHttp\ClientInterface::request() * @@ -276,16 +279,8 @@ protected function grantPermissionsToTestedRole(array $permissions) { * @return \Psr\Http\Message\ResponseInterface */ protected function request($method, Url $url, array $request_options) { - try { - $response = $this->httpClient->request($method, $url->toString(), $request_options); - } - catch (ClientException $e) { - $response = $e->getResponse(); - } - catch (ServerException $e) { - $response = $e->getResponse(); - } - return $response; + $request_options[RequestOptions::HTTP_ERRORS] = FALSE; + return $this->httpClient->request($method, $url->toString(), $request_options); } /**