3 namespace Drupal\metatag\Tests;
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\Component\Utility\Html;
10 * Base class to test all of the meta tags that are in a specific module.
12 abstract class MetatagTagsTestBase extends WebTestBase {
17 public static $modules = [
18 // This is needed for the 'access content' permission.
27 // This module will be used to load a static page which will inherit the
28 // global defaults, without loading values from other configs.
29 'metatag_test_custom_route',
33 * All of the meta tags defined by this module which will be tested.
38 * The tag to look for when testing the output.
40 public $test_tag = 'meta';
43 * The attribute to look for to indicate which tag.
45 public $test_name_attribute = 'name';
48 * The attribute to look for when testing the output.
50 public $test_value_attribute = 'content';
55 protected function setUp() {
58 // Use the test page as the front page.
59 $this->config('system.site')->set('page.front', '/test-page')->save();
61 // Initiate session with a user who can manage metatags and access content.
63 'administer site configuration',
64 'administer meta tags',
67 $account = $this->drupalCreateUser($permissions);
68 $this->drupalLogin($account);
74 protected function verbose($message, $title = NULL) {
75 // Handle arrays, objects, etc.
76 if (!is_string($message)) {
77 $message = "<pre>\n" . print_r($message, TRUE) . "\n</pre>\n";
80 // Optional title to go before the output.
82 $title = '<h2>' . Html::escape($title) . "</h2>\n";
85 parent::verbose($title . $message);
89 * Tests that this module's tags are available.
91 function testTagsArePresent() {
92 // Load the global config.
93 $this->drupalGet('admin/config/search/metatag/global');
94 $this->assertResponse(200);
96 // Confirm the various meta tags are available.
97 foreach ($this->tags as $tag) {
98 // Look for a custom method named "{$tagname}_test_field_xpath", if found
99 // use that method to get the xpath definition for this meta tag,
100 // otherwise it defaults to just looking for a text input field.
101 $method = "{$tag}_test_field_xpath";
102 if (method_exists($this, $method)) {
103 $xpath = $this->$method();
106 $xpath = "//input[@name='{$tag}' and @type='text']";
109 $this->assertFieldByXPath($xpath, NULL, new FormattableMarkup('Found the @tag meta tag field.', ['@tag' => $tag]));
112 $this->drupalLogout();
116 * Confirm that each tag can be saved and that the output of each tag is
119 function testTagsInputOutput() {
120 // Create a content type to test with.
121 $this->createContentType(['type' => 'page']);
122 $node = $this->drupalCreateNode([
123 'title' => t('Hello, world!'),
127 // Test a non-entity path and an entity path. The non-entity path inherits
128 // the global meta tags, the entity path inherits from its entity config.
131 'admin/config/search/metatag/global',
132 'metatag_test_custom_route',
133 'Saved the Global Metatag defaults.',
136 'admin/config/search/metatag/node',
138 'Saved the Content Metatag defaults',
143 foreach ($paths as $item) {
144 list($path1, $path2, $save_message) = $item;
146 // Load the global config.
147 $this->drupalGet($path1);
148 $this->assertResponse(200);
150 // Update the Global defaults and test them.
151 $all_values = $values = [];
152 foreach ($this->tags as $tag_name) {
153 // Look for a custom method named "{$tagname}_test_key", if found use
154 // that method to get the test string for this meta tag, otherwise it
155 // defaults to the meta tag's name.
156 $method = "{$tag_name}_test_key";
157 if (method_exists($this, $method)) {
158 $test_key = $this->$method();
161 $test_key = $tag_name;
164 // Look for a custom method named "{$tagname}_test_value", if found use
165 // that method to get the test string for this meta tag, otherwise it
166 // defaults to just generating a random string.
167 $method = "{$tag_name}_test_value";
168 if (method_exists($this, $method)) {
169 $test_value = $this->$method();
172 // Generate a random string. Generating two words of 8 characters each
173 // with simple machine name -style strings.
174 $test_value = $this->randomMachineName() . ' ' . $this->randomMachineName();
177 $values[$test_key] = $test_value;
178 $all_values[$tag_name] = $test_value;
180 $this->drupalPostForm(NULL, $values, 'Save');
181 $this->assertText($save_message);
183 // Load the test page.
184 $this->drupalGet($path2);
185 $this->assertResponse(200);
187 // Look for the values.
188 foreach ($this->tags as $tag_name) {
189 // Look for a custom method named "{$tag_name}_test_output_xpath", if
190 // found use that method to get the xpath definition for this meta tag,
191 // otherwise it defaults to just looking for a meta tag matching:
192 // <$test_tag $test_name_attribute=$tag_name $test_value_attribute=$value />
193 $method = "{$tag_name}_test_output_xpath";
194 if (method_exists($this, $method)) {
195 $xpath_string = $this->$method();
198 // Look for a custom method named "{$tag_name}_test_tag", if
199 // found use that method to get the xpath definition for this meta
200 // tag, otherwise it defaults to $this->test_tag.
201 $method = "{$tag_name}_test_tag";
202 if (method_exists($this, $method)) {
203 $xpath_tag = $this->$method();
206 $xpath_tag = $this->test_tag;
209 // Look for a custom method named "{$tag_name}_test_name_attribute",
210 // if found use that method to get the xpath definition for this meta
211 // tag, otherwise it defaults to $this->test_name_attribute.
212 $method = "{$tag_name}_test_name_attribute";
213 if (method_exists($this, $method)) {
214 $xpath_name_attribute = $this->$method();
217 $xpath_name_attribute = $this->test_name_attribute;
220 // Look for a custom method named "{$tag_name}_test_tag_name", if
221 // found use that method to get the xpath definition for this meta
222 // tag, otherwise it defaults to $tag_name.
223 $method = "{$tag_name}_test_tag_name";
224 if (method_exists($this, $method)) {
225 $xpath_name_tag = $this->$method();
228 $xpath_name_tag = $this->getTestTagName($tag_name);
231 // Compile the xpath.
232 $xpath_string = "//{$xpath_tag}[@{$xpath_name_attribute}='{$xpath_name_tag}']";
235 // Look for a custom method named "{$tag_name}_test_value_attribute", if
236 // found use that method to get the xpath definition for this meta tag,
237 // otherwise it defaults to $this->test_value_attribute.
238 $method = "{$tag_name}_test_value_attribute";
239 if (method_exists($this, $method)) {
240 $xpath_value_attribute = $this->$method();
243 $xpath_value_attribute = $this->test_value_attribute;
246 // Extract the meta tag from the HTML.
247 $xpath = $this->xpath($xpath_string);
248 $this->assertEqual(count($xpath), 1, new FormattableMarkup('One @name tag found.', ['@name' => $tag_name]));
249 if (count($xpath) !== 1) {
250 $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
253 // Run various tests on the output variables.
254 // $this->assertTrue($xpath_string);
255 // $this->assertTrue($xpath_value_attribute);
256 // $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
257 // $this->assertTrue($all_values[$tag_name]);
258 // $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
259 // $this->assertEqual($xpath[0][$xpath_value_attribute], $all_values[$tag_name], "The meta tag was found with the expected value.");
260 // Most meta tags have an attribute, but some don't.
261 if (!empty($xpath_value_attribute)) {
262 $this->assertTrue($xpath_value_attribute);
263 $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
264 // Help with debugging.
265 if (!isset($xpath[0][$xpath_value_attribute])) {
266 $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
269 if ((string)$xpath[0][$xpath_value_attribute] != $all_values[$tag_name]) {
270 $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
272 $this->assertTrue($xpath[0][$xpath_value_attribute]);
273 $this->assertEqual($xpath[0][$xpath_value_attribute], $all_values[$tag_name], "The meta tag was found with the expected value.");
277 $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
278 $this->assertTrue((string)$xpath[0]);
279 $this->assertEqual((string)$xpath[0], $all_values[$tag_name], "The meta tag was found with the expected value.");
284 $this->drupalLogout();
288 * Convert a tag's internal name to the string which is actually used in HTML.
290 * The meta tag internal name will be machine names, i.e. only contain a-z,
291 * A-Z, 0-9 and the underline character. Meta tag names will actually contain
292 * any possible character.
294 * @param string $tag_name
295 * The tag name to be converted.
298 * The converted tag name.
300 public function getTestTagName($tag_name) {
305 * Generate a random value for testing meta tag fields.
307 * As a reasonable default, this will generating two words of 8 characters
308 * each with simple machine name -style strings.
313 public function getTestTagValue() {
314 return $this->randomMachineName() . ' ' . $this->randomMachineName();
318 * Generate a URL for an image.
321 * An absolute URL to a non-existant image.
323 public function randomImageUrl() {
324 return 'http://www.example.com/images/' . $this->randomMachineName() . '.png';