You are here

public function ReplicateUITest::testMultilingual in Replicate UI 8

Test replicating on multi-lingual environments.

File

tests/src/Functional/ReplicateUITest.php, line 146

Class

ReplicateUITest
Tests the UI functionality.

Namespace

Drupal\Tests\replicate_ui\Functional

Code

public function testMultilingual() {
  $this
    ->drupalLogin($this->user);
  foreach ([
    'es',
    'fr',
  ] as $langcode) {
    ConfigurableLanguage::createFromLangcode($langcode)
      ->save();
  }

  // Enable translation for page nodes.
  $this
    ->drupalGet('/admin/config/regional/content-language');
  $edit = [
    'entity_types[node]' => TRUE,
    'settings[node][page][fields][title]' => TRUE,
    'settings[node][page][translatable]' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save configuration');
  $node = Node::create([
    'type' => 'page',
    'title' => 'Multilingual test',
  ]);
  $node
    ->save();
  $node
    ->addTranslation('es', [
    'title' => 'Spanish title',
  ]);
  $node
    ->addTranslation('fr', [
    'title' => 'French title',
  ]);
  $node
    ->save();
  $this
    ->drupalGet($node
    ->toUrl());
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Replicate');
  $test_sets = [
    'en' => $node,
    'es' => $node
      ->getTranslation('es'),
    'fr' => $node
      ->getTranslation('fr'),
  ];

  /** @var \Drupal\node\NodeInterface $translation */
  foreach ($test_sets as $langcode => $translation) {
    $new_element_label = $this
      ->assertSession()
      ->elementExists('css', 'input[name="new_label_' . $langcode . '"]');
    $this
      ->assertequals($translation
      ->label() . ' (Copy)', $new_element_label
      ->getValue());
    $this
      ->getSession()
      ->getPage()
      ->fillField('new_label_' . $langcode, 'Overriden replicate label - ' . $langcode);
  }
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Replicate');
  $this
    ->assertSession()
    ->responseContains('<em class="placeholder">node</em> (<em class="placeholder">' . $node
    ->id() . '</em>) has been replicated');
  $this
    ->assertSession()
    ->titleEquals('Overriden replicate label - en | Drupal');

  // Check the replicated translations have the correct labels as well.
  $replicated = $this
    ->getNodeByTitle('Overriden replicate label - en');
  $replicated_es = $replicated
    ->getTranslation('es');
  $this
    ->assertEquals('Overriden replicate label - es', $replicated_es
    ->label());
  $replicated_fr = $replicated
    ->getTranslation('fr');
  $this
    ->assertEquals('Overriden replicate label - fr', $replicated_fr
    ->label());
}