You are here

protected function FileUploadResourceTestBase::getExpectedNormalizedEntity in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php \Drupal\Tests\rest\Functional\FileUploadResourceTestBase::getExpectedNormalizedEntity()
  2. 9 core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php \Drupal\Tests\rest\Functional\FileUploadResourceTestBase::getExpectedNormalizedEntity()

Gets the expected file entity.

Parameters

int $fid: The file ID to load and create normalized data for.

string $expected_filename: The expected filename for the stored file.

bool $expected_as_filename: Whether the expected filename should be the filename property too.

Return value

array The expected normalized data array.

File

core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php, line 679

Class

FileUploadResourceTestBase
Tests binary data file upload route.

Namespace

Drupal\Tests\rest\Functional

Code

protected function getExpectedNormalizedEntity($fid = 1, $expected_filename = 'example.txt', $expected_as_filename = FALSE) {
  $author = User::load(static::$auth ? $this->account
    ->id() : 0);
  $file = File::load($fid);
  $expected_normalization = [
    'fid' => [
      [
        'value' => (int) $file
          ->id(),
      ],
    ],
    'uuid' => [
      [
        'value' => $file
          ->uuid(),
      ],
    ],
    'langcode' => [
      [
        'value' => 'en',
      ],
    ],
    'uid' => [
      [
        'target_id' => (int) $author
          ->id(),
        'target_type' => 'user',
        'target_uuid' => $author
          ->uuid(),
        'url' => base_path() . 'user/' . $author
          ->id(),
      ],
    ],
    'filename' => [
      [
        'value' => $expected_as_filename ? $expected_filename : 'example.txt',
      ],
    ],
    'uri' => [
      [
        'value' => 'public://foobar/' . $expected_filename,
        'url' => base_path() . $this->siteDirectory . '/files/foobar/' . rawurlencode($expected_filename),
      ],
    ],
    'filemime' => [
      [
        'value' => 'text/plain',
      ],
    ],
    'filesize' => [
      [
        'value' => strlen($this->testFileData),
      ],
    ],
    'status' => [
      [
        'value' => FALSE,
      ],
    ],
    'created' => [
      [
        'value' => (new \DateTime())
          ->setTimestamp($file
          ->getCreatedTime())
          ->setTimezone(new \DateTimeZone('UTC'))
          ->format(\DateTime::RFC3339),
        'format' => \DateTime::RFC3339,
      ],
    ],
    'changed' => [
      [
        'value' => (new \DateTime())
          ->setTimestamp($file
          ->getChangedTime())
          ->setTimezone(new \DateTimeZone('UTC'))
          ->format(\DateTime::RFC3339),
        'format' => \DateTime::RFC3339,
      ],
    ],
  ];
  return $expected_normalization;
}