951d1907f88f1b9ed4e3c0963b86d3d73e1caf08
[yaffs-website] / web / core / modules / image / tests / src / Kernel / ImageItemTest.php
1 <?php
2
3 namespace Drupal\Tests\image\Kernel;
4
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
15 /**
16  * Tests using entity fields of the image field type.
17  *
18  * @group image
19  */
20 class ImageItemTest extends FieldKernelTestBase {
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = ['file', 'image'];
28
29   /**
30    * Created file entity.
31    *
32    * @var \Drupal\file\Entity\File
33    */
34   protected $image;
35
36   /**
37    * @var \Drupal\Core\Image\ImageFactory
38    */
39   protected $imageFactory;
40
41   protected function setUp() {
42     parent::setUp();
43
44     $this->installEntitySchema('file');
45     $this->installSchema('file', ['file_usage']);
46
47     FieldStorageConfig::create([
48       'entity_type' => 'entity_test',
49       'field_name' => 'image_test',
50       'type' => 'image',
51       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
52     ])->save();
53     FieldConfig::create([
54       'entity_type' => 'entity_test',
55       'field_name' => 'image_test',
56       'bundle' => 'entity_test',
57       'settings' => [
58         'file_extensions' => 'jpg',
59       ],
60     ])->save();
61     file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg');
62     $this->image = File::create([
63       'uri' => 'public://example.jpg',
64     ]);
65     $this->image->save();
66     $this->imageFactory = $this->container->get('image.factory');
67   }
68
69   /**
70    * Tests using entity fields of the image field type.
71    */
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();
79     $entity->save();
80
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());
92
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',
97     ]);
98     $image2->save();
99
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;
104     $entity->save();
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);
111
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());
115
116     // Delete the image and try to save the entity again.
117     $this->image->delete();
118     $entity = EntityTest::create(['mame' => $this->randomMachineName()]);
119     $entity->save();
120
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);
125
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');
131   }
132
133   /**
134    * Tests a malformed image.
135    */
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.
142     try {
143       $entity->save();
144       $this->fail('Exception did not fail');
145     }
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);
151     }
152
153   }
154
155 }