function ImageItemTest::testImageItem

Same name in other branches
  1. 9 core/modules/image/tests/src/Kernel/ImageItemTest.php \Drupal\Tests\image\Kernel\ImageItemTest::testImageItem()
  2. 8.9.x core/modules/image/tests/src/Kernel/ImageItemTest.php \Drupal\Tests\image\Kernel\ImageItemTest::testImageItem()
  3. 10 core/modules/image/tests/src/Kernel/ImageItemTest.php \Drupal\Tests\image\Kernel\ImageItemTest::testImageItem()

Tests using entity fields of the image field type.

File

core/modules/image/tests/src/Kernel/ImageItemTest.php, line 100

Class

ImageItemTest
Tests using entity fields of the image field type.

Namespace

Drupal\Tests\image\Kernel

Code

public function testImageItem() : void {
    // Create a test entity with the image field set.
    $entity = EntityTest::create();
    $entity->image_test->target_id = $this->image
        ->id();
    $entity->image_test->alt = $alt = $this->randomMachineName();
    $entity->image_test->title = $title = $this->randomMachineName();
    $entity->name->value = $this->randomMachineName();
    $entity->save();
    $entity = EntityTest::load($entity->id());
    $this->assertInstanceOf(FieldItemListInterface::class, $entity->image_test);
    $this->assertInstanceOf(FieldItemInterface::class, $entity->image_test[0]);
    $this->assertEquals($this->image
        ->id(), $entity->image_test->target_id);
    $this->assertEquals($alt, $entity->image_test->alt);
    $this->assertEquals($title, $entity->image_test->title);
    $image = $this->imageFactory
        ->get('public://example.jpg');
    $this->assertEquals($image->getWidth(), $entity->image_test->width);
    $this->assertEquals($image->getHeight(), $entity->image_test->height);
    $this->assertEquals($this->image
        ->id(), $entity->image_test->entity
        ->id());
    $this->assertEquals($this->image
        ->uuid(), $entity->image_test->entity
        ->uuid());
    // Make sure the computed entity reflects updates to the referenced file.
    \Drupal::service('file_system')->copy($this->root . '/core/misc/druplicon.png', 'public://example-2.jpg');
    $image2 = File::create([
        'uri' => 'public://example-2.jpg',
    ]);
    $image2->save();
    $entity->image_test->target_id = $image2->id();
    $entity->image_test->alt = $new_alt = $this->randomMachineName();
    // The width and height is only updated when width is not set.
    $entity->image_test->width = NULL;
    $entity->save();
    $this->assertEquals($image2->id(), $entity->image_test->entity
        ->id());
    $this->assertEquals($image2->getFileUri(), $entity->image_test->entity
        ->getFileUri());
    $image = $this->imageFactory
        ->get('public://example-2.jpg');
    $this->assertEquals($image->getWidth(), $entity->image_test->width);
    $this->assertEquals($image->getHeight(), $entity->image_test->height);
    $this->assertEquals($new_alt, $entity->image_test->alt);
    // Check that the image item can be set to the referenced file directly.
    $entity->image_test = $this->image;
    $this->assertEquals($this->image
        ->id(), $entity->image_test->target_id);
    // Delete the image and try to save the entity again.
    $this->image
        ->delete();
    $entity = EntityTest::create([
        'name' => $this->randomMachineName(),
    ]);
    $entity->save();
    // Test image item properties.
    $expected = [
        'target_id',
        'entity',
        'alt',
        'title',
        'width',
        'height',
    ];
    $properties = $entity->getFieldDefinition('image_test')
        ->getFieldStorageDefinition()
        ->getPropertyDefinitions();
    $this->assertEquals($expected, array_keys($properties));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.