You are here

public function EntityTest::testNode 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::testNode()

Tests the normalization of nodes.

File

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

Class

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

Namespace

Drupal\hal\Tests

Code

public function testNode() {
  $node_type = entity_create('node_type', array(
    'type' => 'example_type',
  ));
  $node_type
    ->save();
  $user = entity_create('user', array(
    '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 = entity_create('node', array(
    'title' => $this
      ->randomMachineName(),
    'uid' => $user
      ->id(),
    'type' => $node_type
      ->id(),
    'status' => NODE_PUBLISHED,
    'promote' => 1,
    'sticky' => 0,
    'body' => array(
      'value' => $this
        ->randomMachineName(),
      'format' => $this
        ->randomMachineName(),
    ),
    'revision_log' => $this
      ->randomString(),
  ));
  $node
    ->save();
  $original_values = $node
    ->toArray();
  unset($original_values['nid']);
  unset($original_values['vid']);
  $normalized = $this->serializer
    ->normalize($node, $this->format);
  $denormalized_node = $this->serializer
    ->denormalize($normalized, 'Drupal\\node\\Entity\\Node', $this->format);

  // Verify that the ID and revision ID were skipped by the normalizer.
  $this
    ->assertEqual(NULL, $denormalized_node
    ->id());
  $this
    ->assertEqual(NULL, $denormalized_node
    ->getRevisionId());

  // Loop over the remaining fields and verify that they are identical.
  foreach ($original_values as $field_name => $field_values) {
    $this
      ->assertEqual($field_values, $denormalized_node
      ->get($field_name)
      ->getValue());
  }
}