function MediaTest::testTranslationAlt

Same name and namespace in other branches
  1. 9 core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testTranslationAlt()
  2. 9 core/modules/ckeditor/tests/src/FunctionalJavascript/MediaTest.php \Drupal\Tests\ckeditor\FunctionalJavascript\MediaTest::testTranslationAlt()
  3. 11.x 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 484

Class

MediaTest
@coversDefaultClass \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Media[[api-linebreak]] @group ckeditor5 @group #slow @internal

Namespace

Drupal\Tests\ckeditor5\FunctionalJavascript

Code

public function testTranslationAlt() : void {
  \Drupal::service('module_installer')->install([
    'language',
    'content_translation',
  ]);
  $this->resetAll();
  ConfigurableLanguage::createFromLangcode('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 . '")]');
}

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