You are here

public function EntityResolverTest::testUuidEntityResolver in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/serialization/tests/src/Kernel/EntityResolverTest.php \Drupal\Tests\serialization\Kernel\EntityResolverTest::testUuidEntityResolver()

Test that fields referencing UUIDs can be denormalized.

File

core/modules/serialization/tests/src/Kernel/EntityResolverTest.php, line 57

Class

EntityResolverTest
Tests that entities references can be resolved.

Namespace

Drupal\Tests\serialization\Kernel

Code

public function testUuidEntityResolver() {

  // Create an entity to get the UUID from.
  $entity = EntityTestMulRev::create([
    'type' => 'entity_test_mulrev',
  ]);
  $entity
    ->set('name', 'foobar');
  $entity
    ->set('field_test_entity_reference', [
    [
      'target_id' => 1,
    ],
  ]);
  $entity
    ->save();
  $field_uri = Url::fromUri('base:rest/relation/entity_test_mulrev/entity_test_mulrev/field_test_entity_reference', [
    'absolute' => TRUE,
  ])
    ->toString();
  $data = [
    '_links' => [
      'type' => [
        'href' => Url::fromUri('base:rest/type/entity_test_mulrev/entity_test_mulrev', [
          'absolute' => TRUE,
        ])
          ->toString(),
      ],
      $field_uri => [
        [
          'href' => $entity
            ->toUrl()
            ->toString(),
        ],
      ],
    ],
    '_embedded' => [
      $field_uri => [
        [
          '_links' => [
            'self' => $entity
              ->toUrl()
              ->toString(),
          ],
          'uuid' => [
            [
              'value' => $entity
                ->uuid(),
            ],
          ],
        ],
      ],
    ],
  ];
  $denormalized = $this->container
    ->get('serializer')
    ->denormalize($data, 'Drupal\\entity_test\\Entity\\EntityTestMulRev', $this->format);
  $field_value = $denormalized
    ->get('field_test_entity_reference')
    ->getValue();
  $this
    ->assertEqual($field_value[0]['target_id'], 1, 'Entity reference resolved using UUID.');
}