You are here

public function MediaTest::testTranslationAlt in Drupal 10

Same name in this branch
  1. 10 core/modules/ckeditor/tests/src/FunctionalJavascript/MediaTest.php \Drupal\Tests\ckeditor\FunctionalJavascript\MediaTest::testTranslationAlt()
  2. 10 core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testTranslationAlt()

Tests the CKEditor 5 media plugin loads the translated alt attribute.

File

core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php, line 751

Class

MediaTest
@coversDefaultClass \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Media @group ckeditor5 @internal

Namespace

Drupal\Tests\ckeditor5\FunctionalJavascript

Code

public function testTranslationAlt() {
  \Drupal::service('module_installer')
    ->install([
    'language',
    'content_translation',
  ]);
  $this
    ->resetAll();
  ConfigurableLanguage::create([
    'id' => 'fr',
  ])
    ->save();
  ContentLanguageSettings::loadByEntityTypeBundle('media', 'image')
    ->setDefaultLangcode('en')
    ->setLanguageAlterable(TRUE)
    ->save();
  $media = Media::create([
    'bundle' => 'image',
    'name' => 'Screaming hairy armadillo',
    'field_media_image' => [
      [
        'target_id' => 1,
        'alt' => 'default alt',
        'title' => 'default title',
      ],
    ],
  ]);
  $media
    ->save();
  $media_fr = $media
    ->addTranslation('fr');
  $media_fr->name = "Tatou poilu hurlant";
  $media_fr->field_media_image
    ->setValue([
    [
      'target_id' => '1',
      'alt' => "texte alternatif par défaut",
      'title' => "titre alternatif par défaut",
    ],
  ]);
  $media_fr
    ->save();
  ContentLanguageSettings::loadByEntityTypeBundle('node', 'blog')
    ->setDefaultLangcode('en')
    ->setLanguageAlterable(TRUE)
    ->save();
  $host = $this
    ->createNode([
    'type' => 'blog',
    'title' => 'Animals with strange names',
    'body' => [
      'value' => '<drupal-media data-caption="baz" data-entity-type="media" data-entity-uuid="' . $media
        ->uuid() . '"></drupal-media>',
      'format' => 'test_format',
    ],
  ]);
  $host
    ->save();
  $translation = $host
    ->addTranslation('fr');

  // cSpell:disable-next-line
  $translation->title = 'Animaux avec des noms étranges';
  $translation->body->value = $host->body->value;
  $translation->body->format = $host->body->format;
  $translation
    ->save();
  Role::load(RoleInterface::AUTHENTICATED_ID)
    ->grantPermission('translate any entity')
    ->save();
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $this
    ->drupalGet('/fr/node/' . $host
    ->id() . '/edit');
  $this
    ->waitForEditor();

  // Test that the default alt attribute displays without an override.
  // cSpell:disable-next-line
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('xpath', '//img[contains(@alt, "texte alternatif par défaut")]'));

  // Test `aria-label` attribute appears on the preview wrapper.
  // cSpell:disable-next-line
  $assert_session
    ->elementExists('css', '[data-drupal-media-preview][aria-label="Tatou poilu hurlant"]');
  $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 on the UI is the default alt text from the
  // media entity.
  // cSpell:disable-next-line
  $assert_session
    ->elementTextEquals('css', '.ck-media-alternative-text-form__default-alt-text-value', 'texte alternatif par défaut');

  // Fill in the alt field in the balloon form.
  // cSpell:disable-next-line
  $qui_est_zartan = 'Zartan est le chef des Dreadnoks.';
  $alt_override_input = $page
    ->find('css', '.ck-balloon-panel .ck-media-alternative-text-form input[type=text]');
  $alt_override_input
    ->setValue($qui_est_zartan);
  $this
    ->getBalloonButton('Save')
    ->click();

  // Assert that the img within the media embed within CKEditor 5 contains
  // the overridden alt text set in CKEditor 5.
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('xpath', '//img[contains(@alt, "' . $qui_est_zartan . '")]'));
  $this
    ->getSession()
    ->switchToIFrame();
  $page
    ->pressButton('Save');
  $assert_session
    ->elementExists('xpath', '//img[contains(@alt, "' . $qui_est_zartan . '")]');
}