You are here

function EntityResolverTest::testUuidEntityResolver in Zircon Profile 8

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

Test that fields referencing UUIDs can be denormalized.

File

core/modules/serialization/src/Tests/EntityResolverTest.php, line 58
Contains \Drupal\serialization\Tests\EntityResolverTest.

Class

EntityResolverTest
Tests that entities references can be resolved.

Namespace

Drupal\serialization\Tests

Code

function testUuidEntityResolver() {

  // Create an entity to get the UUID from.
  $entity = entity_create('entity_test_mulrev', array(
    'type' => 'entity_test_mulrev',
  ));
  $entity
    ->set('name', 'foobar');
  $entity
    ->set('field_test_entity_reference', array(
    array(
      'target_id' => 1,
    ),
  ));
  $entity
    ->save();
  $field_uri = Url::fromUri('base:rest/relation/entity_test_mulrev/entity_test_mulrev/field_test_entity_reference', array(
    'absolute' => TRUE,
  ))
    ->toString();
  $data = array(
    '_links' => array(
      'type' => array(
        'href' => Url::fromUri('base:rest/type/entity_test_mulrev/entity_test_mulrev', array(
          'absolute' => TRUE,
        ))
          ->toString(),
      ),
      $field_uri => array(
        array(
          'href' => $entity
            ->url(),
        ),
      ),
    ),
    '_embedded' => array(
      $field_uri => array(
        array(
          '_links' => array(
            'self' => $entity
              ->url(),
          ),
          'uuid' => array(
            array(
              '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.');
}