namespace Drupal\metatag\Tests;
use Drupal\simpletest\WebTestBase;
+use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
/**
$xpath = "//input[@name='{$tag}' and @type='text']";
}
- $this->assertFieldByXPath($xpath, NULL, format_string('Found the @tag meta tag field.', ['@tag' => $tag]));
+ $this->assertFieldByXPath($xpath, NULL, new FormattableMarkup('Found the @tag meta tag field.', ['@tag' => $tag]));
}
$this->drupalLogout();
* correct.
*/
function testTagsInputOutput() {
- // Load the global config.
- $this->drupalGet('admin/config/search/metatag/global');
- $this->assertResponse(200);
-
- // Update the Global defaults and test them.
- $all_values = $values = [];
- foreach ($this->tags as $tag_name) {
- // Look for a custom method named "{$tagname}_test_key", if found use
- // that method to get the test string for this meta tag, otherwise it
- // defaults to the meta tag's name.
- $method = "{$tag_name}_test_key";
- if (method_exists($this, $method)) {
- $test_key = $this->$method();
- }
- else {
- $test_key = $tag_name;
- }
-
- // Look for a custom method named "{$tagname}_test_value", if found use
- // that method to get the test string for this meta tag, otherwise it
- // defaults to just generating a random string.
- $method = "{$tag_name}_test_value";
- if (method_exists($this, $method)) {
- $test_value = $this->$method();
- }
- else {
- // Generate a random string. Generating two words of 8 characters each
- // with simple machine name -style strings.
- $test_value = $this->randomMachineName() . ' ' . $this->randomMachineName();
- }
-
- $values[$test_key] = $test_value;
- $all_values[$tag_name] = $test_value;
- }
- $this->drupalPostForm(NULL, $values, 'Save');
- $this->assertText('Saved the Global Metatag defaults.');
-
- // Load the test page.
- $this->drupalGet('metatag_test_custom_route');
- $this->assertResponse(200);
+ // Create a content type to test with.
+ $this->createContentType(['type' => 'page']);
+ $node = $this->drupalCreateNode([
+ 'title' => t('Hello, world!'),
+ 'type' => 'page',
+ ]);
+
+ // Test a non-entity path and an entity path. The non-entity path inherits
+ // the global meta tags, the entity path inherits from its entity config.
+ $paths = [
+ [
+ 'admin/config/search/metatag/global',
+ 'metatag_test_custom_route',
+ 'Saved the Global Metatag defaults.',
+ ],
+ [
+ 'admin/config/search/metatag/node',
+ 'node/1',
+ 'Saved the Content Metatag defaults',
+ ],
+ ];
- // Look for the values.
- foreach ($this->tags as $tag_name) {
- // Look for a custom method named "{$tag_name}_test_output_xpath", if
- // found use that method to get the xpath definition for this meta tag,
- // otherwise it defaults to just looking for a meta tag matching:
- // <$test_tag $test_name_attribute=$tag_name $test_value_attribute=$value />
- $method = "{$tag_name}_test_output_xpath";
- if (method_exists($this, $method)) {
- $xpath_string = $this->$method();
- }
- else {
- // Look for a custom method named "{$tag_name}_test_tag", if
- // found use that method to get the xpath definition for this meta tag,
- // otherwise it defaults to $this->test_tag.
- $method = "{$tag_name}_test_tag";
+ //
+ foreach ($paths as $item) {
+ list($path1, $path2, $save_message) = $item;
+
+ // Load the global config.
+ $this->drupalGet($path1);
+ $this->assertResponse(200);
+
+ // Update the Global defaults and test them.
+ $all_values = $values = [];
+ foreach ($this->tags as $tag_name) {
+ // Look for a custom method named "{$tagname}_test_key", if found use
+ // that method to get the test string for this meta tag, otherwise it
+ // defaults to the meta tag's name.
+ $method = "{$tag_name}_test_key";
if (method_exists($this, $method)) {
- $xpath_tag = $this->$method();
+ $test_key = $this->$method();
}
else {
- $xpath_tag = $this->test_tag;
+ $test_key = $tag_name;
}
- // Look for a custom method named "{$tag_name}_test_name_attribute", if
- // found use that method to get the xpath definition for this meta tag,
- // otherwise it defaults to $this->test_name_attribute.
- $method = "{$tag_name}_test_name_attribute";
+ // Look for a custom method named "{$tagname}_test_value", if found use
+ // that method to get the test string for this meta tag, otherwise it
+ // defaults to just generating a random string.
+ $method = "{$tag_name}_test_value";
if (method_exists($this, $method)) {
- $xpath_name_attribute = $this->$method();
+ $test_value = $this->$method();
}
else {
- $xpath_name_attribute = $this->test_name_attribute;
+ // Generate a random string. Generating two words of 8 characters each
+ // with simple machine name -style strings.
+ $test_value = $this->randomMachineName() . ' ' . $this->randomMachineName();
}
- // Look for a custom method named "{$tag_name}_test_tag_name", if
+ $values[$test_key] = $test_value;
+ $all_values[$tag_name] = $test_value;
+ }
+ $this->drupalPostForm(NULL, $values, 'Save');
+ $this->assertText($save_message);
+
+ // Load the test page.
+ $this->drupalGet($path2);
+ $this->assertResponse(200);
+
+ // Look for the values.
+ foreach ($this->tags as $tag_name) {
+ // Look for a custom method named "{$tag_name}_test_output_xpath", if
// found use that method to get the xpath definition for this meta tag,
- // otherwise it defaults to $tag_name.
- $method = "{$tag_name}_test_tag_name";
+ // otherwise it defaults to just looking for a meta tag matching:
+ // <$test_tag $test_name_attribute=$tag_name $test_value_attribute=$value />
+ $method = "{$tag_name}_test_output_xpath";
if (method_exists($this, $method)) {
- $xpath_name_tag = $this->$method();
+ $xpath_string = $this->$method();
}
else {
- $xpath_name_tag = $this->getTestTagName($tag_name);
- }
+ // Look for a custom method named "{$tag_name}_test_tag", if
+ // found use that method to get the xpath definition for this meta
+ // tag, otherwise it defaults to $this->test_tag.
+ $method = "{$tag_name}_test_tag";
+ if (method_exists($this, $method)) {
+ $xpath_tag = $this->$method();
+ }
+ else {
+ $xpath_tag = $this->test_tag;
+ }
- // Compile the xpath.
- $xpath_string = "//{$xpath_tag}[@{$xpath_name_attribute}='{$xpath_name_tag}']";
- }
+ // Look for a custom method named "{$tag_name}_test_name_attribute",
+ // if found use that method to get the xpath definition for this meta
+ // tag, otherwise it defaults to $this->test_name_attribute.
+ $method = "{$tag_name}_test_name_attribute";
+ if (method_exists($this, $method)) {
+ $xpath_name_attribute = $this->$method();
+ }
+ else {
+ $xpath_name_attribute = $this->test_name_attribute;
+ }
- // Look for a custom method named "{$tag_name}_test_value_attribute", if
- // found use that method to get the xpath definition for this meta tag,
- // otherwise it defaults to $this->test_value_attribute.
- $method = "{$tag_name}_test_value_attribute";
- if (method_exists($this, $method)) {
- $xpath_value_attribute = $this->$method();
- }
- else {
- $xpath_value_attribute = $this->test_value_attribute;
- }
+ // Look for a custom method named "{$tag_name}_test_tag_name", if
+ // found use that method to get the xpath definition for this meta
+ // tag, otherwise it defaults to $tag_name.
+ $method = "{$tag_name}_test_tag_name";
+ if (method_exists($this, $method)) {
+ $xpath_name_tag = $this->$method();
+ }
+ else {
+ $xpath_name_tag = $this->getTestTagName($tag_name);
+ }
- // Extract the meta tag from the HTML.
- $xpath = $this->xpath($xpath_string);
- $this->assertEqual(count($xpath), 1, format_string('One @name tag found.', ['@name' => $tag_name]));
- if (count($xpath) !== 1) {
- $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
- }
+ // Compile the xpath.
+ $xpath_string = "//{$xpath_tag}[@{$xpath_name_attribute}='{$xpath_name_tag}']";
+ }
- // Run various tests on the output variables.
- // $this->assertTrue($xpath_string);
- // $this->assertTrue($xpath_value_attribute);
- // $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
- // $this->assertTrue($all_values[$tag_name]);
- // $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
- // $this->assertEqual($xpath[0][$xpath_value_attribute], $all_values[$tag_name], "The meta tag was found with the expected value.");
- // Most meta tags have an attribute, but some don't.
- if (!empty($xpath_value_attribute)) {
- $this->assertTrue($xpath_value_attribute);
- $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
- // Help with debugging.
- if (!isset($xpath[0][$xpath_value_attribute])) {
- $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
+ // Look for a custom method named "{$tag_name}_test_value_attribute", if
+ // found use that method to get the xpath definition for this meta tag,
+ // otherwise it defaults to $this->test_value_attribute.
+ $method = "{$tag_name}_test_value_attribute";
+ if (method_exists($this, $method)) {
+ $xpath_value_attribute = $this->$method();
}
else {
- if ((string)$xpath[0][$xpath_value_attribute] != $all_values[$tag_name]) {
+ $xpath_value_attribute = $this->test_value_attribute;
+ }
+
+ // Extract the meta tag from the HTML.
+ $xpath = $this->xpath($xpath_string);
+ $this->assertEqual(count($xpath), 1, new FormattableMarkup('One @name tag found.', ['@name' => $tag_name]));
+ if (count($xpath) !== 1) {
+ $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
+ }
+
+ // Run various tests on the output variables.
+ // $this->assertTrue($xpath_string);
+ // $this->assertTrue($xpath_value_attribute);
+ // $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
+ // $this->assertTrue($all_values[$tag_name]);
+ // $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
+ // $this->assertEqual($xpath[0][$xpath_value_attribute], $all_values[$tag_name], "The meta tag was found with the expected value.");
+ // Most meta tags have an attribute, but some don't.
+ if (!empty($xpath_value_attribute)) {
+ $this->assertTrue($xpath_value_attribute);
+ $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
+ // Help with debugging.
+ if (!isset($xpath[0][$xpath_value_attribute])) {
$this->verbose($xpath, $tag_name . ': ' . $xpath_string);
}
- $this->assertTrue($xpath[0][$xpath_value_attribute]);
- $this->assertEqual($xpath[0][$xpath_value_attribute], $all_values[$tag_name], "The meta tag was found with the expected value.");
+ else {
+ if ((string)$xpath[0][$xpath_value_attribute] != $all_values[$tag_name]) {
+ $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
+ }
+ $this->assertTrue($xpath[0][$xpath_value_attribute]);
+ $this->assertEqual($xpath[0][$xpath_value_attribute], $all_values[$tag_name], "The meta tag was found with the expected value.");
+ }
+ }
+ else {
+ $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
+ $this->assertTrue((string)$xpath[0]);
+ $this->assertEqual((string)$xpath[0], $all_values[$tag_name], "The meta tag was found with the expected value.");
}
- }
- else {
- $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
- $this->assertTrue((string)$xpath[0]);
- $this->assertEqual((string)$xpath[0], $all_values[$tag_name], "The meta tag was found with the expected value.");
}
}