View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\shortcut\Entity\Shortcut;
use Drupal\shortcut\Entity\ShortcutSet;
use Drupal\Tests\jsonapi\Traits\CommonCollectionFilterAccessTestPatternsTrait;
use GuzzleHttp\RequestOptions;
class ShortcutTest extends ResourceTestBase {
use CommonCollectionFilterAccessTestPatternsTrait;
public static $modules = [
'comment',
'shortcut',
];
protected static $entityTypeId = 'shortcut';
protected static $resourceTypeName = 'shortcut--default';
protected $entity;
protected static $patchProtectedFieldNames = [];
protected function setUpAuthorization($method) {
$this
->grantPermissionsToTestedRole([
'access shortcuts',
'customize shortcut links',
]);
}
protected function createEntity() {
$shortcut = Shortcut::create([
'shortcut_set' => 'default',
'title' => t('Comments'),
'weight' => -20,
'link' => [
'uri' => 'internal:/user/logout',
],
]);
$shortcut
->save();
return $shortcut;
}
protected function getExpectedDocument() {
$self_url = Url::fromUri('base:/jsonapi/shortcut/default/' . $this->entity
->uuid())
->setAbsolute()
->toString(TRUE)
->getGeneratedUrl();
return [
'jsonapi' => [
'meta' => [
'links' => [
'self' => [
'href' => 'http://jsonapi.org/format/1.0/',
],
],
],
'version' => '1.0',
],
'links' => [
'self' => [
'href' => $self_url,
],
],
'data' => [
'id' => $this->entity
->uuid(),
'type' => 'shortcut--default',
'links' => [
'self' => [
'href' => $self_url,
],
],
'attributes' => [
'title' => 'Comments',
'link' => [
'uri' => 'internal:/user/logout',
'title' => NULL,
'options' => [],
],
'langcode' => 'en',
'default_langcode' => TRUE,
'weight' => -20,
'drupal_internal__id' => (int) $this->entity
->id(),
],
'relationships' => [
'shortcut_set' => [
'data' => [
'type' => 'shortcut_set--shortcut_set',
'id' => ShortcutSet::load('default')
->uuid(),
],
'links' => [
'related' => [
'href' => $self_url . '/shortcut_set',
],
'self' => [
'href' => $self_url . '/relationships/shortcut_set',
],
],
],
],
],
];
}
protected function getPostDocument() {
return [
'data' => [
'type' => 'shortcut--default',
'attributes' => [
'title' => 'Comments',
'link' => [
'uri' => 'internal:/',
],
],
],
];
}
protected function getExpectedUnauthorizedAccessMessage($method) {
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.";
}
public function testPostIndividual() {
$this
->markTestSkipped('Disabled until https://www.drupal.org/project/drupal/issues/2982060 is fixed.');
}
public function testRelationships() {
$this
->markTestSkipped('Disabled until https://www.drupal.org/project/drupal/issues/2982060 is fixed.');
}
public function testPatchIndividual() {
$this
->markTestSkipped('Disabled until https://www.drupal.org/project/drupal/issues/2982060 is fixed.');
}
public function testCollectionFilterAccess() {
$label_field_name = 'title';
$this
->grantPermissionsToTestedRole([
'customize shortcut links',
]);
$this
->doTestCollectionFilterAccessBasedOnPermissions($label_field_name, 'access shortcuts');
$alternate_shortcut_set = ShortcutSet::create([
'id' => 'alternate',
'label' => 'Alternate',
]);
$alternate_shortcut_set
->save();
$this->entity->shortcut_set = $alternate_shortcut_set
->id();
$this->entity
->save();
$collection_url = Url::fromRoute('jsonapi.entity_test--bar.collection');
$collection_filter_url = $collection_url
->setOption('query', [
"filter[spotlight.{$label_field_name}]" => $this->entity
->label(),
]);
$request_options = [];
$request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
$request_options = NestedArray::mergeDeep($request_options, $this
->getAuthenticationRequestOptions());
$response = $this
->request('GET', $collection_filter_url, $request_options);
$doc = Json::decode((string) $response
->getBody());
$this
->assertCount(0, $doc['data']);
$this->container
->get('entity_type.manager')
->getStorage('shortcut_set')
->assignUser($alternate_shortcut_set, $this->account);
$response = $this
->request('GET', $collection_filter_url, $request_options);
$doc = Json::decode((string) $response
->getBody());
$this
->assertCount(1, $doc['data']);
}
protected static function getExpectedCollectionCacheability(AccountInterface $account, array $collection, array $sparse_fieldset = NULL, $filtered = FALSE) {
$cacheability = parent::getExpectedCollectionCacheability($account, $collection, $sparse_fieldset, $filtered);
if ($filtered) {
$cacheability
->addCacheContexts([
'user',
]);
}
return $cacheability;
}
}