annotate 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
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\image\Tests;
Chris@0 4
Chris@14 5 use Drupal\field\Entity\FieldStorageConfig;
Chris@14 6 use Drupal\field\Entity\FieldConfig;
Chris@14 7
Chris@0 8 /**
Chris@0 9 * Tests validation functions such as min/max resolution.
Chris@0 10 *
Chris@0 11 * @group image
Chris@0 12 */
Chris@0 13 class ImageFieldValidateTest extends ImageFieldTestBase {
Chris@14 14
Chris@14 15 /**
Chris@14 16 * Test image validity.
Chris@14 17 */
Chris@14 18 public function testValid() {
Chris@14 19 $file_system = $this->container->get('file_system');
Chris@14 20 $image_files = $this->drupalGetTestFiles('image');
Chris@14 21
Chris@14 22 $field_name = strtolower($this->randomMachineName());
Chris@14 23 $this->createImageField($field_name, 'article', [], ['file_directory' => 'test-upload']);
Chris@14 24 $expected_path = 'public://test-upload';
Chris@14 25
Chris@14 26 // Create alt text for the image.
Chris@14 27 $alt = $this->randomMachineName();
Chris@14 28
Chris@14 29 // Create a node with a valid image.
Chris@14 30 $node = $this->uploadNodeImage($image_files[0], $field_name, 'article', $alt);
Chris@14 31 $this->assertTrue(file_exists($expected_path . '/' . $image_files[0]->filename));
Chris@14 32
Chris@14 33 // Remove the image.
Chris@14 34 $this->drupalPostForm('node/' . $node . '/edit', [], t('Remove'));
Chris@14 35 $this->drupalPostForm(NULL, [], t('Save'));
Chris@14 36
Chris@14 37 // Get invalid image test files from simpletest.
Chris@14 38 $files = file_scan_directory(drupal_get_path('module', 'simpletest') . '/files', '/invalid-img-.*/');
Chris@14 39 $invalid_image_files = [];
Chris@14 40 foreach ($files as $file) {
Chris@14 41 $invalid_image_files[$file->filename] = $file;
Chris@14 42 }
Chris@14 43
Chris@14 44 // Try uploading a zero-byte image.
Chris@14 45 $zero_size_image = $invalid_image_files['invalid-img-zero-size.png'];
Chris@14 46 $edit = [
Chris@14 47 'files[' . $field_name . '_0]' => $file_system->realpath($zero_size_image->uri),
Chris@14 48 ];
Chris@14 49 $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload'));
Chris@14 50 $this->assertFalse(file_exists($expected_path . '/' . $zero_size_image->filename));
Chris@14 51
Chris@14 52 // Try uploading an invalid image.
Chris@14 53 $invalid_image = $invalid_image_files['invalid-img-test.png'];
Chris@14 54 $edit = [
Chris@14 55 'files[' . $field_name . '_0]' => $file_system->realpath($invalid_image->uri),
Chris@14 56 ];
Chris@14 57 $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload'));
Chris@14 58 $this->assertFalse(file_exists($expected_path . '/' . $invalid_image->filename));
Chris@14 59
Chris@14 60 // Upload a valid image again.
Chris@14 61 $valid_image = $image_files[0];
Chris@14 62 $edit = [
Chris@14 63 'files[' . $field_name . '_0]' => $file_system->realpath($valid_image->uri),
Chris@14 64 ];
Chris@14 65 $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload'));
Chris@14 66 $this->assertTrue(file_exists($expected_path . '/' . $valid_image->filename));
Chris@14 67 }
Chris@14 68
Chris@0 69 /**
Chris@0 70 * Test min/max resolution settings.
Chris@0 71 */
Chris@0 72 public function testResolution() {
Chris@0 73 $field_names = [
Chris@0 74 0 => strtolower($this->randomMachineName()),
Chris@0 75 1 => strtolower($this->randomMachineName()),
Chris@0 76 2 => strtolower($this->randomMachineName()),
Chris@0 77 ];
Chris@0 78 $min_resolution = [
Chris@0 79 'width' => 50,
Chris@0 80 'height' => 50
Chris@0 81 ];
Chris@0 82 $max_resolution = [
Chris@0 83 'width' => 100,
Chris@0 84 'height' => 100
Chris@0 85 ];
Chris@0 86 $no_height_min_resolution = [
Chris@0 87 'width' => 50,
Chris@0 88 'height' => NULL
Chris@0 89 ];
Chris@0 90 $no_height_max_resolution = [
Chris@0 91 'width' => 100,
Chris@0 92 'height' => NULL
Chris@0 93 ];
Chris@0 94 $no_width_min_resolution = [
Chris@0 95 'width' => NULL,
Chris@0 96 'height' => 50
Chris@0 97 ];
Chris@0 98 $no_width_max_resolution = [
Chris@0 99 'width' => NULL,
Chris@0 100 'height' => 100
Chris@0 101 ];
Chris@0 102 $field_settings = [
Chris@0 103 0 => $this->getFieldSettings($min_resolution, $max_resolution),
Chris@0 104 1 => $this->getFieldSettings($no_height_min_resolution, $no_height_max_resolution),
Chris@0 105 2 => $this->getFieldSettings($no_width_min_resolution, $no_width_max_resolution),
Chris@0 106 ];
Chris@0 107 $this->createImageField($field_names[0], 'article', [], $field_settings[0]);
Chris@0 108 $this->createImageField($field_names[1], 'article', [], $field_settings[1]);
Chris@0 109 $this->createImageField($field_names[2], 'article', [], $field_settings[2]);
Chris@0 110
Chris@0 111 // We want a test image that is too small, and a test image that is too
Chris@0 112 // big, so cycle through test image files until we have what we need.
Chris@0 113 $image_that_is_too_big = FALSE;
Chris@0 114 $image_that_is_too_small = FALSE;
Chris@0 115 $image_factory = $this->container->get('image.factory');
Chris@0 116 foreach ($this->drupalGetTestFiles('image') as $image) {
Chris@0 117 $image_file = $image_factory->get($image->uri);
Chris@0 118 if ($image_file->getWidth() > $max_resolution['width']) {
Chris@0 119 $image_that_is_too_big = $image;
Chris@0 120 }
Chris@0 121 if ($image_file->getWidth() < $min_resolution['width']) {
Chris@0 122 $image_that_is_too_small = $image;
Chris@14 123 $image_that_is_too_small_file = $image_file;
Chris@0 124 }
Chris@0 125 if ($image_that_is_too_small && $image_that_is_too_big) {
Chris@0 126 break;
Chris@0 127 }
Chris@0 128 }
Chris@0 129 $this->uploadNodeImage($image_that_is_too_small, $field_names[0], 'article');
Chris@0 130 $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename]));
Chris@14 131 $this->assertRaw(t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', [
Chris@14 132 '%dimensions' => '50x50',
Chris@14 133 '%width' => $image_that_is_too_small_file->getWidth(),
Chris@14 134 '%height' => $image_that_is_too_small_file->getHeight(),
Chris@14 135 ]));
Chris@0 136 $this->uploadNodeImage($image_that_is_too_big, $field_names[0], 'article');
Chris@0 137 $this->assertText(t('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.'));
Chris@0 138 $this->uploadNodeImage($image_that_is_too_small, $field_names[1], 'article');
Chris@0 139 $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename]));
Chris@0 140 $this->uploadNodeImage($image_that_is_too_big, $field_names[1], 'article');
Chris@0 141 $this->assertText(t('The image was resized to fit within the maximum allowed width of 100 pixels.'));
Chris@0 142 $this->uploadNodeImage($image_that_is_too_small, $field_names[2], 'article');
Chris@0 143 $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename]));
Chris@0 144 $this->uploadNodeImage($image_that_is_too_big, $field_names[2], 'article');
Chris@0 145 $this->assertText(t('The image was resized to fit within the maximum allowed height of 100 pixels.'));
Chris@0 146 }
Chris@0 147
Chris@0 148 /**
Chris@0 149 * Test that required alt/title fields gets validated right.
Chris@0 150 */
Chris@0 151 public function testRequiredAttributes() {
Chris@0 152 $field_name = strtolower($this->randomMachineName());
Chris@0 153 $field_settings = [
Chris@0 154 'alt_field' => 1,
Chris@0 155 'alt_field_required' => 1,
Chris@0 156 'title_field' => 1,
Chris@0 157 'title_field_required' => 1,
Chris@0 158 'required' => 1,
Chris@0 159 ];
Chris@0 160 $instance = $this->createImageField($field_name, 'article', [], $field_settings);
Chris@0 161 $images = $this->drupalGetTestFiles('image');
Chris@0 162 // Let's just use the first image.
Chris@0 163 $image = $images[0];
Chris@0 164 $this->uploadNodeImage($image, $field_name, 'article');
Chris@0 165
Chris@0 166 // Look for form-required for the alt text.
Chris@0 167 $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 168
Chris@0 169 $this->assertTrue(isset($elements[0]), 'Required marker is shown for the required alt text.');
Chris@0 170
Chris@0 171 $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 172
Chris@0 173 $this->assertTrue(isset($elements[0]), 'Required marker is shown for the required title text.');
Chris@0 174
Chris@0 175 $this->assertText(t('Alternative text field is required.'));
Chris@0 176 $this->assertText(t('Title field is required.'));
Chris@0 177
Chris@0 178 $instance->setSetting('alt_field_required', 0);
Chris@0 179 $instance->setSetting('title_field_required', 0);
Chris@0 180 $instance->save();
Chris@0 181
Chris@0 182 $edit = [
Chris@0 183 'title[0][value]' => $this->randomMachineName(),
Chris@0 184 ];
Chris@0 185 $this->drupalPostForm('node/add/article', $edit, t('Save'));
Chris@0 186
Chris@0 187 $this->assertNoText(t('Alternative text field is required.'));
Chris@0 188 $this->assertNoText(t('Title field is required.'));
Chris@0 189
Chris@0 190 $instance->setSetting('required', 0);
Chris@0 191 $instance->setSetting('alt_field_required', 1);
Chris@0 192 $instance->setSetting('title_field_required', 1);
Chris@0 193 $instance->save();
Chris@0 194
Chris@0 195 $edit = [
Chris@0 196 'title[0][value]' => $this->randomMachineName(),
Chris@0 197 ];
Chris@0 198 $this->drupalPostForm('node/add/article', $edit, t('Save'));
Chris@0 199
Chris@0 200 $this->assertNoText(t('Alternative text field is required.'));
Chris@0 201 $this->assertNoText(t('Title field is required.'));
Chris@0 202 }
Chris@0 203
Chris@0 204 /**
Chris@0 205 * Returns field settings.
Chris@0 206 *
Chris@0 207 * @param int[] $min_resolution
Chris@0 208 * The minimum width and height resolution setting.
Chris@0 209 * @param int[] $max_resolution
Chris@0 210 * The maximum width and height resolution setting.
Chris@0 211 *
Chris@0 212 * @return array
Chris@0 213 */
Chris@0 214 protected function getFieldSettings($min_resolution, $max_resolution) {
Chris@0 215 return [
Chris@0 216 'max_resolution' => $max_resolution['width'] . 'x' . $max_resolution['height'],
Chris@0 217 'min_resolution' => $min_resolution['width'] . 'x' . $min_resolution['height'],
Chris@0 218 'alt_field' => 0,
Chris@0 219 ];
Chris@0 220 }
Chris@0 221
Chris@12 222 /**
Chris@12 223 * Test the validation message is displayed only once for ajax uploads.
Chris@12 224 */
Chris@12 225 public function testAJAXValidationMessage() {
Chris@12 226 $field_name = strtolower($this->randomMachineName());
Chris@12 227 $this->createImageField($field_name, 'article', ['cardinality' => -1]);
Chris@12 228
Chris@12 229 $this->drupalGet('node/add/article');
Chris@12 230 /** @var \Drupal\file\FileInterface[] $text_files */
Chris@12 231 $text_files = $this->drupalGetTestFiles('text');
Chris@12 232 $text_file = reset($text_files);
Chris@12 233 $edit = [
Chris@12 234 'files[' . $field_name . '_0][]' => $this->container->get('file_system')->realpath($text_file->uri),
Chris@12 235 'title[0][value]' => $this->randomMachineName(),
Chris@12 236 ];
Chris@12 237 $this->drupalPostAjaxForm(NULL, $edit, $field_name . '_0_upload_button');
Chris@12 238 $elements = $this->xpath('//div[contains(@class, :class)]', [
Chris@12 239 ':class' => 'messages--error',
Chris@12 240 ]);
Chris@12 241 $this->assertEqual(count($elements), 1, 'Ajax validation messages are displayed once.');
Chris@12 242 }
Chris@12 243
Chris@14 244 /**
Chris@14 245 * Tests that image field validation works with other form submit handlers.
Chris@14 246 */
Chris@14 247 public function testFriendlyAjaxValidation() {
Chris@14 248 // Add a custom field to the Article content type that contains an AJAX
Chris@14 249 // handler on a select field.
Chris@14 250 $field_storage = FieldStorageConfig::create([
Chris@14 251 'field_name' => 'field_dummy_select',
Chris@14 252 'type' => 'image_module_test_dummy_ajax',
Chris@14 253 'entity_type' => 'node',
Chris@14 254 'cardinality' => 1,
Chris@14 255 ]);
Chris@14 256 $field_storage->save();
Chris@14 257
Chris@14 258 $field = FieldConfig::create([
Chris@14 259 'field_storage' => $field_storage,
Chris@14 260 'entity_type' => 'node',
Chris@14 261 'bundle' => 'article',
Chris@14 262 'field_name' => 'field_dummy_select',
Chris@14 263 'label' => t('Dummy select'),
Chris@14 264 ])->save();
Chris@14 265
Chris@14 266 \Drupal::entityTypeManager()
Chris@14 267 ->getStorage('entity_form_display')
Chris@14 268 ->load('node.article.default')
Chris@14 269 ->setComponent(
Chris@14 270 'field_dummy_select',
Chris@14 271 [
Chris@14 272 'type' => 'image_module_test_dummy_ajax_widget',
Chris@14 273 'weight' => 1,
Chris@14 274 ])
Chris@14 275 ->save();
Chris@14 276
Chris@14 277 // Then, add an image field.
Chris@14 278 $this->createImageField('field_dummy_image', 'article');
Chris@14 279
Chris@14 280 // Open an article and trigger the AJAX handler.
Chris@14 281 $this->drupalGet('node/add/article');
Chris@14 282 $edit = [
Chris@14 283 'field_dummy_select[select_widget]' => 'bam',
Chris@14 284 ];
Chris@14 285 $this->drupalPostAjaxForm(NULL, $edit, 'field_dummy_select[select_widget]');
Chris@14 286 }
Chris@14 287
Chris@0 288 }