3 namespace Drupal\image\Tests;
6 * Tests validation functions such as min/max resolution.
10 class ImageFieldValidateTest extends ImageFieldTestBase {
12 * Test min/max resolution settings.
14 public function testResolution() {
16 0 => strtolower($this->randomMachineName()),
17 1 => strtolower($this->randomMachineName()),
18 2 => strtolower($this->randomMachineName()),
28 $no_height_min_resolution = [
32 $no_height_max_resolution = [
36 $no_width_min_resolution = [
40 $no_width_max_resolution = [
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),
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]);
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;
63 if ($image_file->getWidth() < $min_resolution['width']) {
64 $image_that_is_too_small = $image;
66 if ($image_that_is_too_small && $image_that_is_too_big) {
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.'));
86 * Test that required alt/title fields gets validated right.
88 public function testRequiredAttributes() {
89 $field_name = strtolower($this->randomMachineName());
92 'alt_field_required' => 1,
94 'title_field_required' => 1,
97 $instance = $this->createImageField($field_name, 'article', [], $field_settings);
98 $images = $this->drupalGetTestFiles('image');
99 // Let's just use the first image.
101 $this->uploadNodeImage($image, $field_name, 'article');
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"]');
106 $this->assertTrue(isset($elements[0]), 'Required marker is shown for the required alt text.');
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"]');
110 $this->assertTrue(isset($elements[0]), 'Required marker is shown for the required title text.');
112 $this->assertText(t('Alternative text field is required.'));
113 $this->assertText(t('Title field is required.'));
115 $instance->setSetting('alt_field_required', 0);
116 $instance->setSetting('title_field_required', 0);
120 'title[0][value]' => $this->randomMachineName(),
122 $this->drupalPostForm('node/add/article', $edit, t('Save'));
124 $this->assertNoText(t('Alternative text field is required.'));
125 $this->assertNoText(t('Title field is required.'));
127 $instance->setSetting('required', 0);
128 $instance->setSetting('alt_field_required', 1);
129 $instance->setSetting('title_field_required', 1);
133 'title[0][value]' => $this->randomMachineName(),
135 $this->drupalPostForm('node/add/article', $edit, t('Save'));
137 $this->assertNoText(t('Alternative text field is required.'));
138 $this->assertNoText(t('Title field is required.'));
142 * Returns field settings.
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.
151 protected function getFieldSettings($min_resolution, $max_resolution) {
153 'max_resolution' => $max_resolution['width'] . 'x' . $max_resolution['height'],
154 'min_resolution' => $min_resolution['width'] . 'x' . $min_resolution['height'],
160 * Test the validation message is displayed only once for ajax uploads.
162 public function testAJAXValidationMessage() {
163 $field_name = strtolower($this->randomMachineName());
164 $this->createImageField($field_name, 'article', ['cardinality' => -1]);
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);
171 'files[' . $field_name . '_0][]' => $this->container->get('file_system')->realpath($text_file->uri),
172 'title[0][value]' => $this->randomMachineName(),
174 $this->drupalPostAjaxForm(NULL, $edit, $field_name . '_0_upload_button');
175 $elements = $this->xpath('//div[contains(@class, :class)]', [
176 ':class' => 'messages--error',
178 $this->assertEqual(count($elements), 1, 'Ajax validation messages are displayed once.');