commit 46344fa1b2d2cdc32f1691b80da1cc4d3587cc99 Author: Klaus Purer Date: Thu Dec 13 11:37:24 2012 +0100 Implemented test to delete properties when updating the entity. diff --git a/core/modules/rest/lib/Drupal/rest/Tests/CreateTest.php b/core/modules/rest/lib/Drupal/rest/Tests/CreateTest.php index f5ef159..82f9685 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/CreateTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/CreateTest.php @@ -63,8 +63,9 @@ public function testCreate() { // entity references is implemented. unset($entity_values['user_id']); foreach ($entity_values as $property => $value) { - $actual_value = $loaded_entity->get($property); - $this->assertEqual($value, $actual_value->value, 'Created property ' . $property . ' expected: ' . $value . ', actual: ' . $actual_value->value); + $actual_value = $loaded_entity->{$property}->value; + $create_value = $entity->{$property}->value; + $this->assertEqual($create_value, $actual_value, 'Created property ' . $property . ' expected: ' . $create_value . ', actual: ' . $actual_value); } // Try to create an entity without proper permissions. diff --git a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php index 3640e48..78803c8 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php @@ -126,7 +126,11 @@ protected function entityCreate($entity_type) { protected function entityValues($entity_type) { switch ($entity_type) { case 'entity_test': - return array('name' => $this->randomName(), 'user_id' => 1); + return array( + 'name' => $this->randomName(), + 'user_id' => 1, + 'field_test_text' => array(0 => array('value' => $this->randomString())), + ); case 'node': return array('title' => $this->randomString()); case 'user': diff --git a/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php b/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php index accf33d..1340d79 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php @@ -45,32 +45,42 @@ public function testUpdate() { $this->drupalLogin($account); // Create an entity and save it to the database. - $entity_values = $this->entityValues($entity_type); - $entity = entity_create($entity_type, $entity_values); + $entity = $this->entityCreate($entity_type); $entity->save(); - // Create a second set of entity values that will overwrite the original. + // Create a second entity that will overwrite the original. $update_values = $this->entityValues($entity_type); - // @todo Remove the user reference field for now until deserialization for - // entity references is implemented. - unset($update_values['user_id']); - // Overwrite the properties with the updated values. - foreach ($update_values as $property => $value) { - $entity->set($property, $value); - } + $update_entity = entity_create($entity_type, $update_values); + // Copy the identifier properties over from the original. + $update_entity->uuid->value = $entity->uuid(); + $update_entity->id->value = $entity->id(); - $serialized = $serializer->serialize($entity, 'drupal_jsonld'); + $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', 'HTTP response code is correct.'); + $this->assertResponse(200); - // Re-load updated entity from the database. + // Re-load the updated entity from the database. $entity = entity_load($entity_type, $entity->id(), TRUE); + // @todo Don't check the user reference field for now until deserialization + // for entity references is implemented. + unset($update_values['user_id']); foreach ($update_values as $property => $value) { - $actual_value = $entity->get($property); - $this->assertEqual($value, $actual_value->value, 'Updated property ' . $property . ' expected: ' . $value . ', actual: ' . $actual_value->value); + $update_value = $update_entity->{$property}->value; + $stored_value = $entity->{$property}->value; + $this->assertEqual($stored_value, $update_value, 'Updated property ' . $property . ' expected: ' . $update_value . ', actual: ' . $stored_value); } + // Try to delete a property. + 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); + + // 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 update an entity without proper permissions. $this->drupalLogout(); $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'PUT', $serialized, 'application/vnd.drupal.ld+json');