View source
<?php
namespace Drupal\Tests\aggregator\Functional\Rest;
use Drupal\aggregator\Entity\Feed;
use Drupal\aggregator\Entity\Item;
use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
abstract class ItemResourceTestBase extends EntityResourceTestBase {
use BcTimestampNormalizerUnixTestTrait;
public static $modules = [
'aggregator',
];
protected static $entityTypeId = 'aggregator_item';
protected static $patchProtectedFieldNames = [];
protected $entity;
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;
}
}
protected function createEntity() {
$feed = Feed::create([
'title' => 'Camelids',
'url' => 'https://groups.drupal.org/not_used/167169',
'refresh' => 900,
'checked' => 1389919932,
'description' => 'Drupal Core Group feed',
]);
$feed
->save();
$item = Item::create();
$item
->setTitle('Llama')
->setFeedId($feed
->id())
->setLink('https://www.drupal.org/')
->setPostedTime(123456789)
->save();
return $item;
}
protected function createAnotherEntity() {
$entity = $this->entity
->createDuplicate();
$entity
->setLink('https://www.example.org/');
$label_key = $entity
->getEntityType()
->getKey('label');
if ($label_key) {
$entity
->set($label_key, $entity
->label() . '_dupe');
}
$entity
->save();
return $entity;
}
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' => [
$this
->formatExpectedTimestampItemValues(123456789),
],
'guid' => [],
];
}
protected function getNormalizedPostEntity() {
return [
'fid' => [
[
'target_id' => 1,
],
],
'title' => [
[
'value' => 'Llama',
],
],
'link' => [
[
'value' => 'https://www.drupal.org/',
],
],
];
}
protected function getExpectedCacheContexts() {
return [
'user.permissions',
];
}
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);
}
}
}