Chris@0: container->get('file_system'); Chris@14: $image_files = $this->drupalGetTestFiles('image'); Chris@14: Chris@14: $field_name = strtolower($this->randomMachineName()); Chris@14: $this->createImageField($field_name, 'article', [], ['file_directory' => 'test-upload']); Chris@14: $expected_path = 'public://test-upload'; Chris@14: Chris@14: // Create alt text for the image. Chris@14: $alt = $this->randomMachineName(); Chris@14: Chris@14: // Create a node with a valid image. Chris@14: $node = $this->uploadNodeImage($image_files[0], $field_name, 'article', $alt); Chris@14: $this->assertTrue(file_exists($expected_path . '/' . $image_files[0]->filename)); Chris@14: Chris@14: // Remove the image. Chris@14: $this->drupalPostForm('node/' . $node . '/edit', [], t('Remove')); Chris@14: $this->drupalPostForm(NULL, [], t('Save')); Chris@14: Chris@14: // Get invalid image test files from simpletest. Chris@14: $files = file_scan_directory(drupal_get_path('module', 'simpletest') . '/files', '/invalid-img-.*/'); Chris@14: $invalid_image_files = []; Chris@14: foreach ($files as $file) { Chris@14: $invalid_image_files[$file->filename] = $file; Chris@14: } Chris@14: Chris@14: // Try uploading a zero-byte image. Chris@14: $zero_size_image = $invalid_image_files['invalid-img-zero-size.png']; Chris@14: $edit = [ Chris@14: 'files[' . $field_name . '_0]' => $file_system->realpath($zero_size_image->uri), Chris@14: ]; Chris@14: $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload')); Chris@14: $this->assertFalse(file_exists($expected_path . '/' . $zero_size_image->filename)); Chris@14: Chris@14: // Try uploading an invalid image. Chris@14: $invalid_image = $invalid_image_files['invalid-img-test.png']; Chris@14: $edit = [ Chris@14: 'files[' . $field_name . '_0]' => $file_system->realpath($invalid_image->uri), Chris@14: ]; Chris@14: $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload')); Chris@14: $this->assertFalse(file_exists($expected_path . '/' . $invalid_image->filename)); Chris@14: Chris@14: // Upload a valid image again. Chris@14: $valid_image = $image_files[0]; Chris@14: $edit = [ Chris@14: 'files[' . $field_name . '_0]' => $file_system->realpath($valid_image->uri), Chris@14: ]; Chris@14: $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload')); Chris@14: $this->assertTrue(file_exists($expected_path . '/' . $valid_image->filename)); Chris@14: } Chris@14: Chris@0: /** Chris@0: * Test min/max resolution settings. Chris@0: */ Chris@0: public function testResolution() { Chris@0: $field_names = [ Chris@0: 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@14: $image_that_is_too_small_file = $image_file; 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@14: $this->assertRaw(t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', [ Chris@14: '%dimensions' => '50x50', Chris@14: '%width' => $image_that_is_too_small_file->getWidth(), Chris@14: '%height' => $image_that_is_too_small_file->getHeight(), Chris@14: ])); 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@14: /** Chris@14: * Tests that image field validation works with other form submit handlers. Chris@14: */ Chris@14: public function testFriendlyAjaxValidation() { Chris@14: // Add a custom field to the Article content type that contains an AJAX Chris@14: // handler on a select field. Chris@14: $field_storage = FieldStorageConfig::create([ Chris@14: 'field_name' => 'field_dummy_select', Chris@14: 'type' => 'image_module_test_dummy_ajax', Chris@14: 'entity_type' => 'node', Chris@14: 'cardinality' => 1, Chris@14: ]); Chris@14: $field_storage->save(); Chris@14: Chris@14: $field = FieldConfig::create([ Chris@14: 'field_storage' => $field_storage, Chris@14: 'entity_type' => 'node', Chris@14: 'bundle' => 'article', Chris@14: 'field_name' => 'field_dummy_select', Chris@14: 'label' => t('Dummy select'), Chris@14: ])->save(); Chris@14: Chris@14: \Drupal::entityTypeManager() Chris@14: ->getStorage('entity_form_display') Chris@14: ->load('node.article.default') Chris@14: ->setComponent( Chris@14: 'field_dummy_select', Chris@14: [ Chris@14: 'type' => 'image_module_test_dummy_ajax_widget', Chris@14: 'weight' => 1, Chris@14: ]) Chris@14: ->save(); Chris@14: Chris@14: // Then, add an image field. Chris@14: $this->createImageField('field_dummy_image', 'article'); Chris@14: Chris@14: // Open an article and trigger the AJAX handler. Chris@14: $this->drupalGet('node/add/article'); Chris@14: $edit = [ Chris@14: 'field_dummy_select[select_widget]' => 'bam', Chris@14: ]; Chris@14: $this->drupalPostAjaxForm(NULL, $edit, 'field_dummy_select[select_widget]'); Chris@14: } Chris@14: Chris@0: }