Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / metatag_page_manager / src / Tests / Functional / MetatagPageManagerTest.php
1 <?php
2
3 namespace Drupal\metatag_page_manager\Tests\Functional;
4
5 use Drupal\page_manager\Entity\Page;
6 use Drupal\page_manager\Entity\PageVariant;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\Tests\metatag\Functional\MetatagHelperTrait;
9
10 /**
11  * Confirm the Page Manager integration works.
12  *
13  * @group metatag
14  */
15 class MetatagPageManagerTest extends BrowserTestBase {
16
17   use MetatagHelperTrait;
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = [
23     // This module.
24     'metatag_page_manager',
25   ];
26
27   /**
28    * The assert session object.
29    *
30    * @var \Drupal\Tests\WebAssert
31    */
32   public $assertSession;
33
34   /**
35    * {@inheritdoc}
36    */
37   public function setUp() {
38     // TODO: Change the autogenerated stub.
39     parent::setUp();
40
41     $this->assertSession = $this->assertSession();
42
43     Page::create([
44       'id' => 'metatag_page_manager_test',
45       'label' => 'Metatag Page',
46       'path' => '/metatag-test',
47     ])->save();
48     PageVariant::create([
49       'id' => 'metatag_page_manager_variant_test',
50       'variant' => 'block_display',
51       'label' => 'Metatag Variant',
52       'page' => 'metatag_page_manager_test',
53       'weight' => 10,
54     ])->save();
55
56     \Drupal::service("router.builder")->rebuild();
57
58     // Log in as user 1.
59     $this->loginUser1();
60   }
61
62   /**
63    * Tests a single variant page.
64    */
65   public function testSingleVariantPage() {
66     $this->drupalGet('/metatag-test');
67     $this->assertSession->statusCodeEquals(200);
68
69     // Confirm what the page title looks like by default.
70     $this->assertSession->titleEquals('Metatag Page | Drupal');
71
72     // Create the Metatag object through the UI to check the custom label.
73     $edit = [
74       'id' => 'page_variant__metatag_page_manager_variant_test',
75       'title' => 'My title',
76     ];
77
78     $this->drupalPostForm('/admin/config/search/metatag/add', $edit, 'Save');
79     $this->assertSession->pageTextContains('Page Variant: Metatag Page: Metatag Variant');
80
81     // Clear caches to load the right metatags.
82     drupal_flush_all_caches();
83
84     $this->drupalGet('/metatag-test');
85     $this->assertSession->statusCodeEquals(200);
86
87     // Confirm what the page title is overridden.
88     $this->assertSession->titleEquals('My title');
89   }
90
91   /**
92    * Tests a single variant page.
93    */
94   public function testMultipleVariantPage() {
95     // Add a new variant.
96     $new_variant = PageVariant::create([
97       'id' => 'metatag_page_manager_multiple_variant_test',
98       'variant' => 'block_display',
99       'label' => 'Metatag Multiple Variant',
100       'page' => 'metatag_page_manager_test',
101       'weight' => 0,
102     ]);
103     $anonymous_selection = [
104       'id' => 'user_role',
105       'roles' => [
106         'anonymous' => 'anonymous',
107       ],
108       'negate' => FALSE,
109       'context_mapping' => [
110         'user' => 'current_user',
111       ],
112     ];
113     $new_variant->set('selection_criteria', [$anonymous_selection]);
114     $new_variant->save();
115
116     // Clear caches to load the right metatags.
117     drupal_flush_all_caches();
118
119     $this->drupalGet('/metatag-test');
120     $this->assertSession->statusCodeEquals(200);
121
122     // Confirm what the page title looks like by default.
123     $this->assertSession->titleEquals('Metatag Page | Drupal');
124
125     // Create the Metatag object through the UI to check the custom label.
126     $edit = [
127       'id' => 'page_variant__metatag_page_manager_variant_test',
128       'title' => 'My title',
129     ];
130
131     $this->drupalPostForm('/admin/config/search/metatag/add', $edit, 'Save');
132     $this->assertSession->pageTextContains('Page Variant: Metatag Page: Metatag Variant');
133
134     // Clear caches to load the right metatags.
135     drupal_flush_all_caches();
136
137     $this->drupalGet('/metatag-test');
138     $this->assertSession->statusCodeEquals(200);
139
140     // Confirm what the page title is overridden.
141     $this->assertSession->titleEquals('My title');
142
143     // Visiting page as anon user, should get the default title.
144     $this->drupalLogout();
145     $this->drupalGet('/metatag-test');
146     $this->assertSession->statusCodeEquals(200);
147
148     // Confirm what the page title looks like by default.
149     $this->assertSession->titleEquals('Metatag Page | Drupal');
150
151     // Login and add custom metatag for anonymous user variant.
152     $this->loginUser1();
153     // Create the Metatag object through the UI to check the custom label.
154     $edit = [
155       'id' => 'page_variant__metatag_page_manager_multiple_variant_test',
156       'title' => 'My title anonymous',
157     ];
158
159     $this->drupalPostForm('/admin/config/search/metatag/add', $edit, 'Save');
160     $this->assertSession->pageTextContains('Page Variant: Metatag Page: Metatag Multiple Variant');
161
162     // Clear caches to load the right metatags.
163     drupal_flush_all_caches();
164
165     // Visit page as logged in user and confirm the right title.
166     $this->drupalGet('/metatag-test');
167     $this->assertSession->statusCodeEquals(200);
168     $this->assertSession->titleEquals('My title');
169
170     // Visit page as anonymous user and confirm the right title.
171     $this->drupalLogout();
172     $this->drupalGet('/metatag-test');
173     $this->assertSession->statusCodeEquals(200);
174     $this->assertSession->titleEquals('My title anonymous');
175   }
176
177 }