From 8419d5e2ebf78daa096cb643b4ccea704d76b32c Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 28 Mar 2017 18:43:26 +0100
Subject: [PATCH] Issue #2843752 by arshadcn, Wim Leers: EntityResource:
 Provide comprehensive test coverage for Item entity

---
 .../Item/ItemHalJsonAnonTest.php              |  29 +++
 .../Item/ItemHalJsonBasicAuthTest.php         |  34 ++++
 .../Item/ItemHalJsonCookieTest.php            |  34 ++++
 .../Item/ItemHalJsonTestBase.php              | 100 ++++++++++
 .../EntityResource/EntityResourceTestBase.php |   7 +-
 .../EntityResource/Item/ItemJsonAnonTest.php  |  24 +++
 .../Item/ItemJsonBasicAuthTest.php            |  34 ++++
 .../Item/ItemJsonCookieTest.php               |  29 +++
 .../Item/ItemResourceTestBase.php             | 177 ++++++++++++++++++
 9 files changed, 466 insertions(+), 2 deletions(-)
 create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonAnonTest.php
 create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonBasicAuthTest.php
 create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonCookieTest.php
 create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonTestBase.php
 create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonAnonTest.php
 create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonBasicAuthTest.php
 create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonCookieTest.php
 create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/Item/ItemResourceTestBase.php

diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonAnonTest.php
new file mode 100644
index 000000000000..6c3515968528
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonAnonTest.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\Item;
+
+use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
+
+/**
+ * @group hal
+ */
+class ItemHalJsonAnonTest extends ItemHalJsonTestBase {
+
+  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/Item/ItemHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonBasicAuthTest.php
new file mode 100644
index 000000000000..2c96fbc68ac6
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonBasicAuthTest.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\Item;
+
+use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
+
+/**
+ * @group hal
+ */
+class ItemHalJsonBasicAuthTest extends ItemHalJsonTestBase {
+
+  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/Item/ItemHalJsonCookieTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonCookieTest.php
new file mode 100644
index 000000000000..9c39918b5551
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonCookieTest.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\Item;
+
+use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
+
+/**
+ * @group hal
+ */
+class ItemHalJsonCookieTest extends ItemHalJsonTestBase {
+
+  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/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonTestBase.php b/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonTestBase.php
new file mode 100644
index 000000000000..24157e597904
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonTestBase.php
@@ -0,0 +1,100 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\Item;
+
+use Drupal\aggregator\Entity\Feed;
+use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
+use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
+use Drupal\Tests\rest\Functional\EntityResource\Item\ItemResourceTestBase;
+
+/**
+ * ResourceTestBase for Item entity.
+ */
+abstract class ItemHalJsonTestBase extends ItemResourceTestBase {
+
+  use HalEntityNormalizationTrait;
+  use AnonResourceTestTrait;
+
+  /**
+   * {@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);
+    $feed = Feed::load($this->entity->getFeedId());
+
+    return $normalization + [
+      '_embedded' => [
+        $this->baseUrl . '/rest/relation/aggregator_item/aggregator_item/fid' => [
+          [
+            '_links' => [
+              'self' => [
+                'href' => $this->baseUrl . '/aggregator/sources/1?_format=hal_json',
+              ],
+              'type' => [
+                'href' => $this->baseUrl . '/rest/type/aggregator_feed/aggregator_feed',
+              ],
+            ],
+            'uuid' => [
+              [
+                'value' => $feed->uuid(),
+              ],
+            ],
+          ],
+        ],
+      ],
+      '_links' => [
+        'self' => [
+          'href' => '',
+        ],
+        'type' => [
+          'href' => $this->baseUrl . '/rest/type/aggregator_item/aggregator_item',
+        ],
+        $this->baseUrl . '/rest/relation/aggregator_item/aggregator_item/fid' => [
+          [
+            'href' => $this->baseUrl . '/aggregator/sources/' . $feed->id() . '?_format=hal_json',
+          ],
+        ],
+      ],
+    ];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getNormalizedPostEntity() {
+    return parent::getNormalizedPostEntity() + [
+      '_links' => [
+        'type' => [
+          'href' => $this->baseUrl . '/rest/type/aggregator_item/aggregator_item',
+        ],
+      ],
+    ];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getExpectedCacheContexts() {
+    return [
+      'url.site',
+      'user.permissions',
+    ];
+  }
+
+}
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
index 4a8c304fac08..53796c8aeaf4 100644
--- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
+++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
@@ -695,8 +695,11 @@ public function testPost() {
 
 
     // DX: 422 when invalid entity: UUID field too long.
-    $response = $this->request('POST', $url, $request_options);
-    $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nuuid.0.value: UUID: may not be longer than 128 characters.\n", $response);
+    // @todo Fix this in https://www.drupal.org/node/2149851.
+    if ($this->entity->getEntityType()->hasKey('uuid')) {
+      $response = $this->request('POST', $url, $request_options);
+      $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nuuid.0.value: UUID: may not be longer than 128 characters.\n", $response);
+    }
 
 
     $request_options[RequestOptions::BODY] = $parseable_invalid_request_body_3;
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonAnonTest.php
new file mode 100644
index 000000000000..dac1b0a6d01c
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonAnonTest.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\Item;
+
+use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
+
+/**
+ * @group rest
+ */
+class ItemJsonAnonTest extends ItemResourceTestBase {
+
+  use AnonResourceTestTrait;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $format = 'json';
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $mimeType = 'application/json';
+
+}
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonBasicAuthTest.php
new file mode 100644
index 000000000000..19b6179c7bf2
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonBasicAuthTest.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\Item;
+
+use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
+
+/**
+ * @group rest
+ */
+class ItemJsonBasicAuthTest extends ItemResourceTestBase {
+
+  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/Item/ItemJsonCookieTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonCookieTest.php
new file mode 100644
index 000000000000..1c149d7c62ca
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemJsonCookieTest.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\Item;
+
+use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
+
+/**
+ * @group rest
+ */
+class ItemJsonCookieTest extends ItemResourceTestBase {
+
+  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/Item/ItemResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemResourceTestBase.php
new file mode 100644
index 000000000000..9b72b66142e2
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemResourceTestBase.php
@@ -0,0 +1,177 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\Item;
+
+use Drupal\aggregator\Entity\Feed;
+use Drupal\aggregator\Entity\Item;
+use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
+
+/**
+ * ResourceTestBase for Item entity.
+ */
+abstract class ItemResourceTestBase extends EntityResourceTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static $modules = ['aggregator'];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $entityTypeId = 'aggregator_item';
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $patchProtectedFieldNames = [];
+
+  /**
+   * The Item entity.
+   *
+   * @var \Drupal\aggregator\ItemInterface
+   */
+  protected $entity;
+
+  /**
+   * {@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}
+   */
+  protected function createEntity() {
+    // Create a "Camelids" feed.
+    $feed = Feed::create([
+      'title' => 'Camelids',
+      'url' => 'https://groups.drupal.org/not_used/167169',
+      'refresh' => 900,
+      'checked' => 1389919932,
+      'description' => 'Drupal Core Group feed',
+    ]);
+    $feed->save();
+
+    // Create a "Llama" item.
+    $item = Item::create();
+    $item->setTitle('Llama')
+      ->setFeedId($feed->id())
+      ->setLink('https://www.drupal.org/')
+      ->setPostedTime(123456789)
+      ->save();
+
+    return $item;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getExpectedNormalizedEntity() {
+    $feed = Feed::load($this->entity->getFeedId());
+
+    return [
+      'iid' => [
+        [
+          'value' => 1,
+        ],
+      ],
+      'langcode' => [
+        [
+          'value' => 'en',
+        ],
+      ],
+      'fid' => [
+        [
+          'target_id' => 1,
+          'target_type' => 'aggregator_feed',
+          'target_uuid' => $feed->uuid(),
+          'url' => base_path() . 'aggregator/sources/1',
+        ],
+      ],
+      'title' => [
+        [
+          'value' => 'Llama',
+        ],
+      ],
+      'link' => [
+        [
+          'value' => 'https://www.drupal.org/',
+        ],
+      ],
+      'author' => [],
+      'description' => [],
+      'timestamp' => [
+        [
+          'value' => 123456789,
+        ],
+      ],
+      'guid' => [],
+    ];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getNormalizedPostEntity() {
+    return [
+      'fid' => [
+        [
+          'target_id' => 1,
+        ],
+      ],
+      'title' => [
+        [
+          'value' => 'Llama',
+        ],
+      ],
+      'link' => [
+        [
+          'value' => 'https://www.drupal.org/',
+        ],
+      ],
+    ];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getExpectedCacheContexts() {
+    // @see ::createEntity()
+    return ['user.permissions'];
+  }
+
+  /**
+   * {@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