Updated to Drupal 8.5. Core Media not yet in use.
[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 meta tags are rendering correctly on home page.
9  *
10  * @group metatag
11  */
12 class MetatagFrontpageTest extends WebTestBase {
13
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;
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = [
23     'token',
24     'metatag',
25     'node',
26     'system',
27     'test_page_test',
28   ];
29
30   /**
31    * The path to a node that is created for testing.
32    *
33    * @var string
34    */
35   protected $nodeId;
36
37   /**
38    * Setup basic environment.
39    */
40   protected function setUp() {
41     parent::setUp();
42
43     // Login user 1.
44     $this->loginUser1();
45
46     // Create content type.
47     $this->drupalCreateContentType(['type' => 'page', 'display_submitted' => FALSE]);
48     $this->nodeId = $this->drupalCreateNode(
49       [
50         'title' => $this->randomMachineName(8),
51         'promote' => 1,
52       ])->id();
53
54     $this->config('system.site')->set('page.front', '/node/' . $this->nodeId)->save();
55   }
56
57   /**
58    * The front page config is enabled, its meta tags should be used.
59    */
60   public function testFrontPageMetatagsEnabledConfig() {
61     // Add something to the front page config.
62     $this->drupalGet('admin/config/search/metatag/front');
63     $this->assertResponse(200);
64     $edit = [
65       'title' => 'Test title',
66       'description' => 'Test description',
67       'keywords' => 'testing,keywords',
68     ];
69     $this->drupalPostForm(NULL, $edit, t('Save'));
70     $this->assertResponse(200);
71     $this->assertText(t('Saved the Front page Metatag defaults.'));
72
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);
80     }
81
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);
90     }
91
92     // Change the front page to a valid custom route.
93     $site_edit = [
94       'site_frontpage' => '/test-page',
95     ];
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.');
100     return;
101
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);
110     }
111   }
112
113   /**
114    * Test front page meta tags when front page config is disabled.
115    */
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.'));
123
124     // Update the Metatag Node defaults.
125     $this->drupalGet('admin/config/search/metatag/node');
126     $this->assertResponse(200);
127     $edit = [
128       'title' => 'Test title for a node.',
129       'description' => 'Test description for a node.',
130     ];
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);
139     }
140
141     // Change the front page to a valid path.
142     $this->drupalGet('admin/config/system/site-information');
143     $this->assertResponse(200);
144     $edit = [
145       'site_frontpage' => '/test-page',
146     ];
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.');
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     $edit = [
155       'title' => 'Test title.',
156       'description' => 'Test description.',
157     ];
158     $this->drupalPostForm(NULL, $edit, 'Save');
159     $this->assertText('Saved the Global Metatag defaults.');
160
161     // Test Metatags.
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);
169     }
170   }
171
172 }