Mercurial > hg > isophonics-drupal-site
comparison core/modules/image/src/Tests/ImageFieldValidateTest.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 7a779792577d |
children |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\image\Tests; | 3 namespace Drupal\image\Tests; |
4 | |
5 use Drupal\field\Entity\FieldStorageConfig; | |
6 use Drupal\field\Entity\FieldConfig; | |
4 | 7 |
5 /** | 8 /** |
6 * Tests validation functions such as min/max resolution. | 9 * Tests validation functions such as min/max resolution. |
7 * | 10 * |
8 * @group image | 11 * @group image |
9 */ | 12 */ |
10 class ImageFieldValidateTest extends ImageFieldTestBase { | 13 class ImageFieldValidateTest extends ImageFieldTestBase { |
14 | |
15 /** | |
16 * Test image validity. | |
17 */ | |
18 public function testValid() { | |
19 $file_system = $this->container->get('file_system'); | |
20 $image_files = $this->drupalGetTestFiles('image'); | |
21 | |
22 $field_name = strtolower($this->randomMachineName()); | |
23 $this->createImageField($field_name, 'article', [], ['file_directory' => 'test-upload']); | |
24 $expected_path = 'public://test-upload'; | |
25 | |
26 // Create alt text for the image. | |
27 $alt = $this->randomMachineName(); | |
28 | |
29 // Create a node with a valid image. | |
30 $node = $this->uploadNodeImage($image_files[0], $field_name, 'article', $alt); | |
31 $this->assertTrue(file_exists($expected_path . '/' . $image_files[0]->filename)); | |
32 | |
33 // Remove the image. | |
34 $this->drupalPostForm('node/' . $node . '/edit', [], t('Remove')); | |
35 $this->drupalPostForm(NULL, [], t('Save')); | |
36 | |
37 // Get invalid image test files from simpletest. | |
38 $files = file_scan_directory(drupal_get_path('module', 'simpletest') . '/files', '/invalid-img-.*/'); | |
39 $invalid_image_files = []; | |
40 foreach ($files as $file) { | |
41 $invalid_image_files[$file->filename] = $file; | |
42 } | |
43 | |
44 // Try uploading a zero-byte image. | |
45 $zero_size_image = $invalid_image_files['invalid-img-zero-size.png']; | |
46 $edit = [ | |
47 'files[' . $field_name . '_0]' => $file_system->realpath($zero_size_image->uri), | |
48 ]; | |
49 $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload')); | |
50 $this->assertFalse(file_exists($expected_path . '/' . $zero_size_image->filename)); | |
51 | |
52 // Try uploading an invalid image. | |
53 $invalid_image = $invalid_image_files['invalid-img-test.png']; | |
54 $edit = [ | |
55 'files[' . $field_name . '_0]' => $file_system->realpath($invalid_image->uri), | |
56 ]; | |
57 $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload')); | |
58 $this->assertFalse(file_exists($expected_path . '/' . $invalid_image->filename)); | |
59 | |
60 // Upload a valid image again. | |
61 $valid_image = $image_files[0]; | |
62 $edit = [ | |
63 'files[' . $field_name . '_0]' => $file_system->realpath($valid_image->uri), | |
64 ]; | |
65 $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload')); | |
66 $this->assertTrue(file_exists($expected_path . '/' . $valid_image->filename)); | |
67 } | |
68 | |
11 /** | 69 /** |
12 * Test min/max resolution settings. | 70 * Test min/max resolution settings. |
13 */ | 71 */ |
14 public function testResolution() { | 72 public function testResolution() { |
15 $field_names = [ | 73 $field_names = [ |
60 if ($image_file->getWidth() > $max_resolution['width']) { | 118 if ($image_file->getWidth() > $max_resolution['width']) { |
61 $image_that_is_too_big = $image; | 119 $image_that_is_too_big = $image; |
62 } | 120 } |
63 if ($image_file->getWidth() < $min_resolution['width']) { | 121 if ($image_file->getWidth() < $min_resolution['width']) { |
64 $image_that_is_too_small = $image; | 122 $image_that_is_too_small = $image; |
123 $image_that_is_too_small_file = $image_file; | |
65 } | 124 } |
66 if ($image_that_is_too_small && $image_that_is_too_big) { | 125 if ($image_that_is_too_small && $image_that_is_too_big) { |
67 break; | 126 break; |
68 } | 127 } |
69 } | 128 } |
70 $this->uploadNodeImage($image_that_is_too_small, $field_names[0], 'article'); | 129 $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])); | 130 $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'])); | 131 $this->assertRaw(t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', [ |
132 '%dimensions' => '50x50', | |
133 '%width' => $image_that_is_too_small_file->getWidth(), | |
134 '%height' => $image_that_is_too_small_file->getHeight(), | |
135 ])); | |
73 $this->uploadNodeImage($image_that_is_too_big, $field_names[0], 'article'); | 136 $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.')); | 137 $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'); | 138 $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])); | 139 $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'); | 140 $this->uploadNodeImage($image_that_is_too_big, $field_names[1], 'article'); |
176 ':class' => 'messages--error', | 239 ':class' => 'messages--error', |
177 ]); | 240 ]); |
178 $this->assertEqual(count($elements), 1, 'Ajax validation messages are displayed once.'); | 241 $this->assertEqual(count($elements), 1, 'Ajax validation messages are displayed once.'); |
179 } | 242 } |
180 | 243 |
244 /** | |
245 * Tests that image field validation works with other form submit handlers. | |
246 */ | |
247 public function testFriendlyAjaxValidation() { | |
248 // Add a custom field to the Article content type that contains an AJAX | |
249 // handler on a select field. | |
250 $field_storage = FieldStorageConfig::create([ | |
251 'field_name' => 'field_dummy_select', | |
252 'type' => 'image_module_test_dummy_ajax', | |
253 'entity_type' => 'node', | |
254 'cardinality' => 1, | |
255 ]); | |
256 $field_storage->save(); | |
257 | |
258 $field = FieldConfig::create([ | |
259 'field_storage' => $field_storage, | |
260 'entity_type' => 'node', | |
261 'bundle' => 'article', | |
262 'field_name' => 'field_dummy_select', | |
263 'label' => t('Dummy select'), | |
264 ])->save(); | |
265 | |
266 \Drupal::entityTypeManager() | |
267 ->getStorage('entity_form_display') | |
268 ->load('node.article.default') | |
269 ->setComponent( | |
270 'field_dummy_select', | |
271 [ | |
272 'type' => 'image_module_test_dummy_ajax_widget', | |
273 'weight' => 1, | |
274 ]) | |
275 ->save(); | |
276 | |
277 // Then, add an image field. | |
278 $this->createImageField('field_dummy_image', 'article'); | |
279 | |
280 // Open an article and trigger the AJAX handler. | |
281 $this->drupalGet('node/add/article'); | |
282 $edit = [ | |
283 'field_dummy_select[select_widget]' => 'bam', | |
284 ]; | |
285 $this->drupalPostAjaxForm(NULL, $edit, 'field_dummy_select[select_widget]'); | |
286 } | |
287 | |
181 } | 288 } |