diff --git a/core/core.services.yml b/core/core.services.yml index abee307..c6d9636 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -366,6 +366,10 @@ services: class: Drupal\Core\EventSubscriber\SpecialAttributesRouteSubscriber tags: - { name: event_subscriber } + route_http_method_subscriber: + class: Drupal\Core\EventSubscriber\RouteMethodSubscriber + tags: + - { name: event_subscriber } controller.page: class: Drupal\Core\Controller\HtmlPageController arguments: ['@http_kernel', '@controller_resolver', '@string_translation', '@title_resolver'] diff --git a/core/lib/Drupal/Core/EventSubscriber/RouteMethodSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/RouteMethodSubscriber.php new file mode 100644 index 0000000..19b5488 --- /dev/null +++ b/core/lib/Drupal/Core/EventSubscriber/RouteMethodSubscriber.php @@ -0,0 +1,48 @@ +getRouteCollection() as $route) { + $methods = $route->getMethods(); + if (empty($methods)) { + $route->setMethods(array('GET', 'POST')); + } + } + } + + /** + * {@inheritdoc} + */ + static function getSubscribedEvents() { + $events[RoutingEvents::ALTER][] = 'onRouteBuilding'; + return $events; + } + +} diff --git a/core/modules/rest/lib/Drupal/rest/Plugin/rest/resource/EntityResource.php b/core/modules/rest/lib/Drupal/rest/Plugin/rest/resource/EntityResource.php index d0f9421..c8d9b64 100644 --- a/core/modules/rest/lib/Drupal/rest/Plugin/rest/resource/EntityResource.php +++ b/core/modules/rest/lib/Drupal/rest/Plugin/rest/resource/EntityResource.php @@ -185,17 +185,20 @@ public function patch($original_entity, EntityInterface $entity = NULL) { /** * Responds to entity DELETE requests. * - * @param mixed $id - * The entity ID. + * @param mixed $entity + * The entity ID or the already upcasted entity object. * * @return \Drupal\rest\ResourceResponse * The HTTP response object. * * @throws \Symfony\Component\HttpKernel\Exception\HttpException */ - public function delete($id) { - $definition = $this->getPluginDefinition(); - $entity = entity_load($definition['entity_type'], $id); + public function delete($entity) { + $id = $entity; + if (is_scalar($entity)) { + $definition = $this->getPluginDefinition(); + $entity = entity_load($definition['entity_type'], $id); + } if ($entity) { if (!$entity->access('delete')) { throw new AccessDeniedHttpException(); diff --git a/core/modules/rest/lib/Drupal/rest/Tests/DeleteTest.php b/core/modules/rest/lib/Drupal/rest/Tests/DeleteTest.php index 8aa417b..62341f9 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/DeleteTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/DeleteTest.php @@ -61,8 +61,13 @@ public function testDelete() { // Try to delete an entity that does not exist. $response = $this->httpRequest($this->entityBasePath($entity_type) . '/9999', 'DELETE'); $this->assertResponse(404); - $decoded = drupal_json_decode($response); - $this->assertEqual($decoded['error'], 'Entity with ID 9999 not found', 'Response message is correct.'); + if ($entity_type == 'entity_test') { + $decoded = drupal_json_decode($response); + $this->assertEqual($decoded['error'], 'Entity with ID 9999 not found', 'Response message is correct.'); + } + else { + $this->assertText('The requested page "/node/9999" could not be found.'); + } // Try to delete an entity without proper permissions. $this->drupalLogout(); diff --git a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php index 25c1425..bfc2736 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php @@ -248,16 +248,22 @@ protected function assertHeader($header, $value, $message = '', $group = 'Browse } /** - * Overrides WebTestBase::drupalLogin(). + * {@inheritdoc} + * + * This method is overridden to deal with a cURL quirk: the usage of + * CURLOPT_CUSTOMREQUEST cannot be unset on the cURL handle, so we need to + * override it every time it is omitted. */ - protected function drupalLogin(AccountInterface $user) { - if (isset($this->curlHandle)) { - // cURL quirk: when setting CURLOPT_CUSTOMREQUEST to anything other than - // POST in httpRequest() it has to be restored to POST here. Otherwise the - // POST request to login a user will not work. - curl_setopt($this->curlHandle, CURLOPT_CUSTOMREQUEST, 'POST'); + protected function curlExec($curl_options, $redirect = FALSE) { + if (!isset($curl_options[CURLOPT_CUSTOMREQUEST])) { + if (!empty($curl_options[CURLOPT_HTTPGET])) { + $curl_options[CURLOPT_CUSTOMREQUEST] = 'GET'; + } + if (!empty($curl_options[CURLOPT_POST])) { + $curl_options[CURLOPT_CUSTOMREQUEST] = 'POST'; + } } - parent::drupalLogin($user); + return parent::curlExec($curl_options, $redirect); } /** diff --git a/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php b/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php index 51c2983..46f1f0d 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php @@ -65,7 +65,7 @@ public function testRead() { $this->assertEqual($response, 'An error occurred: No route found for the specified formats application/wrongformat.'); } if ($entity_type == 'node') { - // Nodes are different because there is always as HTML route which will + // Nodes are different because there is always a HTML route which will // fire if no other format could be found. $this->assertResponse(200); }