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