Mercurial > hg > isophonics-drupal-site
comparison core/modules/image/tests/src/Functional/ImageFieldValidateTest.php @ 16:c2387f117808
Routine composer update
author | Chris Cannam |
---|---|
date | Tue, 10 Jul 2018 15:07:59 +0100 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
15:e200cb7efeb3 | 16:c2387f117808 |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\image\Functional; | |
4 | |
5 use Drupal\Tests\TestFileCreationTrait; | |
6 | |
7 /** | |
8 * Tests validation functions such as min/max resolution. | |
9 * | |
10 * @group image | |
11 */ | |
12 class ImageFieldValidateTest extends ImageFieldTestBase { | |
13 | |
14 use TestFileCreationTrait { | |
15 getTestFiles as drupalGetTestFiles; | |
16 compareFiles as drupalCompareFiles; | |
17 } | |
18 | |
19 /** | |
20 * Test image validity. | |
21 */ | |
22 public function testValid() { | |
23 $file_system = $this->container->get('file_system'); | |
24 $image_files = $this->drupalGetTestFiles('image'); | |
25 | |
26 $field_name = strtolower($this->randomMachineName()); | |
27 $this->createImageField($field_name, 'article', [], ['file_directory' => 'test-upload']); | |
28 $expected_path = 'public://test-upload'; | |
29 | |
30 // Create alt text for the image. | |
31 $alt = $this->randomMachineName(); | |
32 | |
33 // Create a node with a valid image. | |
34 $node = $this->uploadNodeImage($image_files[0], $field_name, 'article', $alt); | |
35 $this->assertTrue(file_exists($expected_path . '/' . $image_files[0]->filename)); | |
36 | |
37 // Remove the image. | |
38 $this->drupalPostForm('node/' . $node . '/edit', [], t('Remove')); | |
39 $this->drupalPostForm(NULL, [], t('Save')); | |
40 | |
41 // Get invalid image test files from simpletest. | |
42 $files = file_scan_directory(drupal_get_path('module', 'simpletest') . '/files', '/invalid-img-.*/'); | |
43 $invalid_image_files = []; | |
44 foreach ($files as $file) { | |
45 $invalid_image_files[$file->filename] = $file; | |
46 } | |
47 | |
48 // Try uploading a zero-byte image. | |
49 $zero_size_image = $invalid_image_files['invalid-img-zero-size.png']; | |
50 $edit = [ | |
51 'files[' . $field_name . '_0]' => $file_system->realpath($zero_size_image->uri), | |
52 ]; | |
53 $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload')); | |
54 $this->assertFalse(file_exists($expected_path . '/' . $zero_size_image->filename)); | |
55 | |
56 // Try uploading an invalid image. | |
57 $invalid_image = $invalid_image_files['invalid-img-test.png']; | |
58 $edit = [ | |
59 'files[' . $field_name . '_0]' => $file_system->realpath($invalid_image->uri), | |
60 ]; | |
61 $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload')); | |
62 $this->assertFalse(file_exists($expected_path . '/' . $invalid_image->filename)); | |
63 | |
64 // Upload a valid image again. | |
65 $valid_image = $image_files[0]; | |
66 $edit = [ | |
67 'files[' . $field_name . '_0]' => $file_system->realpath($valid_image->uri), | |
68 ]; | |
69 $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload')); | |
70 $this->assertTrue(file_exists($expected_path . '/' . $valid_image->filename)); | |
71 } | |
72 | |
73 /** | |
74 * Test min/max resolution settings. | |
75 */ | |
76 public function testResolution() { | |
77 $field_names = [ | |
78 0 => strtolower($this->randomMachineName()), | |
79 1 => strtolower($this->randomMachineName()), | |
80 2 => strtolower($this->randomMachineName()), | |
81 ]; | |
82 $min_resolution = [ | |
83 'width' => 50, | |
84 'height' => 50 | |
85 ]; | |
86 $max_resolution = [ | |
87 'width' => 100, | |
88 'height' => 100 | |
89 ]; | |
90 $no_height_min_resolution = [ | |
91 'width' => 50, | |
92 'height' => NULL | |
93 ]; | |
94 $no_height_max_resolution = [ | |
95 'width' => 100, | |
96 'height' => NULL | |
97 ]; | |
98 $no_width_min_resolution = [ | |
99 'width' => NULL, | |
100 'height' => 50 | |
101 ]; | |
102 $no_width_max_resolution = [ | |
103 'width' => NULL, | |
104 'height' => 100 | |
105 ]; | |
106 $field_settings = [ | |
107 0 => $this->getFieldSettings($min_resolution, $max_resolution), | |
108 1 => $this->getFieldSettings($no_height_min_resolution, $no_height_max_resolution), | |
109 2 => $this->getFieldSettings($no_width_min_resolution, $no_width_max_resolution), | |
110 ]; | |
111 $this->createImageField($field_names[0], 'article', [], $field_settings[0]); | |
112 $this->createImageField($field_names[1], 'article', [], $field_settings[1]); | |
113 $this->createImageField($field_names[2], 'article', [], $field_settings[2]); | |
114 | |
115 // We want a test image that is too small, and a test image that is too | |
116 // big, so cycle through test image files until we have what we need. | |
117 $image_that_is_too_big = FALSE; | |
118 $image_that_is_too_small = FALSE; | |
119 $image_factory = $this->container->get('image.factory'); | |
120 foreach ($this->drupalGetTestFiles('image') as $image) { | |
121 $image_file = $image_factory->get($image->uri); | |
122 if ($image_file->getWidth() > $max_resolution['width']) { | |
123 $image_that_is_too_big = $image; | |
124 } | |
125 if ($image_file->getWidth() < $min_resolution['width']) { | |
126 $image_that_is_too_small = $image; | |
127 $image_that_is_too_small_file = $image_file; | |
128 } | |
129 if ($image_that_is_too_small && $image_that_is_too_big) { | |
130 break; | |
131 } | |
132 } | |
133 $this->uploadNodeImage($image_that_is_too_small, $field_names[0], 'article'); | |
134 $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename])); | |
135 $this->assertRaw(t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', [ | |
136 '%dimensions' => '50x50', | |
137 '%width' => $image_that_is_too_small_file->getWidth(), | |
138 '%height' => $image_that_is_too_small_file->getHeight(), | |
139 ])); | |
140 $this->uploadNodeImage($image_that_is_too_big, $field_names[0], 'article'); | |
141 $this->assertText(t('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.')); | |
142 $this->uploadNodeImage($image_that_is_too_small, $field_names[1], 'article'); | |
143 $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename])); | |
144 $this->uploadNodeImage($image_that_is_too_big, $field_names[1], 'article'); | |
145 $this->assertText(t('The image was resized to fit within the maximum allowed width of 100 pixels.')); | |
146 $this->uploadNodeImage($image_that_is_too_small, $field_names[2], 'article'); | |
147 $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename])); | |
148 $this->uploadNodeImage($image_that_is_too_big, $field_names[2], 'article'); | |
149 $this->assertText(t('The image was resized to fit within the maximum allowed height of 100 pixels.')); | |
150 } | |
151 | |
152 /** | |
153 * Test that required alt/title fields gets validated right. | |
154 */ | |
155 public function testRequiredAttributes() { | |
156 $field_name = strtolower($this->randomMachineName()); | |
157 $field_settings = [ | |
158 'alt_field' => 1, | |
159 'alt_field_required' => 1, | |
160 'title_field' => 1, | |
161 'title_field_required' => 1, | |
162 'required' => 1, | |
163 ]; | |
164 $instance = $this->createImageField($field_name, 'article', [], $field_settings); | |
165 $images = $this->drupalGetTestFiles('image'); | |
166 // Let's just use the first image. | |
167 $image = $images[0]; | |
168 $this->uploadNodeImage($image, $field_name, 'article'); | |
169 | |
170 // Look for form-required for the alt text. | |
171 $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"]'); | |
172 | |
173 $this->assertTrue(isset($elements[0]), 'Required marker is shown for the required alt text.'); | |
174 | |
175 $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"]'); | |
176 | |
177 $this->assertTrue(isset($elements[0]), 'Required marker is shown for the required title text.'); | |
178 | |
179 $this->assertText(t('Alternative text field is required.')); | |
180 $this->assertText(t('Title field is required.')); | |
181 | |
182 $instance->setSetting('alt_field_required', 0); | |
183 $instance->setSetting('title_field_required', 0); | |
184 $instance->save(); | |
185 | |
186 $edit = [ | |
187 'title[0][value]' => $this->randomMachineName(), | |
188 ]; | |
189 $this->drupalPostForm('node/add/article', $edit, t('Save')); | |
190 | |
191 $this->assertNoText(t('Alternative text field is required.')); | |
192 $this->assertNoText(t('Title field is required.')); | |
193 | |
194 $instance->setSetting('required', 0); | |
195 $instance->setSetting('alt_field_required', 1); | |
196 $instance->setSetting('title_field_required', 1); | |
197 $instance->save(); | |
198 | |
199 $edit = [ | |
200 'title[0][value]' => $this->randomMachineName(), | |
201 ]; | |
202 $this->drupalPostForm('node/add/article', $edit, t('Save')); | |
203 | |
204 $this->assertNoText(t('Alternative text field is required.')); | |
205 $this->assertNoText(t('Title field is required.')); | |
206 } | |
207 | |
208 /** | |
209 * Returns field settings. | |
210 * | |
211 * @param int[] $min_resolution | |
212 * The minimum width and height resolution setting. | |
213 * @param int[] $max_resolution | |
214 * The maximum width and height resolution setting. | |
215 * | |
216 * @return array | |
217 */ | |
218 protected function getFieldSettings($min_resolution, $max_resolution) { | |
219 return [ | |
220 'max_resolution' => $max_resolution['width'] . 'x' . $max_resolution['height'], | |
221 'min_resolution' => $min_resolution['width'] . 'x' . $min_resolution['height'], | |
222 'alt_field' => 0, | |
223 ]; | |
224 } | |
225 | |
226 } |