You are here

function ImageFieldValidateTest::testRequiredAttributes in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/image/src/Tests/ImageFieldValidateTest.php \Drupal\image\Tests\ImageFieldValidateTest::testRequiredAttributes()

Test that required alt/title fields gets validated right.

File

core/modules/image/src/Tests/ImageFieldValidateTest.php, line 57
Contains \Drupal\image\Tests\ImageFieldValidateTest.

Class

ImageFieldValidateTest
Tests validation functions such as min/max resolution.

Namespace

Drupal\image\Tests

Code

function testRequiredAttributes() {
  $field_name = strtolower($this
    ->randomMachineName());
  $field_settings = array(
    'alt_field' => 1,
    'alt_field_required' => 1,
    'title_field' => 1,
    'title_field_required' => 1,
    'required' => 1,
  );
  $instance = $this
    ->createImageField($field_name, 'article', array(), $field_settings);
  $images = $this
    ->drupalGetTestFiles('image');

  // Let's just use the first image.
  $image = $images[0];
  $this
    ->uploadNodeImage($image, $field_name, 'article');

  // Look for form-required for the alt text.
  $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"]');
  $this
    ->assertTrue(isset($elements[0]), 'Required marker is shown for the required alt text.');
  $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"]');
  $this
    ->assertTrue(isset($elements[0]), 'Required marker is shown for the required title text.');
  $this
    ->assertText(t('Alternative text field is required.'));
  $this
    ->assertText(t('Title field is required.'));
  $instance
    ->setSetting('alt_field_required', 0);
  $instance
    ->setSetting('title_field_required', 0);
  $instance
    ->save();
  $edit = array(
    'title[0][value]' => $this
      ->randomMachineName(),
  );
  $this
    ->drupalPostForm('node/add/article', $edit, t('Save and publish'));
  $this
    ->assertNoText(t('Alternative text field is required.'));
  $this
    ->assertNoText(t('Title field is required.'));
  $instance
    ->setSetting('required', 0);
  $instance
    ->setSetting('alt_field_required', 1);
  $instance
    ->setSetting('title_field_required', 1);
  $instance
    ->save();
  $edit = array(
    'title[0][value]' => $this
      ->randomMachineName(),
  );
  $this
    ->drupalPostForm('node/add/article', $edit, t('Save and publish'));
  $this
    ->assertNoText(t('Alternative text field is required.'));
  $this
    ->assertNoText(t('Title field is required.'));
}