Commit d87af026 authored by alexpott's avatar alexpott

Issue #2303765 by davidhernandez, larowlan, mgifford: Make the default 'alt'...

Issue #2303765 by davidhernandez, larowlan, mgifford: Make the default 'alt' attribute for Image fields required
parent e5d6c142
......@@ -76,8 +76,8 @@ public static function defaultStorageSettings() {
public static function defaultFieldSettings() {
$settings = array(
'file_extensions' => 'png gif jpg jpeg',
'alt_field' => 0,
'alt_field_required' => 0,
'alt_field' => 1,
'alt_field_required' => 1,
'title_field' => 0,
'title_field_required' => 0,
'max_resolution' => '',
......@@ -263,7 +263,7 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
'#type' => 'checkbox',
'#title' => t('Enable <em>Alt</em> field'),
'#default_value' => $settings['alt_field'],
'#description' => t('The alt attribute may be used by search engines, screen readers, and when the image cannot be loaded. Enabling this field is recommended'),
'#description' => t('The alt attribute may be used by search engines, screen readers, and when the image cannot be loaded. Enabling this field is recommended.'),
'#weight' => 9,
);
$element['alt_field_required'] = array(
......
......@@ -296,7 +296,7 @@ function testStyleReplacement() {
// Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image'));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article');
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
$node = Node::load($nid);
// Get node field original image URI.
......@@ -430,7 +430,7 @@ function testConfigImport() {
// Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image'));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article');
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
$node = Node::load($nid);
// Get node field original image URI.
......
......@@ -49,7 +49,8 @@ function testImageFieldFormattersPrivate() {
function _testImageFieldFormatters($scheme) {
$node_storage = $this->container->get('entity.manager')->getStorage('node');
$field_name = strtolower($this->randomMachineName());
$this->createImageField($field_name, 'article', array('uri_scheme' => $scheme));
$field_settings = array('alt_field_required' => 0);
$instance = $this->createImageField($field_name, 'article', array('uri_scheme' => $scheme), $field_settings);
// Go to manage display page.
$this->drupalGet("admin/structure/types/manage/article/display");
......@@ -78,8 +79,16 @@ function _testImageFieldFormatters($scheme) {
// Ensure that preview works.
$this->previewNodeImage($test_image, $field_name, 'article');
// After previewing, make the alt field required. It cannot be required
// during preview because the form validation will fail.
$instance->settings['alt_field_required'] = 1;
$instance->save();
// Create alt text for the image.
$alt = $this->randomMachineName();
// Save node.
$nid = $this->uploadNodeImage($test_image, $field_name, 'article');
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt);
$node_storage->resetCache(array($nid));
$node = $node_storage->load($nid);
......@@ -90,6 +99,7 @@ function _testImageFieldFormatters($scheme) {
'#uri' => $image_uri,
'#width' => 40,
'#height' => 20,
'#alt' => $alt,
);
$default_output = str_replace("\n", NULL, drupal_render($image));
$this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
......@@ -108,6 +118,7 @@ function _testImageFieldFormatters($scheme) {
'#uri' => $image_uri,
'#width' => 40,
'#height' => 20,
'#alt' => $alt,
);
$default_output = '<a href="' . file_create_url($image_uri) . '">' . drupal_render($image) . '</a>';
$this->drupalGet('node/' . $nid);
......@@ -145,12 +156,13 @@ function _testImageFieldFormatters($scheme) {
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
$this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
$elements = $this->xpath(
'//a[@href=:path]/img[@src=:url and @alt="" and @width=:width and @height=:height]',
'//a[@href=:path]/img[@src=:url and @alt=:alt and @width=:width and @height=:height]',
array(
':path' => $node->url(),
':url' => file_create_url($image['#uri']),
':width' => $image['#width'],
':height' => $image['#height'],
':alt' => $alt,
)
);
$this->assertEqual(count($elements), 1, 'Image linked to content formatter displaying correctly on full node view.');
......@@ -170,6 +182,7 @@ function _testImageFieldFormatters($scheme) {
'#width' => 40,
'#height' => 20,
'#style_name' => 'thumbnail',
'#alt' => $alt,
);
$default_output = drupal_render($image_style);
$this->drupalGet('node/' . $nid);
......@@ -221,7 +234,11 @@ function testImageFieldSettings() {
// 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.
$nid = $this->uploadNodeImage($test_image, $field_name, 'article');
// 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.');
......@@ -243,7 +260,7 @@ function testImageFieldSettings() {
$image = array(
'#theme' => 'image',
'#uri' => file_load($node->{$field_name}->target_id)->getFileUri(),
'#alt' => $this->randomMachineName(),
'#alt' => $alt,
'#title' => $this->randomMachineName(),
'#width' => 40,
'#height' => 20,
......@@ -281,15 +298,19 @@ function testImageFieldSettings() {
// 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('field_storage[cardinality]' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED), t('Save field settings'));
$edit = array();
$edit['files[' . $field_name . '_1][]'] = drupal_realpath($test_image->uri);
$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();
$edit['files[' . $field_name . '_2][]'] = drupal_realpath($test_image->uri);
$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="form-file">');
$this->assertRaw('<input multiple type="file" id="edit-' . strtr($field_name, '_', '-') . '-3-upload" name="files[' . $field_name . '_3][]" size="22" class="form-file">');
......@@ -346,7 +367,11 @@ function testImageFieldDefaultImage() {
// Create a node with an image attached and ensure that the default image
// is not displayed.
$nid = $this->uploadNodeImage($images[1], $field_name, 'article');
// Create alt text for the image.
$alt = $this->randomMachineName();
$nid = $this->uploadNodeImage($images[1], $field_name, 'article', $alt);
$node_storage->resetCache(array($nid));
$node = $node_storage->load($nid);
$image = array(
......@@ -354,6 +379,7 @@ function testImageFieldDefaultImage() {
'#uri' => file_load($node->{$field_name}->target_id)->getFileUri(),
'#width' => 40,
'#height' => 20,
'#alt' => $alt,
);
$image_output = str_replace("\n", NULL, drupal_render($image));
$this->drupalGet('node/' . $nid);
......
......@@ -129,13 +129,19 @@ function previewNodeImage($image, $field_name, $type) {
* Name of the image field the image should be attached to.
* @param $type
* The type of node to create.
* @param $alt
* The alt text for the image. Use if the field settings require alt text.
*/
function uploadNodeImage($image, $field_name, $type) {
function uploadNodeImage($image, $field_name, $type, $alt = '') {
$edit = array(
'title[0][value]' => $this->randomMachineName(),
);
$edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
$this->drupalPostForm('node/add/' . $type, $edit, t('Save and publish'));
if ($alt) {
// Add alt text.
$this->drupalPostForm(NULL, [$field_name . '[0][alt]' => $alt], t('Save and publish'));
}
// Retrieve ID of the newly created node from the current URL.
$matches = array();
......
......@@ -23,6 +23,7 @@ function testResolution() {
$field_settings = array(
'max_resolution' => $max_resolution . 'x' . $max_resolution,
'min_resolution' => $min_resolution . 'x' . $min_resolution,
'alt_field' => 0,
);
$this->createImageField($field_name, 'article', array(), $field_settings);
......
......@@ -66,7 +66,7 @@ protected function setUp() {
$image = current($this->drupalGetTestFiles('image'));
// Save a node with the image.
$nid = $this->uploadNodeImage($image, $this->fieldName, 'article');
$nid = $this->uploadNodeImage($image, $this->fieldName, 'article', $this->randomMachineName());
$this->node = Node::load($nid);
$this->file = file_load($this->node->{$this->fieldName}->target_id);
}
......
......@@ -168,7 +168,11 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles =
// Create a new node with an image attached. Make sure we use a large image
// so the scale effects of the image styles always have an effect.
$test_image = current($this->drupalGetTestFiles('image', 39325));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article');
// Create alt text for the image.
$alt = $this->randomMachineName();
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt);
$node_storage->resetCache(array($nid));
$node = $node_storage->load($nid);
......@@ -179,6 +183,7 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles =
'#uri' => $image_uri,
'#width' => 360,
'#height' => 240,
'#alt' => $alt,
);
$default_output = str_replace("\n", NULL, drupal_render($image));
$this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
......@@ -199,6 +204,7 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles =
'#uri' => $image_uri,
'#width' => 360,
'#height' => 240,
'#alt' => $alt,
);
$default_output = '<a href="' . file_create_url($image_uri) . '">' . drupal_render($image) . '</a>';
$this->drupalGet('node/' . $nid);
......@@ -273,6 +279,7 @@ protected function doTestResponsiveImageFieldFormatters($scheme, $empty_styles =
$image = \Drupal::service('image.factory')->get($image_uri);
$fallback_image = array(
'#theme' => 'image',
'#alt' => $alt,
'#srcset' => array(
array(
'uri' => $large_style->buildUrl($image->getSource()),
......@@ -329,7 +336,7 @@ public function testResponsiveImageFieldFormattersEmptyMediaQuery() {
$this->createImageField($field_name, 'article', array('uri_scheme' => 'public'));
// Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image'));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article');
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
$node_storage->resetCache(array($nid));
// Use the responsive image formatter linked to file formatter.
......@@ -366,7 +373,8 @@ public function testResponsiveImageFieldFormattersEmptyMediaQuery() {
*/
private function assertResponsiveImageFieldFormattersLink($link_type) {
$field_name = Unicode::strtolower($this->randomMachineName());
$this->createImageField($field_name, 'article', array('uri_scheme' => 'public'));
$field_settings = array('alt_field_required' => 0);
$this->createImageField($field_name, 'article', array('uri_scheme' => 'public'), $field_settings);
// Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image'));
......
......@@ -78,6 +78,7 @@ public function testTaxonomyImageAccess() {
$edit['name[0][value]'] = $this->randomMachineName();
$edit['files[field_test_0]'] = drupal_realpath($image->uri);
$this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
$this->drupalPostForm(NULL, ['field_test[0][alt]' => $this->randomMachineName()], t('Save'));
$terms = entity_load_multiple_by_properties('taxonomy_term', array('name' => $edit['name[0][value]']));
$term = reset($terms);
$this->assertText(t('Created new term @name.', array('@name' => $term->getName())));
......
......@@ -15,7 +15,7 @@ settings:
min_resolution: ''
alt_field: true
title_field: false
alt_field_required: false
alt_field_required: true
title_field_required: false
default_image:
uuid: null
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment