d04a1c4d702f33c02083001f99a635a806d4de3c
[yaffs-website] / web / core / modules / image / src / Tests / ImageFieldDefaultImagesTest.php
1 <?php
2
3 namespace Drupal\image\Tests;
4 use Drupal\Component\Utility\Unicode;
5 use Drupal\Core\Entity\Entity\EntityViewDisplay;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\file\Entity\File;
8 use Drupal\field\Entity\FieldStorageConfig;
9
10 /**
11  * Tests setting up default images both to the field and field field.
12  *
13  * @group image
14  */
15 class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['field_ui'];
23
24   /**
25    * Tests CRUD for fields and fields fields with default images.
26    */
27   public function testDefaultImages() {
28     $node_storage = $this->container->get('entity.manager')->getStorage('node');
29     // Create files to use as the default images.
30     $files = $this->drupalGetTestFiles('image');
31     // Create 10 files so the default image fids are not a single value.
32     for ($i = 1; $i <= 10; $i++) {
33       $filename = $this->randomMachineName() . "$i";
34       $desired_filepath = 'public://' . $filename;
35       file_unmanaged_copy($files[0]->uri, $desired_filepath, FILE_EXISTS_ERROR);
36       $file = File::create(['uri' => $desired_filepath, 'filename' => $filename, 'name' => $filename]);
37       $file->save();
38     }
39     $default_images = [];
40     foreach (['field', 'field', 'field2', 'field_new', 'field_new'] as $image_target) {
41       $file = File::create((array) array_pop($files));
42       $file->save();
43       $default_images[$image_target] = $file;
44     }
45
46     // Create an image field and add an field to the article content type.
47     $field_name = strtolower($this->randomMachineName());
48     $storage_settings['default_image'] = [
49       'uuid' => $default_images['field']->uuid(),
50       'alt' => '',
51       'title' => '',
52       'width' => 0,
53       'height' => 0,
54     ];
55     $field_settings['default_image'] = [
56       'uuid' => $default_images['field']->uuid(),
57       'alt' => '',
58       'title' => '',
59       'width' => 0,
60       'height' => 0,
61     ];
62     $widget_settings = [
63       'preview_image_style' => 'medium',
64     ];
65     $field = $this->createImageField($field_name, 'article', $storage_settings, $field_settings, $widget_settings);
66
67     // The field default image id should be 2.
68     $this->assertEqual($field->getSetting('default_image')['uuid'], $default_images['field']->uuid());
69
70     // Also test \Drupal\field\Entity\FieldConfig::getSettings().
71     $this->assertEqual($field->getSettings()['default_image']['uuid'], $default_images['field']->uuid());
72
73     $field_storage = $field->getFieldStorageDefinition();
74
75     // The field default image id should be 1.
76     $this->assertEqual($field_storage->getSetting('default_image')['uuid'], $default_images['field']->uuid());
77
78     // Also test \Drupal\field\Entity\FieldStorageConfig::getSettings().
79     $this->assertEqual($field_storage->getSettings()['default_image']['uuid'], $default_images['field']->uuid());
80
81     // Add another field with another default image to the page content type.
82     $field2 = FieldConfig::create([
83       'field_storage' => $field_storage,
84       'bundle' => 'page',
85       'label' => $field->label(),
86       'required' => $field->isRequired(),
87       'settings' => [
88         'default_image' => [
89           'uuid' => $default_images['field2']->uuid(),
90           'alt' => '',
91           'title' => '',
92           'width' => 0,
93           'height' => 0,
94         ],
95       ],
96     ]);
97     $field2->save();
98
99     $widget_settings = entity_get_form_display('node', $field->getTargetBundle(), 'default')->getComponent($field_name);
100     entity_get_form_display('node', 'page', 'default')
101       ->setComponent($field_name, $widget_settings)
102       ->save();
103     entity_get_display('node', 'page', 'default')
104       ->setComponent($field_name)
105       ->save();
106
107     // Confirm the defaults are present on the article field settings form.
108     $field_id = $field->id();
109     $this->drupalGet("admin/structure/types/manage/article/fields/$field_id/storage");
110     $this->assertFieldByXpath(
111       '//input[@name="settings[default_image][uuid][fids]"]',
112       $default_images['field']->id(),
113       format_string(
114         'Article image field default equals expected file ID of @fid.',
115         ['@fid' => $default_images['field']->id()]
116       )
117     );
118     // Confirm the defaults are present on the article field edit form.
119     $this->drupalGet("admin/structure/types/manage/article/fields/$field_id");
120     $this->assertFieldByXpath(
121       '//input[@name="settings[default_image][uuid][fids]"]',
122       $default_images['field']->id(),
123       format_string(
124         'Article image field field default equals expected file ID of @fid.',
125         ['@fid' => $default_images['field']->id()]
126       )
127     );
128
129     // Confirm the defaults are present on the page field settings form.
130     $this->drupalGet("admin/structure/types/manage/page/fields/$field_id/storage");
131     $this->assertFieldByXpath(
132       '//input[@name="settings[default_image][uuid][fids]"]',
133       $default_images['field']->id(),
134       format_string(
135         'Page image field default equals expected file ID of @fid.',
136         ['@fid' => $default_images['field']->id()]
137       )
138     );
139     // Confirm the defaults are present on the page field edit form.
140     $field2_id = $field2->id();
141     $this->drupalGet("admin/structure/types/manage/page/fields/$field2_id");
142     $this->assertFieldByXpath(
143       '//input[@name="settings[default_image][uuid][fids]"]',
144       $default_images['field2']->id(),
145       format_string(
146         'Page image field field default equals expected file ID of @fid.',
147         ['@fid' => $default_images['field2']->id()]
148       )
149     );
150
151     // Confirm that the image default is shown for a new article node.
152     $article = $this->drupalCreateNode(['type' => 'article']);
153     $article_built = $this->drupalBuildEntityView($article);
154     $this->assertEqual(
155       $article_built[$field_name][0]['#item']->target_id,
156       $default_images['field']->id(),
157       format_string(
158         'A new article node without an image has the expected default image file ID of @fid.',
159         ['@fid' => $default_images['field']->id()]
160       )
161     );
162
163     // Also check that the field renders without warnings when the label is
164     // hidden.
165     EntityViewDisplay::load('node.article.default')
166       ->setComponent($field_name, ['label' => 'hidden', 'type' => 'image'])
167       ->save();
168     $this->drupalGet('node/' . $article->id());
169
170     // Confirm that the image default is shown for a new page node.
171     $page = $this->drupalCreateNode(['type' => 'page']);
172     $page_built = $this->drupalBuildEntityView($page);
173     $this->assertEqual(
174       $page_built[$field_name][0]['#item']->target_id,
175       $default_images['field2']->id(),
176       format_string(
177         'A new page node without an image has the expected default image file ID of @fid.',
178         ['@fid' => $default_images['field2']->id()]
179       )
180     );
181
182     // Upload a new default for the field storage.
183     $default_image_settings = $field_storage->getSetting('default_image');
184     $default_image_settings['uuid'] = $default_images['field_new']->uuid();
185     $field_storage->setSetting('default_image', $default_image_settings);
186     $field_storage->save();
187
188     // Confirm that the new default is used on the article field settings form.
189     $this->drupalGet("admin/structure/types/manage/article/fields/$field_id/storage");
190     $this->assertFieldByXpath(
191       '//input[@name="settings[default_image][uuid][fids]"]',
192       $default_images['field_new']->id(),
193       format_string(
194         'Updated image field default equals expected file ID of @fid.',
195         ['@fid' => $default_images['field_new']->id()]
196       )
197     );
198
199     // Reload the nodes and confirm the field field defaults are used.
200     $node_storage->resetCache([$article->id(), $page->id()]);
201     $article_built = $this->drupalBuildEntityView($article = $node_storage->load($article->id()));
202     $page_built = $this->drupalBuildEntityView($page = $node_storage->load($page->id()));
203     $this->assertEqual(
204       $article_built[$field_name][0]['#item']->target_id,
205       $default_images['field']->id(),
206       format_string(
207         'An existing article node without an image has the expected default image file ID of @fid.',
208         ['@fid' => $default_images['field']->id()]
209       )
210     );
211     $this->assertEqual(
212       $page_built[$field_name][0]['#item']->target_id,
213       $default_images['field2']->id(),
214       format_string(
215         'An existing page node without an image has the expected default image file ID of @fid.',
216         ['@fid' => $default_images['field2']->id()]
217       )
218     );
219
220     // Upload a new default for the article's field field.
221     $default_image_settings = $field->getSetting('default_image');
222     $default_image_settings['uuid'] = $default_images['field_new']->uuid();
223     $field->setSetting('default_image', $default_image_settings);
224     $field->save();
225
226     // Confirm the new field field default is used on the article field
227     // admin form.
228     $this->drupalGet("admin/structure/types/manage/article/fields/$field_id");
229     $this->assertFieldByXpath(
230       '//input[@name="settings[default_image][uuid][fids]"]',
231       $default_images['field_new']->id(),
232       format_string(
233         'Updated article image field field default equals expected file ID of @fid.',
234         ['@fid' => $default_images['field_new']->id()]
235       )
236     );
237
238     // Reload the nodes.
239     $node_storage->resetCache([$article->id(), $page->id()]);
240     $article_built = $this->drupalBuildEntityView($article = $node_storage->load($article->id()));
241     $page_built = $this->drupalBuildEntityView($page = $node_storage->load($page->id()));
242
243     // Confirm the article uses the new default.
244     $this->assertEqual(
245       $article_built[$field_name][0]['#item']->target_id,
246       $default_images['field_new']->id(),
247       format_string(
248         'An existing article node without an image has the expected default image file ID of @fid.',
249         ['@fid' => $default_images['field_new']->id()]
250       )
251     );
252     // Confirm the page remains unchanged.
253     $this->assertEqual(
254       $page_built[$field_name][0]['#item']->target_id,
255       $default_images['field2']->id(),
256       format_string(
257         'An existing page node without an image has the expected default image file ID of @fid.',
258         ['@fid' => $default_images['field2']->id()]
259       )
260     );
261
262     // Confirm the default image is shown on the node form.
263     $file = File::load($default_images['field_new']->id());
264     $this->drupalGet('node/add/article');
265     $this->assertRaw($file->getFilename());
266
267     // Remove the instance default from articles.
268     $default_image_settings = $field->getSetting('default_image');
269     $default_image_settings['uuid'] = 0;
270     $field->setSetting('default_image', $default_image_settings);
271     $field->save();
272
273     // Confirm the article field field default has been removed.
274     $this->drupalGet("admin/structure/types/manage/article/fields/$field_id");
275     $this->assertFieldByXpath(
276       '//input[@name="settings[default_image][uuid][fids]"]',
277       '',
278       'Updated article image field field default has been successfully removed.'
279     );
280
281     // Reload the nodes.
282     $node_storage->resetCache([$article->id(), $page->id()]);
283     $article_built = $this->drupalBuildEntityView($article = $node_storage->load($article->id()));
284     $page_built = $this->drupalBuildEntityView($page = $node_storage->load($page->id()));
285     // Confirm the article uses the new field (not field) default.
286     $this->assertEqual(
287       $article_built[$field_name][0]['#item']->target_id,
288       $default_images['field_new']->id(),
289       format_string(
290         'An existing article node without an image has the expected default image file ID of @fid.',
291         ['@fid' => $default_images['field_new']->id()]
292       )
293     );
294     // Confirm the page remains unchanged.
295     $this->assertEqual(
296       $page_built[$field_name][0]['#item']->target_id,
297       $default_images['field2']->id(),
298       format_string(
299         'An existing page node without an image has the expected default image file ID of @fid.',
300         ['@fid' => $default_images['field2']->id()]
301       )
302     );
303
304     $non_image = $this->drupalGetTestFiles('text');
305     $this->drupalPostForm(NULL, ['files[settings_default_image_uuid]' => drupal_realpath($non_image[0]->uri)], t("Upload"));
306     $this->assertText('The specified file text-0.txt could not be uploaded.');
307     $this->assertText('Only files with the following extensions are allowed: png gif jpg jpeg.');
308
309     // Confirm the default image is shown on the node form.
310     $file = File::load($default_images['field_new']->id());
311     $this->drupalGet('node/add/article');
312     $this->assertRaw($file->getFilename());
313   }
314
315   /**
316    * Tests image field and field having an invalid default image.
317    */
318   public function testInvalidDefaultImage() {
319     $field_storage = FieldStorageConfig::create([
320       'field_name' => Unicode::strtolower($this->randomMachineName()),
321       'entity_type' => 'node',
322       'type' => 'image',
323       'settings' => [
324         'default_image' => [
325           'uuid' => 100000,
326         ]
327       ],
328     ]);
329     $field_storage->save();
330     $settings = $field_storage->getSettings();
331     // The non-existent default image should not be saved.
332     $this->assertNull($settings['default_image']['uuid']);
333
334     $field = FieldConfig::create([
335       'field_storage' => $field_storage,
336       'bundle' => 'page',
337       'label' => $this->randomMachineName(),
338       'settings' => [
339         'default_image' => [
340           'uuid' => 100000,
341         ]
342       ],
343     ]);
344     $field->save();
345     $settings = $field->getSettings();
346     // The non-existent default image should not be saved.
347     $this->assertNull($settings['default_image']['uuid']);
348   }
349
350 }