diff --git a/core/modules/rest/config/install/rest.settings.yml b/core/modules/rest/config/install/rest.settings.yml index 0a44346..4dde897 100644 --- a/core/modules/rest/config/install/rest.settings.yml +++ b/core/modules/rest/config/install/rest.settings.yml @@ -4,7 +4,7 @@ # setting up SSL. # There are alternatives to Basic_auth in contrib such as OAuth module. resources: - entity:node: + entity__node: GET: supported_formats: - hal_json @@ -30,7 +30,7 @@ resources: # resource: # # resources: -# entity:node: +# entity__node: # GET: # supported_formats: # - json diff --git a/core/modules/rest/config/schema/rest.schema.yml b/core/modules/rest/config/schema/rest.schema.yml index 8421d13..8a6ea43 100644 --- a/core/modules/rest/config/schema/rest.schema.yml +++ b/core/modules/rest/config/schema/rest.schema.yml @@ -42,3 +42,14 @@ rest_request: sequence: type: string label: 'Authentication' + +rest.endpoint.*: + type: config_entity + label: 'REST endpooint' + mapping: + plugin_id: + type: string + label: 'REST endpoint plugin id' + configuration: + type: rest_resource + label: 'REST endpoint configuration' diff --git a/core/modules/rest/rest.api.php b/core/modules/rest/rest.api.php index 2f3f6ee..1ce2251 100644 --- a/core/modules/rest/rest.api.php +++ b/core/modules/rest/rest.api.php @@ -17,15 +17,15 @@ * The collection of resource definitions. */ function hook_rest_resource_alter(&$definitions) { - if (isset($definitions['entity:node'])) { + if (isset($definitions['entity__node'])) { // We want to handle REST requests regarding nodes with our own plugin // class. - $definitions['entity:node']['class'] = 'Drupal\mymodule\Plugin\rest\resource\NodeResource'; + $definitions['entity__node']['class'] = 'Drupal\mymodule\Plugin\rest\resource\NodeResource'; // Serialized nodes should be expanded to my specific node class. - $definitions['entity:node']['serialization_class'] = 'Drupal\mymodule\Entity\MyNode'; + $definitions['entity__node']['serialization_class'] = 'Drupal\mymodule\Entity\MyNode'; } // We don't want Views to show up in the array of plugins at all. - unset($definitions['entity:view']); + unset($definitions['entity__view']); } /** diff --git a/core/modules/rest/rest.permissions.yml b/core/modules/rest/rest.permissions.yml index 2ab7154..0a276ee 100644 --- a/core/modules/rest/rest.permissions.yml +++ b/core/modules/rest/rest.permissions.yml @@ -1,2 +1,5 @@ permission_callbacks: - Drupal\rest\RestPermissions::permissions + +administer rest endpoints: + title: 'Administer REST endpoint configuration' diff --git a/core/modules/rest/rest.services.yml b/core/modules/rest/rest.services.yml index bfba7bb..5ebc39a 100644 --- a/core/modules/rest/rest.services.yml +++ b/core/modules/rest/rest.services.yml @@ -25,7 +25,7 @@ services: arguments: ['@cache.default', '@entity.manager', '@unrouted_url_assembler'] rest.resource_routes: class: Drupal\rest\Routing\ResourceRoutes - arguments: ['@plugin.manager.rest', '@config.factory', '@logger.channel.rest'] + arguments: ['@plugin.manager.rest', '@entity.manager', '@logger.channel.rest'] tags: - { name: 'event_subscriber' } logger.channel.rest: diff --git a/core/modules/rest/src/Entity/RestEndpoint.php b/core/modules/rest/src/Entity/RestEndpoint.php new file mode 100644 index 0000000..28d3c16 --- /dev/null +++ b/core/modules/rest/src/Entity/RestEndpoint.php @@ -0,0 +1,57 @@ +getDefinition(['id' => $this->plugin_id]); + return $plugin_definition['label']; + } + + /** + * {@inheritdoc} + */ + public function getSettings() { + return $this->configuration; + } + + /** + * @param array $settings + */ + public function setSettings($settings) { + $this->configuration = $settings; + } + +} diff --git a/core/modules/rest/src/Plugin/Deriver/EntityDeriver.php b/core/modules/rest/src/Plugin/Deriver/EntityDeriver.php index 3d987a2..59db920 100644 --- a/core/modules/rest/src/Plugin/Deriver/EntityDeriver.php +++ b/core/modules/rest/src/Plugin/Deriver/EntityDeriver.php @@ -74,7 +74,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { // Add in the default plugin configuration and the resource type. foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) { $this->derivatives[$entity_type_id] = array( - 'id' => 'entity:' . $entity_type_id, + 'id' => 'entity__' . $entity_type_id, 'entity_type' => $entity_type_id, 'serialization_class' => $entity_type->getClass(), 'label' => $entity_type->getLabel(), diff --git a/core/modules/rest/src/Plugin/ResourceBase.php b/core/modules/rest/src/Plugin/ResourceBase.php index 70d6268..fac6eaf 100644 --- a/core/modules/rest/src/Plugin/ResourceBase.php +++ b/core/modules/rest/src/Plugin/ResourceBase.php @@ -78,7 +78,7 @@ public static function create(ContainerInterface $container, array $configuratio * Implements ResourceInterface::permissions(). * * Every plugin operation method gets its own user permission. Example: - * "restful delete entity:node" with the title "Access DELETE on Node + * "restful delete entity__node" with the title "Access DELETE on Node * resource". */ public function permissions() { diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php index 2c5eea9..73691ce 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php @@ -226,7 +226,7 @@ protected function getBaseRoute($canonical_path, $method) { $definition = $this->getPluginDefinition(); $parameters = $route->getOption('parameters') ?: array(); - $parameters[$definition['entity_type']]['type'] = 'entity:' . $definition['entity_type']; + $parameters[$definition['entity_type']]['type'] = 'entity__' . $definition['entity_type']; $route->setOption('parameters', $parameters); return $route; diff --git a/core/modules/rest/src/RequestHandler.php b/core/modules/rest/src/RequestHandler.php index b962b32..4079b2f 100644 --- a/core/modules/rest/src/RequestHandler.php +++ b/core/modules/rest/src/RequestHandler.php @@ -9,6 +9,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\rest\Entity\RestEndpoint; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\HttpFoundation\Request; @@ -41,10 +42,14 @@ public function handle(RouteMatchInterface $route_match, Request $request) { $plugin = $route_match->getRouteObject()->getDefault('_plugin'); $method = strtolower($request->getMethod()); + /** @var \Drupal\rest\Plugin\ResourceInterface $resource */ $resource = $this->container ->get('plugin.manager.rest') ->getInstance(array('id' => $plugin)); + /** @var \Drupal\rest\RestEndpointInterface $endpoint */ + $endpoint = RestEndpoint::load($plugin); + // Deserialize incoming data if available. $serializer = $this->container->get('serializer'); $received = $request->getContent(); @@ -56,8 +61,8 @@ public function handle(RouteMatchInterface $route_match, Request $request) { // formats are configured allow all and hope that the serializer knows the // format. If the serializer cannot handle it an exception will be thrown // that bubbles up to the client. - $config = $this->container->get('config.factory')->get('rest.settings')->get('resources'); - $method_settings = $config[$plugin][$request->getMethod()]; + $config = $endpoint->getSettings(); + $method_settings = $config[$request->getMethod()]; if (empty($method_settings['supported_formats']) || in_array($format, $method_settings['supported_formats'])) { $definition = $resource->getPluginDefinition(); $class = $definition['serialization_class']; @@ -111,7 +116,7 @@ public function handle(RouteMatchInterface $route_match, Request $request) { $response->headers->set('Content-Type', $request->getMimeType($format)); // Add cache tags, but do not overwrite any that exist already on the // response object. - $cache_tags = $this->container->get('config.factory')->get('rest.settings')->getCacheTags(); + $cache_tags = $endpoint->getCacheTags(); if ($response->headers->has('X-Drupal-Cache-Tags')) { $existing_cache_tags = explode(' ', $response->headers->get('X-Drupal-Cache-Tags')); $cache_tags = Cache::mergeTags($existing_cache_tags, $cache_tags); diff --git a/core/modules/rest/src/RestEndpointInterface.php b/core/modules/rest/src/RestEndpointInterface.php new file mode 100644 index 0000000..cf03733 --- /dev/null +++ b/core/modules/rest/src/RestEndpointInterface.php @@ -0,0 +1,17 @@ +restPluginManager = $rest_plugin_manager; - $this->configFactory = $config_factory; + $this->endpoint_storage = $entity_manager->getStorage('rest_endpoint'); } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { - return new static($container->get('plugin.manager.rest'), $container->get('config.factory')); + return new static($container->get('plugin.manager.rest'), $container->get('entity.manager')); } /** @@ -58,10 +58,11 @@ public static function create(ContainerInterface $container) { */ public function permissions() { $permissions = []; - $resources = $this->configFactory->get('rest.settings')->get('resources'); - if ($resources && $enabled = array_intersect_key($this->restPluginManager->getDefinitions(), $resources)) { - foreach ($enabled as $key => $resource) { - $plugin = $this->restPluginManager->getInstance(['id' => $key]); + /** @var \Drupal\rest\RestEndpointInterface[] $endpoints */ + $endpoints = $this->endpoint_storage->loadMultiple(); + foreach ($endpoints as $endpoint) { + if ($this->restPluginManager->getDefinition($endpoint->id(), FALSE)) { + $plugin = $this->restPluginManager->getInstance(['id' => $endpoint->id()]); $permissions = array_merge($permissions, $plugin->permissions()); } } diff --git a/core/modules/rest/src/Routing/ResourceRoutes.php b/core/modules/rest/src/Routing/ResourceRoutes.php index db439dc..e932c55 100644 --- a/core/modules/rest/src/Routing/ResourceRoutes.php +++ b/core/modules/rest/src/Routing/ResourceRoutes.php @@ -9,8 +9,10 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Routing\RouteSubscriberBase; use Drupal\rest\Plugin\Type\ResourcePluginManager; +use Drupal\rest\RestEndpointRepositoryInterface; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -29,11 +31,11 @@ class ResourceRoutes extends RouteSubscriberBase{ protected $manager; /** - * The Drupal configuration factory. + * The REST endpoint storage. * - * @var \Drupal\Core\Config\ConfigFactoryInterface + * @var \Drupal\Core\Entity\EntityManagerInterface */ - protected $config; + protected $endpoint_storage; /** * A logger instance. @@ -47,14 +49,14 @@ class ResourceRoutes extends RouteSubscriberBase{ * * @param \Drupal\rest\Plugin\Type\ResourcePluginManager $manager * The resource plugin manager. - * @param \Drupal\Core\Config\ConfigFactoryInterface $config - * The configuration factory holding resource settings. + * @param \Drupal\rest\RestEndpointRepositoryInterface $endpoint_repository + * The REST endpoint repository * @param \Psr\Log\LoggerInterface $logger * A logger instance. */ - public function __construct(ResourcePluginManager $manager, ConfigFactoryInterface $config, LoggerInterface $logger) { + public function __construct(ResourcePluginManager $manager, EntityManagerInterface $entity_manager, LoggerInterface $logger) { $this->manager = $manager; - $this->config = $config; + $this->endpoint_storage = $entity_manager->getStorage('rest_endpoint'); $this->logger = $logger; } @@ -67,11 +69,13 @@ public function __construct(ResourcePluginManager $manager, ConfigFactoryInterfa */ protected function alterRoutes(RouteCollection $collection) { $routes = array(); - $enabled_resources = $this->config->get('rest.settings')->get('resources') ?: array(); // Iterate over all enabled resource plugins. - foreach ($enabled_resources as $id => $enabled_methods) { - $plugin = $this->manager->getInstance(array('id' => $id)); + /** @var \Drupal\rest\RestEndpointInterface[] $endpoints */ + $endpoints = $this->endpoint_storage->loadMultiple(); + foreach ($endpoints as $endpoint) { + $plugin = $this->manager->getInstance(array('id' => $endpoint->id())); + $enabled_methods = $endpoint->getSettings(); foreach ($plugin->routes() as $name => $route) { $method = $route->getRequirement('_method'); @@ -81,13 +85,13 @@ protected function alterRoutes(RouteCollection $collection) { // Check that authentication providers are defined. if (empty($enabled_methods[$method]['supported_auth']) || !is_array($enabled_methods[$method]['supported_auth'])) { - $this->logger->error('At least one authentication provider must be defined for resource @id', array(':id' => $id)); + $this->logger->error('At least one authentication provider must be defined for resource @id', array(':id' => $endpoint->id())); continue; } // Check that formats are defined. if (empty($enabled_methods[$method]['supported_formats']) || !is_array($enabled_methods[$method]['supported_formats'])) { - $this->logger->error('At least one format must be defined for resource @id', array(':id' => $id)); + $this->logger->error('At least one format must be defined for resource @id', array(':id' => $endpoint->id())); continue; } diff --git a/core/modules/rest/src/Tests/AuthTest.php b/core/modules/rest/src/Tests/AuthTest.php index 09537c8..6a7aa2c 100644 --- a/core/modules/rest/src/Tests/AuthTest.php +++ b/core/modules/rest/src/Tests/AuthTest.php @@ -22,7 +22,7 @@ class AuthTest extends RESTTestBase { * * @var array */ - public static $modules = array('basic_auth', 'hal', 'rest', 'entity_test', 'comment'); + public static $modules = array('basic_auth', 'hal', 'rest', 'entity_test'); /** * Tests reading from an authenticated resource. @@ -31,7 +31,7 @@ public function testRead() { $entity_type = 'entity_test'; // Enable a test resource through GET method and basic HTTP authentication. - $this->enableService('entity:' . $entity_type, 'GET', NULL, array('basic_auth')); + $this->enableService('entity__' . $entity_type, 'GET', NULL, array('basic_auth')); // Create an entity programmatically. $entity = $this->entityCreate($entity_type); @@ -49,7 +49,7 @@ public function testRead() { // resources via the REST API, but the request is authenticated // with session cookies. $permissions = $this->entityPermissions($entity_type, 'view'); - $permissions[] = 'restful get entity:' . $entity_type; + $permissions[] = 'restful get entity__' . $entity_type; $account = $this->drupalCreateUser($permissions); $this->drupalLogin($account); diff --git a/core/modules/rest/src/Tests/CreateTest.php b/core/modules/rest/src/Tests/CreateTest.php index 98b944a..8fee56e 100644 --- a/core/modules/rest/src/Tests/CreateTest.php +++ b/core/modules/rest/src/Tests/CreateTest.php @@ -25,7 +25,7 @@ class CreateTest extends RESTTestBase { * * @var array */ - public static $modules = array('hal', 'rest', 'entity_test'); + public static $modules = array('hal', 'rest', 'entity_test', 'node'); /** * The 'serializer' service. @@ -46,12 +46,12 @@ protected function setUp() { public function testCreateResourceRestApiNotEnabled() { $entity_type = 'entity_test'; // Enables the REST service for a specific entity type. - $this->enableService('entity:' . $entity_type, 'POST'); + $this->enableService('entity__' . $entity_type, 'POST'); // Get the necessary user permissions to create the current entity type. $permissions = $this->entityPermissions($entity_type, 'create'); // POST method must be allowed for the current entity type. - $permissions[] = 'restful post entity:' . $entity_type; + $permissions[] = 'restful post entity__' . $entity_type; // Create the user. $account = $this->drupalCreateUser($permissions); @@ -81,9 +81,9 @@ public function testCreateResourceRestApiNotEnabled() { public function testCreateWithoutPermission() { $entity_type = 'entity_test'; // Enables the REST service for 'entity_test' entity type. - $this->enableService('entity:' . $entity_type, 'POST'); + $this->enableService('entity__' . $entity_type, 'POST'); $permissions = $this->entityPermissions($entity_type, 'create'); - // Create a user without the 'restful post entity:entity_test permission. + // Create a user without the 'restful post entity__entity_test permission. $account = $this->drupalCreateUser($permissions); $this->drupalLogin($account); // Populate some entity properties before create the entity. @@ -105,7 +105,7 @@ public function testCreateWithoutPermission() { public function testCreateEntityTest() { $entity_type = 'entity_test'; // Enables the REST service for 'entity_test' entity type. - $this->enableService('entity:' . $entity_type, 'POST'); + $this->enableService('entity__' . $entity_type, 'POST'); // Create two accounts with the required permissions to create resources. // The second one has administrative permissions. $accounts = $this->createAccountPerEntity($entity_type); @@ -172,7 +172,7 @@ public function testCreateEntityTest() { public function testCreateNode() { $entity_type = 'node'; // Enables the REST service for 'node' entity type. - $this->enableService('entity:' . $entity_type, 'POST'); + $this->enableService('entity__' . $entity_type, 'POST'); // Create two accounts that have the required permissions to create // resources. The second one has administrative permissions. $accounts = $this->createAccountPerEntity($entity_type); @@ -230,7 +230,7 @@ public function testCreateNode() { public function testCreateUser() { $entity_type = 'user'; // Enables the REST service for 'user' entity type. - $this->enableService('entity:' . $entity_type, 'POST'); + $this->enableService('entity__' . $entity_type, 'POST'); // Create two accounts that have the required permissions to create // resources. The second one has administrative permissions. $accounts = $this->createAccountPerEntity($entity_type); @@ -287,7 +287,7 @@ public function createAccountPerEntity($entity_type) { // Get the necessary user permissions for the current $entity_type creation. $permissions = $this->entityPermissions($entity_type, 'create'); // POST method must be allowed for the current entity type. - $permissions[] = 'restful post entity:' . $entity_type; + $permissions[] = 'restful post entity__' . $entity_type; // Create user without administrative permissions. $accounts[] = $this->drupalCreateUser($permissions); // Add administrative permissions for nodes and users. diff --git a/core/modules/rest/src/Tests/CsrfTest.php b/core/modules/rest/src/Tests/CsrfTest.php index a686f448..e1490d6 100644 --- a/core/modules/rest/src/Tests/CsrfTest.php +++ b/core/modules/rest/src/Tests/CsrfTest.php @@ -41,12 +41,12 @@ class CsrfTest extends RESTTestBase { protected function setUp() { parent::setUp(); - $this->enableService('entity:' . $this->testEntityType, 'POST', 'hal_json', array('basic_auth', 'cookie')); + $this->enableService('entity__' . $this->testEntityType, 'POST', 'hal_json', array('basic_auth', 'cookie')); // Create a user account that has the required permissions to create // resources via the REST API. $permissions = $this->entityPermissions($this->testEntityType, 'create'); - $permissions[] = 'restful post entity:' . $this->testEntityType; + $permissions[] = 'restful post entity__' . $this->testEntityType; $this->account = $this->drupalCreateUser($permissions); // Serialize an entity to a string to use in the content body of the POST diff --git a/core/modules/rest/src/Tests/DeleteTest.php b/core/modules/rest/src/Tests/DeleteTest.php index 5a86d77..b190878 100644 --- a/core/modules/rest/src/Tests/DeleteTest.php +++ b/core/modules/rest/src/Tests/DeleteTest.php @@ -22,7 +22,7 @@ class DeleteTest extends RESTTestBase { * * @var array */ - public static $modules = array('hal', 'rest', 'entity_test'); + public static $modules = array('hal', 'rest', 'entity_test', 'node'); /** * Tests several valid and invalid delete requests on all entity types. @@ -33,11 +33,11 @@ public function testDelete() { // controllers are implemented. $entity_types = array('entity_test', 'node'); foreach ($entity_types as $entity_type) { - $this->enableService('entity:' . $entity_type, 'DELETE'); + $this->enableService('entity__' . $entity_type, 'DELETE'); // Create a user account that has the required permissions to delete // resources via the REST API. $permissions = $this->entityPermissions($entity_type, 'delete'); - $permissions[] = 'restful delete entity:' . $entity_type; + $permissions[] = 'restful delete entity__' . $entity_type; $account = $this->drupalCreateUser($permissions); $this->drupalLogin($account); diff --git a/core/modules/rest/src/Tests/NodeTest.php b/core/modules/rest/src/Tests/NodeTest.php index 7169102..d3d3ec0 100644 --- a/core/modules/rest/src/Tests/NodeTest.php +++ b/core/modules/rest/src/Tests/NodeTest.php @@ -24,7 +24,7 @@ class NodeTest extends RESTTestBase { * * @var array */ - public static $modules = array('hal', 'rest', 'comment'); + public static $modules = array('hal', 'rest', 'comment', 'node'); /** * Enables node specific REST API configuration and authentication. @@ -35,9 +35,9 @@ class NodeTest extends RESTTestBase { * The operation, one of 'view', 'create', 'update' or 'delete'. */ protected function enableNodeConfiguration($method, $operation) { - $this->enableService('entity:node', $method); + $this->enableService('entity__node', $method); $permissions = $this->entityPermissions('node', $operation); - $permissions[] = 'restful ' . strtolower($method) . ' entity:node'; + $permissions[] = 'restful ' . strtolower($method) . ' entity__node'; $account = $this->drupalCreateUser($permissions); $this->drupalLogin($account); } @@ -57,7 +57,7 @@ public function testNodes() { // Also check that JSON works and the routing system selects the correct // REST route. - $this->enableService('entity:node', 'GET', 'json'); + $this->enableService('entity__node', 'GET', 'json'); $this->httpRequest($node->urlInfo(), 'GET', NULL, 'application/json'); $this->assertResponse(200); $this->assertHeader('Content-type', 'application/json'); diff --git a/core/modules/rest/src/Tests/PageCacheTest.php b/core/modules/rest/src/Tests/PageCacheTest.php index 9cf2859..2d8f321 100644 --- a/core/modules/rest/src/Tests/PageCacheTest.php +++ b/core/modules/rest/src/Tests/PageCacheTest.php @@ -25,10 +25,11 @@ class PageCacheTest extends RESTTestBase { * Tests that configuration changes also clear the page cache. */ public function testConfigChangePageCache() { - $this->enableService('entity:entity_test', 'GET'); + $endpoint_id = 'entity__entity_test'; + $this->enableService($endpoint_id, 'GET'); // Allow anonymous users to issue GET requests. $permissions = $this->entityPermissions('entity_test', 'view'); - $permissions[] = 'restful get entity:entity_test'; + $permissions[] = 'restful get entity__entity_test'; user_role_grant_permissions('anonymous', $permissions); // Create an entity programmatically. @@ -38,23 +39,23 @@ public function testConfigChangePageCache() { $this->httpRequest($entity->urlInfo(), 'GET', NULL, $this->defaultMimeType); $this->assertResponse(200, 'HTTP response code is correct.'); $this->assertHeader('x-drupal-cache', 'MISS'); - $this->assertCacheTag('config:rest.settings'); + $this->assertCacheTag('config:entity__entity_test'); $this->assertCacheTag('entity_test:1'); // Read it again, should be page-cached now. $this->httpRequest($entity->urlInfo(), 'GET', NULL, $this->defaultMimeType); $this->assertResponse(200, 'HTTP response code is correct.'); $this->assertHeader('x-drupal-cache', 'HIT'); - $this->assertCacheTag('config:rest.settings'); + $this->assertCacheTag('config:entity__entity_test'); $this->assertCacheTag('entity_test:1'); - // Trigger a config save which should clear the page cache, so we should get + // Trigger an endpoint save which should clear the page cache, so we should get // a cache miss now for the same request. - $this->config('rest.settings')->save(); + $this->endpoint_storage->load($endpoint_id)->save(); $this->httpRequest($entity->urlInfo(), 'GET', NULL, $this->defaultMimeType); $this->assertResponse(200, 'HTTP response code is correct.'); $this->assertHeader('x-drupal-cache', 'MISS'); - $this->assertCacheTag('config:rest.settings'); + $this->assertCacheTag('config:entity__entity_test'); $this->assertCacheTag('entity_test:1'); } diff --git a/core/modules/rest/src/Tests/RESTTestBase.php b/core/modules/rest/src/Tests/RESTTestBase.php index ff382d5..1497026 100644 --- a/core/modules/rest/src/Tests/RESTTestBase.php +++ b/core/modules/rest/src/Tests/RESTTestBase.php @@ -19,6 +19,13 @@ abstract class RESTTestBase extends WebTestBase { /** + * The REST endpoint storage. + * + * @var \Drupal\Core\Entity\EntityStorageInterface + */ + protected $endpoint_storage; + + /** * The default serialization format to use for testing REST operations. * * @var string @@ -51,15 +58,18 @@ * * @var array */ - public static $modules = array('rest', 'entity_test', 'node'); + public static $modules = array('rest', 'entity_test'); protected function setUp() { parent::setUp(); $this->defaultFormat = 'hal_json'; $this->defaultMimeType = 'application/hal+json'; $this->defaultAuth = array('cookie'); + $this->endpoint_storage = $this->container->get('entity.manager')->getStorage('rest_endpoint'); // Create a test content type for node testing. - $this->drupalCreateContentType(array('name' => 'resttest', 'type' => 'resttest')); + if (in_array('node', static::$modules)) { + $this->drupalCreateContentType(array('name' => 'resttest', 'type' => 'resttest')); + } } /** @@ -233,22 +243,23 @@ protected function entityValues($entity_type) { */ protected function enableService($resource_type, $method = 'GET', $format = NULL, $auth = NULL) { // Enable REST API for this entity type. - $config = $this->config('rest.settings'); + /** @var \Drupal\rest\RestEndpointInterface $endpoint */ + $endpoint = $this->endpoint_storage->create(['id' => $resource_type]); $settings = array(); if ($resource_type) { if ($format == NULL) { $format = $this->defaultFormat; } - $settings[$resource_type][$method]['supported_formats'][] = $format; + $settings[$method]['supported_formats'][] = $format; if ($auth == NULL) { $auth = $this->defaultAuth; } - $settings[$resource_type][$method]['supported_auth'] = $auth; + $settings[$method]['supported_auth'] = $auth; } - $config->set('resources', $settings); - $config->save(); + $endpoint->setSettings($settings); + $endpoint->save(); $this->rebuildCache(); } diff --git a/core/modules/rest/src/Tests/ReadTest.php b/core/modules/rest/src/Tests/ReadTest.php index d5f2fc1..e02a878 100644 --- a/core/modules/rest/src/Tests/ReadTest.php +++ b/core/modules/rest/src/Tests/ReadTest.php @@ -22,7 +22,7 @@ class ReadTest extends RESTTestBase { * * @var array */ - public static $modules = array('hal', 'rest', 'entity_test'); + public static $modules = array('hal', 'rest', 'entity_test', 'node'); /** * Tests several valid and invalid read requests on all entity types. @@ -32,11 +32,11 @@ public function testRead() { // Define the entity types we want to test. $entity_types = array('entity_test', 'node'); foreach ($entity_types as $entity_type) { - $this->enableService('entity:' . $entity_type, 'GET'); + $this->enableService('entity__' . $entity_type, 'GET'); // Create a user account that has the required permissions to read // resources via the REST API. $permissions = $this->entityPermissions($entity_type, 'view'); - $permissions[] = 'restful get entity:' . $entity_type; + $permissions[] = 'restful get entity__' . $entity_type; $account = $this->drupalCreateUser($permissions); $this->drupalLogin($account); @@ -100,11 +100,11 @@ public function testRead() { */ public function testResourceStructure() { // Enable a service with a format restriction but no authentication. - $this->enableService('entity:node', 'GET', 'json'); + $this->enableService('entity__node', 'GET', 'json'); // Create a user account that has the required permissions to read // resources via the REST API. $permissions = $this->entityPermissions('node', 'view'); - $permissions[] = 'restful get entity:node'; + $permissions[] = 'restful get entity__node'; $account = $this->drupalCreateUser($permissions); $this->drupalLogin($account); diff --git a/core/modules/rest/src/Tests/ResourceTest.php b/core/modules/rest/src/Tests/ResourceTest.php index be02617..2b74fef 100644 --- a/core/modules/rest/src/Tests/ResourceTest.php +++ b/core/modules/rest/src/Tests/ResourceTest.php @@ -7,8 +7,6 @@ namespace Drupal\rest\Tests; -use Drupal\rest\Tests\RESTTestBase; - /** * Tests the structure of a REST resource. * @@ -28,8 +26,6 @@ class ResourceTest extends RESTTestBase { */ protected function setUp() { parent::setUp(); - $this->config = $this->config('rest.settings'); - // Create an entity programmatically. $this->entity = $this->entityCreate('entity_test'); $this->entity->save(); @@ -39,19 +35,17 @@ protected function setUp() { * Tests that a resource without formats cannot be enabled. */ public function testFormats() { - $settings = array( - 'entity:entity_test' => array( - 'GET' => array( - 'supported_auth' => array( - 'basic_auth', - ), - ), - ), - ); - + /** @var \Drupal\rest\RestEndpointInterface $endpoint */ + $endpoint = $this->endpoint_storage->create(['id' => 'entity__entity_test']); + $endpoint->setSettings([ + 'GET' => [ + 'supported_auth' => [ + 'basic_auth', + ], + ], + ]); // Attempt to enable the resource. - $this->config->set('resources', $settings); - $this->config->save(); + $endpoint->save(); $this->rebuildCache(); // Verify that accessing the resource returns 401. @@ -68,19 +62,17 @@ public function testFormats() { * Tests that a resource without authentication cannot be enabled. */ public function testAuthentication() { - $settings = array( - 'entity:entity_test' => array( - 'GET' => array( - 'supported_formats' => array( - 'hal_json', - ), - ), - ), - ); - + /** @var \Drupal\rest\RestEndpointInterface $endpoint */ + $endpoint = $this->endpoint_storage->create(['id' => 'entity__entity_test']); + $endpoint->setSettings([ + 'GET' => [ + 'supported_formats' => [ + 'hal_json', + ], + ], + ]); // Attempt to enable the resource. - $this->config->set('resources', $settings); - $this->config->save(); + $endpoint->save(); $this->rebuildCache(); // Verify that accessing the resource returns 401. diff --git a/core/modules/rest/src/Tests/UpdateTest.php b/core/modules/rest/src/Tests/UpdateTest.php index 481e33b..599c78b 100644 --- a/core/modules/rest/src/Tests/UpdateTest.php +++ b/core/modules/rest/src/Tests/UpdateTest.php @@ -36,7 +36,7 @@ public function testPatchUpdate() { // Create a user account that has the required permissions to create // resources via the REST API. $permissions = $this->entityPermissions($entity_type, 'update'); - $permissions[] = 'restful patch entity:' . $entity_type; + $permissions[] = 'restful patch entity__' . $entity_type; $account = $this->drupalCreateUser($permissions); $this->drupalLogin($account); @@ -170,9 +170,9 @@ public function testUpdateUser() { $serializer = $this->container->get('serializer'); $entity_type = 'user'; // Enables the REST service for 'user' entity type. - $this->enableService('entity:' . $entity_type, 'PATCH'); + $this->enableService('entity__' . $entity_type, 'PATCH'); $permissions = $this->entityPermissions($entity_type, 'update'); - $permissions[] = 'restful patch entity:' . $entity_type; + $permissions[] = 'restful patch entity__' . $entity_type; $account = $this->drupalCreateUser($permissions); $account->set('mail', 'old-email@example.com'); $this->drupalLogin($account);