diff --git a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php index f757a9672c2ce3ffb64eb2e36e13898d924b7a4f..eb09d5ce40019c6f3b719dabe2a4f56d742924ac 100644 --- a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php +++ b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php @@ -216,7 +216,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setDescription(t('The parent comment ID if this is a reply to a comment.')) ->setSetting('target_type', 'comment'); - $fields['entity_id'] = FieldDefinition::create('integer') + $fields['entity_id'] = FieldDefinition::create('entity_reference') ->setLabel(t('Entity ID')) ->setDescription(t('The ID of the entity of which this comment is a reply.')) ->setRequired(TRUE); @@ -302,6 +302,16 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { return $fields; } + /** + * {@inheritdoc} + */ + public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { + list($target_type) = explode('__', $bundle, 2); + $fields['entity_id'] = clone $base_field_definitions['entity_id']; + $fields['entity_id']->setSetting('target_type', $target_type); + return $fields; + } + /** * {@inheritdoc} */ @@ -321,16 +331,14 @@ public function getParentComment() { * {@inheritdoc} */ public function getCommentedEntity() { - $entity_id = $this->getCommentedEntityId(); - $entity_type = $this->getCommentedEntityTypeId(); - return entity_load($entity_type, $entity_id); + return $this->get('entity_id')->entity; } /** * {@inheritdoc} */ public function getCommentedEntityId() { - return $this->get('entity_id')->value; + return $this->get('entity_id')->target_id; } /** diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php index 881c9658d49dfd6495de098ff3ebf793df46f35d..03a0e82decee7306ea15fbbfadc9590c9530732a 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php @@ -39,6 +39,7 @@ public static function getInfo() { public function setUp() { parent::setUp(); $this->installSchema('node', array('node', 'node_field_data', 'node_field_revision', 'node_revision')); + $this->installSchema('comment', array('comment_entity_statistics')); } /** diff --git a/core/modules/hal/lib/Drupal/hal/Tests/EntityTest.php b/core/modules/hal/lib/Drupal/hal/Tests/EntityTest.php index 42bfc4f66f6bb05535e595de00fc9d4ee4f23731..bfbe6f1d938473409f290ecdd3c1cc617e70c62c 100644 --- a/core/modules/hal/lib/Drupal/hal/Tests/EntityTest.php +++ b/core/modules/hal/lib/Drupal/hal/Tests/EntityTest.php @@ -17,7 +17,7 @@ class EntityTest extends NormalizerTestBase { * * @var array */ - public static $modules = array('node', 'taxonomy'); + public static $modules = array('node', 'taxonomy', 'comment'); /** * {@inheritdoc} @@ -39,6 +39,7 @@ function setUp() { \Drupal::service('router.builder')->rebuild(); $this->installSchema('system', array('sequences')); $this->installSchema('node', array('node', 'node_field_data', 'node_revision', 'node_field_revision')); + $this->installSchema('comment', array('comment', 'comment_entity_statistics')); $this->installSchema('user', array('users_roles')); $this->installSchema('taxonomy', array('taxonomy_term_data', 'taxonomy_term_hierarchy')); } @@ -118,4 +119,59 @@ public function testTerm() { } } + /** + * Tests the normalization of comments. + */ + public function testComment() { + $node_type = entity_create('node_type', array('type' => 'example_type')); + $node_type->save(); + + $user = entity_create('user', array('name' => $this->randomName())); + $user->save(); + + $node = entity_create('node', array( + 'title' => $this->randomName(), + 'uid' => $user->id(), + 'type' => $node_type->id(), + 'status' => NODE_PUBLISHED, + 'promote' => 1, + 'sticky' => 0, + 'body' => array( + 'value' => $this->randomName(), + 'format' => $this->randomName(), + ) + )); + $node->save(); + + $this->container->get('comment.manager')->addDefaultField('node', 'example_type'); + + $comment = entity_create('comment', array( + 'uid' => $user->id(), + 'subject' => $this->randomName(), + 'comment_body' => $this->randomName(), + 'entity_id' => $node->id(), + 'entity_type' => 'node', + 'field_name' => 'comment' + )); + $comment->save(); + + $original_values = $comment->toArray(); + unset($original_values['cid']); + + $normalized = $this->serializer->normalize($comment, $this->format); + $denormalized_comment = $this->serializer->denormalize($normalized, 'Drupal\comment\Entity\Comment', $this->format); + + // Verify that the ID and revision ID were skipped by the normalizer. + $this->assertEqual(NULL, $denormalized_comment->id()); + + // Loop over the remaining fields and verify that they are identical. + foreach ($original_values as $field_name => $field_values) { + // The target field comes with revision id which is not set. + if (array_key_exists('revision_id', $field_values[0])) { + unset($field_values[0]['revision_id']); + } + $this->assertEqual($field_values, $denormalized_comment->get($field_name)->getValue()); + } + } + }