Commit f3f18a23 authored by catch's avatar catch

Issue #2882717 by Wim Leers, dawehner: EntityResourceTestBase: assert that...

Issue #2882717 by Wim Leers, dawehner: EntityResourceTestBase: assert that PATCHed and POSTed entities contain the intended values
parent 193c4846
......@@ -219,7 +219,7 @@ protected function getNormalizedPostEntity() {
],
'entity_id' => [
[
'target_id' => EntityTest::load(1)->id(),
'target_id' => (int) EntityTest::load(1)->id(),
],
],
'field_name' => [
......
......@@ -848,6 +848,25 @@ public function testPost() {
$this->assertSame([], $response->getHeader('Location'));
}
$this->assertFalse($response->hasHeader('X-Drupal-Cache'));
// Assert that the entity was indeed created, and that the response body
// contains the serialized created entity.
$created_entity = $this->entityStorage->loadUnchanged(static::$firstCreatedEntityId);
$created_entity_normalization = $this->serializer->normalize($created_entity, static::$format, ['account' => $this->account]);
// @todo Remove this if-test in https://www.drupal.org/node/2543726: execute
// its body unconditionally.
if (static::$entityTypeId !== 'taxonomy_term') {
$this->assertSame($created_entity_normalization, $this->serializer->decode((string) $response->getBody(), static::$format));
}
// Assert that the entity was indeed created using the POSTed values.
foreach ($this->getNormalizedPostEntity() as $field_name => $field_normalization) {
// Some top-level keys in the normalization may not be fields on the
// entity (for example '_links' and '_embedded' in the HAL normalization).
if ($created_entity->hasField($field_name)) {
// Subset, not same, because we can e.g. send just the target_id for the
// bundle in a POST request; the response will include more properties.
$this->assertArraySubset(static::castToString($field_normalization), $created_entity->get($field_name)->getValue(), TRUE);
}
}
$this->config('rest.settings')->set('bc_entity_resource_permissions', TRUE)->save(TRUE);
......@@ -1064,10 +1083,25 @@ public function testPatch() {
$response = $this->request('PATCH', $url, $request_options);
$this->assertResourceResponse(200, FALSE, $response);
$this->assertFalse($response->hasHeader('X-Drupal-Cache'));
// Assert that the entity was indeed updated, and that the response body
// contains the serialized updated entity.
$updated_entity = $this->entityStorage->loadUnchanged($this->entity->id());
$updated_entity_normalization = $this->serializer->normalize($updated_entity, static::$format, ['account' => $this->account]);
$this->assertSame($updated_entity_normalization, $this->serializer->decode((string) $response->getBody(), static::$format));
// Assert that the entity was indeed created using the PATCHed values.
foreach ($this->getNormalizedPatchEntity() as $field_name => $field_normalization) {
// Some top-level keys in the normalization may not be fields on the
// entity (for example '_links' and '_embedded' in the HAL normalization).
if ($updated_entity->hasField($field_name)) {
// Subset, not same, because we can e.g. send just the target_id for the
// bundle in a PATCH request; the response will include more properties.
$this->assertArraySubset(static::castToString($field_normalization), $updated_entity->get($field_name)->getValue(), TRUE);
}
}
// Ensure that fields do not get deleted if they're not present in the PATCH
// request. Test this using the configurable field that we added, but which
// is not sent in the PATCH request.
$this->assertSame('All the faith he had had had had no effect on the outcome of his life.', $this->entityStorage->loadUnchanged($this->entity->id())->get('field_rest_test')->value);
$this->assertSame('All the faith he had had had had no effect on the outcome of his life.', $updated_entity->get('field_rest_test')->value);
$this->config('rest.settings')->set('bc_entity_resource_permissions', TRUE)->save(TRUE);
......
......@@ -116,7 +116,11 @@ protected function getExpectedNormalizedEntity() {
*/
protected function getNormalizedPostEntity() {
return [
'type' => 'entity_test',
'type' => [
[
'value' => 'entity_test',
],
],
'name' => [
[
'value' => 'Dramallama',
......
......@@ -117,7 +117,11 @@ protected function getExpectedNormalizedEntity() {
*/
protected function getNormalizedPostEntity() {
return [
'type' => 'entity_test_label',
'type' => [
[
'value' => 'entity_test_label',
],
],
'name' => [
[
'value' => 'label_llama',
......
......@@ -132,7 +132,11 @@ protected function getNormalizedPostEntity() {
'uri' => 'internal:/',
],
],
'shortcut_set' => 'default',
'shortcut_set' => [
[
'target_id' => 'default',
],
],
];
}
......
......@@ -147,6 +147,12 @@ protected function getNormalizedPostEntity() {
'value' => 'Dramallama',
],
],
'description' => [
[
'value' => 'Dramallamas are the coolest camelids.',
'format' => NULL,
],
],
];
}
......
......@@ -121,7 +121,7 @@ protected function getNormalizedPostEntity() {
return [
'name' => [
[
'value' => 'Dramallama ' . $this->randomMachineName(),
'value' => 'Dramallama',
],
],
];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment