Commit 2dc6d91d authored by alexpott's avatar alexpott

Issue #2843754 by rogierbom, Wim Leers, gaurav.kapoor, vaplas: EntityResource:...

Issue #2843754 by rogierbom, Wim Leers, gaurav.kapoor, vaplas: EntityResource: Provide comprehensive test coverage for Feed entity
parent 5875eb52
<?php
namespace Drupal\Tests\hal\Functional\EntityResource\Feed;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
/**
* @group hal
*/
class FeedHalJsonAnonTest extends FeedHalJsonTestBase {
use AnonResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/hal+json';
}
<?php
namespace Drupal\Tests\hal\Functional\EntityResource\Feed;
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
/**
* @group hal
*/
class FeedHalJsonBasicAuthTest extends FeedHalJsonTestBase {
use BasicAuthResourceTestTrait;
/**
* {@inheritdoc}
*/
public static $modules = ['basic_auth'];
/**
* {@inheritdoc}
*/
protected static $auth = 'basic_auth';
}
<?php
namespace Drupal\Tests\hal\Functional\EntityResource\Feed;
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
/**
* @group hal
*/
class FeedHalJsonCookieTest extends FeedHalJsonTestBase {
use CookieResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $auth = 'cookie';
}
<?php
namespace Drupal\Tests\hal\Functional\EntityResource\Feed;
use Drupal\Tests\rest\Functional\EntityResource\Feed\FeedResourceTestBase;
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
abstract class FeedHalJsonTestBase extends FeedResourceTestBase {
use HalEntityNormalizationTrait;
/**
* {@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 . '/aggregator/sources/1?_format=hal_json'
],
'type' => [
'href' => $this->baseUrl . '/rest/type/aggregator_feed/aggregator_feed'
],
],
];
}
/**
* {@inheritdoc}
*/
protected function getNormalizedPostEntity() {
return parent::getNormalizedPostEntity() + [
'_links' => [
'type' => [
'href' => $this->baseUrl . '/rest/type/aggregator_feed/aggregator_feed'
],
],
];
}
}
......@@ -757,6 +757,8 @@ public function testPost() {
// 201 for well-formed request.
// Delete the first created entity in case there is a uniqueness constraint.
$this->entityStorage->load(static::$firstCreatedEntityId)->delete();
$response = $this->request('POST', $url, $request_options);
$this->assertResourceResponse(201, FALSE, $response);
if ($has_canonical_url) {
......
<?php
namespace Drupal\Tests\rest\Functional\EntityResource\Feed;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
/**
* @group rest
*/
class FeedJsonAnonTest extends FeedResourceTestBase {
use AnonResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'json';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/json';
}
<?php
namespace Drupal\Tests\rest\Functional\EntityResource\Feed;
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
/**
* @group rest
*/
class FeedJsonBasicAuthTest extends FeedResourceTestBase {
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\Feed;
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
/**
* @group rest
*/
class FeedJsonCookieTest extends FeedResourceTestBase {
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\Feed;
use Drupal\Tests\rest\Functional\EntityResource\EntityTest\EntityTestResourceTestBase;
use Drupal\aggregator\Entity\Feed;
abstract class FeedResourceTestBase extends EntityTestResourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['aggregator'];
/**
* {@inheritdoc}
*/
public static $entityTypeId = 'aggregator_feed';
/**
* {@inheritdoc}
*/
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;
}
}
/**
* {@inheritdoc}
*/
public function createEntity() {
$feed = Feed::create();
$feed->set('fid', 1)
->set('uuid', 'abcdefg')
->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;
}
/**
* {@inheritdoc}
*/
protected function getExpectedNormalizedEntity() {
return [
'uuid' => [
[
'value' => 'abcdefg'
]
],
'fid' => [
[
'value' => 1
]
],
'langcode' => [
[
'value' => 'en'
]
],
'url' => [
[
'value' => 'http://example.com/rss.xml'
]
],
'title' => [
[
'value' => 'Feed'
]
],
'refresh' => [
[
'value' => 900
]
],
'checked' => [
[
'value' => 123456789
]
],
'queued' => [
[
'value' => 123456789
]
],
'link' => [
[
'value' => 'http://example.com'
]
],
'description' => [
[
'value' => 'Feed Resource Test 1'
]
],
'image' => [
[
'value' => 'http://example.com/feed_logo'
]
],
'hash' => [
[
'value' => 'abcdefg'
]
],
'etag' => [
[
'value' => 'hijklmn'
]
],
'modified' => [
[
'value' => 123456789
]
],
];
}
/**
* {@inheritdoc}
*/
protected function getNormalizedPostEntity() {
return [
'title' => [
[
'value' => 'Feed Resource Post Test'
]
],
'url' => [
[
'value' => 'http://example.com/feed'
]
],
'refresh' => [
[
'value' => 900
]
],
'description' => [
[
'value' => 'Feed Resource Post Test Description'
]
],
];
}
/**
* {@inheritdoc}
*/
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);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment