You are here

public function MetatagConfigTranslationTest::testConfigTranslations in Metatag 8

Confirm the global configs are translatable page.

File

tests/src/Functional/MetatagConfigTranslationTest.php, line 123

Class

MetatagConfigTranslationTest
Ensures that the Metatag config translations work correctly.

Namespace

Drupal\Tests\metatag\Functional

Code

public function testConfigTranslations() {

  // Add something to the Global config.
  $this
    ->drupalGet('admin/config/search/metatag/global');
  $session = $this
    ->assertSession();
  $session
    ->statusCodeEquals(200);
  $edit = [
    'title' => 'Test title',
    'description' => 'Test description',
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save'));
  $session
    ->statusCodeEquals(200);
  $session
    ->pageTextContains('Saved the Global Metatag defaults.');

  // Confirm the config has languages available to translate into.
  $this
    ->drupalGet('admin/config/search/metatag/global/translate');
  $session
    ->statusCodeEquals(200);

  // Load the translation form.
  $this
    ->drupalGet('admin/config/search/metatag/global/translate/fr/add');
  $session
    ->statusCodeEquals(200);

  // Confirm the meta tag fields are shown on the form. Confirm the fields and
  // values separately to make it easier to pinpoint where the problem is if
  // one should fail.
  $session
    ->fieldExists('translation[config_names][metatag.metatag_defaults.global][tags][title]');
  $session
    ->fieldValueEquals('translation[config_names][metatag.metatag_defaults.global][tags][title]', $edit['title']);
  $session
    ->fieldExists('translation[config_names][metatag.metatag_defaults.global][tags][description]');
  $session
    ->fieldValueEquals('translation[config_names][metatag.metatag_defaults.global][tags][description]', $edit['description']);

  // Confirm the form can be saved correctly.
  $edit = [
    'translation[config_names][metatag.metatag_defaults.global][tags][title]' => 'Le title',
    'translation[config_names][metatag.metatag_defaults.global][tags][description]' => 'Le description',
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save translation'));
  $session
    ->statusCodeEquals(200);
  $session
    ->pageTextContains('Successfully saved French translation');

  // Delete the node metatag defaults to simplify the test.
  MetatagDefaults::load('node')
    ->delete();

  // Create a node in french, request default tags for it. Ensure that the
  // config translation language is afterwards still/again set to EN and
  // tags are returned in FR.
  $this
    ->drupalCreateContentType([
    'type' => 'page',
  ]);
  $node = $this
    ->drupalCreateNode([
    'title' => 'Metatag Test FR',
    'langcode' => 'fr',
  ]);
  $language_manager = \Drupal::languageManager();
  $this
    ->assertEquals('en', $language_manager
    ->getConfigOverrideLanguage()
    ->getId());
  $fr_default_tags = metatag_get_default_tags($node);
  $this
    ->assertEquals('Le title', $fr_default_tags['title']);
  $this
    ->assertEquals('Le description', $fr_default_tags['description']);
  $this
    ->assertEquals('en', $language_manager
    ->getConfigOverrideLanguage()
    ->getId());

  // Delete the default tags as well to test the early return.
  MetatagDefaults::load('global')
    ->delete();
  $fr_default_tags = metatag_get_default_tags($node);
  $this
    ->assertNull($fr_default_tags);
  $this
    ->assertEquals('en', $language_manager
    ->getConfigOverrideLanguage()
    ->getId());
}