Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / metatag / src / Tests / MetatagAdminTest.php
1 <?php
2
3 namespace Drupal\metatag\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Tests the Metatag administration.
9  *
10  * @group metatag
11  */
12 class MetatagAdminTest extends WebTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = [
18     'node',
19     'field_ui',
20     'test_page_test',
21     'token',
22     'metatag',
23
24     // @see testAvailableConfigEntities
25     'block',
26     'block_content',
27     'comment',
28     'contact',
29     'menu_link_content',
30     'menu_ui',
31     'shortcut',
32     'taxonomy',
33     'entity_test',
34   ];
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::setUp();
41
42     // Use the test page as the front page.
43     $this->config('system.site')->set('page.front', '/test-page')->save();
44
45     // Create Basic page and Article node types.
46     if ($this->profile != 'standard') {
47       $this->drupalCreateContentType([
48         'type' => 'page',
49         'name' => 'Basic page',
50         'display_submitted' => FALSE,
51       ]);
52       $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
53     }
54   }
55
56   /**
57    * Tests the interface to manage metatag defaults.
58    */
59   public function testDefaults() {
60     // Save the default title to test the Revert operation at the end.
61     $metatag_defaults = \Drupal::config('metatag.metatag_defaults.global');
62     $default_title = $metatag_defaults->get('tags')['title'];
63
64     // Initiate session with a user who can manage metatags.
65     $permissions = ['administer site configuration', 'administer meta tags'];
66     $account = $this->drupalCreateUser($permissions);
67     $this->drupalLogin($account);
68
69     // Check that the user can see the list of metatag defaults.
70     $this->drupalGet('admin/config/search/metatag');
71     $this->assertResponse(200);
72
73     // Check that the Global defaults were created.
74     $this->assertLinkByHref('admin/config/search/metatag/global', 0, t('Global defaults were created on installation.'));
75
76     // Check that Global and entity defaults can't be deleted.
77     $this->assertNoLinkByHref('admin/config/search/metatag/global/delete', 0, t('Global defaults can\'t be deleted'));
78     $this->assertNoLinkByHref('admin/config/search/metatag/node/delete', 0, t('Entity defaults can\'t be deleted'));
79
80     // Check that the module defaults were injected into the Global config
81     // entity.
82     $this->drupalGet('admin/config/search/metatag/global');
83     $this->assertResponse(200);
84     $this->assertFieldById('edit-title', $metatag_defaults->get('title'), t('Metatag defaults were injected into the Global configuration entity.'));
85
86     // Update the Global defaults and test them.
87     $this->drupalGet('admin/config/search/metatag/global');
88     $this->assertResponse(200);
89     $values = [
90       'title' => 'Test title',
91       'description' => 'Test description',
92     ];
93     $this->drupalPostForm(NULL, $values, 'Save');
94     $this->assertText('Saved the Global Metatag defaults.');
95     $this->drupalGet('hit-a-404');
96     $this->assertResponse(404);
97     foreach ($values as $metatag => $value) {
98       $this->assertRaw($value, t('Updated metatag @tag was found in the HEAD section of the page.', ['@tag' => $metatag]));
99     }
100
101     // Check that tokens are processed.
102     $this->drupalGet('admin/config/search/metatag/global');
103     $this->assertResponse(200);
104     $values = [
105       'title' => '[site:name] | Test title',
106       'description' => '[site:name] | Test description',
107     ];
108     $this->drupalPostForm(NULL, $values, 'Save');
109     $this->assertText('Saved the Global Metatag defaults.');
110     drupal_flush_all_caches();
111     $this->drupalGet('hit-a-404');
112     $this->assertResponse(404);
113     foreach ($values as $metatag => $value) {
114       $processed_value = \Drupal::token()->replace($value);
115       $this->assertRaw($processed_value, t('Processed token for metatag @tag was found in the HEAD section of the page.', ['@tag' => $metatag]));
116     }
117
118     // Test the Robots plugin.
119     $this->drupalGet('admin/config/search/metatag/global');
120     $this->assertResponse(200);
121     $robots_values = ['index', 'follow', 'noydir'];
122     $values = [];
123     foreach ($robots_values as $value) {
124       $values['robots[' . $value . ']'] = TRUE;
125     }
126     $this->drupalPostForm(NULL, $values, 'Save');
127     $this->assertText('Saved the Global Metatag defaults.');
128     drupal_flush_all_caches();
129
130     // Trigger a 404 request.
131     $this->drupalGet('hit-a-404');
132     $this->assertResponse(404);
133     $robots_value = implode(', ', $robots_values);
134     $this->assertRaw($robots_value, t('Robots metatag has the expected values.'));
135
136     // Test reverting global configuration to its defaults.
137     $this->drupalGet('admin/config/search/metatag/global/revert');
138     $this->assertResponse(200);
139     $this->drupalPostForm(NULL, [], 'Revert');
140     $this->assertText('Reverted Global defaults.');
141     $this->assertText($default_title, 'Global title was reverted to its default value.');
142
143     $this->drupalLogout();
144   }
145
146   /**
147    * Confirm the available entity types show on the add-default page.
148    */
149   public function testAvailableConfigEntities() {
150     // Initiate session with a user who can manage metatags.
151     $permissions = [
152       'administer site configuration',
153       'administer meta tags',
154     ];
155     $account = $this->drupalCreateUser($permissions);
156     $this->drupalLogin($account);
157
158     // Load the default-add page.
159     $this->drupalGet('admin/config/search/metatag/add');
160     $this->assertResponse(200);
161
162     // Confirm the 'type' field exists.
163     $this->assertFieldByName('id');
164
165     // Compile a list of entities from the list.
166     $xpath = $this->xpath("//select[@name='id']");
167     $this->verbose('<pre>' . print_r($xpath, TRUE) . '</pre>');
168     $types = [];
169     foreach ($xpath[0]->children() as $item) {
170       if (!empty($item->option)) {
171         $data = (array)$item->option;
172         // $this->verbose('<pre>' . print_r($data, TRUE) . '</pre>');
173         $types[$data['@attributes']['value']] = $data[0];
174       }
175     }
176     $this->verbose('<pre>' . print_r($types, TRUE) . '</pre>');
177
178     // Check through the values that are in the 'select' list, make sure that
179     // unwanted items are not present.
180     $this->assertFalse(isset($types['block_content']), 'Custom block entities are not supported.');
181     $this->assertFalse(isset($types['comment']), 'Comment entities are not supported.');
182     $this->assertFalse(isset($types['menu_link_content']), 'Menu link entities are not supported.');
183     $this->assertFalse(isset($types['shortcut']), 'Shortcut entities are not supported.');
184     $this->assertTrue(isset($types['node__page']), 'Nodes are supported.');
185     $this->assertTrue(isset($types['user__user']), 'Users are supported.');
186     $this->assertTrue(isset($types['entity_test']), 'Test entities are supported.');
187   }
188
189   /**
190    * Tests special pages.
191    */
192   public function testSpecialPages() {
193     // Initiate session with a user who can manage metatags.
194     $permissions = ['administer site configuration', 'administer meta tags'];
195     $account = $this->drupalCreateUser($permissions);
196     $this->drupalLogin($account);
197
198     // Adjust the front page and test it.
199     $this->drupalGet('admin/config/search/metatag/front');
200     $this->assertResponse(200);
201     $values = [
202       'description' => 'Front page description',
203     ];
204     $this->drupalPostForm(NULL, $values, 'Save');
205     $this->assertText('Saved the Front page Metatag defaults.');
206     $this->drupalGet('<front>');
207     $this->assertResponse(200);
208     $this->assertRaw($values['description'], t('Front page defaults are used at the front page.'));
209
210     // Adjust the 403 page and test it.
211     $this->drupalGet('admin/config/search/metatag/403');
212     $this->assertResponse(200);
213     $values = [
214       'description' => '403 page description.',
215     ];
216     $this->drupalPostForm(NULL, $values, 'Save');
217     $this->assertText('Saved the 403 access denied Metatag defaults.');
218     $this->drupalLogout();
219     $this->drupalGet('admin/config/search/metatag');
220     $this->assertResponse(403);
221     $this->assertRaw($values['description'], t('403 page defaults are used at 403 pages.'));
222
223     // Adjust the 404 page and test it.
224     $this->drupalLogin($account);
225     $this->drupalGet('admin/config/search/metatag/404');
226     $this->assertResponse(200);
227     $values = [
228       'description' => '404 page description.',
229     ];
230     $this->drupalPostForm(NULL, $values, 'Save');
231     $this->assertText('Saved the 404 page not found Metatag defaults.');
232     $this->drupalGet('foo');
233     $this->assertResponse(404);
234     $this->assertRaw($values['description'], t('404 page defaults are used at 404 pages.'));
235     $this->drupalLogout();
236   }
237
238   /**
239    * Tests entity and bundle overrides.
240    */
241   public function testOverrides() {
242     // Initiate session with a user who can manage metatags.
243     $permissions = [
244       'administer site configuration',
245       'administer meta tags',
246       'access content',
247       'create article content',
248       'administer nodes',
249       'create article content',
250       'create page content',
251     ];
252     $account = $this->drupalCreateUser($permissions);
253     $this->drupalLogin($account);
254
255     // Update the Metatag Node defaults.
256     $this->drupalGet('admin/config/search/metatag/node');
257     $this->assertResponse(200);
258     $values = [
259       'title' => 'Test title for a node.',
260       'description' => 'Test description for a node.',
261     ];
262     $this->drupalPostForm(NULL, $values, 'Save');
263     $this->assertText('Saved the Content Metatag defaults.');
264
265     // Create a test node.
266     $node = $this->drupalCreateNode([
267       'title' => t('Hello, world!'),
268       'type' => 'article',
269     ]);
270
271     // Check that the new values are found in the response.
272     $this->drupalGet('node/' . $node->id());
273     $this->assertResponse(200);
274     foreach ($values as $metatag => $value) {
275       $this->assertRaw($value, t('Node metatag @tag overrides Global defaults.', ['@tag' => $metatag]));
276     }
277
278     /**
279      * Check that when the node defaults don't define a metatag, the Global one
280      * is used.
281      */
282     // First unset node defaults.
283     $this->drupalGet('admin/config/search/metatag/node');
284     $this->assertResponse(200);
285     $values = [
286       'title' => '',
287       'description' => '',
288     ];
289     $this->drupalPostForm(NULL, $values, 'Save');
290     $this->assertText('Saved the Content Metatag defaults.');
291
292     // Then, set global ones.
293     $this->drupalGet('admin/config/search/metatag/global');
294     $this->assertResponse(200);
295     $values = [
296       'title' => 'Global title',
297       'description' => 'Global description',
298     ];
299     $this->drupalPostForm(NULL, $values, 'Save');
300     $this->assertText('Saved the Global Metatag defaults.');
301
302     // Next, test that global defaults are rendered since node ones are empty.
303     // We are creating a new node as doing a get on the previous one would
304     // return cached results.
305     // @todo BookTest.php resets the cache of a single node, which is way more
306     // performant than creating a node for every set of assertions.
307     // @see BookTest::testDelete().
308     $node = $this->drupalCreateNode([
309       'title' => t('Hello, world!'),
310       'type' => 'article',
311     ]);
312     $this->drupalGet('node/' . $node->id());
313     $this->assertResponse(200);
314     foreach ($values as $metatag => $value) {
315       $this->assertRaw($value, t('Found global @tag tag as Node does not set it.', ['@tag' => $metatag]));
316     }
317
318     // Now create article overrides and then test them.
319     $this->drupalGet('admin/config/search/metatag/add');
320     $this->assertResponse(200);
321     $values = [
322       'id' => 'node__article',
323       'title' => 'Article title override',
324       'description' => 'Article description override',
325     ];
326     $this->drupalPostForm(NULL, $values, 'Save');
327     $this->assertText(strip_tags(t('Created the %label Metatag defaults.', ['%label' => 'Content: Article'])));
328
329     // Confirm the fields load properly on the node/add/article page.
330     $node = $this->drupalCreateNode([
331       'title' => t('Hello, world!'),
332       'type' => 'article',
333     ]);
334     $this->drupalGet('node/' . $node->id());
335     $this->assertResponse(200);
336     unset($values['id']);
337     foreach ($values as $metatag => $value) {
338       $this->assertRaw($value, t('Found bundle override for tag @tag.', ['@tag' => $metatag]));
339     }
340
341     // Test deleting the article defaults.
342     $this->drupalGet('admin/config/search/metatag/node__article/delete');
343     $this->assertResponse(200);
344     $this->drupalPostForm(NULL, [], 'Delete');
345     $this->assertText(t('Deleted @label defaults.', ['@label' => 'Content: Article']));
346   }
347
348   /**
349    * Test that the entity default values load on the entity form, and that they
350    * can then be overridden correctly.
351    */
352   public function testEntityDefaultInheritence() {
353     // Initiate session with a user who can manage metatags and content type
354     // fields.
355     $permissions = [
356       'administer site configuration',
357       'administer meta tags',
358       'access content',
359       'administer node fields',
360       'create article content',
361       'administer nodes',
362       'create article content',
363       'create page content',
364     ];
365     $account = $this->drupalCreateUser($permissions);
366     $this->drupalLogin($account);
367
368     // Add a Metatag field to the Article content type.
369     $this->drupalGet('admin/structure/types/manage/article/fields/add-field');
370     $this->assertResponse(200);
371     $edit = [
372       'new_storage_type' => 'metatag',
373       'label' => 'Meta tags',
374       'field_name' => 'meta_tags',
375     ];
376     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
377     $this->drupalPostForm(NULL, [], t('Save field settings'));
378     $this->assertText(strip_tags(t('Updated field %label field settings.', ['%label' => 'Meta tags'])));
379     $this->drupalPostForm(NULL, [], t('Save settings'));
380     $this->assertText(strip_tags(t('Saved %label configuration.', ['%label' => 'Meta tags'])));
381
382     // Try creating an article, confirm the fields are present. This should be
383     // the node default values that are shown.
384     $this->drupalGet('node/add/article');
385     $this->assertResponse(200);
386     $this->assertFieldByName('field_meta_tags[0][basic][title]', '[node:title] | [site:name]');
387     $this->assertFieldByName('field_meta_tags[0][basic][description]', '[node:summary]');
388
389     // Customize the Article content type defaults.
390     $this->drupalGet('admin/config/search/metatag/add');
391     $this->assertResponse(200);
392     $values = [
393       'id' => 'node__article',
394       'title' => 'Article title override',
395       'description' => 'Article description override',
396     ];
397     $this->drupalPostForm(NULL, $values, 'Save');
398     $this->assertText(strip_tags(t('Created the %label Metatag defaults.', ['%label' => 'Content: Article'])));
399
400     // Try creating an article, this time with the overridden defaults.
401     $this->drupalGet('node/add/article');
402     $this->assertResponse(200);
403     $this->assertFieldByName('field_meta_tags[0][basic][title]', 'Article title override');
404     $this->assertFieldByName('field_meta_tags[0][basic][description]', 'Article description override');
405   }
406
407 }