3 namespace Drupal\metatag\Tests;
5 use Drupal\simpletest\WebTestBase;
8 * Ensures that meta tags are rendering correctly on home page.
12 class MetatagFrontpageTest extends WebTestBase {
14 // Use the helper functions from the Functional trait. This is pretty safe but
15 // remember to rewrite all of these WebTestBase tests using BrowserTestBase
16 // before the next millenium.
17 use MetatagHelperTrait;
22 public static $modules = [
31 * The path to a node that is created for testing.
38 * Setup basic environment.
40 protected function setUp() {
46 // Create content type.
47 $this->drupalCreateContentType(['type' => 'page', 'display_submitted' => FALSE]);
48 $this->nodeId = $this->drupalCreateNode(
50 'title' => $this->randomMachineName(8),
54 $this->config('system.site')->set('page.front', '/node/' . $this->nodeId)->save();
58 * The front page config is enabled, its meta tags should be used.
60 public function testFrontPageMetatagsEnabledConfig() {
61 // Add something to the front page config.
62 $this->drupalGet('admin/config/search/metatag/front');
63 $this->assertResponse(200);
65 'title' => 'Test title',
66 'description' => 'Test description',
67 'keywords' => 'testing,keywords',
69 $this->drupalPostForm(NULL, $edit, t('Save'));
70 $this->assertResponse(200);
71 $this->assertText(t('Saved the Front page Metatag defaults.'));
73 // Testing front page metatags.
74 $this->drupalGet('<front>');
75 foreach ($edit as $metatag => $metatag_value) {
76 $xpath = $this->xpath("//meta[@name='" . $metatag . "']");
77 $this->assertEqual(count($xpath), 1, 'Exactly one ' . $metatag . ' meta tag found.');
78 $value = (string) $xpath[0]['content'];
79 $this->assertEqual($value, $metatag_value);
82 $node_path = '/node/' . $this->nodeId;
83 // Testing front page metatags.
84 $this->drupalGet($node_path);
85 foreach ($edit as $metatag => $metatag_value) {
86 $xpath = $this->xpath("//meta[@name='" . $metatag . "']");
87 $this->assertEqual(count($xpath), 1, 'Exactly one ' . $metatag . ' meta tag found.');
88 $value = (string) $xpath[0]['content'];
89 $this->assertEqual($value, $metatag_value);
92 // Change the front page to a valid custom route.
94 'site_frontpage' => '/test-page',
96 $this->drupalGet('admin/config/system/site-information');
97 $this->assertResponse(200);
98 $this->drupalPostForm(NULL, $site_edit, t('Save configuration'));
99 $this->assertText(t('The configuration options have been saved.'), 'The front page path has been saved.');
102 // @todo Finish this?
103 $this->drupalGet('test-page');
104 $this->assertResponse(200);
105 foreach ($edit as $metatag => $metatag_value) {
106 $xpath = $this->xpath("//meta[@name='" . $metatag . "']");
107 $this->assertEqual(count($xpath), 1, 'Exactly one ' . $metatag . ' meta tag found.');
108 $value = (string) $xpath[0]['content'];
109 $this->assertEqual($value, $metatag_value);
114 * Test front page meta tags when front page config is disabled.
116 public function testFrontPageMetatagDisabledConfig() {
117 // Disable front page metatag, enable node metatag & check.
118 $this->drupalGet('admin/config/search/metatag/front/delete');
119 $this->assertResponse(200);
120 $this->drupalPostForm(NULL, [], t('Delete'));
121 $this->assertResponse(200);
122 $this->assertText(t('Deleted Front page defaults.'));
124 // Update the Metatag Node defaults.
125 $this->drupalGet('admin/config/search/metatag/node');
126 $this->assertResponse(200);
128 'title' => 'Test title for a node.',
129 'description' => 'Test description for a node.',
131 $this->drupalPostForm(NULL, $edit, 'Save');
132 $this->assertText('Saved the Content Metatag defaults.');
133 $this->drupalGet('<front>');
134 foreach ($edit as $metatag => $metatag_value) {
135 $xpath = $this->xpath("//meta[@name='" . $metatag . "']");
136 $this->assertEqual(count($xpath), 1, 'Exactly one ' . $metatag . ' meta tag found.');
137 $value = (string) $xpath[0]['content'];
138 $this->assertEqual($value, $metatag_value);
141 // Change the front page to a valid path.
142 $this->drupalGet('admin/config/system/site-information');
143 $this->assertResponse(200);
145 'site_frontpage' => '/test-page',
147 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
148 $this->assertText(t('The configuration options have been saved.'), 'The front page path has been saved.');
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, $edit, 'Save');
159 $this->assertText('Saved the Global Metatag defaults.');
162 $this->drupalGet('test-page');
163 $this->assertResponse(200);
164 foreach ($edit as $metatag => $metatag_value) {
165 $xpath = $this->xpath("//meta[@name='" . $metatag . "']");
166 $this->assertEqual(count($xpath), 1, 'Exactly one ' . $metatag . ' meta tag found.');
167 $value = (string) $xpath[0]['content'];
168 $this->assertEqual($value, $metatag_value);