function ImageFieldDisplayTest::testImageFieldSettings
Same name in other branches
- 9 core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php \Drupal\Tests\image\Functional\ImageFieldDisplayTest::testImageFieldSettings()
- 10 core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php \Drupal\Tests\image\Functional\ImageFieldDisplayTest::testImageFieldSettings()
- 11.x core/modules/image/tests/src/Functional/ImageFieldDisplayTest.php \Drupal\Tests\image\Functional\ImageFieldDisplayTest::testImageFieldSettings()
Tests for image field settings.
File
-
core/
modules/ image/ tests/ src/ Functional/ ImageFieldDisplayTest.php, line 237
Class
- ImageFieldDisplayTest
- Tests the display of image fields.
Namespace
Drupal\Tests\image\FunctionalCode
public function testImageFieldSettings() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container
->get('renderer');
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$test_image = current($this->drupalGetTestFiles('image'));
list(, $test_image_extension) = explode('.', $test_image->filename);
$field_name = strtolower($this->randomMachineName());
$field_settings = [
'alt_field' => 1,
'file_extensions' => $test_image_extension,
'max_filesize' => '50 KB',
'max_resolution' => '100x100',
'min_resolution' => '10x10',
'title_field' => 1,
];
$widget_settings = [
'preview_image_style' => 'medium',
];
$field = $this->createImageField($field_name, 'article', [], $field_settings, $widget_settings);
// Verify that the min/max resolution set on the field are properly
// extracted, and displayed, on the image field's configuration form.
$this->drupalGet('admin/structure/types/manage/article/fields/' . $field->id());
$this->assertFieldByName('settings[max_resolution][x]', '100', 'Expected max resolution X value of 100.');
$this->assertFieldByName('settings[max_resolution][y]', '100', 'Expected max resolution Y value of 100.');
$this->assertFieldByName('settings[min_resolution][x]', '10', 'Expected min resolution X value of 10.');
$this->assertFieldByName('settings[min_resolution][y]', '10', 'Expected min resolution Y value of 10.');
$this->drupalGet('node/add/article');
$this->assertText(t('50 KB limit.'), 'Image widget max file size is displayed on article form.');
$this->assertText(t('Allowed types: @extensions.', [
'@extensions' => $test_image_extension,
]), 'Image widget allowed file types displayed on article form.');
$this->assertText(t('Images must be larger than 10x10 pixels. Images larger than 100x100 pixels will be resized.'), 'Image widget allowed resolution displayed on article form.');
// We have to create the article first and then edit it because the alt
// and title fields do not display until the image has been attached.
// Create alt text for the image.
$alt = $this->randomMachineName();
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt);
$this->drupalGet('node/' . $nid . '/edit');
// Verify that the optional fields alt & title are saved & filled.
$this->assertFieldByName($field_name . '[0][alt]', $alt, 'Alt field displayed on article form.');
$this->assertFieldByName($field_name . '[0][title]', '', 'Title field displayed on article form.');
// Verify that the attached image is being previewed using the 'medium'
// style.
$node_storage->resetCache([
$nid,
]);
$node = $node_storage->load($nid);
$file = $node->{$field_name}->entity;
$url = file_url_transform_relative(ImageStyle::load('medium')->buildUrl($file->getFileUri()));
$this->assertSession()
->elementExists('css', 'img[width=40][height=20][class=image-style-medium][src="' . $url . '"]');
// Add alt/title fields to the image and verify that they are displayed.
$image = [
'#theme' => 'image',
'#uri' => $file->getFileUri(),
'#alt' => $alt,
'#title' => $this->randomMachineName(),
'#width' => 40,
'#height' => 20,
];
$edit = [
$field_name . '[0][alt]' => $image['#alt'],
$field_name . '[0][title]' => $image['#title'],
];
$this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save'));
$default_output = str_replace("\n", NULL, $renderer->renderRoot($image));
$this->assertRaw($default_output, 'Image displayed using user supplied alt and title attributes.');
// Verify that alt/title longer than allowed results in a validation error.
$test_size = 2000;
$edit = [
$field_name . '[0][alt]' => $this->randomMachineName($test_size),
$field_name . '[0][title]' => $this->randomMachineName($test_size),
];
$this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save'));
$schema = $field->getFieldStorageDefinition()
->getSchema();
$this->assertRaw(t('Alternative text cannot be longer than %max characters but is currently %length characters long.', [
'%max' => $schema['columns']['alt']['length'],
'%length' => $test_size,
]));
$this->assertRaw(t('Title cannot be longer than %max characters but is currently %length characters long.', [
'%max' => $schema['columns']['title']['length'],
'%length' => $test_size,
]));
// Set cardinality to unlimited and add upload a second image.
// The image widget is extending on the file widget, but the image field
// type does not have the 'display_field' setting which is expected by
// the file widget. This resulted in notices before when cardinality is not
// 1, so we need to make sure the file widget prevents these notices by
// providing all settings, even if they are not used.
// @see FileWidget::formMultipleElements().
$this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $field_name . '/storage', [
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
], t('Save field settings'));
$edit = [
'files[' . $field_name . '_1][]' => \Drupal::service('file_system')->realpath($test_image->uri),
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
// Add the required alt text.
$this->drupalPostForm(NULL, [
$field_name . '[1][alt]' => $alt,
], t('Save'));
$this->assertText(new FormattableMarkup('Article @title has been updated.', [
'@title' => $node->getTitle(),
]));
// Assert ImageWidget::process() calls FieldWidget::process().
$this->drupalGet('node/' . $node->id() . '/edit');
$edit = [
'files[' . $field_name . '_2][]' => \Drupal::service('file_system')->realpath($test_image->uri),
];
$this->drupalPostForm(NULL, $edit, $field_name . '_2_upload_button');
$this->assertSession()
->elementNotExists('css', 'input[name="files[' . $field_name . '_2][]"]');
$this->assertSession()
->elementExists('css', 'input[name="files[' . $field_name . '_3][]"]');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.