.../hal/tests/src/Kernel/DenormalizeTest.php | 96 +--------- .../hal/tests/src/Kernel/EntityNormalizeTest.php | 206 --------------------- .../modules/hal/tests/src/Kernel/NormalizeTest.php | 2 +- 3 files changed, 2 insertions(+), 302 deletions(-) diff --git a/core/modules/hal/tests/src/Kernel/DenormalizeTest.php b/core/modules/hal/tests/src/Kernel/DenormalizeTest.php index 64c8311..8f64049 100644 --- a/core/modules/hal/tests/src/Kernel/DenormalizeTest.php +++ b/core/modules/hal/tests/src/Kernel/DenormalizeTest.php @@ -7,7 +7,7 @@ use Symfony\Component\Serializer\Exception\UnexpectedValueException; /** - * Tests that entities can be denormalized from HAL. + * Tests HAL denormalization edge cases for EntityResource. * * @group hal */ @@ -110,98 +110,4 @@ public function testMarkFieldForDeletion() { $this->assertEqual($entity->field_test_text->count(), 0); } - /** - * Test that non-reference fields can be denormalized. - */ - public function testBasicFieldDenormalization() { - $data = array( - '_links' => array( - 'type' => array( - 'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(), - ), - ), - 'uuid' => array( - array( - 'value' => 'e5c9fb96-3acf-4a8d-9417-23de1b6c3311', - ), - ), - 'field_test_text' => array( - array( - 'value' => $this->randomMachineName(), - 'format' => 'full_html', - ), - ), - 'field_test_translatable_text' => array( - array( - 'value' => $this->randomMachineName(), - 'format' => 'full_html', - ), - array( - 'value' => $this->randomMachineName(), - 'format' => 'filtered_html', - ), - array( - 'value' => $this->randomMachineName(), - 'format' => 'filtered_html', - 'lang' => 'de', - ), - array( - 'value' => $this->randomMachineName(), - 'format' => 'full_html', - 'lang' => 'de', - ), - ), - ); - - $expected_value_default = array( - array ( - 'value' => $data['field_test_translatable_text'][0]['value'], - 'format' => 'full_html', - ), - array ( - 'value' => $data['field_test_translatable_text'][1]['value'], - 'format' => 'filtered_html', - ), - ); - $expected_value_de = array( - array ( - 'value' => $data['field_test_translatable_text'][2]['value'], - 'format' => 'filtered_html', - ), - array ( - 'value' => $data['field_test_translatable_text'][3]['value'], - 'format' => 'full_html', - ), - ); - $denormalized = $this->serializer->denormalize($data, $this->entityClass, $this->format); - $this->assertEqual($data['uuid'], $denormalized->get('uuid')->getValue(), 'A preset value (e.g. UUID) is overridden by incoming data.'); - $this->assertEqual($data['field_test_text'], $denormalized->get('field_test_text')->getValue(), 'A basic text field is denormalized.'); - $this->assertEqual($expected_value_default, $denormalized->get('field_test_translatable_text')->getValue(), 'Values in the default language are properly handled for a translatable field.'); - $this->assertEqual($expected_value_de, $denormalized->getTranslation('de')->get('field_test_translatable_text')->getValue(), 'Values in a translation language are properly handled for a translatable field.'); - } - - /** - * Verifies that the denormalized entity is correct in the PATCH context. - */ - public function testPatchDenormalization() { - $data = array( - '_links' => array( - 'type' => array( - 'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(), - ), - ), - 'field_test_text' => array( - array( - 'value' => $this->randomMachineName(), - 'format' => 'full_html', - ), - ), - ); - $denormalized = $this->serializer->denormalize($data, $this->entityClass, $this->format, array('request_method' => 'patch')); - // Check that the one field got populated as expected. - $this->assertEqual($data['field_test_text'], $denormalized->get('field_test_text')->getValue()); - // Check the custom property that contains the list of fields to merge. - $this->assertEqual($denormalized->_restSubmittedFields, ['field_test_text']); - } - } diff --git a/core/modules/hal/tests/src/Kernel/EntityNormalizeTest.php b/core/modules/hal/tests/src/Kernel/EntityNormalizeTest.php deleted file mode 100644 index 88db403..0000000 --- a/core/modules/hal/tests/src/Kernel/EntityNormalizeTest.php +++ /dev/null @@ -1,206 +0,0 @@ -rebuild(); - $this->installSchema('system', array('sequences')); - $this->installSchema('comment', array('comment_entity_statistics')); - $this->installEntitySchema('taxonomy_term'); - $this->installConfig(['node', 'comment']); - } - - /** - * Tests the normalization of nodes. - */ - public function testNode() { - $node_type = NodeType::create(['type' => 'example_type']); - $node_type->save(); - - $user = User::create(['name' => $this->randomMachineName()]); - $user->save(); - - // Add comment type. - $this->container->get('entity.manager')->getStorage('comment_type')->create(array( - 'id' => 'comment', - 'label' => 'comment', - 'target_entity_type_id' => 'node', - ))->save(); - - $this->addDefaultCommentField('node', 'example_type'); - - $node = Node::create([ - 'title' => $this->randomMachineName(), - 'uid' => $user->id(), - 'type' => $node_type->id(), - 'status' => NODE_PUBLISHED, - 'promote' => 1, - 'sticky' => 0, - 'body' => [ - 'value' => $this->randomMachineName(), - 'format' => $this->randomMachineName() - ], - 'revision_log' => $this->randomString(), - ]); - $node->save(); - - $original_values = $node->toArray(); - - $normalized = $this->serializer->normalize($node, $this->format); - - /** @var \Drupal\node\NodeInterface $denormalized_node */ - $denormalized_node = $this->serializer->denormalize($normalized, 'Drupal\node\Entity\Node', $this->format); - - $this->assertEqual($original_values, $denormalized_node->toArray(), 'Node values are restored after normalizing and denormalizing.'); - } - - /** - * Tests the normalization of terms. - */ - public function testTerm() { - $vocabulary = Vocabulary::create(['vid' => 'example_vocabulary']); - $vocabulary->save(); - - $account = User::create(['name' => $this->randomMachineName()]); - $account->save(); - - // @todo Until https://www.drupal.org/node/2327935 is fixed, if no parent is - // set, the test fails because target_id => 0 is reserialized to NULL. - $term_parent = Term::create([ - 'name' => $this->randomMachineName(), - 'vid' => $vocabulary->id(), - ]); - $term_parent->save(); - $term = Term::create([ - 'name' => $this->randomMachineName(), - 'vid' => $vocabulary->id(), - 'description' => array( - 'value' => $this->randomMachineName(), - 'format' => $this->randomMachineName(), - ), - 'parent' => $term_parent->id(), - ]); - $term->save(); - - $original_values = $term->toArray(); - - $normalized = $this->serializer->normalize($term, $this->format, ['account' => $account]); - - /** @var \Drupal\taxonomy\TermInterface $denormalized_term */ - $denormalized_term = $this->serializer->denormalize($normalized, 'Drupal\taxonomy\Entity\Term', $this->format, ['account' => $account]); - - $this->assertEqual($original_values, $denormalized_term->toArray(), 'Term values are restored after normalizing and denormalizing.'); - } - - /** - * Tests the normalization of comments. - */ - public function testComment() { - $node_type = NodeType::create(['type' => 'example_type']); - $node_type->save(); - - $account = User::create(['name' => $this->randomMachineName()]); - $account->save(); - - // Add comment type. - $this->container->get('entity.manager')->getStorage('comment_type')->create(array( - 'id' => 'comment', - 'label' => 'comment', - 'target_entity_type_id' => 'node', - ))->save(); - - $this->addDefaultCommentField('node', 'example_type'); - - $node = Node::create([ - 'title' => $this->randomMachineName(), - 'uid' => $account->id(), - 'type' => $node_type->id(), - 'status' => NODE_PUBLISHED, - 'promote' => 1, - 'sticky' => 0, - 'body' => [[ - 'value' => $this->randomMachineName(), - 'format' => $this->randomMachineName() - ]], - ]); - $node->save(); - - $parent_comment = Comment::create(array( - 'uid' => $account->id(), - 'subject' => $this->randomMachineName(), - 'comment_body' => [ - 'value' => $this->randomMachineName(), - 'format' => NULL, - ], - 'entity_id' => $node->id(), - 'entity_type' => 'node', - 'field_name' => 'comment', - )); - $parent_comment->save(); - - $comment = Comment::create(array( - 'uid' => $account->id(), - 'subject' => $this->randomMachineName(), - 'comment_body' => [ - 'value' => $this->randomMachineName(), - 'format' => NULL, - ], - 'entity_id' => $node->id(), - 'entity_type' => 'node', - 'field_name' => 'comment', - 'pid' => $parent_comment->id(), - 'mail' => 'dries@drupal.org', - 'homepage' => 'http://buytaert.net', - )); - $comment->save(); - - $original_values = $comment->toArray(); - // Hostname will always be denied view access. - // No value will exist for name as this is only for anonymous users. - unset($original_values['hostname'], $original_values['name']); - - $normalized = $this->serializer->normalize($comment, $this->format, ['account' => $account]); - - // Assert that the hostname field does not appear at all in the normalized - // data. - $this->assertFalse(array_key_exists('hostname', $normalized), 'Hostname was not found in normalized comment data.'); - - /** @var \Drupal\comment\CommentInterface $denormalized_comment */ - $denormalized_comment = $this->serializer->denormalize($normalized, 'Drupal\comment\Entity\Comment', $this->format, ['account' => $account]); - - // Before comparing, unset values that are expected to differ. - $denormalized_comment_values = $denormalized_comment->toArray(); - unset($denormalized_comment_values['hostname'], $denormalized_comment_values['name']); - $this->assertEqual($original_values, $denormalized_comment_values, 'The expected comment values are restored after normalizing and denormalizing.'); - } - -} diff --git a/core/modules/hal/tests/src/Kernel/NormalizeTest.php b/core/modules/hal/tests/src/Kernel/NormalizeTest.php index 6ed064c..cfffb4a 100644 --- a/core/modules/hal/tests/src/Kernel/NormalizeTest.php +++ b/core/modules/hal/tests/src/Kernel/NormalizeTest.php @@ -7,7 +7,7 @@ use Drupal\entity_test\Entity\EntityTest; /** - * Tests that entities can be normalized in HAL. + * Tests HAL normalization edge cases for EntityResource. * * @group hal */