public function FileItemTest::testFileItem

Tests using entity fields of the file field type.

File

drupal/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php, line 69
Contains \Drupal\file\Tests\FileItemTest.

Class

FileItemTest
Tests the new entity API for the file field type.

Namespace

Drupal\file\Tests

Code

public function testFileItem() {

  // Create a test entity with the
  $entity = entity_create('entity_test', array());
  $entity->file_test->fid = $this->file
    ->id();
  $entity->file_test->display = 1;
  $entity->file_test->description = $description = $this
    ->randomName();
  $entity->name->value = $this
    ->randomName();
  $entity
    ->save();
  $entity = entity_load('entity_test', $entity
    ->id());
  $this
    ->assertTrue($entity->file_test instanceof FieldInterface, 'Field implements interface.');
  $this
    ->assertTrue($entity->file_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
  $this
    ->assertEqual($entity->file_test->fid, $this->file
    ->id());
  $this
    ->assertEqual($entity->file_test->display, 1);
  $this
    ->assertEqual($entity->file_test->description, $description);
  $this
    ->assertEqual($entity->file_test->entity->uri, $this->file->uri);
  $this
    ->assertEqual($entity->file_test->entity
    ->id(), $this->file
    ->id());
  $this
    ->assertEqual($entity->file_test->entity
    ->uuid(), $this->file
    ->uuid());

  // Make sure the computed files reflects updates to the file.
  file_put_contents('public://example-2.txt', $this
    ->randomName());
  $file2 = entity_create('file', array(
    'uri' => 'public://example-2.txt',
  ));
  $file2
    ->save();
  $entity->file_test->fid = $file2
    ->id();
  $this
    ->assertEqual($entity->file_test->entity
    ->id(), $file2
    ->id());
  $this
    ->assertEqual($entity->file_test->entity->uri, $file2->uri);
}