You are here

public function MetatagStringTest::checkConfig in Metatag 8

Tests that a specific config string is not double encoded.

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

File

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

Class

MetatagStringTest
Ensures that the Metatag field works correctly.

Namespace

Drupal\Tests\metatag\Functional

Code

public function checkConfig($string) {

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

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

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

  // Update the Global defaults and test them.
  $this
    ->drupalGet('admin/config/search/metatag/front');
  $session = $this
    ->assertSession();
  $session
    ->statusCodeEquals(200);
  $edit = [
    'title' => $title_original,
    'description' => $desc_original,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $session
    ->statusCodeEquals(200);
  $metatag_defaults = \Drupal::config('metatag.metatag_defaults.front');
  $default_title = $metatag_defaults
    ->get('tags')['title'];
  $default_description = $metatag_defaults
    ->get('tags')['description'];

  // Make sure the title tag is stored correctly.
  $this
    ->assertEquals($title_original, $default_title, 'The title tag was stored in its original format.');
  $this
    ->assertNotEquals($title_encoded, $default_title, 'The title tag was not stored in an encoded format.');
  $this
    ->assertNotEquals($title_encodeded, $default_title, 'The title tag was not stored in a double-encoded format.');

  // Make sure the description tag is stored correctly.
  $this
    ->assertEquals($desc_original, $default_description, 'The description tag was stored in its original format.');
  $this
    ->assertNotEquals($desc_encoded, $default_description, 'The description tag was not stored in an encoded format.');
  $this
    ->assertNotEquals($desc_encodeded, $default_description, 'The description tag was not stored in a double-encoded format.');

  // 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,
  ];
  $save_label = floatval(\Drupal::VERSION) <= 8.300000000000001 ? $this
    ->t('Save and publish') : $this
    ->t('Save');
  $this
    ->drupalPostForm(NULL, $edit, $save_label);
  $this
    ->config('system.site')
    ->set('page.front', '/node/1')
    ->save();

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

  // Again, with xpath the HTML entities will be parsed automagically.
  $xpath_title = current($this
    ->xpath("//title"))
    ->getText();
  $this
    ->assertEquals($xpath_title, $title_original);
  $this
    ->assertNotEquals($xpath_title, $title_encoded);
  $this
    ->assertNotEquals($xpath_title, $title_encodeded);

  // The page title should be HTML encoded; have to do this check manually
  // because assertRaw() checks the raw HTML, not the parsed strings like
  // xpath does.
  $session
    ->responseContains('<title>' . $title_encoded . '</title>', 'Confirmed the node title tag is available in its encoded format.');
  $session
    ->responseNotContains('<title>' . $title_original . '</title>', 'Confirmed the node title tag is not available in its original format.');
  $session
    ->responseNotContains('<title>' . $title_encodeded . '</title>', 'Confirmed the node title tag is not double-double-encoded?');

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