Version 1
[yaffs-website] / web / modules / contrib / metatag / src / Tests / MetatagTagsTestBase.php
1 <?php
2
3 namespace Drupal\metatag\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\Component\Utility\Html;
7
8 /**
9  * Base class to test all of the meta tags that are in a specific module.
10  */
11 abstract class MetatagTagsTestBase extends WebTestBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public static $modules = [
17     // This is needed for the 'access content' permission.
18     'node',
19
20     // Dependencies.
21     'token',
22
23     // Metatag itself.
24     'metatag',
25
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',
29   ];
30
31   /**
32    * All of the meta tags defined by this module which will be tested.
33    */
34   public $tags = [];
35
36   /**
37    * The tag to look for when testing the output.
38    */
39   public $test_tag = 'meta';
40
41   /**
42    * The attribute to look for to indicate which tag.
43    */
44   public $test_name_attribute = 'name';
45
46   /**
47    * The attribute to look for when testing the output.
48    */
49   public $test_value_attribute = 'content';
50
51   /**
52    * {@inheritdoc}
53    */
54   protected function setUp() {
55     parent::setUp();
56
57     // Use the test page as the front page.
58     $this->config('system.site')->set('page.front', '/test-page')->save();
59
60     // Initiate session with a user who can manage metatags and access content.
61     $permissions = [
62       'administer site configuration',
63       'administer meta tags',
64       'access content',
65     ];
66     $account = $this->drupalCreateUser($permissions);
67     $this->drupalLogin($account);
68   }
69
70   /**
71    * {@inheritdoc}
72    */
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";
77     }
78
79     // Optional title to go before the output.
80     if (!empty($title)) {
81       $title = '<h2>' . Html::escape($title) . "</h2>\n";
82     }
83
84     parent::verbose($title . $message);
85   }
86
87   /**
88    * Tests that this module's tags are available.
89    */
90   function testTagsArePresent() {
91     // Load the global config.
92     $this->drupalGet('admin/config/search/metatag/global');
93     $this->assertResponse(200);
94
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();
103       }
104       else {
105         $xpath = "//input[@name='{$tag}' and @type='text']";
106       }
107
108       $this->assertFieldByXPath($xpath, NULL, format_string('Found the @tag meta tag field.', ['@tag' => $tag]));
109     }
110
111     $this->drupalLogout();
112   }
113
114   /**
115    * Confirm that each tag can be saved and that the output of each tag is
116    * correct.
117    */
118   function testTagsInputOutput() {
119     // Load the global config.
120     $this->drupalGet('admin/config/search/metatag/global');
121     $this->assertResponse(200);
122
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();
132       }
133       else {
134         $test_key = $tag_name;
135       }
136
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();
143       }
144       else {
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();
148       }
149
150       $values[$test_key] = $test_value;
151       $all_values[$tag_name] = $test_value;
152     }
153     $this->drupalPostForm(NULL, $values, 'Save');
154     $this->assertText('Saved the Global Metatag defaults.');
155
156     // Load the test page.
157     $this->drupalGet('metatag_test_custom_route');
158     $this->assertResponse(200);
159
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();
169       }
170       else {
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();
177         }
178         else {
179           $xpath_tag = $this->test_tag;
180         }
181
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();
188         }
189         else {
190           $xpath_name_attribute = $this->test_name_attribute;
191         }
192
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();
199         }
200         else {
201           $xpath_name_tag = $this->getTestTagName($tag_name);
202         }
203
204         // Compile the xpath.
205         $xpath_string = "//{$xpath_tag}[@{$xpath_name_attribute}='{$xpath_name_tag}']";
206       }
207
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();
214       }
215       else {
216         $xpath_value_attribute = $this->test_value_attribute;
217       }
218
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);
224       }
225
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);
240         }
241         else {
242           if ((string)$xpath[0][$xpath_value_attribute] != $all_values[$tag_name]) {
243             $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
244           }
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.");
247         }
248       }
249       else {
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.");
253       }
254     }
255
256     $this->drupalLogout();
257   }
258
259   /**
260    * Convert a tag's internal name to the string which is actually used in HTML.
261    *
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.
265    *
266    * @param string $tag_name
267    *   The tag name to be converted.
268    *
269    * @return string
270    *   The converted tag name.
271    */
272   public function getTestTagName($tag_name) {
273     return $tag_name;
274   }
275
276   /**
277    * Generate a random value for testing meta tag fields.
278    *
279    * As a reasonable default, this will generating two words of 8 characters
280    * each with simple machine name -style strings.
281    *
282    * @return string
283    *   A normal string.
284    */
285   public function getTestTagValue() {
286     return $this->randomMachineName() . ' ' . $this->randomMachineName();
287   }
288
289   /**
290    * Generate a URL for an image.
291    *
292    * @return string
293    *   An absolute URL to a non-existant image.
294    */
295   public function randomImageUrl() {
296     return 'http://www.example.com/images/'  . $this->randomMachineName() . '.png';
297   }
298
299 }