You are here

public function FileEntityNormalizerTest::testNormalizer in Replication 8.2

Tests using entity fields of the file field type.

File

tests/src/Unit/Normalizer/FileEntityNormalizerTest.php, line 42

Class

FileEntityNormalizerTest
Tests the file entity serialization.

Namespace

Drupal\Tests\replication\Unit\Normalizer

Code

public function testNormalizer() {

  // Create two txt files.
  file_put_contents('public://example1.txt', $this
    ->randomMachineName());
  $file = File::create([
    'uri' => 'public://example1.txt',
  ]);
  $file
    ->setOwnerId(1);
  $file
    ->save();

  // Test normalize.
  $uri = $file
    ->getFileUri();
  $file_contents = file_get_contents($uri);
  $expected_attachment = [
    'uuid' => $file
      ->uuid(),
    'uri' => $uri,
    'content_type' => $file
      ->getMimeType(),
    'digest' => 'md5-' . base64_encode(md5($file_contents)),
    'length' => $file
      ->getSize(),
    'data' => base64_encode($file_contents),
  ];
  list($i, $hash) = explode('-', $file->_rev->value);
  $expected = [
    '@context' => [
      '_id' => '@id',
      '@language' => 'en',
    ],
    '@type' => 'file',
    'en' => [
      '@context' => [
        '@language' => 'en',
      ],
      'langcode' => [
        [
          'value' => 'en',
        ],
      ],
      'uid' => [
        [
          'target_id' => 1,
        ],
      ],
      'filename' => [
        [
          'value' => $file
            ->getFilename(),
        ],
      ],
      'uri' => [
        [
          'value' => $uri,
        ],
      ],
      'filemime' => [
        [
          'value' => $file
            ->getMimeType(),
        ],
      ],
      'filesize' => [
        [
          'value' => $file
            ->getSize(),
        ],
      ],
      'status' => [
        [
          'value' => FALSE,
        ],
      ],
      'created' => [
        $this
          ->formatExpectedTimestampItemValues($file->created->value),
      ],
      'changed' => [
        $this
          ->formatExpectedTimestampItemValues($file->changed->value),
      ],
      '_rev' => [
        [
          'value' => $file->_rev->value,
        ],
      ],
    ],
    '@attachment' => $expected_attachment,
    '_id' => $file
      ->uuid(),
    '_rev' => $file->_rev->value,
    '_revisions' => [
      'start' => 1,
      'ids' => [
        $hash,
      ],
    ],
  ];
  $normalized = $this->serializer
    ->normalize($file);
  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(), $file
    ->getEntityTypeId(), 'Expected entity type found.');
  $this
    ->assertSame($denormalized
    ->bundle(), $file
    ->bundle(), 'Expected entity bundle found.');
  $this
    ->assertSame($denormalized
    ->uuid(), $file
    ->uuid(), 'Expected entity UUID found.');
}