function MediaTest::testTranslationAlt

Same name in this branch
  1. 9 core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testTranslationAlt()
Same name and namespace in other branches
  1. 10 core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testTranslationAlt()
  2. 11.x core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testTranslationAlt()

Tests that dialog loads appropriate translation's alt text.

File

core/modules/ckeditor/tests/src/FunctionalJavascript/MediaTest.php, line 760

Class

MediaTest
@coversDefaultClass \Drupal\ckeditor\Plugin\CKEditorPlugin\DrupalMedia[[api-linebreak]] @group ckeditor @group legacy

Namespace

Drupal\Tests\ckeditor\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();
  $this->assignNameToCkeditorIframe();
  $this->getSession()
    ->switchToIFrame('ckeditor');
  // 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 widget wrapper.
  // cSpell:disable-next-line
  $assert_session->elementExists('css', '.cke_widget_drupalmedia[aria-label="Tatou poilu hurlant"]');
  $page->pressButton('Edit media');
  $this->waitForMetadataDialog();
  // Assert that the placeholder is set to the value of the media field's
  // alt text.
  // cSpell:disable-next-line
  $assert_session->elementAttributeContains('named', [
    'field',
    'attributes[alt]',
  ], 'placeholder', 'texte alternatif par défaut');
  // Fill in the alt field in the dialog.
  // cSpell:disable-next-line
  $qui_est_zartan = 'Zartan est le chef des Dreadnoks.';
  $page->fillField('attributes[alt]', $qui_est_zartan);
  $this->submitDialog();
  $this->getSession()
    ->switchToIFrame('ckeditor');
  // Assert that the img within the media embed within CKEditor contains
  // the overridden alt text set in the dialog.
  $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.