You are here

public function ImageFieldValidateTest::testRequiredAttributes in Drupal 9

Same name and namespace in other branches
  1. 8 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()

Tests 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.
  $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()
    ->pageTextContains('Alternative text field is required.');
  $this
    ->assertSession()
    ->pageTextContains('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
    ->drupalGet('node/add/article');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextNotContains('Alternative text field is required.');
  $this
    ->assertSession()
    ->pageTextNotContains('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()
    ->pageTextNotContains('Alternative text field is required.');
  $this
    ->assertSession()
    ->pageTextNotContains('Title field is required.');
}