Skip to content

Instantly share code, notes, and snippets.

@vijaycs85
Created December 14, 2016 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vijaycs85/849efc59b700c6de1cb6e9426ad0182c to your computer and use it in GitHub Desktop.
Save vijaycs85/849efc59b700c6de1cb6e9426ad0182c to your computer and use it in GitHub Desktop.
diff --git a/core/modules/hal/tests/src/Functional/EntityResource/BlockContent/BlockContentHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/BlockContent/BlockContentHalJsonAnonTest.php
new file mode 100644
index 0000000..996da5e
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/BlockContent/BlockContentHalJsonAnonTest.php
@@ -0,0 +1,136 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\BlockContent;
+
+use Drupal\Core\Cache\Cache;
+use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
+use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
+use Drupal\Tests\rest\Functional\EntityResource\BlockContent\BlockContentResourceTestBase;
+use Drupal\user\Entity\User;
+
+/**
+ * @group hal
+ */
+class BlockContentHalJsonAnonTest extends BlockContentResourceTestBase {
+
+ use HalEntityNormalizationTrait;
+ use AnonResourceTestTrait;
+
+ /**
+ * {@inheritdoc}
+ */
+ public static $modules = ['hal'];
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $format = 'hal_json';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $mimeType = 'application/hal+json';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $expectedErrorMimeType = 'application/json';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $patchProtectedFieldNames = [
+ 'created',
+ 'changed',
+ 'revision_id',
+ 'revision_created',
+ 'revision_user',
+ ];
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function getExpectedNormalizedEntity() {
+ $default_normalization = parent::getExpectedNormalizedEntity();
+
+ $normalization = $this->applyHalFieldNormalization($default_normalization);
+
+ $author = User::load($this->entity->getOwnerId());
+ return $normalization + [
+ '_links' => [
+ 'self' => [
+ 'href' => $this->baseUrl . '/block_content/1?_format=hal_json',
+ ],
+ 'type' => [
+ 'href' => $this->baseUrl . '/rest/type/block_content/camelids',
+ ],
+ $this->baseUrl . '/rest/relation/block_content/camelids/uid' => [
+ [
+ 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
+ 'lang' => 'en',
+ ],
+ ],
+ $this->baseUrl . '/rest/relation/block_content/camelids/revision_uid' => [
+ [
+ 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
+ ],
+ ],
+ ],
+ '_embedded' => [
+ $this->baseUrl . '/rest/relation/block_content/camelids/uid' => [
+ [
+ '_links' => [
+ 'self' => [
+ 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
+ ],
+ 'type' => [
+ 'href' => $this->baseUrl . '/rest/type/user/user',
+ ],
+ ],
+ 'uuid' => [
+ ['value' => $author->uuid()]
+ ],
+ 'lang' => 'en',
+ ],
+ ],
+ $this->baseUrl . '/rest/relation/block_content/camelids/revision_uid' => [
+ [
+ '_links' => [
+ 'self' => [
+ 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
+ ],
+ 'type' => [
+ 'href' => $this->baseUrl . '/rest/type/user/user',
+ ],
+ ],
+ 'uuid' => [
+ ['value' => $author->uuid()]
+ ],
+ ],
+ ],
+ ],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function getNormalizedPostEntity() {
+ return parent::getNormalizedPostEntity() + [
+ '_links' => [
+ 'type' => [
+ 'href' => $this->baseUrl . '/rest/type/block_content/camelids',
+ ],
+ ],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function getExpectedCacheContexts() {
+ // The 'url.site' cache context is added for '_links' in the response.
+ return Cache::mergeContexts(parent::getExpectedCacheContexts(), ['url.site']);
+ }
+
+}
diff --git a/core/modules/hal/tests/src/Functional/EntityResource/BlockContent/BlockContentHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/BlockContent/BlockContentHalJsonBasicAuthTest.php
new file mode 100644
index 0000000..0202280
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/BlockContent/BlockContentHalJsonBasicAuthTest.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\BlockContent;
+
+use Drupal\Tests\hal\Functional\HalJsonBasicAuthWorkaroundFor2805281Trait;
+use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
+
+/**
+ * @group hal
+ */
+class BlockContentHalJsonBasicAuthTest extends BlockContentHalJsonAnonTest {
+
+ use BasicAuthResourceTestTrait;
+
+ /**
+ * {@inheritdoc}
+ */
+ public static $modules = ['basic_auth'];
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $auth = 'basic_auth';
+
+ // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage.
+ use HalJsonBasicAuthWorkaroundFor2805281Trait {
+ HalJsonBasicAuthWorkaroundFor2805281Trait::assertResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait;
+ }
+
+}
diff --git a/core/modules/hal/tests/src/Functional/EntityResource/BlockContent/BlockContentHalJsonCookieTest.php b/core/modules/hal/tests/src/Functional/EntityResource/BlockContent/BlockContentHalJsonCookieTest.php
new file mode 100644
index 0000000..9cc0f25
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/BlockContent/BlockContentHalJsonCookieTest.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\BlockContent;
+
+use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
+
+/**
+ * @group hal
+ */
+class BlockContentHalJsonCookieTest extends BlockContentHalJsonAnonTest {
+
+ use CookieResourceTestTrait;
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $auth = 'cookie';
+
+}
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentJsonAnonTest.php
new file mode 100644
index 0000000..a5dae2e
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentJsonAnonTest.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\BlockContent;
+
+use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
+
+/**
+ * @group rest
+ */
+class BlockContentJsonAnonTest extends BlockContentResourceTestBase {
+
+ use AnonResourceTestTrait;
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $format = 'json';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $mimeType = 'application/json';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $expectedErrorMimeType = 'application/json';
+
+}
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentJsonBasicAuthTest.php
new file mode 100644
index 0000000..0884adb
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentJsonBasicAuthTest.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\BlockContent;
+
+use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
+use Drupal\Tests\rest\Functional\JsonBasicAuthWorkaroundFor2805281Trait;
+
+/**
+ * @group rest
+ */
+class BlockContentJsonBasicAuthTest extends BlockContentResourceTestBase {
+
+ use BasicAuthResourceTestTrait;
+
+ /**
+ * {@inheritdoc}
+ */
+ public static $modules = ['basic_auth'];
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $format = 'json';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $mimeType = 'application/json';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $expectedErrorMimeType = 'application/json';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $auth = 'basic_auth';
+
+ // @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage.
+ use JsonBasicAuthWorkaroundFor2805281Trait {
+ JsonBasicAuthWorkaroundFor2805281Trait::assertResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait;
+ }
+
+}
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentJsonCookieTest.php b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentJsonCookieTest.php
new file mode 100644
index 0000000..728a156
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentJsonCookieTest.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\BlockContent;
+
+use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
+
+/**
+ * @group rest
+ */
+class BlockContentJsonCookieTest extends BlockContentResourceTestBase {
+
+ use CookieResourceTestTrait;
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $format = 'json';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $mimeType = 'application/json';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $expectedErrorMimeType = 'application/json';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $auth = 'cookie';
+
+}
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php
new file mode 100644
index 0000000..8506d0f
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php
@@ -0,0 +1,199 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\BlockContent;
+
+use Drupal\block_content\Entity\BlockContent;
+use Drupal\block_content\Entity\BlockContentType;
+use Drupal\node\Entity\Node;
+use Drupal\node\Entity\NodeType;
+use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
+use Drupal\user\Entity\User;
+
+abstract class BlockContentResourceTestBase extends EntityResourceTestBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ public static $modules = ['block_content'];
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $entityTypeId = 'block_content';
+
+ /**
+ * {@inheritdoc}
+ */
+ protected static $patchProtectedFieldNames = [
+ 'id',
+ 'uuid',
+ 'type',
+ 'created',
+ 'changed',
+ 'revision_id',
+ 'revision_created',
+ 'revision_user',
+ ];
+
+ /**
+ * @var \Drupal\block_content\BlockContentInterface
+ */
+ protected $entity;
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function setUpAuthorization($method) {
+ switch ($method) {
+ case 'GET':
+ $this->grantPermissionsToTestedRole(['access content']);
+ break;
+ case 'POST':
+ case 'PATCH':
+ case 'DELETE':
+ $this->grantPermissionsToTestedRole(['administer blocks']);
+ break;
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function createEntity() {
+ if (!BlockContentType::load('camelids')) {
+ // Create a "Camelids" block type.
+ $block_content_type = BlockContentType::create([
+ 'label' => 'Camelids',
+ 'id' => 'camelids',
+ 'revision' => FALSE
+ ]);
+ $block_content_type->save();
+ block_content_add_body_field($block_content_type->id());
+ }
+
+ // Create a "Llama" block.
+ $block_content = BlockContent::create([
+ 'info' => 'Llama',
+ 'type' => 'camelids',
+ 'body' => array(
+ 'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
+ 'format' => 'plain_text',
+ ),
+ ]);
+ $block_content->save();
+
+ return $block_content;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function getExpectedNormalizedEntity() {
+ $author = User::load($this->entity->getOwnerId());
+ return [
+ 'nid' => [
+ ['value' => 1],
+ ],
+ 'uuid' => [
+ ['value' => $this->entity->uuid()],
+ ],
+ 'vid' => [
+ ['value' => 1],
+ ],
+ 'langcode' => [
+ [
+ 'value' => 'en',
+ ],
+ ],
+ 'type' => [
+ [
+ 'target_id' => 'camelids',
+ 'target_type' => 'block_content_type',
+ 'target_uuid' =>BlockContentType::load('camelids')->uuid(),
+ ],
+ ],
+ 'title' => [
+ [
+ 'value' => 'Llama',
+ ],
+ ],
+ 'status' => [
+ [
+ 'value' => 1,
+ ],
+ ],
+ 'created' => [
+ [
+ 'value' => '123456789',
+ ],
+ ],
+ 'changed' => [
+ [
+ 'value' => '123456789',
+ ],
+ ],
+ 'promote' => [
+ [
+ 'value' => 1,
+ ],
+ ],
+ 'sticky' => [
+ [
+ 'value' => '0',
+ ],
+ ],
+ 'revision_timestamp' => [
+ [
+ 'value' => '123456789',
+ ],
+ ],
+ 'revision_translation_affected' => [
+ [
+ 'value' => TRUE,
+ ],
+ ],
+ 'default_langcode' => [
+ [
+ 'value' => TRUE,
+ ],
+ ],
+ 'uid' => [
+ [
+ 'target_id' => $author->id(),
+ 'target_type' => 'user',
+ 'target_uuid' => $author->uuid(),
+ 'url' => base_path() . 'user/' . $author->id(),
+ ],
+ ],
+ 'revision_uid' => [
+ [
+ 'target_id' => $author->id(),
+ 'target_type' => 'user',
+ 'target_uuid' => $author->uuid(),
+ 'url' => base_path() . 'user/' . $author->id(),
+ ],
+ ],
+ 'revision_log' => [
+ ],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function getNormalizedPostEntity() {
+ return [
+ 'type' => [
+ [
+ 'target_id' => 'camelids',
+ ],
+ ],
+ 'title' => [
+ [
+ 'value' => 'Dramallama',
+ ],
+ ],
+ ];
+ }
+
+}
@vijaycs85
Copy link
Author

Questions:
a) how to decide $patchProtectedFieldNames
b) how to get getExpectedNormalizedEntity() for both modules?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment