From 2dc6d91d609ee76a3d463eedcb9eba0903c94580 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Tue, 18 Apr 2017 18:52:39 +0100 Subject: [PATCH] Issue #2843754 by rogierbom, Wim Leers, gaurav.kapoor, vaplas: EntityResource: Provide comprehensive test coverage for Feed entity --- .../Feed/FeedHalJsonAnonTest.php | 19 ++ .../Feed/FeedHalJsonBasicAuthTest.php | 24 +++ .../Feed/FeedHalJsonCookieTest.php | 19 ++ .../Feed/FeedHalJsonTestBase.php | 60 ++++++ .../EntityResource/EntityResourceTestBase.php | 2 + .../EntityResource/Feed/FeedJsonAnonTest.php | 24 +++ .../Feed/FeedJsonBasicAuthTest.php | 34 ++++ .../Feed/FeedJsonCookieTest.php | 29 +++ .../Feed/FeedResourceTestBase.php | 185 ++++++++++++++++++ 9 files changed, 396 insertions(+) create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonAnonTest.php create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonBasicAuthTest.php create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonCookieTest.php create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonTestBase.php create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedJsonAnonTest.php create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedJsonBasicAuthTest.php create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedJsonCookieTest.php create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedResourceTestBase.php diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonAnonTest.php new file mode 100644 index 000000000000..293b7e4f7c22 --- /dev/null +++ b/core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonAnonTest.php @@ -0,0 +1,19 @@ +<?php + +namespace Drupal\Tests\hal\Functional\EntityResource\Feed; + +use Drupal\Tests\rest\Functional\AnonResourceTestTrait; + +/** + * @group hal + */ +class FeedHalJsonAnonTest extends FeedHalJsonTestBase { + + use AnonResourceTestTrait; + + /** + * {@inheritdoc} + */ + protected static $mimeType = 'application/hal+json'; + +} diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonBasicAuthTest.php new file mode 100644 index 000000000000..c3f91a7466d2 --- /dev/null +++ b/core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonBasicAuthTest.php @@ -0,0 +1,24 @@ +<?php + +namespace Drupal\Tests\hal\Functional\EntityResource\Feed; + +use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait; + +/** + * @group hal + */ +class FeedHalJsonBasicAuthTest extends FeedHalJsonTestBase { + + use BasicAuthResourceTestTrait; + + /** + * {@inheritdoc} + */ + public static $modules = ['basic_auth']; + + /** + * {@inheritdoc} + */ + protected static $auth = 'basic_auth'; + +} diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonCookieTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonCookieTest.php new file mode 100644 index 000000000000..fb4d64e76ba5 --- /dev/null +++ b/core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonCookieTest.php @@ -0,0 +1,19 @@ +<?php + +namespace Drupal\Tests\hal\Functional\EntityResource\Feed; + +use Drupal\Tests\rest\Functional\CookieResourceTestTrait; + +/** + * @group hal + */ +class FeedHalJsonCookieTest extends FeedHalJsonTestBase { + + use CookieResourceTestTrait; + + /** + * {@inheritdoc} + */ + protected static $auth = 'cookie'; + +} diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonTestBase.php b/core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonTestBase.php new file mode 100644 index 000000000000..43181323e32e --- /dev/null +++ b/core/modules/hal/tests/src/Functional/EntityResource/Feed/FeedHalJsonTestBase.php @@ -0,0 +1,60 @@ +<?php + +namespace Drupal\Tests\hal\Functional\EntityResource\Feed; + +use Drupal\Tests\rest\Functional\EntityResource\Feed\FeedResourceTestBase; +use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait; + +abstract class FeedHalJsonTestBase extends FeedResourceTestBase { + + use HalEntityNormalizationTrait; + + /** + * {@inheritdoc} + */ + public static $modules = ['hal']; + + /** + * {@inheritdoc} + */ + protected static $format = 'hal_json'; + + /** + * {@inheritdoc} + */ + protected static $mimeType = 'application/hal+json'; + + /** + * {@inheritdoc} + */ + protected function getExpectedNormalizedEntity() { + $default_normalization = parent::getExpectedNormalizedEntity(); + + $normalization = $this->applyHalFieldNormalization($default_normalization); + + return $normalization + [ + '_links' => [ + 'self' => [ + 'href' => $this->baseUrl . '/aggregator/sources/1?_format=hal_json' + ], + 'type' => [ + 'href' => $this->baseUrl . '/rest/type/aggregator_feed/aggregator_feed' + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getNormalizedPostEntity() { + return parent::getNormalizedPostEntity() + [ + '_links' => [ + 'type' => [ + 'href' => $this->baseUrl . '/rest/type/aggregator_feed/aggregator_feed' + ], + ], + ]; + } + +} diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index 80d8a0610044..a55ae929e988 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -757,6 +757,8 @@ public function testPost() { // 201 for well-formed request. + // Delete the first created entity in case there is a uniqueness constraint. + $this->entityStorage->load(static::$firstCreatedEntityId)->delete(); $response = $this->request('POST', $url, $request_options); $this->assertResourceResponse(201, FALSE, $response); if ($has_canonical_url) { diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedJsonAnonTest.php new file mode 100644 index 000000000000..a4b8e0dcdbba --- /dev/null +++ b/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedJsonAnonTest.php @@ -0,0 +1,24 @@ +<?php + +namespace Drupal\Tests\rest\Functional\EntityResource\Feed; + +use Drupal\Tests\rest\Functional\AnonResourceTestTrait; + +/** + * @group rest + */ +class FeedJsonAnonTest extends FeedResourceTestBase { + + use AnonResourceTestTrait; + + /** + * {@inheritdoc} + */ + protected static $format = 'json'; + + /** + * {@inheritdoc} + */ + protected static $mimeType = 'application/json'; + +} diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedJsonBasicAuthTest.php new file mode 100644 index 000000000000..e5685c9cd6c2 --- /dev/null +++ b/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedJsonBasicAuthTest.php @@ -0,0 +1,34 @@ +<?php + +namespace Drupal\Tests\rest\Functional\EntityResource\Feed; + +use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait; + +/** + * @group rest + */ +class FeedJsonBasicAuthTest extends FeedResourceTestBase { + + 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/Feed/FeedJsonCookieTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedJsonCookieTest.php new file mode 100644 index 000000000000..7c1cc9f87a7e --- /dev/null +++ b/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedJsonCookieTest.php @@ -0,0 +1,29 @@ +<?php + +namespace Drupal\Tests\rest\Functional\EntityResource\Feed; + +use Drupal\Tests\rest\Functional\CookieResourceTestTrait; + +/** + * @group rest + */ +class FeedJsonCookieTest extends FeedResourceTestBase { + + 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/Feed/FeedResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedResourceTestBase.php new file mode 100644 index 000000000000..f15a5e055440 --- /dev/null +++ b/core/modules/rest/tests/src/Functional/EntityResource/Feed/FeedResourceTestBase.php @@ -0,0 +1,185 @@ +<?php + +namespace Drupal\Tests\rest\Functional\EntityResource\Feed; + +use Drupal\Tests\rest\Functional\EntityResource\EntityTest\EntityTestResourceTestBase; +use Drupal\aggregator\Entity\Feed; + +abstract class FeedResourceTestBase extends EntityTestResourceTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = ['aggregator']; + + /** + * {@inheritdoc} + */ + public static $entityTypeId = 'aggregator_feed'; + + /** + * {@inheritdoc} + */ + protected function setUpAuthorization($method) { + switch ($method) { + case 'GET': + $this->grantPermissionsToTestedRole(['access news feeds']); + break; + case 'POST': + case 'PATCH': + case 'DELETE': + $this->grantPermissionsToTestedRole(['administer news feeds']); + break; + } + } + + /** + * {@inheritdoc} + */ + public function createEntity() { + $feed = Feed::create(); + $feed->set('fid', 1) + ->set('uuid', 'abcdefg') + ->setTitle('Feed') + ->setUrl('http://example.com/rss.xml') + ->setDescription('Feed Resource Test 1') + ->setRefreshRate(900) + ->setLastCheckedTime(123456789) + ->setQueuedTime(123456789) + ->setWebsiteUrl('http://example.com') + ->setImage('http://example.com/feed_logo') + ->setHash('abcdefg') + ->setEtag('hijklmn') + ->setLastModified(123456789) + ->save(); + + return $feed; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedNormalizedEntity() { + return [ + 'uuid' => [ + [ + 'value' => 'abcdefg' + ] + ], + 'fid' => [ + [ + 'value' => 1 + ] + ], + 'langcode' => [ + [ + 'value' => 'en' + ] + ], + 'url' => [ + [ + 'value' => 'http://example.com/rss.xml' + ] + ], + 'title' => [ + [ + 'value' => 'Feed' + ] + ], + 'refresh' => [ + [ + 'value' => 900 + ] + ], + 'checked' => [ + [ + 'value' => 123456789 + ] + ], + 'queued' => [ + [ + 'value' => 123456789 + ] + ], + 'link' => [ + [ + 'value' => 'http://example.com' + ] + ], + 'description' => [ + [ + 'value' => 'Feed Resource Test 1' + ] + ], + 'image' => [ + [ + 'value' => 'http://example.com/feed_logo' + ] + ], + 'hash' => [ + [ + 'value' => 'abcdefg' + ] + ], + 'etag' => [ + [ + 'value' => 'hijklmn' + ] + ], + 'modified' => [ + [ + 'value' => 123456789 + ] + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getNormalizedPostEntity() { + return [ + 'title' => [ + [ + 'value' => 'Feed Resource Post Test' + ] + ], + 'url' => [ + [ + 'value' => 'http://example.com/feed' + ] + ], + 'refresh' => [ + [ + 'value' => 900 + ] + ], + 'description' => [ + [ + 'value' => 'Feed Resource Post Test Description' + ] + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedUnauthorizedAccessMessage($method) { + if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) { + return parent::getExpectedUnauthorizedAccessMessage($method); + } + + switch ($method) { + case 'GET': + return "The 'access news feeds' permission is required."; + case 'POST': + case 'PATCH': + case 'DELETE': + return "The 'administer news feeds' permission is required."; + default: + return parent::getExpectedUnauthorizedAccessMessage($method); + } + } + +} -- GitLab