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 42d0335..95a2587 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 @@ -102,13 +102,20 @@ public function put($id, EntityInterface $entity) { if (empty($id)) { throw new NotFoundHttpException(); } + $definition = $this->getDefinition(); + $original_entity = entity_load($definition['entity_type'], $id); + // We don't support creating entities with PUT, so we throw an error if + // there is no existing entity. + if ($original_entity == FALSE) { + throw new NotFoundHttpException(); + } $info = $entity->entityInfo(); // Make sure that the entity ID is the one provided in the URL. $entity->{$info['entity_keys']['id']} = $id; try { $entity->save(); // Update responses have an empty body. - return new ResourceResponse(NULL, 200); + return new ResourceResponse(NULL, 204); } catch (EntityStorageException $e) { throw new HttpException(500, 'Internal Server Error', $e); diff --git a/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php b/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php index 1340d79..f47cd7e 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php @@ -58,7 +58,7 @@ public function testUpdate() { $serialized = $serializer->serialize($update_entity, 'drupal_jsonld'); // Update the entity over the web API. $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'PUT', $serialized, 'application/vnd.drupal.ld+json'); - $this->assertResponse(200); + $this->assertResponse(204); // Re-load the updated entity from the database. $entity = entity_load($entity_type, $entity->id(), TRUE); @@ -75,12 +75,18 @@ public function testUpdate() { unset($update_entity->field_test_text); $serialized = $serializer->serialize($update_entity, 'drupal_jsonld'); $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'PUT', $serialized, 'application/vnd.drupal.ld+json'); - $this->assertResponse(200); + $this->assertResponse(204); // Re-load the updated entity from the database. $entity = entity_load($entity_type, $entity->id(), TRUE); $this->assertTrue($entity->field_test_text->isEmpty(), 'Property has been deleted.'); + // Try to create an entity with ID 9999. + $this->httpRequest('entity/' . $entity_type . '/9999', 'PUT', $serialized, 'application/vnd.drupal.ld+json'); + $this->assertResponse(404); + $loaded_entity = entity_load($entity_type, 9999, TRUE); + $this->assertFalse($loaded_entity, 'Entity 9999 was not created.'); + // Try to update an entity without proper permissions. $this->drupalLogout(); $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'PUT', $serialized, 'application/vnd.drupal.ld+json');