You are here

public function LinkItemNormalizerTest::testLinkFieldNormalization in Replication 8.2

Tests field link normalization.

File

tests/src/Unit/Normalizer/LinkItemNormalizerTest.php, line 45

Class

LinkItemNormalizerTest
Tests the link serialization format.

Namespace

Drupal\Tests\replication\Unit\Normalizer

Code

public function testLinkFieldNormalization() {

  // Create two entities that will be used as references fot the link field.
  $referenced_entity1 = EntityTestMulRev::create([
    'name' => $this
      ->randomMachineName(),
    'user_id' => 1,
  ]);
  $referenced_entity1
    ->save();
  $referenced_entity2 = EntityTestMulRev::create([
    'name' => $this
      ->randomMachineName(),
    'user_id' => 1,
  ]);
  $referenced_entity2
    ->save();

  // Create a test entity to serialize.
  $this->values = [
    'name' => $this
      ->randomMachineName(),
    'user_id' => 1,
    'field_test_text' => [
      'value' => $this
        ->randomMachineName(),
      'format' => 'full_html',
    ],
    'field_test_link' => [
      [
        'uri' => 'entity:/entity_test_mulrev/' . $referenced_entity1
          ->id(),
        'options' => [],
      ],
      [
        'uri' => 'internal:/entity_test_mulrev/' . $referenced_entity2
          ->id(),
        'options' => [],
      ],
    ],
  ];
  $this->entity = EntityTestMulRev::create($this->values);
  $this->entity
    ->save();
  list($i, $hash) = explode('-', $this->entity->_rev->value);
  $expected = [
    '@context' => [
      '_id' => '@id',
      '@language' => 'en',
    ],
    '@type' => 'entity_test_mulrev',
    'en' => [
      '@context' => [
        '@language' => 'en',
      ],
      'langcode' => [
        [
          'value' => 'en',
        ],
      ],
      'name' => [
        [
          'value' => $this->values['name'],
        ],
      ],
      'type' => [
        [
          'value' => 'entity_test_mulrev',
        ],
      ],
      'created' => [
        $this
          ->formatExpectedTimestampItemValues($this->entity->created->value),
      ],
      'default_langcode' => [
        [
          'value' => TRUE,
        ],
      ],
      'user_id' => [
        [
          'target_id' => $this->values['user_id'],
        ],
      ],
      '_rev' => [
        [
          'value' => $this->entity->_rev->value,
        ],
      ],
      'non_rev_field' => [],
      'field_test_text' => [
        [
          'value' => $this->values['field_test_text']['value'],
          'format' => $this->values['field_test_text']['format'],
          'processed' => '',
        ],
      ],
      'field_test_link' => [
        [
          'uri' => 'entity:entity_test_mulrev/' . $referenced_entity1
            ->uuid(),
          'title' => NULL,
          'options' => [],
          'type' => $referenced_entity1
            ->bundle(),
        ],
        [
          'uri' => 'internal:/entity_test_mulrev/' . $referenced_entity2
            ->uuid(),
          'title' => NULL,
          'options' => [],
          'type' => $referenced_entity2
            ->bundle(),
        ],
      ],
    ],
    '_id' => $this->entity
      ->uuid(),
    '_rev' => $this->entity->_rev->value,
    '_revisions' => [
      'start' => 1,
      'ids' => [
        $hash,
      ],
    ],
  ];

  // Test normalize.
  $normalized = $this->serializer
    ->normalize($this->entity);
  foreach (array_keys($expected) as $key) {
    $this
      ->assertEquals($expected[$key], $normalized[$key], "Field {$key} is normalized correctly.");
  }
  $this
    ->assertEquals(array_diff_key($normalized, $expected), [], 'No unexpected data is added to the normalized array.');

  // Test denormalize.
  $denormalized = $this->serializer
    ->denormalize($normalized, $this->entityClass, 'json');
  $this
    ->assertTrue($denormalized instanceof $this->entityClass, SafeMarkup::format('Denormalized entity is an instance of @class', [
    '@class' => $this->entityClass,
  ]));
  $this
    ->assertSame($denormalized
    ->getEntityTypeId(), $this->entity
    ->getEntityTypeId(), 'Expected entity type found.');
  $this
    ->assertSame($denormalized
    ->bundle(), $this->entity
    ->bundle(), 'Expected entity bundle found.');
  $this
    ->assertSame($denormalized
    ->uuid(), $this->entity
    ->uuid(), 'Expected entity UUID found.');
  $expected_link_field_values = [
    [
      'uri' => 'entity:entity_test_mulrev/' . $referenced_entity1
        ->id(),
      'title' => NULL,
      'options' => [],
      'type' => 'entity_test_mulrev',
    ],
    [
      'uri' => 'internal:/entity_test_mulrev/' . $referenced_entity2
        ->id(),
      'title' => NULL,
      'options' => [],
      'type' => 'entity_test_mulrev',
    ],
  ];
  foreach ($denormalized
    ->get('field_test_link')
    ->getValue() as $key => $item) {
    $this
      ->assertEquals($expected_link_field_values[$key], $item, "Field {$key} is normalized correctly.");
  }
  $normalized2 = $normalized;

  // Test denormalize.
  $denormalized2 = $this->serializer
    ->denormalize($normalized2, $this->entityClass, 'json');
  $expected_link_field_values = [
    [
      'uri' => 'entity:entity_test_mulrev/' . $referenced_entity1
        ->id(),
      'title' => NULL,
      'options' => [],
      'type' => 'entity_test_mulrev',
    ],
    [
      'uri' => 'internal:/entity_test_mulrev/' . $referenced_entity2
        ->id(),
      'title' => NULL,
      'options' => [],
      'type' => 'entity_test_mulrev',
    ],
  ];
  foreach ($denormalized2
    ->get('field_test_link')
    ->getValue() as $key => $item) {
    $this
      ->assertEquals($expected_link_field_values[$key], $item, "Field {$key} is normalized correctly.");
  }
}