.../Block/BlockHalJsonBasicAuthTest.php | 35 +++++++++++ .../Block/BlockJsonBasicAuthTest.php | 35 +++++++++++ .../EntityResource/Block/BlockResourceTestBase.php | 70 ++++++++++++++++++++++ .../EntityResource/HalEntityNormalizationTrait.php | 5 ++ .../Role/RoleHalJsonBasicAuthTest.php | 35 +++++++++++ .../EntityResource/Role/RoleJsonBasicAuthTest.php | 35 +++++++++++ .../EntityResource/Role/RoleResourceTestBase.php | 55 +++++++++++++++++ 7 files changed, 270 insertions(+) diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockHalJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockHalJsonBasicAuthTest.php new file mode 100644 index 0000000..25512fa --- /dev/null +++ b/core/modules/rest/tests/src/Functional/EntityResource/Block/BlockHalJsonBasicAuthTest.php @@ -0,0 +1,35 @@ + 'llama_block', + 'region' => 'header', + 'id' => 'llama', + 'theme' => 'classy', + ]); + $block->save(); + + // Give anonymous users permission to view comments, so that we can verify + // the cache tags of cached versions of comment pages. + $user_role = Role::load(RoleInterface::ANONYMOUS_ID); + $user_role->grantPermission('administer blocks'); + $user_role->save(); + + return $block; + } + + protected function getExpectedNormalizedEntity() { + assert('$this->entity instanceof \Drupal\Core\Entity\EntityInterface', 'Entity must already have been created.'); + return [ + 'uuid' => $this->entity->uuid(), + 'id' => 'llama', + 'weight' => NULL, + 'langcode' => 'en', + 'status' => TRUE, + 'dependencies' => [ + 'theme' => [ + 'classy', + ], + ], + 'theme' => 'classy', + 'region' => 'header', + 'provider' => NULL, + 'plugin' => 'llama_block', + 'settings' => [ + 'id' => 'broken', + 'label' => '', + 'provider' => 'core', + 'label_display' => 'visible', + ], + 'visibility' => [], + ]; + } + +} diff --git a/core/modules/rest/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php b/core/modules/rest/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php index a16b4ba..1dcea7e 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php @@ -2,12 +2,17 @@ namespace Drupal\Tests\rest\Functional\EntityResource; +use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Field\EntityReferenceFieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface; trait HalEntityNormalizationTrait { protected function applyHalFieldNormalization(array $normalization) { + if (!$this->entity instanceof FieldableEntityInterface) { + throw new \LogicException('This trait should only be used for fieldable entity types.'); + } + // In the HAL normalization, all translatable fields get a 'lang' attribute. $translatable_non_reference_fields = array_keys(array_filter($this->entity->getTranslatableFields(), function (FieldItemListInterface $field) { return !$field instanceof EntityReferenceFieldItemListInterface; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleHalJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleHalJsonBasicAuthTest.php new file mode 100644 index 0000000..9021546 --- /dev/null +++ b/core/modules/rest/tests/src/Functional/EntityResource/Role/RoleHalJsonBasicAuthTest.php @@ -0,0 +1,35 @@ + 'llama', + 'name' => $this->randomString(), + ]); + $role->save(); + + // Give anonymous users permission to view comments, so that we can verify + // the cache tags of cached versions of comment pages. + $user_role = Role::load(RoleInterface::ANONYMOUS_ID); + $user_role->grantPermission('administer permissions'); + $user_role->save(); + + return $role; + } + + protected function getExpectedNormalizedEntity() { + assert('$this->entity instanceof \Drupal\Core\Entity\EntityInterface', 'Entity must already have been created.'); + return [ + 'uuid' => $this->entity->uuid(), + 'weight' => 2, + 'langcode' => 'en', + 'status' => TRUE, + 'dependencies' => [], + 'id' => 'llama', + 'label' => NULL, + 'is_admin' => NULL, + 'permissions' => [], + ]; + } + +}