You are here

public function FileUrlFieldItemListTest::testReferencedEntities in File URL 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Kernel/FileUrlFieldItemListTest.php \Drupal\Tests\file_url\Kernel\FileUrlFieldItemListTest::testReferencedEntities()

Tests the retrieval of referenced files.

@covers ::referencedEntities

File

tests/src/Kernel/FileUrlFieldItemListTest.php, line 96

Class

FileUrlFieldItemListTest
Tests the overridden field item list.

Namespace

Drupal\Tests\file_url\Kernel

Code

public function testReferencedEntities() {

  // Create a test entity.
  $entity = EntityTest::create();

  // Check that the test file field has our overridden field item list.
  if (!$entity->file_url_test instanceof FileUrlFieldItemList) {
    $this
      ->fail('A freshly created File Url field has the correct item list class.');
  }

  // Populate the file field with references to both a local and a remote
  // file.
  $entity->file_url_test
    ->setValue([
    0 => [
      'target_id' => $this->localFile
        ->id(),
    ],
    1 => [
      'target_id' => $this->remoteFile
        ->id(),
    ],
  ]);
  $entity->name->value = $this
    ->randomMachineName();
  $entity
    ->save();
  $reloaded_entity = $this
    ->reloadEntity($entity);

  // Check that the file field still has our overridden field item list after
  // loading it from the database.
  if (!$entity->file_url_test instanceof FileUrlFieldItemList) {
    $this
      ->fail('A freshly created File Url field has the correct item list class.');
  }

  // Check that both the local and the remote file references are returned.
  $referenced_entities = $reloaded_entity->file_url_test
    ->referencedEntities();
  $this
    ->assertTrue(!empty($referenced_entities[0]));
  $this
    ->assertEquals('Drupal\\file\\Entity\\File', get_class($referenced_entities[0]));
  $this
    ->assertEquals($this->localFile
    ->id(), $referenced_entities[0]
    ->id());
  $this
    ->assertTrue(!empty($referenced_entities[1]));
  $this
    ->assertEquals('Drupal\\file_url\\Entity\\RemoteFile', get_class($referenced_entities[1]));
  $this
    ->assertEquals($this->remoteFile
    ->id(), $referenced_entities[1]
    ->id());
}