function ImageFieldDisplayTest::testImageFieldSettings in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/image/src/Tests/ImageFieldDisplayTest.php \Drupal\image\Tests\ImageFieldDisplayTest::testImageFieldSettings()
Tests for image field settings.
File
- core/
modules/ image/ src/ Tests/ ImageFieldDisplayTest.php, line 210 - Contains \Drupal\image\Tests\ImageFieldDisplayTest.
Class
- ImageFieldDisplayTest
- Tests the display of image fields.
Namespace
Drupal\image\TestsCode
function testImageFieldSettings() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container
->get('renderer');
$node_storage = $this->container
->get('entity.manager')
->getStorage('node');
$test_image = current($this
->drupalGetTestFiles('image'));
list(, $test_image_extension) = explode('.', $test_image->filename);
$field_name = strtolower($this
->randomMachineName());
$field_settings = array(
'alt_field' => 1,
'file_extensions' => $test_image_extension,
'max_filesize' => '50 KB',
'max_resolution' => '100x100',
'min_resolution' => '10x10',
'title_field' => 1,
);
$widget_settings = array(
'preview_image_style' => 'medium',
);
$field = $this
->createImageField($field_name, 'article', array(), $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.', array(
'@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');
$this
->assertFieldByName($field_name . '[0][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(array(
$nid,
));
$node = $node_storage
->load($nid);
$file = $node->{$field_name}->entity;
$url = file_create_url(ImageStyle::load('medium')
->buildUrl($file
->getFileUri()));
$this
->assertTrue($this
->cssSelect('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 = array(
'#theme' => 'image',
'#uri' => $file
->getFileUri(),
'#alt' => $alt,
'#title' => $this
->randomMachineName(),
'#width' => 40,
'#height' => 20,
);
$edit = array(
$field_name . '[0][alt]' => $image['#alt'],
$field_name . '[0][title]' => $image['#title'],
);
$this
->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save and keep published'));
$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 = array(
$field_name . '[0][alt]' => $this
->randomMachineName($test_size),
$field_name . '[0][title]' => $this
->randomMachineName($test_size),
);
$this
->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save and keep published'));
$schema = $field
->getFieldStorageDefinition()
->getSchema();
$this
->assertRaw(t('Alternative text cannot be longer than %max characters but is currently %length characters long.', array(
'%max' => $schema['columns']['alt']['length'],
'%length' => $test_size,
)));
$this
->assertRaw(t('Title cannot be longer than %max characters but is currently %length characters long.', array(
'%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', array(
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
), t('Save field settings'));
$edit = array(
'files[' . $field_name . '_1][]' => drupal_realpath($test_image->uri),
);
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, t('Save and keep published'));
// Add the required alt text.
$this
->drupalPostForm(NULL, [
$field_name . '[1][alt]' => $alt,
], t('Save and keep published'));
$this
->assertText(format_string('Article @title has been updated.', array(
'@title' => $node
->getTitle(),
)));
// Assert ImageWidget::process() calls FieldWidget::process().
$this
->drupalGet('node/' . $node
->id() . '/edit');
$edit = array(
'files[' . $field_name . '_2][]' => drupal_realpath($test_image->uri),
);
$this
->drupalPostAjaxForm(NULL, $edit, $field_name . '_2_upload_button');
$this
->assertNoRaw('<input multiple type="file" id="edit-' . strtr($field_name, '_', '-') . '-2-upload" name="files[' . $field_name . '_2][]" size="22" class="js-form-file form-file">');
$this
->assertRaw('<input multiple type="file" id="edit-' . strtr($field_name, '_', '-') . '-3-upload" name="files[' . $field_name . '_3][]" size="22" class="js-form-file form-file">');
}