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;
14 use Drupal\user\Entity\Role;
17 * Tests using entity fields of the image field type.
21 class ImageItemTest extends FieldKernelTestBase {
28 public static $modules = ['file', 'image'];
31 * Created file entity.
33 * @var \Drupal\file\Entity\File
38 * @var \Drupal\Core\Image\ImageFactory
40 protected $imageFactory;
42 protected function setUp() {
45 $this->installEntitySchema('user');
46 $this->installConfig(['user']);
47 // Give anonymous users permission to access content, so that we can view
48 // and download public file.
49 $anonymous_role = Role::load(Role::ANONYMOUS_ID);
50 $anonymous_role->grantPermission('access content');
51 $anonymous_role->save();
53 $this->installEntitySchema('file');
54 $this->installSchema('file', ['file_usage']);
56 FieldStorageConfig::create([
57 'entity_type' => 'entity_test',
58 'field_name' => 'image_test',
60 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
63 'entity_type' => 'entity_test',
64 'field_name' => 'image_test',
65 'bundle' => 'entity_test',
67 'file_extensions' => 'jpg',
70 file_unmanaged_copy($this->root . '/core/misc/druplicon.png', 'public://example.jpg');
71 $this->image = File::create([
72 'uri' => 'public://example.jpg',
75 $this->imageFactory = $this->container->get('image.factory');
79 * Tests using entity fields of the image field type.
81 public function testImageItem() {
82 // Create a test entity with the image field set.
83 $entity = EntityTest::create();
84 $entity->image_test->target_id = $this->image->id();
85 $entity->image_test->alt = $alt = $this->randomMachineName();
86 $entity->image_test->title = $title = $this->randomMachineName();
87 $entity->name->value = $this->randomMachineName();
90 $entity = EntityTest::load($entity->id());
91 $this->assertTrue($entity->image_test instanceof FieldItemListInterface, 'Field implements interface.');
92 $this->assertTrue($entity->image_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
93 $this->assertEqual($entity->image_test->target_id, $this->image->id());
94 $this->assertEqual($entity->image_test->alt, $alt);
95 $this->assertEqual($entity->image_test->title, $title);
96 $image = $this->imageFactory->get('public://example.jpg');
97 $this->assertEqual($entity->image_test->width, $image->getWidth());
98 $this->assertEqual($entity->image_test->height, $image->getHeight());
99 $this->assertEqual($entity->image_test->entity->id(), $this->image->id());
100 $this->assertEqual($entity->image_test->entity->uuid(), $this->image->uuid());
102 // Make sure the computed entity reflects updates to the referenced file.
103 file_unmanaged_copy($this->root . '/core/misc/druplicon.png', 'public://example-2.jpg');
104 $image2 = File::create([
105 'uri' => 'public://example-2.jpg',
109 $entity->image_test->target_id = $image2->id();
110 $entity->image_test->alt = $new_alt = $this->randomMachineName();
111 // The width and height is only updated when width is not set.
112 $entity->image_test->width = NULL;
114 $this->assertEqual($entity->image_test->entity->id(), $image2->id());
115 $this->assertEqual($entity->image_test->entity->getFileUri(), $image2->getFileUri());
116 $image = $this->imageFactory->get('public://example-2.jpg');
117 $this->assertEqual($entity->image_test->width, $image->getWidth());
118 $this->assertEqual($entity->image_test->height, $image->getHeight());
119 $this->assertEqual($entity->image_test->alt, $new_alt);
121 // Check that the image item can be set to the referenced file directly.
122 $entity->image_test = $this->image;
123 $this->assertEqual($entity->image_test->target_id, $this->image->id());
125 // Delete the image and try to save the entity again.
126 $this->image->delete();
127 $entity = EntityTest::create(['mame' => $this->randomMachineName()]);
130 // Test image item properties.
131 $expected = ['target_id', 'entity', 'alt', 'title', 'width', 'height'];
132 $properties = $entity->getFieldDefinition('image_test')->getFieldStorageDefinition()->getPropertyDefinitions();
133 $this->assertEqual(array_keys($properties), $expected);
135 // Test the generateSampleValue() method.
136 $entity = EntityTest::create();
137 $entity->image_test->generateSampleItems();
138 $this->entityValidateAndSave($entity);
139 $this->assertEqual($entity->image_test->entity->get('filemime')->value, 'image/jpeg');
143 * Tests a malformed image.
145 public function testImageItemMalformed() {
146 // Validate entity is an image and don't gather dimensions if it is not.
147 $entity = EntityTest::create();
148 $entity->image_test = NULL;
149 $entity->image_test->target_id = 9999;
150 // PHPUnit re-throws E_USER_WARNING as an exception.
153 $this->fail('Exception did not fail');
155 catch (EntityStorageException $exception) {
156 $this->assertInstanceOf(\PHPUnit_Framework_Error_Warning::class, $exception->getPrevious());
157 $this->assertEquals($exception->getMessage(), 'Missing file with ID 9999.');
158 $this->assertEmpty($entity->image_test->width);
159 $this->assertEmpty($entity->image_test->height);