9621423823d566cb4e130d9fe59e842c2f6c9157
[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 use Drupal\user\Entity\Role;
15
16 /**
17  * Tests using entity fields of the image field type.
18  *
19  * @group image
20  */
21 class ImageItemTest extends FieldKernelTestBase {
22
23   /**
24    * Modules to enable.
25    *
26    * @var array
27    */
28   public static $modules = ['file', 'image'];
29
30   /**
31    * Created file entity.
32    *
33    * @var \Drupal\file\Entity\File
34    */
35   protected $image;
36
37   /**
38    * @var \Drupal\Core\Image\ImageFactory
39    */
40   protected $imageFactory;
41
42   protected function setUp() {
43     parent::setUp();
44
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();
52
53     $this->installEntitySchema('file');
54     $this->installSchema('file', ['file_usage']);
55
56     FieldStorageConfig::create([
57       'entity_type' => 'entity_test',
58       'field_name' => 'image_test',
59       'type' => 'image',
60       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
61     ])->save();
62     FieldConfig::create([
63       'entity_type' => 'entity_test',
64       'field_name' => 'image_test',
65       'bundle' => 'entity_test',
66       'settings' => [
67         'file_extensions' => 'jpg',
68       ],
69     ])->save();
70     file_unmanaged_copy($this->root . '/core/misc/druplicon.png', 'public://example.jpg');
71     $this->image = File::create([
72       'uri' => 'public://example.jpg',
73     ]);
74     $this->image->save();
75     $this->imageFactory = $this->container->get('image.factory');
76   }
77
78   /**
79    * Tests using entity fields of the image field type.
80    */
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();
88     $entity->save();
89
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());
101
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',
106     ]);
107     $image2->save();
108
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;
113     $entity->save();
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);
120
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());
124
125     // Delete the image and try to save the entity again.
126     $this->image->delete();
127     $entity = EntityTest::create(['mame' => $this->randomMachineName()]);
128     $entity->save();
129
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);
134
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');
140   }
141
142   /**
143    * Tests a malformed image.
144    */
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.
151     try {
152       $entity->save();
153       $this->fail('Exception did not fail');
154     }
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);
160     }
161
162   }
163
164 }