Skip to content
Snippets Groups Projects
Commit e1524ff3 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2843750 by vaplas, sumanthkumarc, Wim Leers: EntityResource: Provide...

Issue #2843750 by vaplas, sumanthkumarc, Wim Leers: EntityResource: Provide comprehensive test coverage for Shortcut entity
parent 59554628
No related branches found
No related tags found
No related merge requests found
Showing with 367 additions and 1 deletion
<?php
namespace Drupal\Tests\hal\Functional\EntityResource\Shortcut;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
use Drupal\Tests\rest\Functional\EntityResource\Shortcut\ShortcutResourceTestBase;
/**
* @group hal
*/
class ShortcutHalJsonAnonTest extends ShortcutResourceTestBase {
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/config/user-interface/shortcut/link/1?_format=hal_json',
],
'type' => [
'href' => $this->baseUrl . '/rest/type/shortcut/default',
],
],
];
}
/**
* {@inheritdoc}
*/
protected function getNormalizedPostEntity() {
return parent::getNormalizedPostEntity() + [
'_links' => [
'type' => [
'href' => $this->baseUrl . '/rest/type/shortcut/default',
],
],
];
}
/**
* {@inheritdoc}
*/
protected function getExpectedCacheContexts() {
// The 'url.site' cache context is added for '_links' in the response.
return Cache::mergeContexts(parent::getExpectedCacheContexts(), ['url.site']);
}
}
<?php
namespace Drupal\Tests\hal\Functional\EntityResource\Shortcut;
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
/**
* @group hal
*/
class ShortcutHalJsonBasicAuthTest extends ShortcutHalJsonAnonTest {
use BasicAuthResourceTestTrait;
/**
* {@inheritdoc}
*/
public static $modules = ['basic_auth'];
/**
* {@inheritdoc}
*/
protected static $auth = 'basic_auth';
}
<?php
namespace Drupal\Tests\hal\Functional\EntityResource\Shortcut;
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
/**
* @group hal
*/
class ShortcutHalJsonCookieTest extends ShortcutHalJsonAnonTest {
use CookieResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $auth = 'cookie';
}
<?php
namespace Drupal\Tests\rest\Functional\EntityResource\Shortcut;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
/**
* @group rest
*/
class ShortcutJsonAnonTest extends ShortcutResourceTestBase {
use AnonResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'json';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/json';
}
<?php
namespace Drupal\Tests\rest\Functional\EntityResource\Shortcut;
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
/**
* @group rest
*/
class ShortcutJsonBasicAuthTest extends ShortcutResourceTestBase {
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';
}
<?php
namespace Drupal\Tests\rest\Functional\EntityResource\Shortcut;
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
/**
* @group rest
*/
class ShortcutJsonCookieTest extends ShortcutResourceTestBase {
use CookieResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'json';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $auth = 'cookie';
}
<?php
namespace Drupal\Tests\rest\Functional\EntityResource\Shortcut;
use Drupal\shortcut\Entity\Shortcut;
use Drupal\shortcut\Entity\ShortcutSet;
use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
/**
* ResourceTestBase for Shortcut entity.
*/
abstract class ShortcutResourceTestBase extends EntityResourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['comment', 'shortcut'];
/**
* {@inheritdoc}
*/
protected static $entityTypeId = 'shortcut';
/**
* {@inheritdoc}
*/
protected static $patchProtectedFieldNames = [];
/**
* The Shortcut entity.
*
* @var \Drupal\shortcut\ShortcutInterface
*/
protected $entity;
/**
* {@inheritdoc}
*/
protected function setUpAuthorization($method) {
switch ($method) {
case 'GET':
case 'POST':
case 'PATCH':
case 'DELETE':
$this->grantPermissionsToTestedRole(['access shortcuts', 'customize shortcut links']);
break;
}
}
/**
* {@inheritdoc}
*/
protected function createEntity() {
// Create shortcut.
$shortcut = Shortcut::create([
'shortcut_set' => 'default',
'title' => t('Comments'),
'weight' => -20,
'link' => [
'uri' => 'internal:/admin/content/comment',
],
]);
$shortcut->save();
return $shortcut;
}
/**
* {@inheritdoc}
*/
protected function getExpectedNormalizedEntity() {
return [
'uuid' => [
[
'value' => $this->entity->uuid(),
],
],
'id' => [
[
'value' => (int) $this->entity->id(),
],
],
'title' => [
[
'value' => 'Comments',
],
],
'shortcut_set' => [
[
'target_id' => 'default',
'target_type' => 'shortcut_set',
'target_uuid' => ShortcutSet::load('default')->uuid(),
],
],
'link' => [
[
'uri' => 'internal:/admin/content/comment',
'title' => NULL,
'options' => [],
],
],
'weight' => [
[
'value' => -20,
],
],
'langcode' => [
[
'value' => 'en',
],
],
'default_langcode' => [
[
'value' => TRUE,
],
],
];
}
/**
* {@inheritdoc}
*/
protected function getNormalizedPostEntity() {
return [
'title' => [
[
'value' => 'Comments',
],
],
'link' => [
[
'uri' => 'internal:/',
],
],
'shortcut_set' => 'default',
];
}
/**
* {@inheritdoc}
*/
protected function getExpectedUnauthorizedAccessMessage($method) {
if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
return parent::getExpectedUnauthorizedAccessMessage($method);
}
switch ($method) {
case 'GET':
case 'POST':
case 'PATCH':
case 'DELETE':
return "The shortcut set must be the currently displayed set for the user and the user must have 'access shortcuts' AND 'customize shortcut links' permissions.";
default:
return parent::getExpectedUnauthorizedAccessMessage($method);
}
}
}
......@@ -65,7 +65,11 @@ function shortcut_set_edit_access(ShortcutSetInterface $shortcut_set = NULL) {
// Sufficiently-privileged users can edit their currently displayed shortcut
// set, but not other sets. They must also be able to access shortcuts.
$may_edit_current_shortcut_set = $account->hasPermission('customize shortcut links') && (!isset($shortcut_set) || $shortcut_set == shortcut_current_displayed_set()) && $account->hasPermission('access shortcuts');
return AccessResult::allowedIf($may_edit_current_shortcut_set)->cachePerPermissions();
$result = AccessResult::allowedIf($may_edit_current_shortcut_set)->cachePerPermissions();
if (!$result->isAllowed()) {
$result->setReason("The shortcut set must be the currently displayed set for the user and the user must have 'access shortcuts' AND 'customize shortcut links' permissions.");
}
return $result;
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment