function MediaOverviewPageTest::testImageAltTextDisplay

Same name in other branches
  1. 11.x core/modules/media/tests/src/Functional/MediaOverviewPageTest.php \Drupal\Tests\media\Functional\MediaOverviewPageTest::testImageAltTextDisplay()

Tests the display of the alt attribute.

File

core/modules/media/tests/src/Functional/MediaOverviewPageTest.php, line 196

Class

MediaOverviewPageTest
Tests the Media overview page.

Namespace

Drupal\Tests\media\Functional

Code

public function testImageAltTextDisplay() : void {
    $this->drupalLogin($this->adminUser);
    $media_type = $this->createMediaType('image');
    $media_type_id = $media_type->id();
    $media_type->setFieldMap([
        'name' => 'name',
    ]);
    $media_type->save();
    
    /** @var \Drupal\field\FieldConfigInterface $field */
    $field = FieldConfig::load("media.{$media_type_id}.field_media_image");
    $settings = $field->getSettings();
    $settings['alt_field'] = TRUE;
    $settings['alt_field_required'] = FALSE;
    $field->set('settings', $settings);
    $field->save();
    $file = File::create([
        'uri' => $this->getTestFiles('image')[0]->uri,
    ]);
    $file->save();
    // Set the alt text to an empty string.
    $media = Media::create([
        'name' => 'Custom name',
        'bundle' => $media_type_id,
        'field_media_image' => [
            [
                'target_id' => $file->id(),
                'alt' => '',
                'title' => 'default title',
            ],
        ],
    ]);
    $media->save();
    $this->drupalGet('/admin/content/media');
    // Confirm that the alt text attribute is present.
    $assert_session = $this->assertSession();
    $element = $assert_session->elementAttributeExists('css', 'td.views-field-thumbnail__target-id img', 'alt');
    $this->assertSame('', (string) $element->getAttribute('alt'));
}

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