You are here

public function MetatagNodeTranslationTest::testMetatagValueTranslation in Metatag 8

Tests the metatag value translations.

File

tests/src/Functional/MetatagNodeTranslationTest.php, line 87

Class

MetatagNodeTranslationTest
Ensures that meta tag values are translated correctly on nodes.

Namespace

Drupal\Tests\metatag\Functional

Code

public function testMetatagValueTranslation() {
  if (floatval(\Drupal::VERSION) <= 8.300000000000001) {
    $save_label = $this
      ->t('Save and publish');
    $save_label_i18n = $this
      ->t('Save and keep published (this translation)');
  }
  else {
    $save_label = $this
      ->t('Save');
    $save_label_i18n = $this
      ->t('Save (this translation)');
  }

  // Set up a content type.
  $name = $this
    ->randomMachineName() . ' ' . $this
    ->randomMachineName();
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalCreateContentType([
    'type' => 'metatag_node',
    'name' => $name,
  ]);

  // Add a metatag field to the content type.
  $this
    ->drupalGet('admin/structure/types');
  $session = $this
    ->assertSession();
  $session
    ->statusCodeEquals(200);
  $this
    ->drupalGet('admin/structure/types/manage/metatag_node');
  $session
    ->statusCodeEquals(200);
  $edit = [
    'language_configuration[language_alterable]' => TRUE,
    'language_configuration[content_translation]' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save content type'));
  $session
    ->statusCodeEquals(200);
  $this
    ->drupalGet('admin/structure/types/manage/metatag_node/fields/add-field');
  $session
    ->statusCodeEquals(200);
  $edit = [
    'label' => 'Meta tags',
    'field_name' => 'meta_tags',
    'new_storage_type' => 'metatag',
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save and continue'));
  $session
    ->statusCodeEquals(200);
  $this
    ->drupalPostForm(NULL, [], $this
    ->t('Save field settings'));
  $session
    ->statusCodeEquals(200);
  $edit = [
    'translatable' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save settings'));
  $session
    ->statusCodeEquals(200);
  $this
    ->drupalGet('admin/structure/types/manage/metatag_node/fields/node.metatag_node.field_meta_tags');
  $session
    ->statusCodeEquals(200);

  // Set up a node without explicit metatag description. This causes the
  // global default to be used, which contains a token (node:summary). The
  // token value should be correctly translated.
  // Load the node form.
  $this
    ->drupalGet('node/add/metatag_node');
  $session
    ->statusCodeEquals(200);

  // Check the default values are correct.
  $session
    ->fieldValueEquals('field_meta_tags[0][basic][title]', '[node:title] | [site:name]');
  $session
    ->fieldValueEquals('field_meta_tags[0][basic][description]', '[node:summary]');

  // Create a node.
  $edit = [
    'title[0][value]' => 'Node Français',
    'body[0][value]' => 'French summary.',
  ];
  $this
    ->drupalPostForm(NULL, $edit, $save_label);
  $session
    ->statusCodeEquals(200);
  $xpath = $this
    ->xpath("//meta[@name='description']");
  $this
    ->assertCount(1, $xpath, 'Exactly one description meta tag found.');
  $value = $xpath[0]
    ->getAttribute('content');
  $this
    ->assertEquals($value, 'French summary.');
  $this
    ->drupalGet('node/1/translations/add/en/es');
  $session
    ->statusCodeEquals(200);

  // Check the default values are there.
  $session
    ->fieldValueEquals('field_meta_tags[0][basic][title]', '[node:title] | [site:name]');
  $session
    ->fieldValueEquals('field_meta_tags[0][basic][description]', '[node:summary]');
  $edit = [
    'title[0][value]' => 'Node Español',
    'body[0][value]' => 'Spanish summary.',
  ];
  $this
    ->drupalPostForm(NULL, $edit, $save_label_i18n);
  $session
    ->statusCodeEquals(200);
  $this
    ->drupalGet('es/node/1');
  $session
    ->statusCodeEquals(200);
  $xpath = $this
    ->xpath("//meta[@name='description']");
  $this
    ->assertCount(1, $xpath, 'Exactly one description meta tag found.');
  $value = $xpath[0]
    ->getAttribute('content');
  $this
    ->assertEquals($value, 'Spanish summary.');
  $this
    ->assertNotEquals($value, 'French summary.');
  $this
    ->drupalGet('node/1/edit');
  $session
    ->statusCodeEquals(200);

  // Check the default values are there.
  $session
    ->fieldValueEquals('field_meta_tags[0][basic][title]', '[node:title] | [site:name]');
  $session
    ->fieldValueEquals('field_meta_tags[0][basic][description]', '[node:summary]');

  // Set explicit values on the description metatag instead using the
  // defaults.
  $this
    ->drupalGet('node/1/edit');
  $session
    ->statusCodeEquals(200);
  $edit = [
    'field_meta_tags[0][basic][description]' => 'Overridden French description.',
  ];
  $this
    ->drupalPostForm(NULL, $edit, $save_label_i18n);
  $session
    ->statusCodeEquals(200);
  $xpath = $this
    ->xpath("//meta[@name='description']");
  $this
    ->assertCount(1, $xpath, 'Exactly one description meta tag found.');
  $value = $xpath[0]
    ->getAttribute('content');
  $this
    ->assertEquals($value, 'Overridden French description.');
  $this
    ->assertNotEquals($value, 'Spanish summary.');
  $this
    ->assertNotEquals($value, 'French summary.');
  $this
    ->drupalGet('es/node/1/edit');
  $session
    ->statusCodeEquals(200);
  $edit = [
    'field_meta_tags[0][basic][description]' => 'Overridden Spanish description.',
  ];
  $this
    ->drupalPostForm(NULL, $edit, $save_label_i18n);
  $session
    ->statusCodeEquals(200);
  $xpath = $this
    ->xpath("//meta[@name='description']");
  $this
    ->assertCount(1, $xpath, 'Exactly one description meta tag found.');
  $value = $xpath[0]
    ->getAttribute('content');
  $this
    ->assertEquals($value, 'Overridden Spanish description.');
  $this
    ->assertNotEquals($value, 'Spanish summary.');
  $this
    ->assertNotEquals($value, 'French summary.');
}