public function ImageFieldDisplayTest::testImageFieldSettings in Drupal 9
Same name and namespace in other branches
- 8 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
->assertSession()
->fieldValueEquals('settings[max_resolution][x]', '100');
$this
->assertSession()
->fieldValueEquals('settings[max_resolution][y]', '100');
$this
->assertSession()
->fieldValueEquals('settings[min_resolution][x]', '10');
$this
->assertSession()
->fieldValueEquals('settings[min_resolution][y]', '10');
$this
->drupalGet('node/add/article');
$this
->assertSession()
->pageTextContains('50 KB limit.');
$this
->assertSession()
->pageTextContains('Allowed types: ' . $test_image_extension . '.');
$this
->assertSession()
->pageTextContains('Images must be larger than 10x10 pixels. Images larger than 100x100 pixels will be resized.');
// 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
->assertSession()
->fieldValueEquals($field_name . '[0][alt]', $alt);
$this
->assertSession()
->fieldValueEquals($field_name . '[0][title]', '');
// 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;
$file_url_generator = \Drupal::service('file_url_generator');
$url = $file_url_generator
->transformRelative(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
->drupalGet('node/' . $nid . '/edit');
$this
->submitForm($edit, 'Save');
$default_output = str_replace("\n", NULL, $renderer
->renderRoot($image));
$this
->assertSession()
->responseContains($default_output);
// 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
->drupalGet('node/' . $nid . '/edit');
$this
->submitForm($edit, 'Save');
$schema = $field
->getFieldStorageDefinition()
->getSchema();
$this
->assertSession()
->pageTextContains("Alternative text cannot be longer than {$schema['columns']['alt']['length']} characters but is currently {$test_size} characters long.");
$this
->assertSession()
->pageTextContains("Title cannot be longer than {$schema['columns']['title']['length']} characters but is currently {$test_size} characters long.");
// 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
->drupalGet('admin/structure/types/manage/article/fields/node.article.' . $field_name . '/storage');
$this
->submitForm([
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
], 'Save field settings');
$edit = [
'files[' . $field_name . '_1][]' => \Drupal::service('file_system')
->realpath($test_image->uri),
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
// Add the required alt text.
$this
->submitForm([
$field_name . '[1][alt]' => $alt,
], 'Save');
$this
->assertSession()
->pageTextContains('Article ' . $node
->getTitle() . ' has been updated.');
// 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
->submitForm($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][]"]');
}