Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / metatag / src / Tests / MetatagAdminTest.php
1 <?php
2
3 namespace Drupal\metatag\Tests;
4
5 use Drupal\metatag\MetatagManager;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests the Metatag administration.
10  *
11  * @group metatag
12  */
13 class MetatagAdminTest extends WebTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = [
19     'node',
20     'field_ui',
21     'test_page_test',
22     'token',
23     'metatag',
24
25     // @see testAvailableConfigEntities
26     'block',
27     'block_content',
28     'comment',
29     'contact',
30     'menu_link_content',
31     'menu_ui',
32     'shortcut',
33     'taxonomy',
34     'entity_test',
35   ];
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42
43     // Use the test page as the front page.
44     $this->config('system.site')->set('page.front', '/test-page')->save();
45
46     // Create Basic page and Article node types.
47     if ($this->profile != 'standard') {
48       $this->drupalCreateContentType([
49         'type' => 'page',
50         'name' => 'Basic page',
51         'display_submitted' => FALSE,
52       ]);
53       $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
54     }
55   }
56
57   /**
58    * Tests the interface to manage metatag defaults.
59    */
60   public function testDefaults() {
61     // Save the default title to test the Revert operation at the end.
62     $metatag_defaults = \Drupal::config('metatag.metatag_defaults.global');
63     $default_title = $metatag_defaults->get('tags')['title'];
64
65     // Initiate session with a user who can manage metatags.
66     $permissions = ['administer site configuration', 'administer meta tags'];
67     $account = $this->drupalCreateUser($permissions);
68     $this->drupalLogin($account);
69
70     // Check that the user can see the list of metatag defaults.
71     $this->drupalGet('admin/config/search/metatag');
72     $this->assertResponse(200);
73
74     // Check that the Global defaults were created.
75     $this->assertLinkByHref('admin/config/search/metatag/global', 0, t('Global defaults were created on installation.'));
76
77     // Check that Global and entity defaults can't be deleted.
78     $this->assertNoLinkByHref('admin/config/search/metatag/global/delete', 0, t("Global defaults can't be deleted"));
79     $this->assertNoLinkByHref('admin/config/search/metatag/node/delete', 0, t("Entity defaults can't be deleted"));
80
81     // Check that the module defaults were injected into the Global config
82     // entity.
83     $this->drupalGet('admin/config/search/metatag/global');
84     $this->assertResponse(200);
85     $this->assertFieldById('edit-title', $metatag_defaults->get('title'), t('Metatag defaults were injected into the Global configuration entity.'));
86
87     // Update the Global defaults and test them.
88     $this->drupalGet('admin/config/search/metatag/global');
89     $this->assertResponse(200);
90     $values = [
91       'title' => 'Test title',
92       'description' => 'Test description',
93     ];
94     $this->drupalPostForm(NULL, $values, 'Save');
95     $this->assertText('Saved the Global Metatag defaults.');
96     $this->drupalGet('hit-a-404');
97     $this->assertResponse(404);
98     foreach ($values as $metatag => $value) {
99       $this->assertRaw($value, t('Updated metatag @tag was found in the HEAD section of the page.', ['@tag' => $metatag]));
100     }
101
102     // Check that tokens are processed.
103     $this->drupalGet('admin/config/search/metatag/global');
104     $this->assertResponse(200);
105     $values = [
106       'title' => '[site:name] | Test title',
107       'description' => '[site:name] | Test description',
108     ];
109     $this->drupalPostForm(NULL, $values, 'Save');
110     $this->assertText('Saved the Global Metatag defaults.');
111     drupal_flush_all_caches();
112     $this->drupalGet('hit-a-404');
113     $this->assertResponse(404);
114     foreach ($values as $metatag => $value) {
115       $processed_value = \Drupal::token()->replace($value);
116       $this->assertRaw($processed_value, t('Processed token for metatag @tag was found in the HEAD section of the page.', ['@tag' => $metatag]));
117     }
118
119     // Test the Robots plugin.
120     $this->drupalGet('admin/config/search/metatag/global');
121     $this->assertResponse(200);
122     $robots_values = ['index', 'follow', 'noydir'];
123     $values = [];
124     foreach ($robots_values as $value) {
125       $values['robots[' . $value . ']'] = TRUE;
126     }
127     $this->drupalPostForm(NULL, $values, 'Save');
128     $this->assertText('Saved the Global Metatag defaults.');
129     drupal_flush_all_caches();
130
131     // Trigger a 404 request.
132     $this->drupalGet('hit-a-404');
133     $this->assertResponse(404);
134     $robots_value = implode(', ', $robots_values);
135     $this->assertRaw($robots_value, t('Robots metatag has the expected values.'));
136
137     // Test reverting global configuration to its defaults.
138     $this->drupalGet('admin/config/search/metatag/global/revert');
139     $this->assertResponse(200);
140     $this->drupalPostForm(NULL, [], 'Revert');
141     $this->assertText('Reverted Global defaults.');
142     $this->assertText($default_title, 'Global title was reverted to its default value.');
143
144     $this->drupalLogout();
145   }
146
147   /**
148    * Confirm the available entity types show on the add-default page.
149    */
150   public function testAvailableConfigEntities() {
151     // Initiate session with a user who can manage metatags.
152     $permissions = [
153       'administer site configuration',
154       'administer meta tags',
155     ];
156     $account = $this->drupalCreateUser($permissions);
157     $this->drupalLogin($account);
158
159     // Load the default-add page.
160     $this->drupalGet('admin/config/search/metatag/add');
161     $this->assertResponse(200);
162
163     // Confirm the 'type' field exists.
164     $this->assertFieldByName('id');
165
166     // Compile a list of entities from the list.
167     $xpath = $this->xpath("//select[@name='id']");
168     $this->verbose('<pre>' . print_r($xpath, TRUE) . '</pre>');
169     $types = [];
170     foreach ($xpath[0]->children() as $item) {
171       if (!empty($item->option)) {
172         $data = (array) $item->option;
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     // Check that when the node defaults don't define a metatag, the Global one
279     // is used.
280     // First unset node defaults.
281     $this->drupalGet('admin/config/search/metatag/node');
282     $this->assertResponse(200);
283     $values = [
284       'title' => '',
285       'description' => '',
286     ];
287     $this->drupalPostForm(NULL, $values, 'Save');
288     $this->assertText('Saved the Content Metatag defaults.');
289
290     // Then, set global ones.
291     $this->drupalGet('admin/config/search/metatag/global');
292     $this->assertResponse(200);
293     $values = [
294       'title' => 'Global title',
295       'description' => 'Global description',
296     ];
297     $this->drupalPostForm(NULL, $values, 'Save');
298     $this->assertText('Saved the Global Metatag defaults.');
299
300     // Next, test that global defaults are rendered since node ones are empty.
301     // We are creating a new node as doing a get on the previous one would
302     // return cached results.
303     // @todo BookTest.php resets the cache of a single node, which is way more
304     // performant than creating a node for every set of assertions.
305     // @see BookTest::testDelete()
306     $node = $this->drupalCreateNode([
307       'title' => t('Hello, world!'),
308       'type' => 'article',
309     ]);
310     $this->drupalGet('node/' . $node->id());
311     $this->assertResponse(200);
312     foreach ($values as $metatag => $value) {
313       $this->assertRaw($value, t('Found global @tag tag as Node does not set it.', ['@tag' => $metatag]));
314     }
315
316     // Now create article overrides and then test them.
317     $this->drupalGet('admin/config/search/metatag/add');
318     $this->assertResponse(200);
319     $values = [
320       'id' => 'node__article',
321       'title' => 'Article title override',
322       'description' => 'Article description override',
323     ];
324     $this->drupalPostForm(NULL, $values, 'Save');
325     $this->assertText(strip_tags(t('Created the %label Metatag defaults.', ['%label' => 'Content: Article'])));
326
327     // Confirm the fields load properly on the node/add/article page.
328     $node = $this->drupalCreateNode([
329       'title' => t('Hello, world!'),
330       'type' => 'article',
331     ]);
332     $this->drupalGet('node/' . $node->id());
333     $this->assertResponse(200);
334     unset($values['id']);
335     foreach ($values as $metatag => $value) {
336       $this->assertRaw($value, t('Found bundle override for tag @tag.', ['@tag' => $metatag]));
337     }
338
339     // Test deleting the article defaults.
340     $this->drupalGet('admin/config/search/metatag/node__article/delete');
341     $this->assertResponse(200);
342     $this->drupalPostForm(NULL, [], 'Delete');
343     $this->assertText(t('Deleted @label defaults.', ['@label' => 'Content: Article']));
344   }
345
346   /**
347    * Test that the entity default values load on the entity form.
348    *
349    * And that they can then be overridden correctly.
350    */
351   public function testEntityDefaultInheritence() {
352     // Initiate session with a user who can manage meta tags and content type
353     // fields.
354     $permissions = [
355       'administer site configuration',
356       'administer meta tags',
357       'access content',
358       'administer node fields',
359       'create article content',
360       'administer nodes',
361       'create article content',
362       'create page content',
363     ];
364     $account = $this->drupalCreateUser($permissions);
365     $this->drupalLogin($account);
366
367     // Add a Metatag field to the Article content type.
368     $this->drupalGet('admin/structure/types/manage/article/fields/add-field');
369     $this->assertResponse(200);
370     $edit = [
371       'new_storage_type' => 'metatag',
372       'label' => 'Meta tags',
373       'field_name' => 'meta_tags',
374     ];
375     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
376     $this->drupalPostForm(NULL, [], t('Save field settings'));
377     $this->assertText(strip_tags(t('Updated field %label field settings.', ['%label' => 'Meta tags'])));
378     $this->drupalPostForm(NULL, [], t('Save settings'));
379     $this->assertText(strip_tags(t('Saved %label configuration.', ['%label' => 'Meta tags'])));
380
381     // Try creating an article, confirm the fields are present. This should be
382     // the node default values that are shown.
383     $this->drupalGet('node/add/article');
384     $this->assertResponse(200);
385     $this->assertFieldByName('field_meta_tags[0][basic][title]', '[node:title] | [site:name]');
386     $this->assertFieldByName('field_meta_tags[0][basic][description]', '[node:summary]');
387
388     // Customize the Article content type defaults.
389     $this->drupalGet('admin/config/search/metatag/add');
390     $this->assertResponse(200);
391     $values = [
392       'id' => 'node__article',
393       'title' => 'Article title override',
394       'description' => 'Article description override',
395     ];
396     $this->drupalPostForm(NULL, $values, 'Save');
397     $this->assertText(strip_tags(t('Created the %label Metatag defaults.', ['%label' => 'Content: Article'])));
398
399     // Try creating an article, this time with the overridden defaults.
400     $this->drupalGet('node/add/article');
401     $this->assertResponse(200);
402     $this->assertFieldByName('field_meta_tags[0][basic][title]', 'Article title override');
403     $this->assertFieldByName('field_meta_tags[0][basic][description]', 'Article description override');
404   }
405
406   /**
407    * Test that protected Metatag defaults cannot be deleted.
408    */
409   public function testDefaultProtected() {
410     // Initiate session with a user who can manage metatags.
411     $permissions = ['administer site configuration', 'administer meta tags'];
412     $account = $this->drupalCreateUser($permissions);
413     $this->drupalLogin($account);
414
415     // Add default metatag for Articles.
416     $edit = [
417       'id' => 'node__article',
418     ];
419     $this->drupalPostForm('/admin/config/search/metatag/add', $edit, 'Save');
420
421     // Check that protected defaults contains "Revert" link instead of "Delete".
422     foreach (MetatagManager::protectedDefaults() as $protected) {
423       $this->assertLinkByHref('/admin/config/search/metatag/' . $protected);
424       $this->assertLinkByHref('/admin/config/search/metatag/' . $protected . '/revert');
425       $this->assertNoLinkByHref('/admin/config/search/metatag/' . $protected . '/delete');
426     }
427
428     // Confirm that non protected defaults can be deleted.
429     $this->assertLinkByHref('/admin/config/search/metatag/node__article');
430     $this->assertNoLinkByHref('/admin/config/search/metatag/node__article/revert');
431     $this->assertLinkByHref('/admin/config/search/metatag/node__article/delete');
432
433     // Visit each protected default page to confirm "Delete" button is hidden.
434     foreach (MetatagManager::protectedDefaults() as $protected) {
435       $this->drupalGet('/admin/config/search/metatag/' . $protected);
436       $this->assertNoLink('Delete');
437     }
438
439     // Confirm that non protected defaults can be deleted.
440     $this->drupalGet('/admin/config/search/metatag/node__article');
441     $this->assertLink('Delete');
442   }
443
444 }