3 namespace Drupal\Tests\image\Kernel;
5 use Drupal\Core\Entity\EntityStorageException;
6 use Drupal\Core\Field\FieldItemInterface;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\Core\Field\FieldStorageDefinitionInterface;
9 use Drupal\entity_test\Entity\EntityTest;
10 use Drupal\field\Entity\FieldConfig;
11 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
12 use Drupal\field\Entity\FieldStorageConfig;
13 use Drupal\file\Entity\File;
16 * Tests using entity fields of the image field type.
20 class ImageItemTest extends FieldKernelTestBase {
27 public static $modules = ['file', 'image'];
30 * Created file entity.
32 * @var \Drupal\file\Entity\File
37 * @var \Drupal\Core\Image\ImageFactory
39 protected $imageFactory;
41 protected function setUp() {
44 $this->installEntitySchema('file');
45 $this->installSchema('file', ['file_usage']);
47 FieldStorageConfig::create([
48 'entity_type' => 'entity_test',
49 'field_name' => 'image_test',
51 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
54 'entity_type' => 'entity_test',
55 'field_name' => 'image_test',
56 'bundle' => 'entity_test',
58 'file_extensions' => 'jpg',
61 file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg');
62 $this->image = File::create([
63 'uri' => 'public://example.jpg',
66 $this->imageFactory = $this->container->get('image.factory');
70 * Tests using entity fields of the image field type.
72 public function testImageItem() {
73 // Create a test entity with the image field set.
74 $entity = EntityTest::create();
75 $entity->image_test->target_id = $this->image->id();
76 $entity->image_test->alt = $alt = $this->randomMachineName();
77 $entity->image_test->title = $title = $this->randomMachineName();
78 $entity->name->value = $this->randomMachineName();
81 $entity = EntityTest::load($entity->id());
82 $this->assertTrue($entity->image_test instanceof FieldItemListInterface, 'Field implements interface.');
83 $this->assertTrue($entity->image_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
84 $this->assertEqual($entity->image_test->target_id, $this->image->id());
85 $this->assertEqual($entity->image_test->alt, $alt);
86 $this->assertEqual($entity->image_test->title, $title);
87 $image = $this->imageFactory->get('public://example.jpg');
88 $this->assertEqual($entity->image_test->width, $image->getWidth());
89 $this->assertEqual($entity->image_test->height, $image->getHeight());
90 $this->assertEqual($entity->image_test->entity->id(), $this->image->id());
91 $this->assertEqual($entity->image_test->entity->uuid(), $this->image->uuid());
93 // Make sure the computed entity reflects updates to the referenced file.
94 file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example-2.jpg');
95 $image2 = File::create([
96 'uri' => 'public://example-2.jpg',
100 $entity->image_test->target_id = $image2->id();
101 $entity->image_test->alt = $new_alt = $this->randomMachineName();
102 // The width and height is only updated when width is not set.
103 $entity->image_test->width = NULL;
105 $this->assertEqual($entity->image_test->entity->id(), $image2->id());
106 $this->assertEqual($entity->image_test->entity->getFileUri(), $image2->getFileUri());
107 $image = $this->imageFactory->get('public://example-2.jpg');
108 $this->assertEqual($entity->image_test->width, $image->getWidth());
109 $this->assertEqual($entity->image_test->height, $image->getHeight());
110 $this->assertEqual($entity->image_test->alt, $new_alt);
112 // Check that the image item can be set to the referenced file directly.
113 $entity->image_test = $this->image;
114 $this->assertEqual($entity->image_test->target_id, $this->image->id());
116 // Delete the image and try to save the entity again.
117 $this->image->delete();
118 $entity = EntityTest::create(['mame' => $this->randomMachineName()]);
121 // Test image item properties.
122 $expected = ['target_id', 'entity', 'alt', 'title', 'width', 'height'];
123 $properties = $entity->getFieldDefinition('image_test')->getFieldStorageDefinition()->getPropertyDefinitions();
124 $this->assertEqual(array_keys($properties), $expected);
126 // Test the generateSampleValue() method.
127 $entity = EntityTest::create();
128 $entity->image_test->generateSampleItems();
129 $this->entityValidateAndSave($entity);
130 $this->assertEqual($entity->image_test->entity->get('filemime')->value, 'image/jpeg');
134 * Tests a malformed image.
136 public function testImageItemMalformed() {
137 // Validate entity is an image and don't gather dimensions if it is not.
138 $entity = EntityTest::create();
139 $entity->image_test = NULL;
140 $entity->image_test->target_id = 9999;
141 // PHPUnit re-throws E_USER_WARNING as an exception.
144 $this->fail('Exception did not fail');
146 catch (EntityStorageException $exception) {
147 $this->assertInstanceOf(\PHPUnit_Framework_Error_Warning::class, $exception->getPrevious());
148 $this->assertEquals($exception->getMessage(), 'Missing file with ID 9999.');
149 $this->assertEmpty($entity->image_test->width);
150 $this->assertEmpty($entity->image_test->height);