You are here

public function EntityTest::testComment in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/hal/src/Tests/EntityTest.php \Drupal\hal\Tests\EntityTest::testComment()

Tests the normalization of comments.

File

core/modules/hal/src/Tests/EntityTest.php, line 140
Contains \Drupal\hal\Tests\EntityTest.

Class

EntityTest
Tests that nodes and terms are correctly normalized and denormalized.

Namespace

Drupal\hal\Tests

Code

public function testComment() {
  $node_type = entity_create('node_type', array(
    'type' => 'example_type',
  ));
  $node_type
    ->save();
  $account = entity_create('user', array(
    '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 = entity_create('node', array(
    'title' => $this
      ->randomMachineName(),
    'uid' => $account
      ->id(),
    'type' => $node_type
      ->id(),
    'status' => NODE_PUBLISHED,
    'promote' => 1,
    'sticky' => 0,
    'body' => array(
      'value' => $this
        ->randomMachineName(),
      'format' => $this
        ->randomMachineName(),
    ),
  ));
  $node
    ->save();
  $parent_comment = entity_create('comment', 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 = entity_create('comment', 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();

  // cid will not exist and hostname will always be denied view access.
  // No value will exist for name as this is only for anonymous users.
  unset($original_values['cid'], $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.');
  $denormalized_comment = $this->serializer
    ->denormalize($normalized, 'Drupal\\comment\\Entity\\Comment', $this->format, [
    'account' => $account,
  ]);

  // 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());
  }
}