Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / metatag / src / Tests / MetatagFrontpageTest.php
1 <?php
2
3 namespace Drupal\metatag\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Ensures that metatags are rendering correctly on home page.
9  *
10  * @group metatag
11  */
12 class MetatagFrontpageTest extends WebTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = [
18     'token',
19     'metatag',
20     'node',
21     'system',
22     'test_page_test',
23   ];
24
25   /**
26    * The path to a node that is created for testing.
27    *
28    * @var string
29    */
30   protected $nodeId;
31
32   /**
33    * Administrator user for tests.
34    *
35    * @var \Drupal\user\UserInterface
36    */
37   protected $adminUser;
38
39   /**
40    * Setup basic environment.
41    */
42   protected function setUp() {
43     parent::setUp();
44
45     $admin_permissions = [
46       'administer content types',
47       'administer nodes',
48       'bypass node access',
49       'administer meta tags',
50       'administer site configuration',
51       'access content',
52     ];
53
54     // Create and login user.
55     $this->adminUser = $this->drupalCreateUser($admin_permissions);
56     $this->drupalLogin($this->adminUser);
57
58     // Create content type.
59     $this->drupalCreateContentType(['type' => 'page', 'display_submitted' => FALSE]);
60     $this->nodeId = $this->drupalCreateNode(
61       [
62         'title' => $this->randomMachineName(8),
63         'promote' => 1,
64       ])->id();
65
66     $this->config('system.site')->set('page.front', '/node/' . $this->nodeId)->save();
67   }
68
69   /**
70    * The front page config is enabled, its meta tags should be used.
71    */
72   public function testFrontPageMetatagsEnabledConfig() {
73     $this->drupalLogin($this->adminUser);
74
75     // Add something to the front page config.
76     $this->drupalGet('admin/config/search/metatag/front');
77     $this->assertResponse(200);
78     $values = [
79       'title' => 'Test title',
80       'description' => 'Test description',
81       'keywords' => 'testing,keywords'
82     ];
83     $this->drupalPostForm(NULL, $values, t('Save'));
84     $this->assertResponse(200);
85     $this->assertText(t('Saved the Front page Metatag defaults.'));
86
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);
94     }
95
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);
104     }
105
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.');
112
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);
119     }
120   }
121
122   /**
123    * Test front page metatags when front page config is disabled.
124    */
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.'));
132
133     // Update the Metatag Node defaults.
134     $this->drupalGet('admin/config/search/metatag/node');
135     $this->assertResponse(200);
136     $values = [
137       'title' => 'Test title for a node.',
138       'description' => 'Test description for a node.',
139     ];
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);
148     }
149
150     // Front page is custom route.
151     // Update the Metatag Node global.
152     $this->drupalGet('admin/config/search/metatag/global');
153     $this->assertResponse(200);
154     $values = [
155       'title' => 'Test title.',
156       'description' => 'Test description.',
157     ];
158     $this->drupalPostForm(NULL, $values, 'Save');
159     $this->assertText('Saved the Global Metatag defaults.');
160
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.');
167
168     // Test Metatags.
169     $this->drupalGet('test-page');
170     $this->assertResponse(200);
171     foreach ($values as $metatag => $metatag_value) {
172       $xpath = $this->xpath("//meta[@name='" . $metatag . "']");
173       $this->assertEqual(count($xpath), 1, 'Exactly one ' . $metatag . ' meta tag found.');
174       $value = (string) $xpath[0]['content'];
175       $this->assertEqual($value, $metatag_value);
176     }
177   }
178
179 }