You are here

public function MetatagStringTest::checkEncodedField in Metatag 8

Tests that fields with encoded HTML entities will not be double-encoded.

1 call to MetatagStringTest::checkEncodedField()
MetatagStringTest::checkString in tests/src/Functional/MetatagStringTest.php
Tests that specific strings are not double escaped.

File

tests/src/Functional/MetatagStringTest.php, line 268

Class

MetatagStringTest
Ensures that the Metatag field works correctly.

Namespace

Drupal\Tests\metatag\Functional

Code

public function checkEncodedField($string) {
  $save_label = floatval(\Drupal::VERSION) <= 8.300000000000001 ? $this
    ->t('Save and publish') : $this
    ->t('Save');

  // The original strings.
  $title_original = 'Title: ' . $string;
  $desc_original = 'Description: ' . $string;

  // The strings after they're encoded, but quotes will not be encoded.
  $desc_encoded = htmlentities($desc_original, ENT_QUOTES);

  // The strings double-encoded, to make sure the tags aren't broken.
  $desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);

  // Update the Global defaults and test them.
  $this
    ->drupalGet('admin/config/search/metatag/global');
  $session = $this
    ->assertSession();
  $session
    ->statusCodeEquals(200);
  $edit = [
    'title' => $title_original,
    'description' => $desc_original,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save'));
  $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.
  // Create a node.
  $this
    ->drupalGet('node/add/page');
  $session
    ->statusCodeEquals(200);
  $edit = [
    'title[0][value]' => $title_original,
    'body[0][value]' => $desc_original,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $save_label);
  $session
    ->statusCodeEquals(200);

  // Load the node page.
  $this
    ->drupalGet('node/1');
  $session
    ->statusCodeEquals(200);

  // With xpath the HTML entities will be parsed automagically.
  $xpath = $this
    ->xpath("//meta[@name='description']");
  $value = $xpath[0]
    ->getAttribute('content');
  $this
    ->assertEquals($value, $desc_original);
  $this
    ->assertNotEquals($value, $desc_encoded);
  $this
    ->assertNotEquals($value, $desc_encodeded);

  // Normal meta tags should be encoded properly.
  $session
    ->responseContains('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');

  // Normal meta tags with HTML entities should be displayed in their original
  // format.
  $session
    ->responseNotContains('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');

  // Normal meta tags should not be double-encoded.
  $session
    ->responseNotContains('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');
}