View source
<?php
namespace Drupal\Tests\metatag\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class MetatagStringTest extends BrowserTestBase {
use StringTranslationTrait;
protected $adminUser;
protected static $modules = [
'token',
'node',
'field_ui',
'metatag',
];
protected $defaultTheme = 'stark';
protected $permissions = [
'administer node fields',
'administer content types',
'access administration pages',
'administer meta tags',
'administer nodes',
'bypass node access',
'administer meta tags',
'administer site configuration',
'access content',
];
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser($this->permissions);
$this
->drupalLogin($this->adminUser);
$this
->drupalCreateContentType([
'type' => 'page',
'display_submitted' => FALSE,
]);
$this
->drupalGet('admin/structure/types');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/structure/types/manage/page/fields/add-field');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'label' => 'Metatag',
'field_name' => 'metatag_field',
'new_storage_type' => 'metatag',
];
$this
->drupalPostForm(NULL, $edit, $this
->t('Save and continue'));
$this
->drupalPostForm(NULL, [], $this
->t('Save field settings'));
$this->container
->get('entity_field.manager')
->clearCachedFieldDefinitions();
}
public function testSingleQuote() {
$this
->checkString("bla'bleblu");
}
public function testDoubleQuote() {
$this
->checkString('bla"bleblu');
}
public function testAmpersand() {
$this
->checkString("blable&blu");
}
public function checkString($string) {
$this
->checkConfig($string);
$this
->checkNode($string);
$this
->checkEncodedField($string);
}
public function checkConfig($string) {
$title_original = 'Title: ' . $string;
$desc_original = 'Description: ' . $string;
$title_encoded = htmlentities($title_original, ENT_QUOTES);
$desc_encoded = htmlentities($desc_original, ENT_QUOTES);
$title_encodeded = htmlentities($title_encoded, ENT_QUOTES);
$desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
$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'];
$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.');
$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.');
$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();
$this
->drupalGet('<front>');
$session
->statusCodeEquals(200);
$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);
$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?');
$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);
}
public function checkNode($string) {
$save_label = floatval(\Drupal::VERSION) <= 8.300000000000001 ? $this
->t('Save and publish') : $this
->t('Save');
$title_original = 'Title: ' . $string;
$desc_original = 'Description: ' . $string;
$title_encoded = htmlentities($title_original, ENT_QUOTES);
$desc_encoded = htmlentities($desc_original, ENT_QUOTES);
$title_encodeded = htmlentities($title_encoded, ENT_QUOTES);
$desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
$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);
$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);
$this
->drupalGet('node/1');
$session
->statusCodeEquals(200);
$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);
$session
->responseContains('<title>' . $title_encoded . '</title>', 'Confirmed the node title tag is encoded.');
$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);
$session
->responseContains('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
$session
->responseNotContains('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
$session
->responseNotContains('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');
}
public function checkEncodedField($string) {
$save_label = floatval(\Drupal::VERSION) <= 8.300000000000001 ? $this
->t('Save and publish') : $this
->t('Save');
$title_original = 'Title: ' . $string;
$desc_original = 'Description: ' . $string;
$desc_encoded = htmlentities($desc_original, ENT_QUOTES);
$desc_encodeded = htmlentities($desc_encoded, ENT_QUOTES);
$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);
$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);
$this
->drupalGet('node/1');
$session
->statusCodeEquals(200);
$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);
$session
->responseContains('"' . $desc_encoded . '"', 'Confirmed the node "description" meta tag string was encoded properly.');
$session
->responseNotContains('"' . $desc_original . '"', 'Confirmed the node "description" meta tag string does not show in its original form.');
$session
->responseNotContains('"' . $desc_encodeded . '"', 'Confirmed the node "description" meta tag string was not double-encoded.');
}
}