3 namespace Drupal\metatag\Tests;
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\Component\Utility\Html;
9 * Base class to test all of the meta tags that are in a specific module.
11 abstract class MetatagTagsTestBase extends WebTestBase {
16 public static $modules = [
17 // This is needed for the 'access content' permission.
26 // This module will be used to load a static page which will inherit the
27 // global defaults, without loading values from other configs.
28 'metatag_test_custom_route',
32 * All of the meta tags defined by this module which will be tested.
37 * The tag to look for when testing the output.
39 public $test_tag = 'meta';
42 * The attribute to look for to indicate which tag.
44 public $test_name_attribute = 'name';
47 * The attribute to look for when testing the output.
49 public $test_value_attribute = 'content';
54 protected function setUp() {
57 // Use the test page as the front page.
58 $this->config('system.site')->set('page.front', '/test-page')->save();
60 // Initiate session with a user who can manage metatags and access content.
62 'administer site configuration',
63 'administer meta tags',
66 $account = $this->drupalCreateUser($permissions);
67 $this->drupalLogin($account);
73 protected function verbose($message, $title = NULL) {
74 // Handle arrays, objects, etc.
75 if (!is_string($message)) {
76 $message = "<pre>\n" . print_r($message, TRUE) . "\n</pre>\n";
79 // Optional title to go before the output.
81 $title = '<h2>' . Html::escape($title) . "</h2>\n";
84 parent::verbose($title . $message);
88 * Tests that this module's tags are available.
90 function testTagsArePresent() {
91 // Load the global config.
92 $this->drupalGet('admin/config/search/metatag/global');
93 $this->assertResponse(200);
95 // Confirm the various meta tags are available.
96 foreach ($this->tags as $tag) {
97 // Look for a custom method named "{$tagname}_test_field_xpath", if found
98 // use that method to get the xpath definition for this meta tag,
99 // otherwise it defaults to just looking for a text input field.
100 $method = "{$tag}_test_field_xpath";
101 if (method_exists($this, $method)) {
102 $xpath = $this->$method();
105 $xpath = "//input[@name='{$tag}' and @type='text']";
108 $this->assertFieldByXPath($xpath, NULL, format_string('Found the @tag meta tag field.', ['@tag' => $tag]));
111 $this->drupalLogout();
115 * Confirm that each tag can be saved and that the output of each tag is
118 function testTagsInputOutput() {
119 // Load the global config.
120 $this->drupalGet('admin/config/search/metatag/global');
121 $this->assertResponse(200);
123 // Update the Global defaults and test them.
124 $all_values = $values = [];
125 foreach ($this->tags as $tag_name) {
126 // Look for a custom method named "{$tagname}_test_key", if found use
127 // that method to get the test string for this meta tag, otherwise it
128 // defaults to the meta tag's name.
129 $method = "{$tag_name}_test_key";
130 if (method_exists($this, $method)) {
131 $test_key = $this->$method();
134 $test_key = $tag_name;
137 // Look for a custom method named "{$tagname}_test_value", if found use
138 // that method to get the test string for this meta tag, otherwise it
139 // defaults to just generating a random string.
140 $method = "{$tag_name}_test_value";
141 if (method_exists($this, $method)) {
142 $test_value = $this->$method();
145 // Generate a random string. Generating two words of 8 characters each
146 // with simple machine name -style strings.
147 $test_value = $this->randomMachineName() . ' ' . $this->randomMachineName();
150 $values[$test_key] = $test_value;
151 $all_values[$tag_name] = $test_value;
153 $this->drupalPostForm(NULL, $values, 'Save');
154 $this->assertText('Saved the Global Metatag defaults.');
156 // Load the test page.
157 $this->drupalGet('metatag_test_custom_route');
158 $this->assertResponse(200);
160 // Look for the values.
161 foreach ($this->tags as $tag_name) {
162 // Look for a custom method named "{$tag_name}_test_output_xpath", if
163 // found use that method to get the xpath definition for this meta tag,
164 // otherwise it defaults to just looking for a meta tag matching:
165 // <$test_tag $test_name_attribute=$tag_name $test_value_attribute=$value />
166 $method = "{$tag_name}_test_output_xpath";
167 if (method_exists($this, $method)) {
168 $xpath_string = $this->$method();
171 // Look for a custom method named "{$tag_name}_test_tag", if
172 // found use that method to get the xpath definition for this meta tag,
173 // otherwise it defaults to $this->test_tag.
174 $method = "{$tag_name}_test_tag";
175 if (method_exists($this, $method)) {
176 $xpath_tag = $this->$method();
179 $xpath_tag = $this->test_tag;
182 // Look for a custom method named "{$tag_name}_test_name_attribute", if
183 // found use that method to get the xpath definition for this meta tag,
184 // otherwise it defaults to $this->test_name_attribute.
185 $method = "{$tag_name}_test_name_attribute";
186 if (method_exists($this, $method)) {
187 $xpath_name_attribute = $this->$method();
190 $xpath_name_attribute = $this->test_name_attribute;
193 // Look for a custom method named "{$tag_name}_test_tag_name", if
194 // found use that method to get the xpath definition for this meta tag,
195 // otherwise it defaults to $tag_name.
196 $method = "{$tag_name}_test_tag_name";
197 if (method_exists($this, $method)) {
198 $xpath_name_tag = $this->$method();
201 $xpath_name_tag = $this->getTestTagName($tag_name);
204 // Compile the xpath.
205 $xpath_string = "//{$xpath_tag}[@{$xpath_name_attribute}='{$xpath_name_tag}']";
208 // Look for a custom method named "{$tag_name}_test_value_attribute", if
209 // found use that method to get the xpath definition for this meta tag,
210 // otherwise it defaults to $this->test_value_attribute.
211 $method = "{$tag_name}_test_value_attribute";
212 if (method_exists($this, $method)) {
213 $xpath_value_attribute = $this->$method();
216 $xpath_value_attribute = $this->test_value_attribute;
219 // Extract the meta tag from the HTML.
220 $xpath = $this->xpath($xpath_string);
221 $this->assertEqual(count($xpath), 1, format_string('One @name tag found.', ['@name' => $tag_name]));
222 if (count($xpath) !== 1) {
223 $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
226 // Run various tests on the output variables.
227 // $this->assertTrue($xpath_string);
228 // $this->assertTrue($xpath_value_attribute);
229 // $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
230 // $this->assertTrue($all_values[$tag_name]);
231 // $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
232 // $this->assertEqual($xpath[0][$xpath_value_attribute], $all_values[$tag_name], "The meta tag was found with the expected value.");
233 // Most meta tags have an attribute, but some don't.
234 if (!empty($xpath_value_attribute)) {
235 $this->assertTrue($xpath_value_attribute);
236 $this->assertTrue(isset($xpath[0][$xpath_value_attribute]));
237 // Help with debugging.
238 if (!isset($xpath[0][$xpath_value_attribute])) {
239 $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
242 if ((string)$xpath[0][$xpath_value_attribute] != $all_values[$tag_name]) {
243 $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
245 $this->assertTrue($xpath[0][$xpath_value_attribute]);
246 $this->assertEqual($xpath[0][$xpath_value_attribute], $all_values[$tag_name], "The meta tag was found with the expected value.");
250 $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
251 $this->assertTrue((string)$xpath[0]);
252 $this->assertEqual((string)$xpath[0], $all_values[$tag_name], "The meta tag was found with the expected value.");
256 $this->drupalLogout();
260 * Convert a tag's internal name to the string which is actually used in HTML.
262 * The meta tag internal name will be machine names, i.e. only contain a-z,
263 * A-Z, 0-9 and the underline character. Meta tag names will actually contain
264 * any possible character.
266 * @param string $tag_name
267 * The tag name to be converted.
270 * The converted tag name.
272 public function getTestTagName($tag_name) {
277 * Generate a random value for testing meta tag fields.
279 * As a reasonable default, this will generating two words of 8 characters
280 * each with simple machine name -style strings.
285 public function getTestTagValue() {
286 return $this->randomMachineName() . ' ' . $this->randomMachineName();
290 * Generate a URL for an image.
293 * An absolute URL to a non-existant image.
295 public function randomImageUrl() {
296 return 'http://www.example.com/images/' . $this->randomMachineName() . '.png';