3 namespace Drupal\metatag\Tests;
5 use Drupal\metatag\MetatagManager;
6 use Drupal\simpletest\WebTestBase;
9 * Tests the Metatag administration.
13 class MetatagAdminTest extends WebTestBase {
18 public static $modules = [
25 // @see testAvailableConfigEntities
40 protected function setUp() {
43 // Use the test page as the front page.
44 $this->config('system.site')->set('page.front', '/test-page')->save();
46 // Create Basic page and Article node types.
47 if ($this->profile != 'standard') {
48 $this->drupalCreateContentType([
50 'name' => 'Basic page',
51 'display_submitted' => FALSE,
53 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
58 * Tests the interface to manage metatag defaults.
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'];
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);
70 // Check that the user can see the list of metatag defaults.
71 $this->drupalGet('admin/config/search/metatag');
72 $this->assertResponse(200);
74 // Check that the Global defaults were created.
75 $this->assertLinkByHref('admin/config/search/metatag/global', 0, t('Global defaults were created on installation.'));
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"));
81 // Check that the module defaults were injected into the Global config
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.'));
87 // Update the Global defaults and test them.
88 $this->drupalGet('admin/config/search/metatag/global');
89 $this->assertResponse(200);
91 'title' => 'Test title',
92 'description' => 'Test description',
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]));
102 // Check that tokens are processed.
103 $this->drupalGet('admin/config/search/metatag/global');
104 $this->assertResponse(200);
106 'title' => '[site:name] | Test title',
107 'description' => '[site:name] | Test description',
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]));
119 // Test the Robots plugin.
120 $this->drupalGet('admin/config/search/metatag/global');
121 $this->assertResponse(200);
122 $robots_values = ['index', 'follow', 'noydir'];
124 foreach ($robots_values as $value) {
125 $values['robots[' . $value . ']'] = TRUE;
127 $this->drupalPostForm(NULL, $values, 'Save');
128 $this->assertText('Saved the Global Metatag defaults.');
129 drupal_flush_all_caches();
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.'));
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.');
144 $this->drupalLogout();
148 * Confirm the available entity types show on the add-default page.
150 public function testAvailableConfigEntities() {
151 // Initiate session with a user who can manage metatags.
153 'administer site configuration',
154 'administer meta tags',
156 $account = $this->drupalCreateUser($permissions);
157 $this->drupalLogin($account);
159 // Load the default-add page.
160 $this->drupalGet('admin/config/search/metatag/add');
161 $this->assertResponse(200);
163 // Confirm the 'type' field exists.
164 $this->assertFieldByName('id');
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>');
170 foreach ($xpath[0]->children() as $item) {
171 if (!empty($item->option)) {
172 $data = (array) $item->option;
173 $types[$data['@attributes']['value']] = $data[0];
176 $this->verbose('<pre>' . print_r($types, TRUE) . '</pre>');
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.');
190 * Tests special pages.
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);
198 // Adjust the front page and test it.
199 $this->drupalGet('admin/config/search/metatag/front');
200 $this->assertResponse(200);
202 'description' => 'Front page description',
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.'));
210 // Adjust the 403 page and test it.
211 $this->drupalGet('admin/config/search/metatag/403');
212 $this->assertResponse(200);
214 'description' => '403 page description.',
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.'));
223 // Adjust the 404 page and test it.
224 $this->drupalLogin($account);
225 $this->drupalGet('admin/config/search/metatag/404');
226 $this->assertResponse(200);
228 'description' => '404 page description.',
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();
239 * Tests entity and bundle overrides.
241 public function testOverrides() {
242 // Initiate session with a user who can manage metatags.
244 'administer site configuration',
245 'administer meta tags',
247 'create article content',
249 'create article content',
250 'create page content',
252 $account = $this->drupalCreateUser($permissions);
253 $this->drupalLogin($account);
255 // Update the Metatag Node defaults.
256 $this->drupalGet('admin/config/search/metatag/node');
257 $this->assertResponse(200);
259 'title' => 'Test title for a node.',
260 'description' => 'Test description for a node.',
262 $this->drupalPostForm(NULL, $values, 'Save');
263 $this->assertText('Saved the Content Metatag defaults.');
265 // Create a test node.
266 $node = $this->drupalCreateNode([
267 'title' => t('Hello, world!'),
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]));
278 // Check that when the node defaults don't define a metatag, the Global one
280 // First unset node defaults.
281 $this->drupalGet('admin/config/search/metatag/node');
282 $this->assertResponse(200);
287 $this->drupalPostForm(NULL, $values, 'Save');
288 $this->assertText('Saved the Content Metatag defaults.');
290 // Then, set global ones.
291 $this->drupalGet('admin/config/search/metatag/global');
292 $this->assertResponse(200);
294 'title' => 'Global title',
295 'description' => 'Global description',
297 $this->drupalPostForm(NULL, $values, 'Save');
298 $this->assertText('Saved the Global Metatag defaults.');
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!'),
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]));
316 // Now create article overrides and then test them.
317 $this->drupalGet('admin/config/search/metatag/add');
318 $this->assertResponse(200);
320 'id' => 'node__article',
321 'title' => 'Article title override',
322 'description' => 'Article description override',
324 $this->drupalPostForm(NULL, $values, 'Save');
325 $this->assertText(strip_tags(t('Created the %label Metatag defaults.', ['%label' => 'Content: Article'])));
327 // Confirm the fields load properly on the node/add/article page.
328 $node = $this->drupalCreateNode([
329 'title' => t('Hello, world!'),
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]));
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']));
347 * Test that the entity default values load on the entity form.
349 * And that they can then be overridden correctly.
351 public function testEntityDefaultInheritence() {
352 // Initiate session with a user who can manage meta tags and content type
355 'administer site configuration',
356 'administer meta tags',
358 'administer node fields',
359 'create article content',
361 'create article content',
362 'create page content',
364 $account = $this->drupalCreateUser($permissions);
365 $this->drupalLogin($account);
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);
371 'new_storage_type' => 'metatag',
372 'label' => 'Meta tags',
373 'field_name' => 'meta_tags',
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'])));
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]');
388 // Customize the Article content type defaults.
389 $this->drupalGet('admin/config/search/metatag/add');
390 $this->assertResponse(200);
392 'id' => 'node__article',
393 'title' => 'Article title override',
394 'description' => 'Article description override',
396 $this->drupalPostForm(NULL, $values, 'Save');
397 $this->assertText(strip_tags(t('Created the %label Metatag defaults.', ['%label' => 'Content: Article'])));
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');
407 * Test that protected Metatag defaults cannot be deleted.
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);
415 // Add default metatag for Articles.
417 'id' => 'node__article',
419 $this->drupalPostForm('/admin/config/search/metatag/add', $edit, 'Save');
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');
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');
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');
439 // Confirm that non protected defaults can be deleted.
440 $this->drupalGet('/admin/config/search/metatag/node__article');
441 $this->assertLink('Delete');