Yaffs site version 1.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\Render\FormattableMarkup;
7 use Drupal\Component\Utility\Html;
8
9 /**
10  * Base class to test all of the meta tags that are in a specific module.
11  */
12 abstract class MetatagTagsTestBase extends WebTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = [
18     // This is needed for the 'access content' permission.
19     'node',
20
21     // Dependencies.
22     'token',
23
24     // Metatag itself.
25     'metatag',
26
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',
30   ];
31
32   /**
33    * All of the meta tags defined by this module which will be tested.
34    */
35   public $tags = [];
36
37   /**
38    * The tag to look for when testing the output.
39    */
40   public $test_tag = 'meta';
41
42   /**
43    * The attribute to look for to indicate which tag.
44    */
45   public $test_name_attribute = 'name';
46
47   /**
48    * The attribute to look for when testing the output.
49    */
50   public $test_value_attribute = 'content';
51
52   /**
53    * {@inheritdoc}
54    */
55   protected function setUp() {
56     parent::setUp();
57
58     // Use the test page as the front page.
59     $this->config('system.site')->set('page.front', '/test-page')->save();
60
61     // Initiate session with a user who can manage metatags and access content.
62     $permissions = [
63       'administer site configuration',
64       'administer meta tags',
65       'access content',
66     ];
67     $account = $this->drupalCreateUser($permissions);
68     $this->drupalLogin($account);
69   }
70
71   /**
72    * {@inheritdoc}
73    */
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";
78     }
79
80     // Optional title to go before the output.
81     if (!empty($title)) {
82       $title = '<h2>' . Html::escape($title) . "</h2>\n";
83     }
84
85     parent::verbose($title . $message);
86   }
87
88   /**
89    * Tests that this module's tags are available.
90    */
91   function testTagsArePresent() {
92     // Load the global config.
93     $this->drupalGet('admin/config/search/metatag/global');
94     $this->assertResponse(200);
95
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();
104       }
105       else {
106         $xpath = "//input[@name='{$tag}' and @type='text']";
107       }
108
109       $this->assertFieldByXPath($xpath, NULL, new FormattableMarkup('Found the @tag meta tag field.', ['@tag' => $tag]));
110     }
111
112     $this->drupalLogout();
113   }
114
115   /**
116    * Confirm that each tag can be saved and that the output of each tag is
117    * correct.
118    */
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!'),
124       'type' => 'page',
125     ]);
126
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.
129     $paths = [
130       [
131         'admin/config/search/metatag/global',
132         'metatag_test_custom_route',
133         'Saved the Global Metatag defaults.',
134       ],
135       [
136         'admin/config/search/metatag/node',
137         'node/1',
138         'Saved the Content Metatag defaults',
139       ],
140     ];
141
142     // 
143     foreach ($paths as $item) {
144       list($path1, $path2, $save_message) = $item;
145
146       // Load the global config.
147       $this->drupalGet($path1);
148       $this->assertResponse(200);
149
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();
159         }
160         else {
161           $test_key = $tag_name;
162         }
163
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();
170         }
171         else {
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();
175         }
176
177         $values[$test_key] = $test_value;
178         $all_values[$tag_name] = $test_value;
179       }
180       $this->drupalPostForm(NULL, $values, 'Save');
181       $this->assertText($save_message);
182
183       // Load the test page.
184       $this->drupalGet($path2);
185       $this->assertResponse(200);
186
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();
196         }
197         else {
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();
204           }
205           else {
206             $xpath_tag = $this->test_tag;
207           }
208
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();
215           }
216           else {
217             $xpath_name_attribute = $this->test_name_attribute;
218           }
219
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();
226           }
227           else {
228             $xpath_name_tag = $this->getTestTagName($tag_name);
229           }
230
231           // Compile the xpath.
232           $xpath_string = "//{$xpath_tag}[@{$xpath_name_attribute}='{$xpath_name_tag}']";
233         }
234
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();
241         }
242         else {
243           $xpath_value_attribute = $this->test_value_attribute;
244         }
245
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);
251         }
252
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);
267           }
268           else {
269             if ((string)$xpath[0][$xpath_value_attribute] != $all_values[$tag_name]) {
270               $this->verbose($xpath, $tag_name . ': ' . $xpath_string);
271             }
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.");
274           }
275         }
276         else {
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.");
280         }
281       }
282     }
283
284     $this->drupalLogout();
285   }
286
287   /**
288    * Convert a tag's internal name to the string which is actually used in HTML.
289    *
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.
293    *
294    * @param string $tag_name
295    *   The tag name to be converted.
296    *
297    * @return string
298    *   The converted tag name.
299    */
300   public function getTestTagName($tag_name) {
301     return $tag_name;
302   }
303
304   /**
305    * Generate a random value for testing meta tag fields.
306    *
307    * As a reasonable default, this will generating two words of 8 characters
308    * each with simple machine name -style strings.
309    *
310    * @return string
311    *   A normal string.
312    */
313   public function getTestTagValue() {
314     return $this->randomMachineName() . ' ' . $this->randomMachineName();
315   }
316
317   /**
318    * Generate a URL for an image.
319    *
320    * @return string
321    *   An absolute URL to a non-existant image.
322    */
323   public function randomImageUrl() {
324     return 'http://www.example.com/images/'  . $this->randomMachineName() . '.png';
325   }
326
327 }