You are here

public function FileNormalizeTest::testNormalize in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/hal/tests/src/Kernel/FileNormalizeTest.php \Drupal\Tests\hal\Kernel\FileNormalizeTest::testNormalize()

Tests the normalize function.

File

core/modules/hal/tests/src/Kernel/FileNormalizeTest.php, line 32

Class

FileNormalizeTest
Tests that file entities can be normalized in HAL.

Namespace

Drupal\Tests\hal\Kernel

Code

public function testNormalize() {
  $file_params = [
    'filename' => 'test_1.txt',
    'uri' => 'public://test_1.txt',
    'filemime' => 'text/plain',
  ];

  // Create a new file entity.
  $file = File::create($file_params);
  $file
    ->setPermanent();
  file_put_contents($file
    ->getFileUri(), 'hello world');
  $file
    ->save();
  $expected_array = [
    'uri' => [
      [
        'value' => $file
          ->getFileUri(),
        'url' => $file
          ->createFileUrl(),
      ],
    ],
  ];
  $normalized = $this->serializer
    ->normalize($file, $this->format);
  $this
    ->assertEquals($expected_array['uri'], $normalized['uri'], 'URI is normalized.');
}