From af81a985ea11bd2d1524a5716918ec31d74a7fcb Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Tue, 4 Apr 2017 12:06:22 +0100
Subject: [PATCH] Issue #2843782 by arshadcn: EntityResource: Provide
 comprehensive test coverage for Editor entity

---
 .../Editor/EditorHalJsonAnonTest.php          |  30 +++
 .../Editor/EditorHalJsonBasicAuthTest.php     |  35 ++++
 .../Editor/EditorHalJsonCookieTest.php        |  35 ++++
 .../Editor/EditorJsonAnonTest.php             |  24 +++
 .../Editor/EditorJsonBasicAuthTest.php        |  34 ++++
 .../Editor/EditorJsonCookieTest.php           |  29 +++
 .../Editor/EditorResourceTestBase.php         | 184 ++++++++++++++++++
 7 files changed, 371 insertions(+)
 create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/Editor/EditorHalJsonAnonTest.php
 create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/Editor/EditorHalJsonBasicAuthTest.php
 create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/Editor/EditorHalJsonCookieTest.php
 create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorJsonAnonTest.php
 create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorJsonBasicAuthTest.php
 create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorJsonCookieTest.php
 create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorResourceTestBase.php

diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Editor/EditorHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Editor/EditorHalJsonAnonTest.php
new file mode 100644
index 000000000000..9b68c54f343a
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/Editor/EditorHalJsonAnonTest.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\Editor;
+
+use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
+use Drupal\Tests\rest\Functional\EntityResource\Editor\EditorResourceTestBase;
+
+/**
+ * @group hal
+ */
+class EditorHalJsonAnonTest extends EditorResourceTestBase {
+
+  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/Editor/EditorHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Editor/EditorHalJsonBasicAuthTest.php
new file mode 100644
index 000000000000..9a317a52fd66
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/Editor/EditorHalJsonBasicAuthTest.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\Editor;
+
+use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
+use Drupal\Tests\rest\Functional\EntityResource\Editor\EditorResourceTestBase;
+
+/**
+ * @group hal
+ */
+class EditorHalJsonBasicAuthTest extends EditorResourceTestBase {
+
+  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/Editor/EditorHalJsonCookieTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Editor/EditorHalJsonCookieTest.php
new file mode 100644
index 000000000000..a59392fe9d1f
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/Editor/EditorHalJsonCookieTest.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\Editor;
+
+use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
+use Drupal\Tests\rest\Functional\EntityResource\Editor\EditorResourceTestBase;
+
+/**
+ * @group hal
+ */
+class EditorHalJsonCookieTest extends EditorResourceTestBase {
+
+  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/Editor/EditorJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorJsonAnonTest.php
new file mode 100644
index 000000000000..b01ac2ac46d5
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorJsonAnonTest.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\Editor;
+
+use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
+
+/**
+ * @group rest
+ */
+class EditorJsonAnonTest extends EditorResourceTestBase {
+
+  use AnonResourceTestTrait;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $format = 'json';
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $mimeType = 'application/json';
+
+}
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorJsonBasicAuthTest.php
new file mode 100644
index 000000000000..265bd48ee0d2
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorJsonBasicAuthTest.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\Editor;
+
+use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
+
+/**
+ * @group rest
+ */
+class EditorJsonBasicAuthTest extends EditorResourceTestBase {
+
+  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/Editor/EditorJsonCookieTest.php b/core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorJsonCookieTest.php
new file mode 100644
index 000000000000..b4f509a2eeb2
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorJsonCookieTest.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\Editor;
+
+use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
+
+/**
+ * @group rest
+ */
+class EditorJsonCookieTest extends EditorResourceTestBase {
+
+  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/Editor/EditorResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorResourceTestBase.php
new file mode 100644
index 000000000000..4eb4a6e1b0c2
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/Editor/EditorResourceTestBase.php
@@ -0,0 +1,184 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\Editor;
+
+use Drupal\editor\Entity\Editor;
+use Drupal\filter\Entity\FilterFormat;
+use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
+
+/**
+ * ResourceTestBase for Editor entity.
+ */
+abstract class EditorResourceTestBase extends EntityResourceTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static $modules = ['ckeditor', 'editor'];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $entityTypeId = 'editor';
+
+  /**
+   * The Editor entity.
+   *
+   * @var \Drupal\editor\EditorInterface
+   */
+  protected $entity;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUpAuthorization($method) {
+    $this->grantPermissionsToTestedRole(['administer filters']);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function createEntity() {
+    // Create a "Llama" filter format.
+    $llama_format = FilterFormat::create([
+      'name' => 'Llama',
+      'format' => 'llama',
+      'langcode' => 'es',
+      'filters' => [
+        'filter_html' => [
+          'status' => TRUE,
+          'settings' => [
+            'allowed_html' => '<p> <a> <b> <lo>',
+          ],
+        ],
+      ],
+    ]);
+
+    $llama_format->save();
+
+    // Create a "Camelids" editor.
+    $camelids = Editor::create([
+      'format' => 'llama',
+      'editor' => 'ckeditor',
+    ]);
+    $camelids
+      ->setImageUploadSettings([
+        'status' => FALSE,
+        'scheme' => file_default_scheme(),
+        'directory' => 'inline-images',
+        'max_size' => '',
+        'max_dimensions' => [
+          'width' => '',
+          'height' => '',
+        ],
+      ])
+      ->save();
+
+    return $camelids;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getExpectedNormalizedEntity() {
+    return [
+      'dependencies' => [
+        'config' => [
+          'filter.format.llama',
+        ],
+        'module' => [
+          'ckeditor',
+        ],
+      ],
+      'editor' => 'ckeditor',
+      'format' => 'llama',
+      'image_upload' => [
+        'status' => FALSE,
+        'scheme' => 'public',
+        'directory' => 'inline-images',
+        'max_size' => '',
+        'max_dimensions' => [
+          'width' => NULL,
+          'height' => NULL,
+        ],
+      ],
+      'langcode' => 'en',
+      'settings' => [
+        'toolbar' => [
+          'rows' => [
+            [
+              [
+                'name' => 'Formatting',
+                'items' => [
+                  'Bold',
+                  'Italic',
+                ],
+              ],
+              [
+                'name' => 'Links',
+                'items' => [
+                  'DrupalLink',
+                  'DrupalUnlink',
+                ],
+              ],
+              [
+                'name' => 'Lists',
+                'items' => [
+                  'BulletedList',
+                  'NumberedList',
+                ],
+              ],
+              [
+                'name' => 'Media',
+                'items' => [
+                  'Blockquote',
+                  'DrupalImage',
+                ],
+              ],
+              [
+                'name' => 'Tools',
+                'items' => [
+                  'Source',
+                ],
+              ],
+            ],
+          ],
+        ],
+        'plugins' => [
+          'language' => [
+            'language_list' => 'un',
+          ],
+        ],
+      ],
+      'status' => TRUE,
+      'uuid' => $this->entity->uuid(),
+    ];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getNormalizedPostEntity() {
+    // @todo Update in https://www.drupal.org/node/2300677.
+  }
+
+  /**
+   * {@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);
+    }
+
+    return "The 'administer filters' permission is required.";
+  }
+
+}
-- 
GitLab