You are here

public function CKEditorIntegrationTest::testTranslationAlt in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php \Drupal\Tests\media\FunctionalJavascript\CKEditorIntegrationTest::testTranslationAlt()

Test that dialog loads appropriate translation's alt text.

File

core/modules/media/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php, line 749

Class

CKEditorIntegrationTest
@coversDefaultClass \Drupal\media\Plugin\CKEditorPlugin\DrupalMedia @group media

Namespace

Drupal\Tests\media\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');
  $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.
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('xpath', '//img[contains(@alt, "texte alternatif par défaut")]'));

  // Test `aria-label` attribute appears on the widget wrapper.
  $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.
  $assert_session
    ->elementAttributeContains('named', [
    'field',
    'attributes[alt]',
  ], 'placeholder', 'texte alternatif par défaut');

  // Fill in the alt field in the dialog.
  $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 . '")]');
}