Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / image / src / Tests / ImageFieldValidateTest.php
1 <?php
2
3 namespace Drupal\image\Tests;
4
5 /**
6  * Tests validation functions such as min/max resolution.
7  *
8  * @group image
9  */
10 class ImageFieldValidateTest extends ImageFieldTestBase {
11   /**
12    * Test min/max resolution settings.
13    */
14   public function testResolution() {
15     $field_names = [
16       0 => strtolower($this->randomMachineName()),
17       1 => strtolower($this->randomMachineName()),
18       2 => strtolower($this->randomMachineName()),
19     ];
20     $min_resolution = [
21       'width' => 50,
22       'height' => 50
23     ];
24     $max_resolution = [
25       'width' => 100,
26       'height' => 100
27     ];
28     $no_height_min_resolution = [
29       'width' => 50,
30       'height' => NULL
31     ];
32     $no_height_max_resolution = [
33       'width' => 100,
34       'height' => NULL
35     ];
36     $no_width_min_resolution = [
37       'width' => NULL,
38       'height' => 50
39     ];
40     $no_width_max_resolution = [
41       'width' => NULL,
42       'height' => 100
43     ];
44     $field_settings = [
45       0 => $this->getFieldSettings($min_resolution, $max_resolution),
46       1 => $this->getFieldSettings($no_height_min_resolution, $no_height_max_resolution),
47       2 => $this->getFieldSettings($no_width_min_resolution, $no_width_max_resolution),
48     ];
49     $this->createImageField($field_names[0], 'article', [], $field_settings[0]);
50     $this->createImageField($field_names[1], 'article', [], $field_settings[1]);
51     $this->createImageField($field_names[2], 'article', [], $field_settings[2]);
52
53     // We want a test image that is too small, and a test image that is too
54     // big, so cycle through test image files until we have what we need.
55     $image_that_is_too_big = FALSE;
56     $image_that_is_too_small = FALSE;
57     $image_factory = $this->container->get('image.factory');
58     foreach ($this->drupalGetTestFiles('image') as $image) {
59       $image_file = $image_factory->get($image->uri);
60       if ($image_file->getWidth() > $max_resolution['width']) {
61         $image_that_is_too_big = $image;
62       }
63       if ($image_file->getWidth() < $min_resolution['width']) {
64         $image_that_is_too_small = $image;
65       }
66       if ($image_that_is_too_small && $image_that_is_too_big) {
67         break;
68       }
69     }
70     $this->uploadNodeImage($image_that_is_too_small, $field_names[0], 'article');
71     $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename]));
72     $this->assertRaw(t('The image is too small; the minimum dimensions are %dimensions pixels.', ['%dimensions' => '50x50']));
73     $this->uploadNodeImage($image_that_is_too_big, $field_names[0], 'article');
74     $this->assertText(t('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.'));
75     $this->uploadNodeImage($image_that_is_too_small, $field_names[1], 'article');
76     $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename]));
77     $this->uploadNodeImage($image_that_is_too_big, $field_names[1], 'article');
78     $this->assertText(t('The image was resized to fit within the maximum allowed width of 100 pixels.'));
79     $this->uploadNodeImage($image_that_is_too_small, $field_names[2], 'article');
80     $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename]));
81     $this->uploadNodeImage($image_that_is_too_big, $field_names[2], 'article');
82     $this->assertText(t('The image was resized to fit within the maximum allowed height of 100 pixels.'));
83   }
84
85   /**
86    * Test that required alt/title fields gets validated right.
87    */
88   public function testRequiredAttributes() {
89     $field_name = strtolower($this->randomMachineName());
90     $field_settings = [
91       'alt_field' => 1,
92       'alt_field_required' => 1,
93       'title_field' => 1,
94       'title_field_required' => 1,
95       'required' => 1,
96     ];
97     $instance = $this->createImageField($field_name, 'article', [], $field_settings);
98     $images = $this->drupalGetTestFiles('image');
99     // Let's just use the first image.
100     $image = $images[0];
101     $this->uploadNodeImage($image, $field_name, 'article');
102
103     // Look for form-required for the alt text.
104     $elements = $this->xpath('//label[@for="edit-' . $field_name . '-0-alt" and @class="js-form-required form-required"]/following-sibling::input[@id="edit-' . $field_name . '-0-alt"]');
105
106     $this->assertTrue(isset($elements[0]), 'Required marker is shown for the required alt text.');
107
108     $elements = $this->xpath('//label[@for="edit-' . $field_name . '-0-title" and @class="js-form-required form-required"]/following-sibling::input[@id="edit-' . $field_name . '-0-title"]');
109
110     $this->assertTrue(isset($elements[0]), 'Required marker is shown for the required title text.');
111
112     $this->assertText(t('Alternative text field is required.'));
113     $this->assertText(t('Title field is required.'));
114
115     $instance->setSetting('alt_field_required', 0);
116     $instance->setSetting('title_field_required', 0);
117     $instance->save();
118
119     $edit = [
120       'title[0][value]' => $this->randomMachineName(),
121     ];
122     $this->drupalPostForm('node/add/article', $edit, t('Save'));
123
124     $this->assertNoText(t('Alternative text field is required.'));
125     $this->assertNoText(t('Title field is required.'));
126
127     $instance->setSetting('required', 0);
128     $instance->setSetting('alt_field_required', 1);
129     $instance->setSetting('title_field_required', 1);
130     $instance->save();
131
132     $edit = [
133       'title[0][value]' => $this->randomMachineName(),
134     ];
135     $this->drupalPostForm('node/add/article', $edit, t('Save'));
136
137     $this->assertNoText(t('Alternative text field is required.'));
138     $this->assertNoText(t('Title field is required.'));
139   }
140
141   /**
142    * Returns field settings.
143    *
144    * @param int[] $min_resolution
145    *   The minimum width and height resolution setting.
146    * @param int[] $max_resolution
147    *   The maximum width and height resolution setting.
148    *
149    * @return array
150    */
151   protected function getFieldSettings($min_resolution, $max_resolution) {
152     return [
153       'max_resolution' => $max_resolution['width'] . 'x' . $max_resolution['height'],
154       'min_resolution' => $min_resolution['width'] . 'x' . $min_resolution['height'],
155       'alt_field' => 0,
156     ];
157   }
158
159   /**
160    * Test the validation message is displayed only once for ajax uploads.
161    */
162   public function testAJAXValidationMessage() {
163     $field_name = strtolower($this->randomMachineName());
164     $this->createImageField($field_name, 'article', ['cardinality' => -1]);
165
166     $this->drupalGet('node/add/article');
167     /** @var \Drupal\file\FileInterface[] $text_files */
168     $text_files = $this->drupalGetTestFiles('text');
169     $text_file = reset($text_files);
170     $edit = [
171       'files[' . $field_name . '_0][]' => $this->container->get('file_system')->realpath($text_file->uri),
172       'title[0][value]' => $this->randomMachineName(),
173     ];
174     $this->drupalPostAjaxForm(NULL, $edit, $field_name . '_0_upload_button');
175     $elements = $this->xpath('//div[contains(@class, :class)]', [
176       ':class' => 'messages--error',
177     ]);
178     $this->assertEqual(count($elements), 1, 'Ajax validation messages are displayed once.');
179   }
180
181 }