You are here

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

Tests the normalization of terms.

File

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

Class

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

Namespace

Drupal\hal\Tests

Code

public function testTerm() {
  $vocabulary = entity_create('taxonomy_vocabulary', array(
    'vid' => 'example_vocabulary',
  ));
  $vocabulary
    ->save();
  $account = entity_create('user', array(
    '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 = entity_create('taxonomy_term', array(
    'name' => $this
      ->randomMachineName(),
    'vid' => $vocabulary
      ->id(),
  ));
  $term_parent
    ->save();
  $term = entity_create('taxonomy_term', array(
    'name' => $this
      ->randomMachineName(),
    'vid' => $vocabulary
      ->id(),
    'description' => array(
      'value' => $this
        ->randomMachineName(),
      'format' => $this
        ->randomMachineName(),
    ),
    'parent' => $term_parent
      ->id(),
  ));
  $term
    ->save();
  $original_values = $term
    ->toArray();
  unset($original_values['tid']);
  $normalized = $this->serializer
    ->normalize($term, $this->format, [
    'account' => $account,
  ]);
  $denormalized_term = $this->serializer
    ->denormalize($normalized, 'Drupal\\taxonomy\\Entity\\Term', $this->format, [
    'account' => $account,
  ]);

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

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