function MediaTest::testAlt
Same name in this branch
- 9 core/modules/ckeditor/tests/src/FunctionalJavascript/MediaTest.php \Drupal\Tests\ckeditor\FunctionalJavascript\MediaTest::testAlt()
Same name in other branches
- 10 core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testAlt()
- 11.x core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testAlt()
Tests the CKEditor 5 media plugin can override image media's alt attribute.
File
-
core/
modules/ ckeditor5/ tests/ src/ FunctionalJavascript/ MediaTest.php, line 684
Class
- MediaTest
- @coversDefaultClass \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Media @group ckeditor5 @internal
Namespace
Drupal\Tests\ckeditor5\FunctionalJavascriptCode
public function testAlt() {
$page = $this->getSession()
->getPage();
$assert_session = $this->assertSession();
$this->drupalGet($this->host
->toUrl('edit-form'));
$this->waitForEditor();
// Wait for the media preview to load.
$this->assertNotEmpty($assert_session->waitForElementVisible('css', '.ck-widget.drupal-media img'));
// Test that by default no alt attribute is present on the drupal-media
// element.
$this->assertSourceAttributeSame('alt', NULL);
// Test that the preview shows the alt value from the media field's
// alt text.
$this->assertNotEmpty($assert_session->waitForElementVisible('css', '.ck-widget.drupal-media img[alt*="default alt"]'));
// Test that clicking the media widget triggers a CKEditor balloon panel
// with a single button to override the alt text.
$this->click('.ck-widget.drupal-media');
$this->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
// Click the "Override media image alternative text" button.
$this->getBalloonButton('Override media image alternative text')
->click();
$this->assertVisibleBalloon('.ck-media-alternative-text-form');
// Assert that the default alt text is visible in the UI.
$assert_session->elementTextEquals('css', '.ck-media-alternative-text-form__default-alt-text-value', 'default alt');
// Assert that the value is currently empty.
$alt_override_input = $page->find('css', '.ck-balloon-panel .ck-media-alternative-text-form input[type=text]');
$this->assertSame('', $alt_override_input->getValue());
// Fill in the alt field and submit.
// cSpell:disable-next-line
$who_is_zartan = 'Zartan is the leader of the Dreadnoks.';
$alt_override_input->setValue($who_is_zartan);
$this->getBalloonButton('Save')
->click();
// Assert that the img within the media embed within the CKEditor contains
// the overridden alt text set in the dialog.
$this->assertNotEmpty($assert_session->waitForElementVisible('css', '.ck-widget.drupal-media img[alt*="' . $who_is_zartan . '"]'));
// Ensure that the Drupal Media widget doesn't have alt attribute.
// @see https://www.drupal.org/project/drupal/issues/3248440
$assert_session->elementNotExists('css', '.ck-widget.drupal-media[alt]');
// Test `aria-label` attribute appears on the widget wrapper.
$assert_session->elementExists('css', '.ck-widget.drupal-media [aria-label="Screaming hairy armadillo"]');
// Test that the downcast drupal-media element now has the alt attribute
// entered in the balloon.
$this->assertSourceAttributeSame('alt', $who_is_zartan);
// The alt field should now display the override instead of the default.
$this->getBalloonButton('Override media image alternative text')
->click();
$this->assertVisibleBalloon('.ck-media-alternative-text-form');
$alt_override_input = $page->find('css', '.ck-balloon-panel .ck-media-alternative-text-form input[type=text]');
$this->assertSame($who_is_zartan, $alt_override_input->getValue());
// Assert that the default alt text is still visible in the UI.
$assert_session->elementTextEquals('css', '.ck-media-alternative-text-form__default-alt-text-value', 'default alt');
// Test the process again with a different alt text to make sure it works
// the second time around.
$cobra_commander_bio = 'The supreme leader of the terrorist organization Cobra';
// Set the alt field to the new alt text.
$alt_override_input->setValue($cobra_commander_bio);
$this->getBalloonButton('Save')
->click();
// Assert that the img within the media embed preview inside CKEditor 5
// contains the overridden alt text set in the balloon.
$this->assertNotEmpty($assert_session->waitForElementVisible('css', '.ck-widget.drupal-media img[alt*="' . $cobra_commander_bio . '"]'));
// Test that the downcast drupal-media element now has the alt attribute
// entered in the dialog.
$this->assertSourceAttributeSame('alt', $cobra_commander_bio);
// The default value of the alt field should now display the override
// instead of the value on the media image field.
$this->getBalloonButton('Override media image alternative text')
->click();
$this->assertVisibleBalloon('.ck-media-alternative-text-form');
$alt_override_input = $page->find('css', '.ck-balloon-panel .ck-media-alternative-text-form input[type=text]');
$this->assertSame($cobra_commander_bio, $alt_override_input->getValue());
// Test that setting alt value to two double quotes will signal to the
// MediaEmbed filter to unset the attribute on the media image field.
// We intentionally add a space after the two double quotes to test that the
// string is trimmed to two quotes.
$alt_override_input->setValue('"" ');
$this->getBalloonButton('Save')
->click();
// Verify that the two double quote empty alt indicator ('""') set in
// the alt text form balloon has successfully resulted in a media image
// field with the alt attribute present but without a value.
$this->assertNotEmpty($assert_session->waitForElementVisible('css', '[data-media-embed-test-view-mode] img[alt=""]'));
// Test that the downcast drupal-media element's alt attribute now has the
// empty string indicator.
$this->assertSourceAttributeSame('alt', '""');
// Test that setting alt to back to an empty string within the balloon will
// restore the default alt value saved in to the media image field of the
// media item.
$this->getBalloonButton('Override media image alternative text')
->click();
$this->assertVisibleBalloon('.ck-media-alternative-text-form');
$alt_override_input = $page->find('css', '.ck-balloon-panel .ck-media-alternative-text-form input[type=text]');
$alt_override_input->setValue('');
$this->getBalloonButton('Save')
->click();
$this->assertNotEmpty($assert_session->waitForElementVisible('css', '.ck-widget.drupal-media img[alt*="default alt"]'));
// Test that the downcast drupal-media element no longer has an alt
// attribute.
$this->assertSourceAttributeSame('alt', NULL);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.