From e4461e6dc8cb7198d0fd39d188a43d29174748d0 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 7 Mar 2017 16:47:20 +0000
Subject: [PATCH] Issue #2832859 by vaplas, webflo, harings_rob, Wim Leers:
 Write EntityResourceTestBase subclasses for: MenuLinkContent

---
 .../MenuLinkContentHalJsonAnonTest.php        |  74 +++++++
 .../MenuLinkContentHalJsonBasicAuthTest.php   |  24 +++
 .../MenuLinkContentHalJsonCookieTest.php      |  19 ++
 .../MenuLinkContentAccessControlHandler.php   |   2 +-
 .../EntityResource/EntityResourceTestBase.php |   4 +-
 .../MenuLinkContentJsonAnonTest.php           |  24 +++
 .../MenuLinkContentJsonBasicAuthTest.php      |  34 +++
 .../MenuLinkContentJsonCookieTest.php         |  29 +++
 .../MenuLinkContentResourceTestBase.php       | 193 ++++++++++++++++++
 9 files changed, 400 insertions(+), 3 deletions(-)
 create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentHalJsonAnonTest.php
 create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentHalJsonBasicAuthTest.php
 create mode 100644 core/modules/hal/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentHalJsonCookieTest.php
 create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentJsonAnonTest.php
 create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentJsonBasicAuthTest.php
 create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentJsonCookieTest.php
 create mode 100644 core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentResourceTestBase.php

diff --git a/core/modules/hal/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentHalJsonAnonTest.php
new file mode 100644
index 000000000000..c4c8d943f235
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentHalJsonAnonTest.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\MenuLinkContent;
+
+use Drupal\Core\Cache\Cache;
+use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
+use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
+use Drupal\Tests\rest\Functional\EntityResource\MenuLinkContent\MenuLinkContentResourceTestBase;
+
+/**
+ * @group hal
+ */
+class MenuLinkContentHalJsonAnonTest extends MenuLinkContentResourceTestBase {
+
+  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);
+
+    return $normalization + [
+      '_links' => [
+        'self' => [
+          'href' => $this->baseUrl . '/admin/structure/menu/item/1/edit?_format=hal_json',
+        ],
+        'type' => [
+          'href' => $this->baseUrl . '/rest/type/menu_link_content/menu_link_content',
+        ],
+      ],
+    ];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getNormalizedPostEntity() {
+    return parent::getNormalizedPostEntity() + [
+      '_links' => [
+        'type' => [
+          'href' => $this->baseUrl . '/rest/type/menu_link_content/menu_link_content',
+        ],
+      ],
+    ];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getExpectedCacheContexts() {
+    // The 'url.site' cache context is added for '_links' in the response.
+    return Cache::mergeTags(parent::getExpectedCacheContexts(), ['url.site']);
+  }
+
+}
diff --git a/core/modules/hal/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentHalJsonBasicAuthTest.php b/core/modules/hal/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentHalJsonBasicAuthTest.php
new file mode 100644
index 000000000000..3af8362e6b3e
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentHalJsonBasicAuthTest.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\MenuLinkContent;
+
+use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
+
+/**
+ * @group hal
+ */
+class MenuLinkContentHalJsonBasicAuthTest extends MenuLinkContentHalJsonAnonTest {
+
+  use BasicAuthResourceTestTrait;
+
+  /**
+   * {@inheritdoc}
+   */
+  public static $modules = ['basic_auth'];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $auth = 'basic_auth';
+
+}
diff --git a/core/modules/hal/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentHalJsonCookieTest.php b/core/modules/hal/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentHalJsonCookieTest.php
new file mode 100644
index 000000000000..1bcc1f83f883
--- /dev/null
+++ b/core/modules/hal/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentHalJsonCookieTest.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Drupal\Tests\hal\Functional\EntityResource\MenuLinkContent;
+
+use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
+
+/**
+ * @group hal
+ */
+class MenuLinkContentHalJsonCookieTest extends MenuLinkContentHalJsonAnonTest {
+
+  use CookieResourceTestTrait;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $auth = 'cookie';
+
+}
diff --git a/core/modules/menu_link_content/src/MenuLinkContentAccessControlHandler.php b/core/modules/menu_link_content/src/MenuLinkContentAccessControlHandler.php
index c9771d418eac..c69349e9e934 100644
--- a/core/modules/menu_link_content/src/MenuLinkContentAccessControlHandler.php
+++ b/core/modules/menu_link_content/src/MenuLinkContentAccessControlHandler.php
@@ -56,7 +56,7 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
 
       case 'update':
         if (!$account->hasPermission('administer menu')) {
-          return AccessResult::neutral()->cachePerPermissions();
+          return AccessResult::neutral("The 'administer menu' permission is required.")->cachePerPermissions();
         }
         else {
           // If there is a URL, this is an external link so always accessible.
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
index bbdc9cefe38e..822d8d7d26d4 100644
--- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
+++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
@@ -688,7 +688,7 @@ public function testPost() {
     // DX: 422 when invalid entity: multiple values sent for single-value field.
     $response = $this->request('POST', $url, $request_options);
     $label_field = $this->entity->getEntityType()->hasKey('label') ? $this->entity->getEntityType()->getKey('label') : static::$labelFieldName;
-    $label_field_capitalized = ucfirst($label_field);
+    $label_field_capitalized = $this->entity->getFieldDefinition($label_field)->getLabel();
     $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\n$label_field: $label_field_capitalized: this field cannot hold more than 1 values.\n", $response);
 
 
@@ -877,7 +877,7 @@ public function testPatch() {
     // DX: 422 when invalid entity: multiple values sent for single-value field.
     $response = $this->request('PATCH', $url, $request_options);
     $label_field = $this->entity->getEntityType()->hasKey('label') ? $this->entity->getEntityType()->getKey('label') : static::$labelFieldName;
-    $label_field_capitalized = ucfirst($label_field);
+    $label_field_capitalized = $this->entity->getFieldDefinition($label_field)->getLabel();
     $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\n$label_field: $label_field_capitalized: this field cannot hold more than 1 values.\n", $response);
 
 
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentJsonAnonTest.php b/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentJsonAnonTest.php
new file mode 100644
index 000000000000..219cc53c8782
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentJsonAnonTest.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\MenuLinkContent;
+
+use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
+
+/**
+ * @group rest
+ */
+class MenuLinkContentJsonAnonTest extends MenuLinkContentResourceTestBase {
+
+  use AnonResourceTestTrait;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $format = 'json';
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $mimeType = 'application/json';
+
+}
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentJsonBasicAuthTest.php b/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentJsonBasicAuthTest.php
new file mode 100644
index 000000000000..952088b91260
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentJsonBasicAuthTest.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\MenuLinkContent;
+
+use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
+
+/**
+ * @group rest
+ */
+class MenuLinkContentJsonBasicAuthTest extends MenuLinkContentResourceTestBase {
+
+  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/MenuLinkContent/MenuLinkContentJsonCookieTest.php b/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentJsonCookieTest.php
new file mode 100644
index 000000000000..73b45672ee0c
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentJsonCookieTest.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\MenuLinkContent;
+
+use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
+
+/**
+ * @group rest
+ */
+class MenuLinkContentJsonCookieTest extends MenuLinkContentResourceTestBase {
+
+  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/MenuLinkContent/MenuLinkContentResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentResourceTestBase.php
new file mode 100644
index 000000000000..f907d9a59838
--- /dev/null
+++ b/core/modules/rest/tests/src/Functional/EntityResource/MenuLinkContent/MenuLinkContentResourceTestBase.php
@@ -0,0 +1,193 @@
+<?php
+
+namespace Drupal\Tests\rest\Functional\EntityResource\MenuLinkContent;
+
+use Drupal\menu_link_content\Entity\MenuLinkContent;
+use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
+
+/**
+ * ResourceTestBase for MenuLinkContent entity.
+ */
+abstract class MenuLinkContentResourceTestBase extends EntityResourceTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static $modules = ['menu_link_content'];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $entityTypeId = 'menu_link_content';
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $patchProtectedFieldNames = [
+    'changed',
+  ];
+
+  /**
+   * The MenuLinkContent entity.
+   *
+   * @var \Drupal\menu_link_content\MenuLinkContentInterface
+   */
+  protected $entity;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUpAuthorization($method) {
+    switch ($method) {
+      case 'GET':
+      case 'POST':
+      case 'PATCH':
+      case 'DELETE':
+        $this->grantPermissionsToTestedRole(['administer menu']);
+        break;
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function createEntity() {
+    $menu_link = MenuLinkContent::create([
+      'id' => 'llama',
+      'title' => 'Llama Gabilondo',
+      'description' => 'Llama Gabilondo',
+      'link' => 'https://nl.wikipedia.org/wiki/Llama',
+      'weight' => 0,
+      'menu_name' => 'main',
+    ]);
+    $menu_link->save();
+
+    return $menu_link;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getNormalizedPostEntity() {
+    return [
+      'title' => [
+        [
+          'value' => 'Dramallama',
+        ],
+      ],
+      'link' => [
+        [
+          'uri' => 'http://www.urbandictionary.com/define.php?term=drama%20llama',
+        ],
+      ],
+      'bundle' => [
+        [
+          'value' => 'menu_link_content',
+        ],
+      ],
+    ];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getExpectedNormalizedEntity() {
+    return [
+      'uuid' => [
+        [
+          'value' => $this->entity->uuid(),
+        ],
+      ],
+      'id' => [
+        [
+          'value' => 1,
+        ],
+      ],
+      'title' => [
+        [
+          'value' => 'Llama Gabilondo',
+        ],
+      ],
+      'link' => [
+        [
+          'uri' => 'https://nl.wikipedia.org/wiki/Llama',
+          'title' => NULL,
+          'options' => [],
+        ],
+      ],
+      'weight' => [
+        [
+          'value' => 0,
+        ],
+      ],
+      'menu_name' => [
+        [
+          'value' => 'main',
+        ],
+      ],
+      'langcode' => [
+        [
+          'value' => 'en',
+        ],
+      ],
+      'bundle' => [
+        [
+          'value' => 'menu_link_content',
+        ],
+      ],
+      'description' => [
+        [
+          'value' => 'Llama Gabilondo',
+        ],
+      ],
+      'external' => [
+        [
+          'value' => FALSE,
+        ],
+      ],
+      'rediscover' => [
+        [
+          'value' => FALSE,
+        ],
+      ],
+      'expanded' => [
+        [
+          'value' => FALSE,
+        ],
+      ],
+      'enabled' => [
+        [
+          'value' => TRUE,
+        ],
+      ],
+      'changed' => [
+        [
+          'value' => $this->entity->getChangedTime(),
+        ],
+      ],
+      'default_langcode' => [
+        [
+          'value' => TRUE,
+        ],
+      ],
+      'parent' => [],
+    ];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getExpectedUnauthorizedAccessMessage($method) {
+    if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
+      return parent::getExpectedUnauthorizedAccessMessage($method);
+    }
+
+    switch ($method) {
+      case 'DELETE':
+        return "You are not authorized to delete this menu_link_content entity.";
+      default:
+        return parent::getExpectedUnauthorizedAccessMessage($method);
+    }
+  }
+
+}
-- 
GitLab