3 namespace Drupal\metatag\Tests;
5 use Drupal\simpletest\WebTestBase;
8 * Ensures that metatags are rendering correctly on home page.
12 class MetatagFrontpageTest extends WebTestBase {
17 public static $modules = [
26 * The path to a node that is created for testing.
33 * Administrator user for tests.
35 * @var \Drupal\user\UserInterface
40 * Setup basic environment.
42 protected function setUp() {
45 $admin_permissions = [
46 'administer content types',
49 'administer meta tags',
50 'administer site configuration',
54 // Create and login user.
55 $this->adminUser = $this->drupalCreateUser($admin_permissions);
56 $this->drupalLogin($this->adminUser);
58 // Create content type.
59 $this->drupalCreateContentType(['type' => 'page', 'display_submitted' => FALSE]);
60 $this->nodeId = $this->drupalCreateNode(
62 'title' => $this->randomMachineName(8),
66 $this->config('system.site')->set('page.front', '/node/' . $this->nodeId)->save();
70 * The front page config is enabled, its meta tags should be used.
72 public function testFrontPageMetatagsEnabledConfig() {
73 $this->drupalLogin($this->adminUser);
75 // Add something to the front page config.
76 $this->drupalGet('admin/config/search/metatag/front');
77 $this->assertResponse(200);
79 'title' => 'Test title',
80 'description' => 'Test description',
81 'keywords' => 'testing,keywords'
83 $this->drupalPostForm(NULL, $values, t('Save'));
84 $this->assertResponse(200);
85 $this->assertText(t('Saved the Front page Metatag defaults.'));
87 // Testing front page metatags.
88 $this->drupalGet('<front>');
89 foreach ($values as $metatag => $metatag_value) {
90 $xpath = $this->xpath("//meta[@name='" . $metatag . "']");
91 $this->assertEqual(count($xpath), 1, 'Exactly one ' . $metatag . ' meta tag found.');
92 $value = (string) $xpath[0]['content'];
93 $this->assertEqual($value, $metatag_value);
96 $node_path = '/node/' . $this->nodeId;
97 // Testing front page metatags.
98 $this->drupalGet($node_path);
99 foreach ($values as $metatag => $metatag_value) {
100 $xpath = $this->xpath("//meta[@name='" . $metatag . "']");
101 $this->assertEqual(count($xpath), 1, 'Exactly one ' . $metatag . ' meta tag found.');
102 $value = (string) $xpath[0]['content'];
103 $this->assertEqual($value, $metatag_value);
106 // Change the front page to a valid custom route.
107 $edit['site_frontpage'] = '/test-page';
108 $this->drupalGet('admin/config/system/site-information');
109 $this->assertResponse(200);
110 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
111 $this->assertText(t('The configuration options have been saved.'), 'The front page path has been saved.');
113 $this->drupalGet('test-page');
114 foreach ($values as $metatag => $metatag_value) {
115 $xpath = $this->xpath("//meta[@name='" . $metatag . "']");
116 $this->assertEqual(count($xpath), 1, 'Exactly one ' . $metatag . ' meta tag found.');
117 $value = (string) $xpath[0]['content'];
118 $this->assertEqual($value, $metatag_value);
123 * Test front page metatags when front page config is disabled.
125 public function testFrontPageMetatagDisabledConfig() {
126 // Disable front page metatag, enable node metatag & check.
127 $this->drupalGet('admin/config/search/metatag/front/delete');
128 $this->assertResponse(200);
129 $this->drupalPostForm(NULL, [], t('Delete'));
130 $this->assertResponse(200);
131 $this->assertText(t('Deleted Front page defaults.'));
133 // Update the Metatag Node defaults.
134 $this->drupalGet('admin/config/search/metatag/node');
135 $this->assertResponse(200);
137 'title' => 'Test title for a node.',
138 'description' => 'Test description for a node.',
140 $this->drupalPostForm(NULL, $values, 'Save');
141 $this->assertText('Saved the Content Metatag defaults.');
142 $this->drupalGet('<front>');
143 foreach ($values as $metatag => $metatag_value) {
144 $xpath = $this->xpath("//meta[@name='" . $metatag . "']");
145 $this->assertEqual(count($xpath), 1, 'Exactly one ' . $metatag . ' meta tag found.');
146 $value = (string) $xpath[0]['content'];
147 $this->assertEqual($value, $metatag_value);
150 // Front page is custom route.
151 // Update the Metatag Node global.
152 $this->drupalGet('admin/config/search/metatag/global');
153 $this->assertResponse(200);
155 'title' => 'Test title.',
156 'description' => 'Test description.',
158 $this->drupalPostForm(NULL, $values, 'Save');
159 $this->assertText('Saved the Global Metatag defaults.');
161 // Change the front page to a valid path.
162 $this->drupalGet('admin/config/system/site-information');
163 $this->assertResponse(200);
164 $edit['site_frontpage'] = '/test-page';
165 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
166 $this->assertText(t('The configuration options have been saved.'), 'The front page path has been saved.');
169 $this->drupalGet('test-page');
170 foreach ($values as $metatag => $metatag_value) {
171 $xpath = $this->xpath("//meta[@name='" . $metatag . "']");
172 $this->assertEqual(count($xpath), 1, 'Exactly one ' . $metatag . ' meta tag found.');
173 $value = (string) $xpath[0]['content'];
174 $this->assertEqual($value, $metatag_value);