public function ImageItemTest::testImageItem

Tests using entity fields of the image field type.

File

drupal/core/modules/image/lib/Drupal/image/Tests/ImageItemTest.php, line 68
Contains \Drupal\image\Tests\ImageItemTest.

Class

ImageItemTest
Tests the new entity API for the image field type.

Namespace

Drupal\image\Tests

Code

public function testImageItem() {

  // Create a test entity with the image field set.
  $entity = entity_create('entity_test', array());
  $entity->image_test->fid = $this->image
    ->id();
  $entity->image_test->alt = $alt = $this
    ->randomName();
  $entity->image_test->title = $title = $this
    ->randomName();
  $entity->name->value = $this
    ->randomName();
  $entity
    ->save();
  $entity = entity_load('entity_test', $entity
    ->id());
  $this
    ->assertTrue($entity->image_test instanceof FieldInterface, 'Field implements interface.');
  $this
    ->assertTrue($entity->image_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
  $this
    ->assertEqual($entity->image_test->fid, $this->image
    ->id());
  $this
    ->assertEqual($entity->image_test->alt, $alt);
  $this
    ->assertEqual($entity->image_test->title, $title);
  $info = image_get_info('public://example.jpg');
  $this
    ->assertEqual($entity->image_test->width, $info['width']);
  $this
    ->assertEqual($entity->image_test->height, $info['height']);
  $this
    ->assertEqual($entity->image_test->entity
    ->id(), $this->image
    ->id());
  $this
    ->assertEqual($entity->image_test->entity
    ->uuid(), $this->image
    ->uuid());

  // Make sure the computed entity reflects updates to the referenced file.
  file_unmanaged_copy(DRUPAL_ROOT . '/core/misc/feed.png', 'public://example-2.jpg');
  $image2 = entity_create('file', array(
    'uri' => 'public://example-2.jpg',
  ));
  $image2
    ->save();
  $entity->image_test->fid = $image2
    ->id();
  $entity->image_test->alt = $new_alt = $this
    ->randomName();

  // The width and height is only updated when width is not set.
  $entity->image_test->width = NULL;
  $entity
    ->save();
  $this
    ->assertEqual($entity->image_test->entity
    ->id(), $image2
    ->id());
  $this
    ->assertEqual($entity->image_test->entity->uri, $image2->uri);
  $info = image_get_info('public://example-2.jpg');
  $this
    ->assertEqual($entity->image_test->width, $info['width']);
  $this
    ->assertEqual($entity->image_test->height, $info['height']);
  $this
    ->assertEqual($entity->image_test->alt, $new_alt);

  // Check that the image item can be set to the referenced file directly.
  $entity->image_test = $this->image;
  $this
    ->assertEqual($entity->image_test->fid, $this->image
    ->id());

  // Delete the image and try to save the entity again.
  $this->image
    ->delete();
  $entity = entity_create('entity_test', array(
    'mame' => $this
      ->randomName(),
  ));
  $entity
    ->save();
}