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. 8.9.x core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testRequiredAttributes()
  3. 10 core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testRequiredAttributes()

Tests that required alt/title fields gets validated right.

File

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

Class

ImageFieldValidateTest
Tests validation functions such as min/max dimensions.

Namespace

Drupal\Tests\image\Functional

Code

public function testRequiredAttributes() : void {
    $field_name = $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, 'node', '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.
    $this->assertSession()
        ->elementExists('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->assertSession()
        ->elementExists('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->assertSession()
        ->statusMessageContains('Alternative text field is required.', 'error');
    $this->assertSession()
        ->statusMessageContains('Title field is required.', 'error');
    $instance->setSetting('alt_field_required', 0);
    $instance->setSetting('title_field_required', 0);
    $instance->save();
    $edit = [
        'title[0][value]' => $this->randomMachineName(),
    ];
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->statusMessageNotContains('Alternative text field is required.');
    $this->assertSession()
        ->statusMessageNotContains('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->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->statusMessageNotContains('Alternative text field is required.');
    $this->assertSession()
        ->statusMessageNotContains('Title field is required.');
}

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