You are here

protected function ExportTest::validateFileCdfObject in Acquia Content Hub 8.2

Validates file CDFObject.

Parameters

string $filename_expected: Expected file name.

string $uri_expected: Expected file URI.

string $filemime_expected: Expected file type (mime).

File

tests/src/Kernel/ExportTest.php, line 2124

Class

ExportTest
Tests entity exports.

Namespace

Drupal\Tests\acquia_contenthub\Kernel

Code

protected function validateFileCdfObject($filename_expected, $uri_expected, $filemime_expected) {
  $this
    ->validateBaseCdfObject();
  $cdf = $this->cdfObject;

  // Validate Type attribute.
  $type = $cdf
    ->getType();
  $this
    ->assertEquals('drupal8_content_entity', $type);

  // Validate Metadata attribute.
  $metadata = $cdf
    ->getMetadata();
  $this
    ->assertNotEmpty($metadata['dependencies']['entity']);
  $uuids = array_keys($metadata['dependencies']['entity']);
  $owner_uuid = reset($uuids);
  $has_user_dependency = in_array($owner_uuid, $this->userUuids, TRUE);
  $this
    ->assertTrue($has_user_dependency);
  unset($metadata['dependencies']);
  $metadata_expected = [
    'default_language' => 'en',
    'field' => [
      'uuid' => [
        'type' => 'uuid',
      ],
      'uid' => [
        'type' => 'entity_reference',
        'target' => 'user',
      ],
      'filename' => [
        'type' => 'string',
      ],
      'uri' => [
        'type' => 'file_uri',
      ],
      'filemime' => [
        'type' => 'string',
      ],
      'filesize' => [
        'type' => 'integer',
      ],
      'status' => [
        'type' => 'boolean',
      ],
      'created' => [
        'type' => 'created',
      ],
      'changed' => [
        'type' => 'changed',
      ],
    ],
    'languages' => [
      'en',
    ],
    'version' => 2,
  ];
  unset($metadata['data']);
  $this
    ->assertEquals($metadata_expected, $metadata);

  // Validate "Data" attribute.
  $data = $this
    ->getCdfDataAttribute($cdf);
  $created_timestamp = $data['created']['value']['en']['value'];
  $is_timestamp = is_numeric($created_timestamp) && (int) $created_timestamp == $created_timestamp;
  $this
    ->assertTrue($is_timestamp);
  $data_expected = [
    'uuid' => [
      'value' => [
        'en' => [
          'value' => $cdf
            ->getUuid(),
        ],
      ],
    ],
    'uid' => [
      'value' => [
        'en' => [
          $owner_uuid,
        ],
      ],
    ],
    'filename' => [
      'value' => [
        'en' => $filename_expected,
      ],
    ],
    'uri' => [
      'value' => [
        'en' => [
          'value' => $uri_expected,
        ],
      ],
    ],
    'filemime' => [
      'value' => [
        'en' => $filemime_expected,
      ],
    ],
    'filesize' => [
      'value' => [
        'en' => [],
      ],
    ],
    'status' => [
      'value' => [
        'en' => '1',
      ],
    ],
    'created' => [
      'value' => [
        'en' => [
          'value' => $created_timestamp,
        ],
      ],
    ],
    'changed' => [
      'value' => [
        'en' => [
          'value' => $created_timestamp,
        ],
      ],
    ],
  ];
  $this
    ->assertEquals($data_expected, $data);

  // Validate entity type.
  $entity_type = $this
    ->getCdfAttribute($cdf, 'entity_type');
  $this
    ->assertEquals('file', $entity_type);

  // Validate entity bundle.
  $entity_type = $this
    ->getCdfAttribute($cdf, 'bundle');
  $this
    ->assertEquals('file', $entity_type);

  // Validate file name.
  $filename = $this
    ->getCdfAttribute($cdf, 'label', 'en');
  $this
    ->assertEquals($filename_expected, $filename);

  // Validate file location.
  $file_location = $this
    ->getCdfAttribute($cdf, 'file_location');

  /** @var \Drupal\Core\StreamWrapper\LocalStream $stream_wrapper */
  $stream_wrapper = $this->streamWrapperManager
    ->getViaUri($uri_expected);
  $directory_path = $stream_wrapper
    ->getDirectoryPath();
  if (version_compare(\Drupal::VERSION, '8.8.0', '>=')) {
    $file_location_expected = Url::fromUri('base:' . $directory_path . '/' . $this->streamWrapperManager
      ->getTarget($uri_expected), [
      'absolute' => TRUE,
    ])
      ->toString();
  }
  else {
    $file_location_expected = Url::fromUri('base:' . $directory_path . '/' . file_uri_target($uri_expected), [
      'absolute' => TRUE,
    ])
      ->toString();
  }
  $this
    ->assertEquals($file_location_expected, $file_location);

  // Validate file location.
  $file_scheme = $this
    ->getCdfAttribute($cdf, 'file_scheme');
  $this
    ->assertEquals('public', $file_scheme);

  // Validate file URI.
  $file_uri = $this
    ->getCdfAttribute($cdf, 'file_uri');
  $this
    ->assertEquals($uri_expected, $file_uri);
}