function ImageFieldValidateTest::testRequiredAttributes

Same name in other branches
  1. 9 core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testRequiredAttributes()
  2. 10 core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testRequiredAttributes()
  3. 11.x core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testRequiredAttributes()

Test that required alt/title fields gets validated right.

File

core/modules/image/tests/src/Functional/ImageFieldValidateTest.php, line 165

Class

ImageFieldValidateTest
Tests validation functions such as min/max resolution.

Namespace

Drupal\Tests\image\Functional

Code

public function testRequiredAttributes() {
    $field_name = strtolower($this->randomMachineName());
    $field_settings = [
        'alt_field' => 1,
        'alt_field_required' => 1,
        'title_field' => 1,
        'title_field_required' => 1,
        'required' => 1,
    ];
    $instance = $this->createImageField($field_name, 'article', [], $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 = [
        'title[0][value]' => $this->randomMachineName(),
    ];
    $this->drupalPostForm('node/add/article', $edit, t('Save'));
    $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 = [
        'title[0][value]' => $this->randomMachineName(),
    ];
    $this->drupalPostForm('node/add/article', $edit, t('Save'));
    $this->assertNoText(t('Alternative text field is required.'));
    $this->assertNoText(t('Title field is required.'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.