View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\aggregator\Entity\Feed;
use Drupal\Core\Url;
use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
use Drupal\Tests\jsonapi\Traits\CommonCollectionFilterAccessTestPatternsTrait;
class FeedTest extends ResourceTestBase {
use BcTimestampNormalizerUnixTestTrait;
use CommonCollectionFilterAccessTestPatternsTrait;
public static $modules = [
'aggregator',
];
protected static $entityTypeId = 'aggregator_feed';
protected static $resourceTypeName = 'aggregator_feed--aggregator_feed';
protected $entity;
protected static $patchProtectedFieldNames = [];
protected static $uniqueFieldNames = [
'url',
];
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;
}
}
public function createEntity() {
$feed = Feed::create();
$feed
->set('fid', 1)
->setTitle('Feed')
->setUrl('http://example.com/rss.xml')
->setDescription('Feed Resource Test 1')
->setRefreshRate(900)
->setLastCheckedTime(123456789)
->setQueuedTime(123456789)
->setWebsiteUrl('http://example.com')
->setImage('http://example.com/feed_logo')
->setHash('abcdefg')
->setEtag('hijklmn')
->setLastModified(123456789)
->save();
return $feed;
}
protected function createAnotherEntity($key) {
$duplicate = $this
->getEntityDuplicate($this->entity, $key);
$duplicate
->set('field_rest_test', 'Duplicate feed entity');
$duplicate
->setUrl("http://example.com/{$key}.xml");
$duplicate
->save();
return $duplicate;
}
protected function getExpectedDocument() {
$self_url = Url::fromUri('base:/jsonapi/aggregator_feed/aggregator_feed/' . $this->entity
->uuid())
->setAbsolute()
->toString(TRUE)
->getGeneratedUrl();
return [
'jsonapi' => [
'meta' => [
'links' => [
'self' => 'http://jsonapi.org/format/1.0/',
],
],
'version' => '1.0',
],
'links' => [
'self' => $self_url,
],
'data' => [
'id' => $this->entity
->uuid(),
'type' => 'aggregator_feed--aggregator_feed',
'links' => [
'self' => $self_url,
],
'attributes' => [
'uuid' => $this->entity
->uuid(),
'fid' => 1,
'url' => 'http://example.com/rss.xml',
'title' => 'Feed',
'refresh' => 900,
'checked' => 123456789,
'queued' => 123456789,
'link' => 'http://example.com',
'description' => 'Feed Resource Test 1',
'image' => 'http://example.com/feed_logo',
'hash' => 'abcdefg',
'etag' => 'hijklmn',
'modified' => 123456789,
'langcode' => 'en',
],
],
];
}
protected function getPostDocument() {
return [
'data' => [
'type' => 'aggregator_feed--aggregator_feed',
'attributes' => [
'title' => 'Feed Resource Post Test',
'url' => 'http://example.com/feed',
'refresh' => 900,
'description' => 'Feed Resource Post Test Description',
],
],
];
}
protected function 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.";
}
}
public function testCollectionFilterAccess() {
$this
->doTestCollectionFilterAccessBasedOnPermissions('title', 'access news feeds');
}
}