From a87e8aafdb2f62898a8d5bf7243d3119200147f0 Mon Sep 17 00:00:00 2001 From: Lee Rowlands <lee.rowlands@previousnext.com.au> Date: Wed, 13 Sep 2017 07:51:28 +1000 Subject: [PATCH] Issue #2843778 by Wim Leers: EntityResource: Provide comprehensive test coverage for ContactForm entity --- .../src/ContactFormAccessControlHandler.php | 4 +- .../ContactFormHalJsonAnonTest.php | 30 +++++ .../ContactFormHalJsonBasicAuthTest.php | 35 ++++++ .../ContactFormHalJsonCookieTest.php | 35 ++++++ .../ContactForm/ContactFormJsonAnonTest.php | 24 ++++ .../ContactFormJsonBasicAuthTest.php | 34 ++++++ .../ContactForm/ContactFormJsonCookieTest.php | 29 +++++ .../ContactFormResourceTestBase.php | 104 ++++++++++++++++++ .../EntityResource/EntityResourceTestBase.php | 5 +- 9 files changed, 297 insertions(+), 3 deletions(-) create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/ContactForm/ContactFormHalJsonAnonTest.php create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/ContactForm/ContactFormHalJsonBasicAuthTest.php create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/ContactForm/ContactFormHalJsonCookieTest.php create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormJsonAnonTest.php create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormJsonBasicAuthTest.php create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormJsonCookieTest.php create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormResourceTestBase.php diff --git a/core/modules/contact/src/ContactFormAccessControlHandler.php b/core/modules/contact/src/ContactFormAccessControlHandler.php index ea2501b573e9..320eed697f01 100644 --- a/core/modules/contact/src/ContactFormAccessControlHandler.php +++ b/core/modules/contact/src/ContactFormAccessControlHandler.php @@ -20,12 +20,12 @@ class ContactFormAccessControlHandler extends EntityAccessControlHandler { protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { if ($operation == 'view') { // Do not allow access personal form via site-wide route. - return AccessResult::allowedIf($account->hasPermission('access site-wide contact form') && $entity->id() !== 'personal')->cachePerPermissions(); + return AccessResult::allowedIfHasPermission($account, 'access site-wide contact form')->andIf(AccessResult::allowedIf($entity->id() !== 'personal')); } elseif ($operation == 'delete' || $operation == 'update') { // Do not allow the 'personal' form to be deleted, as it's used for // the personal contact form. - return AccessResult::allowedIf($account->hasPermission('administer contact forms') && $entity->id() !== 'personal')->cachePerPermissions(); + return AccessResult::allowedIfHasPermission($account, 'administer contact forms')->andIf(AccessResult::allowedIf($entity->id() !== 'personal')); } return parent::checkAccess($entity, $operation, $account); diff --git a/core/modules/hal/tests/src/Functional/EntityResource/ContactForm/ContactFormHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/ContactForm/ContactFormHalJsonAnonTest.php new file mode 100644 index 000000000000..57d61779225f --- /dev/null +++ b/core/modules/hal/tests/src/Functional/EntityResource/ContactForm/ContactFormHalJsonAnonTest.php @@ -0,0 +1,30 @@ +<?php + +namespace Drupal\Tests\hal\Functional\EntityResource\ContactForm; + +use Drupal\Tests\rest\Functional\AnonResourceTestTrait; +use Drupal\Tests\rest\Functional\EntityResource\ContactForm\ContactFormResourceTestBase; + +/** + * @group hal + */ +class ContactFormHalJsonAnonTest extends ContactFormResourceTestBase { + + use AnonResourceTestTrait; + + /** + * {@inheritdoc} + */ + public static $modules = ['hal']; + + /** + * {@inheritdoc} + */ + protected static $format = 'hal_json'; + + /** + * {@inheritdoc} + */ + protected static $mimeType = 'application/hal+json'; + +} diff --git a/core/modules/hal/tests/src/Functional/EntityResource/ContactForm/ContactFormHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/ContactForm/ContactFormHalJsonBasicAuthTest.php new file mode 100644 index 000000000000..9aec186e15b3 --- /dev/null +++ b/core/modules/hal/tests/src/Functional/EntityResource/ContactForm/ContactFormHalJsonBasicAuthTest.php @@ -0,0 +1,35 @@ +<?php + +namespace Drupal\Tests\hal\Functional\EntityResource\ContactForm; + +use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait; +use Drupal\Tests\rest\Functional\EntityResource\ContactForm\ContactFormResourceTestBase; + +/** + * @group hal + */ +class ContactFormHalJsonBasicAuthTest extends ContactFormResourceTestBase { + + use BasicAuthResourceTestTrait; + + /** + * {@inheritdoc} + */ + public static $modules = ['hal', 'basic_auth']; + + /** + * {@inheritdoc} + */ + protected static $format = 'hal_json'; + + /** + * {@inheritdoc} + */ + protected static $mimeType = 'application/hal+json'; + + /** + * {@inheritdoc} + */ + protected static $auth = 'basic_auth'; + +} diff --git a/core/modules/hal/tests/src/Functional/EntityResource/ContactForm/ContactFormHalJsonCookieTest.php b/core/modules/hal/tests/src/Functional/EntityResource/ContactForm/ContactFormHalJsonCookieTest.php new file mode 100644 index 000000000000..0d78991f95da --- /dev/null +++ b/core/modules/hal/tests/src/Functional/EntityResource/ContactForm/ContactFormHalJsonCookieTest.php @@ -0,0 +1,35 @@ +<?php + +namespace Drupal\Tests\hal\Functional\EntityResource\ContactForm; + +use Drupal\Tests\rest\Functional\CookieResourceTestTrait; +use Drupal\Tests\rest\Functional\EntityResource\ContactForm\ContactFormResourceTestBase; + +/** + * @group hal + */ +class ContactFormHalJsonCookieTest extends ContactFormResourceTestBase { + + use CookieResourceTestTrait; + + /** + * {@inheritdoc} + */ + public static $modules = ['hal']; + + /** + * {@inheritdoc} + */ + protected static $format = 'hal_json'; + + /** + * {@inheritdoc} + */ + protected static $mimeType = 'application/hal+json'; + + /** + * {@inheritdoc} + */ + protected static $auth = 'cookie'; + +} diff --git a/core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormJsonAnonTest.php new file mode 100644 index 000000000000..a5f360214fc3 --- /dev/null +++ b/core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormJsonAnonTest.php @@ -0,0 +1,24 @@ +<?php + +namespace Drupal\Tests\rest\Functional\EntityResource\ContactForm; + +use Drupal\Tests\rest\Functional\AnonResourceTestTrait; + +/** + * @group rest + */ +class ContactFormJsonAnonTest extends ContactFormResourceTestBase { + + use AnonResourceTestTrait; + + /** + * {@inheritdoc} + */ + protected static $format = 'json'; + + /** + * {@inheritdoc} + */ + protected static $mimeType = 'application/json'; + +} diff --git a/core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormJsonBasicAuthTest.php new file mode 100644 index 000000000000..c41b9a4ef437 --- /dev/null +++ b/core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormJsonBasicAuthTest.php @@ -0,0 +1,34 @@ +<?php + +namespace Drupal\Tests\rest\Functional\EntityResource\ContactForm; + +use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait; + +/** + * @group rest + */ +class ContactFormJsonBasicAuthTest extends ContactFormResourceTestBase { + + use BasicAuthResourceTestTrait; + + /** + * {@inheritdoc} + */ + public static $modules = ['basic_auth']; + + /** + * {@inheritdoc} + */ + protected static $format = 'json'; + + /** + * {@inheritdoc} + */ + protected static $mimeType = 'application/json'; + + /** + * {@inheritdoc} + */ + protected static $auth = 'basic_auth'; + +} diff --git a/core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormJsonCookieTest.php b/core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormJsonCookieTest.php new file mode 100644 index 000000000000..2b8d9137ccc0 --- /dev/null +++ b/core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormJsonCookieTest.php @@ -0,0 +1,29 @@ +<?php + +namespace Drupal\Tests\rest\Functional\EntityResource\ContactForm; + +use Drupal\Tests\rest\Functional\CookieResourceTestTrait; + +/** + * @group rest + */ +class ContactFormJsonCookieTest extends ContactFormResourceTestBase { + + use CookieResourceTestTrait; + + /** + * {@inheritdoc} + */ + protected static $format = 'json'; + + /** + * {@inheritdoc} + */ + protected static $mimeType = 'application/json'; + + /** + * {@inheritdoc} + */ + protected static $auth = 'cookie'; + +} diff --git a/core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormResourceTestBase.php new file mode 100644 index 000000000000..51e297a7b83e --- /dev/null +++ b/core/modules/rest/tests/src/Functional/EntityResource/ContactForm/ContactFormResourceTestBase.php @@ -0,0 +1,104 @@ +<?php + +namespace Drupal\Tests\rest\Functional\EntityResource\ContactForm; + +use Drupal\contact\Entity\ContactForm; +use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait; +use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase; + +abstract class ContactFormResourceTestBase extends EntityResourceTestBase { + + use BcTimestampNormalizerUnixTestTrait; + + /** + * {@inheritdoc} + */ + public static $modules = ['contact']; + + /** + * {@inheritdoc} + */ + protected static $entityTypeId = 'contact_form'; + + /** + * {@inheritdoc} + */ + protected static $patchProtectedFieldNames = []; + + /** + * @var \Drupal\contact\Entity\ContactForm + */ + protected $entity; + + /** + * {@inheritdoc} + */ + protected function setUpAuthorization($method) { + switch ($method) { + case 'GET': + $this->grantPermissionsToTestedRole(['access site-wide contact form']); + default: + $this->grantPermissionsToTestedRole(['administer contact forms']); + } + } + + /** + * {@inheritdoc} + */ + protected function createEntity() { + $contact_form = ContactForm::create([ + 'id' => 'llama', + 'label' => 'Llama', + 'message' => 'Let us know what you think about llamas', + 'reply' => 'Llamas are indeed awesome!', + 'recipients' => [ + 'llama@example.com', + 'contact@example.com', + ], + ]); + $contact_form->save(); + + return $contact_form; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedNormalizedEntity() { + return [ + 'dependencies' => [], + 'id' => 'llama', + 'label' => 'Llama', + 'langcode' => 'en', + 'message' => 'Let us know what you think about llamas', + 'recipients' => [ + 'llama@example.com', + 'contact@example.com', + ], + 'redirect' => NULL, + 'reply' => 'Llamas are indeed awesome!', + 'status' => TRUE, + 'uuid' => $this->entity->uuid(), + 'weight' => 0, + ]; + } + + /** + * {@inheritdoc} + */ + protected function getNormalizedPostEntity() { + // @todo Update in https://www.drupal.org/node/2300677. + } + + /** + * {@inheritdoc} + */ + protected function getExpectedUnauthorizedAccessMessage($method) { + if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) { + return parent::getExpectedUnauthorizedAccessMessage($method); + } + + return "The 'access site-wide contact form' permission is required."; + } + +} diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index f39b7d8fba3a..fe4ebb8310ae 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -1274,7 +1274,10 @@ protected function assertNormalizationEdgeCases($method, Url $url, array $reques */ protected function getEntityResourceUrl() { $has_canonical_url = $this->entity->hasLinkTemplate('canonical'); - return $has_canonical_url ? $this->entity->toUrl() : Url::fromUri('base:entity/' . static::$entityTypeId . '/' . $this->entity->id()); + // Note that the 'canonical' link relation type must be specified explicitly + // in the call to ::toUrl(). 'canonical' is the default for + // \Drupal\Core\Entity\Entity::toUrl(), but ConfigEntityBase overrides this. + return $has_canonical_url ? $this->entity->toUrl('canonical') : Url::fromUri('base:entity/' . static::$entityTypeId . '/' . $this->entity->id()); } /** -- GitLab