core/modules/rest/rest.services.yml | 4 +++ .../src/EventSubscriber/RestRouteSubscriber.php | 40 ++++++++++++++++++++++ .../EntityResource/EntityResourceTestBase.php | 31 ++++------------- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/core/modules/rest/rest.services.yml b/core/modules/rest/rest.services.yml index 869eb19..ebed915 100644 --- a/core/modules/rest/rest.services.yml +++ b/core/modules/rest/rest.services.yml @@ -31,3 +31,7 @@ services: arguments: ['@router.builder'] tags: - { name: event_subscriber } + rest.route_subscriber: + class: Drupal\rest\EventSubscriber\RestRouteSubscriber + tags: + - { name: event_subscriber } diff --git a/core/modules/rest/src/EventSubscriber/RestRouteSubscriber.php b/core/modules/rest/src/EventSubscriber/RestRouteSubscriber.php new file mode 100644 index 0000000..bd81370 --- /dev/null +++ b/core/modules/rest/src/EventSubscriber/RestRouteSubscriber.php @@ -0,0 +1,40 @@ +all() as $name => $route) { + // Removes the '_admin_route' route option from each REST route that had + // it added by \Drupal\system\EventSubscriber\AdminRouteSubscriber. + if (strpos($name, 'rest.') === 0 && $route->hasOption('_admin_route')) { + $route->setOption('_admin_route', FALSE); + } + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events = parent::getSubscribedEvents(); + + // Use a very low priority to run as late as possible. Especially use a + // lower priority than \Drupal\system\EventSubscriber\AdminRouteSubscriber. + $events[RoutingEvents::ALTER] = ['onAlterRoutes', -1000]; + + return $events; + } + +} diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index 19271f0..490d6a1 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -298,7 +298,6 @@ protected function getExpectedCacheContexts() { public function testGet() { $this->initAuthentication(); $has_canonical_url = $this->entity->hasLinkTemplate('canonical'); - $canonical_url_is_admin_route = strpos($this->entity->getEntityType()->getLinkTemplate('canonical'), '/admin/') === 0; // The URL and Guzzle request options that will be used in this test. The // request options will be modified/expanded throughout this test: @@ -387,16 +386,8 @@ public function testGet() { // 200 for well-formed HEAD request. $response = $this->request('HEAD', $url, $request_options); $this->assertResourceResponse(200, '', $response); - // Dynamic Page Cache does not work for admin routes. - // @see \Drupal\dynamic_page_cache\PageCache\ResponsePolicy\DenyAdminRoutes - if (!$canonical_url_is_admin_route) { - $this->assertTrue($response->hasHeader('X-Drupal-Dynamic-Cache')); - $this->assertSame(['MISS'], $response->getHeader('X-Drupal-Dynamic-Cache')); - } - else { - $this->assertFalse($response->hasHeader('X-Drupal-Dynamic-Cache')); - } - // Page Cache does not work for authenticated users. + $this->assertTrue($response->hasHeader('X-Drupal-Dynamic-Cache')); + $this->assertSame(['MISS'], $response->getHeader('X-Drupal-Dynamic-Cache')); if (!$this->account) { $this->assertSame(['MISS'], $response->getHeader('X-Drupal-Cache')); } @@ -409,22 +400,14 @@ public function testGet() { // Same for Dynamic Page Cache hit. $response = $this->request('GET', $url, $request_options); $this->assertResourceResponse(200, FALSE, $response); - // Dynamic Page Cache does not work for admin routes. - // @see \Drupal\dynamic_page_cache\PageCache\ResponsePolicy\DenyAdminRoutes - if (!$canonical_url_is_admin_route) { - $this->assertTrue($response->hasHeader('X-Drupal-Dynamic-Cache')); - if (!static::$auth) { - $this->assertSame(['HIT'], $response->getHeader('X-Drupal-Cache')); - $this->assertSame(['MISS'], $response->getHeader('X-Drupal-Dynamic-Cache')); - } - else { - $this->assertFalse($response->hasHeader('X-Drupal-Cache')); - $this->assertSame(['HIT'], $response->getHeader('X-Drupal-Dynamic-Cache')); - } + $this->assertTrue($response->hasHeader('X-Drupal-Dynamic-Cache')); + if (!static::$auth) { + $this->assertSame(['HIT'], $response->getHeader('X-Drupal-Cache')); + $this->assertSame(['MISS'], $response->getHeader('X-Drupal-Dynamic-Cache')); } else { $this->assertFalse($response->hasHeader('X-Drupal-Cache')); - $this->assertFalse($response->hasHeader('X-Drupal-Dynamic-Cache')); + $this->assertSame(['HIT'], $response->getHeader('X-Drupal-Dynamic-Cache')); } $cache_tags_header_value = $response->getHeader('X-Drupal-Cache-Tags')[0]; $this->assertEquals($this->getExpectedCacheTags(), empty($cache_tags_header_value) ? [] : explode(' ', $cache_tags_header_value));