diff --git a/core/modules/rest/src/Tests/CreateTest.php b/core/modules/rest/src/Tests/CreateTest.php index a9db99e..e547c2c 100644 --- a/core/modules/rest/src/Tests/CreateTest.php +++ b/core/modules/rest/src/Tests/CreateTest.php @@ -68,23 +68,24 @@ public function testCreate() { // Try to create an entity with an access protected field. // @see entity_test_entity_field_access() if ($entity_type == 'entity_test') { - $entity->field_test_text->value = 'no access value'; - $serialized = $serializer->serialize($entity, $this->defaultFormat, ['account' => $account]); - $this->httpRequest('entity/' . $entity_type, 'POST', $serialized, $this->defaultMimeType); + $context = ['account' => $account]; + $normalized = $serializer->normalize($entity, $this->defaultFormat, $context); + $normalized['field_test_text'][0]['value'] = 'no access value'; + $this->httpRequest('entity/' . $entity_type, 'POST', $serializer->serialize($normalized, $this->defaultFormat, $context), $this->defaultMimeType); $this->assertResponse(403); $this->assertFalse(entity_load_multiple($entity_type, NULL, TRUE), 'No entity has been created in the database.'); // Try to create a field with a text format this user has no access to. $entity->field_test_text->value = $entity_values['field_test_text'][0]['value']; $entity->field_test_text->format = 'full_html'; - $serialized = $serializer->serialize($entity, $this->defaultFormat, ['account' => $account]); + $serialized = $serializer->serialize($entity, $this->defaultFormat, $context); $this->httpRequest('entity/' . $entity_type, 'POST', $serialized, $this->defaultMimeType); $this->assertResponse(422); $this->assertFalse(entity_load_multiple($entity_type, NULL, TRUE), 'No entity has been created in the database.'); // Restore the valid test value. $entity->field_test_text->format = 'plain_text'; - $serialized = $serializer->serialize($entity, $this->defaultFormat, ['account' => $account]); + $serialized = $serializer->serialize($entity, $this->defaultFormat, $context); } // Try to send invalid data that cannot be correctly deserialized. @@ -98,7 +99,7 @@ public function testCreate() { // Try to send invalid data to trigger the entity validation constraints. // Send a UUID that is too long. $entity->set('uuid', $this->randomMachineName(129)); - $invalid_serialized = $serializer->serialize($entity, $this->defaultFormat, ['account' => $account]); + $invalid_serialized = $serializer->serialize($entity, $this->defaultFormat, $context); $response = $this->httpRequest('entity/' . $entity_type, 'POST', $invalid_serialized, $this->defaultMimeType); $this->assertResponse(422); $error = Json::decode($response); diff --git a/core/modules/rest/src/Tests/UpdateTest.php b/core/modules/rest/src/Tests/UpdateTest.php index b27d497..6f58844 100644 --- a/core/modules/rest/src/Tests/UpdateTest.php +++ b/core/modules/rest/src/Tests/UpdateTest.php @@ -40,6 +40,8 @@ public function testPatchUpdate() { $account = $this->drupalCreateUser($permissions); $this->drupalLogin($account); + $context = ['account' => $account]; + // Create an entity and save it to the database. $entity = $this->entityCreate($entity_type); $entity->save(); @@ -52,7 +54,7 @@ public function testPatchUpdate() { $patch_entity = entity_create($entity_type, $patch_values); // We don't want to overwrite the UUID. unset($patch_entity->uuid); - $serialized = $serializer->serialize($patch_entity, $this->defaultFormat, ['account' => $account]); + $serialized = $serializer->serialize($patch_entity, $this->defaultFormat, $context); // Update the entity over the REST API. $this->httpRequest($entity->getSystemPath(), 'PATCH', $serialized, $this->defaultMimeType); @@ -64,7 +66,7 @@ public function testPatchUpdate() { // Make sure that the field does not get deleted if it is not present in the // PATCH request. - $normalized = $serializer->normalize($patch_entity, $this->defaultFormat); + $normalized = $serializer->normalize($patch_entity, $this->defaultFormat, $context); unset($normalized['field_test_text']); $serialized = $serializer->encode($normalized, $this->defaultFormat); $this->httpRequest($entity->getSystemPath(), 'PATCH', $serialized, $this->defaultMimeType); @@ -100,8 +102,9 @@ public function testPatchUpdate() { $this->assertEqual($entity->field_test_text->value, 'no delete access value', 'Text field was not deleted.'); // Try to update an access protected field. - $patch_entity->get('field_test_text')->value = 'no access value'; - $serialized = $serializer->serialize($patch_entity, $this->defaultFormat, ['account' => $account]); + $normalized = $serializer->normalize($patch_entity, $this->defaultFormat, $context); + $normalized['field_test_text'][0]['value'] = 'no access value'; + $serialized = $serializer->serialize($normalized, $this->defaultFormat, $context); $this->httpRequest($entity->getSystemPath(), 'PATCH', $serialized, $this->defaultMimeType); $this->assertResponse(403); @@ -114,7 +117,7 @@ public function testPatchUpdate() { 'value' => 'test', 'format' => 'full_html', )); - $serialized = $serializer->serialize($patch_entity, $this->defaultFormat, ['account' => $account]); + $serialized = $serializer->serialize($patch_entity, $this->defaultFormat, $context); $this->httpRequest($entity->getSystemPath(), 'PATCH', $serialized, $this->defaultMimeType); $this->assertResponse(422); @@ -139,7 +142,7 @@ public function testPatchUpdate() { // Try to send invalid data to trigger the entity validation constraints. // Send a UUID that is too long. $entity->set('uuid', $this->randomMachineName(129)); - $invalid_serialized = $serializer->serialize($entity, $this->defaultFormat, ['account' => $account]); + $invalid_serialized = $serializer->serialize($entity, $this->defaultFormat, $context); $response = $this->httpRequest($entity->getSystemPath(), 'PATCH', $invalid_serialized, $this->defaultMimeType); $this->assertResponse(422); $error = Json::decode($response);