Chris@0: strtolower($this->randomMachineName()), Chris@0: 1 => strtolower($this->randomMachineName()), Chris@0: 2 => strtolower($this->randomMachineName()), Chris@0: ]; Chris@0: $min_resolution = [ Chris@0: 'width' => 50, Chris@0: 'height' => 50 Chris@0: ]; Chris@0: $max_resolution = [ Chris@0: 'width' => 100, Chris@0: 'height' => 100 Chris@0: ]; Chris@0: $no_height_min_resolution = [ Chris@0: 'width' => 50, Chris@0: 'height' => NULL Chris@0: ]; Chris@0: $no_height_max_resolution = [ Chris@0: 'width' => 100, Chris@0: 'height' => NULL Chris@0: ]; Chris@0: $no_width_min_resolution = [ Chris@0: 'width' => NULL, Chris@0: 'height' => 50 Chris@0: ]; Chris@0: $no_width_max_resolution = [ Chris@0: 'width' => NULL, Chris@0: 'height' => 100 Chris@0: ]; Chris@0: $field_settings = [ Chris@0: 0 => $this->getFieldSettings($min_resolution, $max_resolution), Chris@0: 1 => $this->getFieldSettings($no_height_min_resolution, $no_height_max_resolution), Chris@0: 2 => $this->getFieldSettings($no_width_min_resolution, $no_width_max_resolution), Chris@0: ]; Chris@0: $this->createImageField($field_names[0], 'article', [], $field_settings[0]); Chris@0: $this->createImageField($field_names[1], 'article', [], $field_settings[1]); Chris@0: $this->createImageField($field_names[2], 'article', [], $field_settings[2]); Chris@0: Chris@0: // We want a test image that is too small, and a test image that is too Chris@0: // big, so cycle through test image files until we have what we need. Chris@0: $image_that_is_too_big = FALSE; Chris@0: $image_that_is_too_small = FALSE; Chris@0: $image_factory = $this->container->get('image.factory'); Chris@0: foreach ($this->drupalGetTestFiles('image') as $image) { Chris@0: $image_file = $image_factory->get($image->uri); Chris@0: if ($image_file->getWidth() > $max_resolution['width']) { Chris@0: $image_that_is_too_big = $image; Chris@0: } Chris@0: if ($image_file->getWidth() < $min_resolution['width']) { Chris@0: $image_that_is_too_small = $image; Chris@0: } Chris@0: if ($image_that_is_too_small && $image_that_is_too_big) { Chris@0: break; Chris@0: } Chris@0: } Chris@0: $this->uploadNodeImage($image_that_is_too_small, $field_names[0], 'article'); Chris@0: $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename])); Chris@0: $this->assertRaw(t('The image is too small; the minimum dimensions are %dimensions pixels.', ['%dimensions' => '50x50'])); Chris@0: $this->uploadNodeImage($image_that_is_too_big, $field_names[0], 'article'); Chris@0: $this->assertText(t('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.')); Chris@0: $this->uploadNodeImage($image_that_is_too_small, $field_names[1], 'article'); Chris@0: $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename])); Chris@0: $this->uploadNodeImage($image_that_is_too_big, $field_names[1], 'article'); Chris@0: $this->assertText(t('The image was resized to fit within the maximum allowed width of 100 pixels.')); Chris@0: $this->uploadNodeImage($image_that_is_too_small, $field_names[2], 'article'); Chris@0: $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename])); Chris@0: $this->uploadNodeImage($image_that_is_too_big, $field_names[2], 'article'); Chris@0: $this->assertText(t('The image was resized to fit within the maximum allowed height of 100 pixels.')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test that required alt/title fields gets validated right. Chris@0: */ Chris@0: public function testRequiredAttributes() { Chris@0: $field_name = strtolower($this->randomMachineName()); Chris@0: $field_settings = [ Chris@0: 'alt_field' => 1, Chris@0: 'alt_field_required' => 1, Chris@0: 'title_field' => 1, Chris@0: 'title_field_required' => 1, Chris@0: 'required' => 1, Chris@0: ]; Chris@0: $instance = $this->createImageField($field_name, 'article', [], $field_settings); Chris@0: $images = $this->drupalGetTestFiles('image'); Chris@0: // Let's just use the first image. Chris@0: $image = $images[0]; Chris@0: $this->uploadNodeImage($image, $field_name, 'article'); Chris@0: Chris@0: // Look for form-required for the alt text. Chris@0: $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: Chris@0: $this->assertTrue(isset($elements[0]), 'Required marker is shown for the required alt text.'); Chris@0: Chris@0: $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: Chris@0: $this->assertTrue(isset($elements[0]), 'Required marker is shown for the required title text.'); Chris@0: Chris@0: $this->assertText(t('Alternative text field is required.')); Chris@0: $this->assertText(t('Title field is required.')); Chris@0: Chris@0: $instance->setSetting('alt_field_required', 0); Chris@0: $instance->setSetting('title_field_required', 0); Chris@0: $instance->save(); Chris@0: Chris@0: $edit = [ Chris@0: 'title[0][value]' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->drupalPostForm('node/add/article', $edit, t('Save')); Chris@0: Chris@0: $this->assertNoText(t('Alternative text field is required.')); Chris@0: $this->assertNoText(t('Title field is required.')); Chris@0: Chris@0: $instance->setSetting('required', 0); Chris@0: $instance->setSetting('alt_field_required', 1); Chris@0: $instance->setSetting('title_field_required', 1); Chris@0: $instance->save(); Chris@0: Chris@0: $edit = [ Chris@0: 'title[0][value]' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->drupalPostForm('node/add/article', $edit, t('Save')); Chris@0: Chris@0: $this->assertNoText(t('Alternative text field is required.')); Chris@0: $this->assertNoText(t('Title field is required.')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns field settings. Chris@0: * Chris@0: * @param int[] $min_resolution Chris@0: * The minimum width and height resolution setting. Chris@0: * @param int[] $max_resolution Chris@0: * The maximum width and height resolution setting. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: protected function getFieldSettings($min_resolution, $max_resolution) { Chris@0: return [ Chris@0: 'max_resolution' => $max_resolution['width'] . 'x' . $max_resolution['height'], Chris@0: 'min_resolution' => $min_resolution['width'] . 'x' . $min_resolution['height'], Chris@0: 'alt_field' => 0, Chris@0: ]; Chris@0: } Chris@0: Chris@12: /** Chris@12: * Test the validation message is displayed only once for ajax uploads. Chris@12: */ Chris@12: public function testAJAXValidationMessage() { Chris@12: $field_name = strtolower($this->randomMachineName()); Chris@12: $this->createImageField($field_name, 'article', ['cardinality' => -1]); Chris@12: Chris@12: $this->drupalGet('node/add/article'); Chris@12: /** @var \Drupal\file\FileInterface[] $text_files */ Chris@12: $text_files = $this->drupalGetTestFiles('text'); Chris@12: $text_file = reset($text_files); Chris@12: $edit = [ Chris@12: 'files[' . $field_name . '_0][]' => $this->container->get('file_system')->realpath($text_file->uri), Chris@12: 'title[0][value]' => $this->randomMachineName(), Chris@12: ]; Chris@12: $this->drupalPostAjaxForm(NULL, $edit, $field_name . '_0_upload_button'); Chris@12: $elements = $this->xpath('//div[contains(@class, :class)]', [ Chris@12: ':class' => 'messages--error', Chris@12: ]); Chris@12: $this->assertEqual(count($elements), 1, 'Ajax validation messages are displayed once.'); Chris@12: } Chris@12: Chris@0: }