Chris@16: container->get('file_system'); Chris@16: $image_files = $this->drupalGetTestFiles('image'); Chris@16: Chris@16: $field_name = strtolower($this->randomMachineName()); Chris@16: $this->createImageField($field_name, 'article', [], ['file_directory' => 'test-upload']); Chris@16: $expected_path = 'public://test-upload'; Chris@16: Chris@16: // Create alt text for the image. Chris@16: $alt = $this->randomMachineName(); Chris@16: Chris@16: // Create a node with a valid image. Chris@16: $node = $this->uploadNodeImage($image_files[0], $field_name, 'article', $alt); Chris@16: $this->assertTrue(file_exists($expected_path . '/' . $image_files[0]->filename)); Chris@16: Chris@16: // Remove the image. Chris@16: $this->drupalPostForm('node/' . $node . '/edit', [], t('Remove')); Chris@16: $this->drupalPostForm(NULL, [], t('Save')); Chris@16: Chris@16: // Get invalid image test files from simpletest. Chris@16: $files = file_scan_directory(drupal_get_path('module', 'simpletest') . '/files', '/invalid-img-.*/'); Chris@16: $invalid_image_files = []; Chris@16: foreach ($files as $file) { Chris@16: $invalid_image_files[$file->filename] = $file; Chris@16: } Chris@16: Chris@16: // Try uploading a zero-byte image. Chris@16: $zero_size_image = $invalid_image_files['invalid-img-zero-size.png']; Chris@16: $edit = [ Chris@16: 'files[' . $field_name . '_0]' => $file_system->realpath($zero_size_image->uri), Chris@16: ]; Chris@16: $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload')); Chris@16: $this->assertFalse(file_exists($expected_path . '/' . $zero_size_image->filename)); Chris@16: Chris@16: // Try uploading an invalid image. Chris@16: $invalid_image = $invalid_image_files['invalid-img-test.png']; Chris@16: $edit = [ Chris@16: 'files[' . $field_name . '_0]' => $file_system->realpath($invalid_image->uri), Chris@16: ]; Chris@16: $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload')); Chris@16: $this->assertFalse(file_exists($expected_path . '/' . $invalid_image->filename)); Chris@16: Chris@16: // Upload a valid image again. Chris@16: $valid_image = $image_files[0]; Chris@16: $edit = [ Chris@16: 'files[' . $field_name . '_0]' => $file_system->realpath($valid_image->uri), Chris@16: ]; Chris@16: $this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload')); Chris@16: $this->assertTrue(file_exists($expected_path . '/' . $valid_image->filename)); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Test min/max resolution settings. Chris@16: */ Chris@16: public function testResolution() { Chris@16: $field_names = [ Chris@16: 0 => strtolower($this->randomMachineName()), Chris@16: 1 => strtolower($this->randomMachineName()), Chris@16: 2 => strtolower($this->randomMachineName()), Chris@16: ]; Chris@16: $min_resolution = [ Chris@16: 'width' => 50, Chris@17: 'height' => 50, Chris@16: ]; Chris@16: $max_resolution = [ Chris@16: 'width' => 100, Chris@17: 'height' => 100, Chris@16: ]; Chris@16: $no_height_min_resolution = [ Chris@16: 'width' => 50, Chris@17: 'height' => NULL, Chris@16: ]; Chris@16: $no_height_max_resolution = [ Chris@16: 'width' => 100, Chris@17: 'height' => NULL, Chris@16: ]; Chris@16: $no_width_min_resolution = [ Chris@16: 'width' => NULL, Chris@17: 'height' => 50, Chris@16: ]; Chris@16: $no_width_max_resolution = [ Chris@16: 'width' => NULL, Chris@17: 'height' => 100, Chris@16: ]; Chris@16: $field_settings = [ Chris@16: 0 => $this->getFieldSettings($min_resolution, $max_resolution), Chris@16: 1 => $this->getFieldSettings($no_height_min_resolution, $no_height_max_resolution), Chris@16: 2 => $this->getFieldSettings($no_width_min_resolution, $no_width_max_resolution), Chris@16: ]; Chris@16: $this->createImageField($field_names[0], 'article', [], $field_settings[0]); Chris@16: $this->createImageField($field_names[1], 'article', [], $field_settings[1]); Chris@16: $this->createImageField($field_names[2], 'article', [], $field_settings[2]); Chris@16: Chris@16: // We want a test image that is too small, and a test image that is too Chris@16: // big, so cycle through test image files until we have what we need. Chris@16: $image_that_is_too_big = FALSE; Chris@16: $image_that_is_too_small = FALSE; Chris@16: $image_factory = $this->container->get('image.factory'); Chris@16: foreach ($this->drupalGetTestFiles('image') as $image) { Chris@16: $image_file = $image_factory->get($image->uri); Chris@16: if ($image_file->getWidth() > $max_resolution['width']) { Chris@16: $image_that_is_too_big = $image; Chris@16: } Chris@16: if ($image_file->getWidth() < $min_resolution['width']) { Chris@16: $image_that_is_too_small = $image; Chris@16: $image_that_is_too_small_file = $image_file; Chris@16: } Chris@16: if ($image_that_is_too_small && $image_that_is_too_big) { Chris@16: break; Chris@16: } Chris@16: } Chris@16: $this->uploadNodeImage($image_that_is_too_small, $field_names[0], 'article'); Chris@16: $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename])); Chris@16: $this->assertRaw(t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', [ Chris@16: '%dimensions' => '50x50', Chris@16: '%width' => $image_that_is_too_small_file->getWidth(), Chris@16: '%height' => $image_that_is_too_small_file->getHeight(), Chris@16: ])); Chris@16: $this->uploadNodeImage($image_that_is_too_big, $field_names[0], 'article'); Chris@16: $this->assertText(t('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.')); Chris@16: $this->uploadNodeImage($image_that_is_too_small, $field_names[1], 'article'); Chris@16: $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename])); Chris@16: $this->uploadNodeImage($image_that_is_too_big, $field_names[1], 'article'); Chris@16: $this->assertText(t('The image was resized to fit within the maximum allowed width of 100 pixels.')); Chris@16: $this->uploadNodeImage($image_that_is_too_small, $field_names[2], 'article'); Chris@16: $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename])); Chris@16: $this->uploadNodeImage($image_that_is_too_big, $field_names[2], 'article'); Chris@16: $this->assertText(t('The image was resized to fit within the maximum allowed height of 100 pixels.')); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Test that required alt/title fields gets validated right. Chris@16: */ Chris@16: public function testRequiredAttributes() { Chris@16: $field_name = strtolower($this->randomMachineName()); Chris@16: $field_settings = [ Chris@16: 'alt_field' => 1, Chris@16: 'alt_field_required' => 1, Chris@16: 'title_field' => 1, Chris@16: 'title_field_required' => 1, Chris@16: 'required' => 1, Chris@16: ]; Chris@16: $instance = $this->createImageField($field_name, 'article', [], $field_settings); Chris@16: $images = $this->drupalGetTestFiles('image'); Chris@16: // Let's just use the first image. Chris@16: $image = $images[0]; Chris@16: $this->uploadNodeImage($image, $field_name, 'article'); Chris@16: Chris@16: // Look for form-required for the alt text. Chris@16: $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@16: Chris@16: $this->assertTrue(isset($elements[0]), 'Required marker is shown for the required alt text.'); Chris@16: Chris@16: $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@16: Chris@16: $this->assertTrue(isset($elements[0]), 'Required marker is shown for the required title text.'); Chris@16: Chris@16: $this->assertText(t('Alternative text field is required.')); Chris@16: $this->assertText(t('Title field is required.')); Chris@16: Chris@16: $instance->setSetting('alt_field_required', 0); Chris@16: $instance->setSetting('title_field_required', 0); Chris@16: $instance->save(); Chris@16: Chris@16: $edit = [ Chris@16: 'title[0][value]' => $this->randomMachineName(), Chris@16: ]; Chris@16: $this->drupalPostForm('node/add/article', $edit, t('Save')); Chris@16: Chris@16: $this->assertNoText(t('Alternative text field is required.')); Chris@16: $this->assertNoText(t('Title field is required.')); Chris@16: Chris@16: $instance->setSetting('required', 0); Chris@16: $instance->setSetting('alt_field_required', 1); Chris@16: $instance->setSetting('title_field_required', 1); Chris@16: $instance->save(); Chris@16: Chris@16: $edit = [ Chris@16: 'title[0][value]' => $this->randomMachineName(), Chris@16: ]; Chris@16: $this->drupalPostForm('node/add/article', $edit, t('Save')); Chris@16: Chris@16: $this->assertNoText(t('Alternative text field is required.')); Chris@16: $this->assertNoText(t('Title field is required.')); Chris@16: } Chris@16: Chris@16: /** Chris@18: * Tests creating an entity while leaving the image field empty. Chris@18: * Chris@18: * This is tested first with edit access to the image field allowed, and then Chris@18: * with it forbidden. Chris@18: * Chris@18: * @dataProvider providerTestEmpty Chris@18: */ Chris@18: public function testEmpty($field_name, $required, $cardinality, $form_element_name, $expected_page_text_when_edit_access_allowed, $expected_page_text_when_edit_access_forbidden) { Chris@18: $this->createImageField($field_name, 'article', ['cardinality' => $cardinality], ['required' => $required]); Chris@18: Chris@18: // Test with field edit access allowed. Chris@18: $this->drupalGet('node/add/article'); Chris@18: $this->assertSession()->fieldExists($form_element_name); Chris@18: $edit = [ Chris@18: 'title[0][value]' => 'Article with edit-access-allowed image field', Chris@18: ]; Chris@18: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@18: $this->assertSession()->pageTextContains($expected_page_text_when_edit_access_allowed); Chris@18: Chris@18: // Test with field edit access forbidden. Chris@18: \Drupal::service('module_installer')->install(['image_access_test_hidden']); Chris@18: $this->drupalGet('node/add/article'); Chris@18: $this->assertSession()->fieldNotExists($form_element_name); Chris@18: $edit = [ Chris@18: 'title[0][value]' => 'Article with edit-access-forbidden image field', Chris@18: ]; Chris@18: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@18: $this->assertSession()->pageTextContains($expected_page_text_when_edit_access_forbidden); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Data provider for ::testEmpty() Chris@18: * Chris@18: * @return array Chris@18: * Test cases. Chris@18: */ Chris@18: public function providerTestEmpty() { Chris@18: return [ Chris@18: 'optional-single' => ['field_image', FALSE, 1, 'files[field_image_0]', 'Article Article with edit-access-allowed image field has been created.', 'Article Article with edit-access-forbidden image field has been created.'], Chris@18: 'optional-unlimited' => ['field_image', FALSE, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'files[field_image_0][]', 'Article Article with edit-access-allowed image field has been created.', 'Article Article with edit-access-forbidden image field has been created.'], Chris@18: 'optional-multiple-limited' => ['field_image', FALSE, 2, 'files[field_image_0][]', 'Article Article with edit-access-allowed image field has been created.', 'Article Article with edit-access-forbidden image field has been created.'], Chris@18: 'required-single' => ['field_image', TRUE, 1, 'files[field_image_0]', 'field_image field is required.', 'field_image field is required.'], Chris@18: 'required-unlimited' => ['field_image', TRUE, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'files[field_image_0][]', 'field_image field is required.', 'field_image field is required.'], Chris@18: Chris@18: // @todo Fix this discrepancy in https://www.drupal.org/project/drupal/issues/3011744. Chris@18: 'required-multiple-limited' => ['field_image', TRUE, 2, 'files[field_image_0][]', 'This value should not be null.', 'Article Article with edit-access-forbidden image field has been created.'], Chris@18: ]; Chris@18: } Chris@18: Chris@18: /** Chris@16: * Returns field settings. Chris@16: * Chris@16: * @param int[] $min_resolution Chris@16: * The minimum width and height resolution setting. Chris@16: * @param int[] $max_resolution Chris@16: * The maximum width and height resolution setting. Chris@16: * Chris@16: * @return array Chris@16: */ Chris@16: protected function getFieldSettings($min_resolution, $max_resolution) { Chris@16: return [ Chris@16: 'max_resolution' => $max_resolution['width'] . 'x' . $max_resolution['height'], Chris@16: 'min_resolution' => $min_resolution['width'] . 'x' . $min_resolution['height'], Chris@16: 'alt_field' => 0, Chris@16: ]; Chris@16: } Chris@16: Chris@16: }