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

Issue #2844046 by Wim Leers, dawehner, tedbow: REST Resource config entities...

Issue #2844046 by Wim Leers, dawehner, tedbow: REST Resource config entities do not respect the status (enabled/disabled)
parent 2135a251
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -59,13 +59,14 @@ public function __construct(ResourcePluginManager $manager, EntityTypeManagerInt ...@@ -59,13 +59,14 @@ public function __construct(ResourcePluginManager $manager, EntityTypeManagerInt
* @return array * @return array
*/ */
protected function alterRoutes(RouteCollection $collection) { protected function alterRoutes(RouteCollection $collection) {
// Iterate over all enabled REST resource configs. // Iterate over all enabled REST resource config entities.
/** @var \Drupal\rest\RestResourceConfigInterface[] $resource_configs */ /** @var \Drupal\rest\RestResourceConfigInterface[] $resource_configs */
$resource_configs = $this->resourceConfigStorage->loadMultiple(); $resource_configs = $this->resourceConfigStorage->loadMultiple();
// Iterate over all enabled resource plugins.
foreach ($resource_configs as $resource_config) { foreach ($resource_configs as $resource_config) {
$resource_routes = $this->getRoutesForResourceConfig($resource_config); if ($resource_config->status()) {
$collection->addCollection($resource_routes); $resource_routes = $this->getRoutesForResourceConfig($resource_config);
$collection->addCollection($resource_routes);
}
} }
} }
......
...@@ -131,22 +131,13 @@ abstract class EntityResourceTestBase extends ResourceTestBase { ...@@ -131,22 +131,13 @@ abstract class EntityResourceTestBase extends ResourceTestBase {
public static $modules = ['rest_test', 'text']; public static $modules = ['rest_test', 'text'];
/** /**
* {@inheritdoc} * Provides an entity resource.
*/ */
protected function provisionEntityResource() { protected function provisionEntityResource() {
// It's possible to not have any authentication providers enabled, when // It's possible to not have any authentication providers enabled, when
// testing public (anonymous) usage of a REST resource. // testing public (anonymous) usage of a REST resource.
$auth = isset(static::$auth) ? [static::$auth] : []; $auth = isset(static::$auth) ? [static::$auth] : [];
$this->provisionResource('entity.' . static::$entityTypeId, [static::$format], $auth); $this->provisionResource([static::$format], $auth);
}
/**
* Deprovisions the tested entity resource.
*/
protected function deprovisionEntityResource() {
$this->resourceConfigStorage->load('entity.' . static::$entityTypeId)
->delete();
$this->refreshTestStateAfterRestConfigChange();
} }
/** /**
...@@ -155,6 +146,9 @@ protected function deprovisionEntityResource() { ...@@ -155,6 +146,9 @@ protected function deprovisionEntityResource() {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
// Calculate REST Resource config entity ID.
static::$resourceConfigId = 'entity.' . static::$entityTypeId;
$this->serializer = $this->container->get('serializer'); $this->serializer = $this->container->get('serializer');
$this->entityStorage = $this->container->get('entity_type.manager') $this->entityStorage = $this->container->get('entity_type.manager')
->getStorage(static::$entityTypeId); ->getStorage(static::$entityTypeId);
...@@ -498,17 +492,29 @@ public function testGet() { ...@@ -498,17 +492,29 @@ public function testGet() {
$this->assertResourceResponse(200, FALSE, $response); $this->assertResourceResponse(200, FALSE, $response);
$this->deprovisionEntityResource(); $this->resourceConfigStorage->load(static::$resourceConfigId)->disable()->save();
$this->refreshTestStateAfterRestConfigChange();
// DX: upon disabling a resource, it's immediately no longer available.
$this->assertResourceNotAvailable($url, $request_options);
// DX: upon deprovisioning, immediate 404 if no route, 406 otherwise. $this->resourceConfigStorage->load(static::$resourceConfigId)->enable()->save();
$this->refreshTestStateAfterRestConfigChange();
// DX: upon re-enabling a resource, immediate 200.
$response = $this->request('GET', $url, $request_options); $response = $this->request('GET', $url, $request_options);
if (!$has_canonical_url) { $this->assertResourceResponse(200, FALSE, $response);
$this->assertSame(404, $response->getStatusCode());
}
else { $this->resourceConfigStorage->load(static::$resourceConfigId)->delete();
$this->assert406Response($response); $this->refreshTestStateAfterRestConfigChange();
}
// DX: upon deleting a resource, it's immediately no longer available.
$this->assertResourceNotAvailable($url, $request_options);
$this->provisionEntityResource(); $this->provisionEntityResource();
...@@ -1176,4 +1182,23 @@ protected function assert406Response(ResponseInterface $response) { ...@@ -1176,4 +1182,23 @@ protected function assert406Response(ResponseInterface $response) {
} }
} }
/**
* Asserts that a resource is unavailable: 404, 406 if it has canonical route.
*
* @param \Drupal\Core\Url $url
* URL to request.
* @param array $request_options
* Request options to apply.
*/
protected function assertResourceNotAvailable(Url $url, array $request_options) {
$has_canonical_url = $this->entity->hasLinkTemplate('canonical');
$response = $this->request('GET', $url, $request_options);
if (!$has_canonical_url) {
$this->assertSame(404, $response->getStatusCode());
}
else {
$this->assert406Response($response);
}
}
} }
...@@ -56,6 +56,15 @@ abstract class ResourceTestBase extends BrowserTestBase { ...@@ -56,6 +56,15 @@ abstract class ResourceTestBase extends BrowserTestBase {
*/ */
protected static $auth = FALSE; protected static $auth = FALSE;
/**
* The REST Resource Config entity ID under test (i.e. a resource type).
*
* The REST Resource plugin ID can be calculated from this.
*
* @var string
*/
protected static $resourceConfigId = NULL;
/** /**
* The account to use for authentication, if any. * The account to use for authentication, if any.
* *
...@@ -133,24 +142,23 @@ public function setUp() { ...@@ -133,24 +142,23 @@ public function setUp() {
} }
/** /**
* Provisions a REST resource. * Provisions the REST resource under test.
* *
* @param string $resource_type
* The resource type (REST resource plugin ID).
* @param string[] $formats * @param string[] $formats
* The allowed formats for this resource. * The allowed formats for this resource.
* @param string[] $authentication * @param string[] $authentication
* The allowed authentication providers for this resource. * The allowed authentication providers for this resource.
*/ */
protected function provisionResource($resource_type, $formats = [], $authentication = []) { protected function provisionResource($formats = [], $authentication = []) {
$this->resourceConfigStorage->create([ $this->resourceConfigStorage->create([
'id' => $resource_type, 'id' => static::$resourceConfigId,
'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY, 'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
'configuration' => [ 'configuration' => [
'methods' => ['GET', 'POST', 'PATCH', 'DELETE'], 'methods' => ['GET', 'POST', 'PATCH', 'DELETE'],
'formats' => $formats, 'formats' => $formats,
'authentication' => $authentication, 'authentication' => $authentication,
] ],
'status' => TRUE,
])->save(); ])->save();
$this->refreshTestStateAfterRestConfigChange(); $this->refreshTestStateAfterRestConfigChange();
} }
......
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