3 namespace Drupal\metatag_page_manager\Tests\Functional;
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;
11 * Confirm the Page Manager integration works.
15 class MetatagPageManagerTest extends BrowserTestBase {
17 use MetatagHelperTrait;
22 public static $modules = [
24 'metatag_page_manager',
28 * The assert session object.
30 * @var \Drupal\Tests\WebAssert
32 public $assertSession;
37 public function setUp() {
38 // TODO: Change the autogenerated stub.
41 $this->assertSession = $this->assertSession();
44 'id' => 'metatag_page_manager_test',
45 'label' => 'Metatag Page',
46 'path' => '/metatag-test',
49 'id' => 'metatag_page_manager_variant_test',
50 'variant' => 'block_display',
51 'label' => 'Metatag Variant',
52 'page' => 'metatag_page_manager_test',
56 \Drupal::service("router.builder")->rebuild();
63 * Tests a single variant page.
65 public function testSingleVariantPage() {
66 $this->drupalGet('/metatag-test');
67 $this->assertSession->statusCodeEquals(200);
69 // Confirm what the page title looks like by default.
70 $this->assertSession->titleEquals('Metatag Page | Drupal');
72 // Create the Metatag object through the UI to check the custom label.
74 'id' => 'page_variant__metatag_page_manager_variant_test',
75 'title' => 'My title',
78 $this->drupalPostForm('/admin/config/search/metatag/add', $edit, 'Save');
79 $this->assertSession->pageTextContains('Page Variant: Metatag Page: Metatag Variant');
81 // Clear caches to load the right metatags.
82 drupal_flush_all_caches();
84 $this->drupalGet('/metatag-test');
85 $this->assertSession->statusCodeEquals(200);
87 // Confirm what the page title is overridden.
88 $this->assertSession->titleEquals('My title');
92 * Tests a single variant page.
94 public function testMultipleVariantPage() {
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',
103 $anonymous_selection = [
106 'anonymous' => 'anonymous',
109 'context_mapping' => [
110 'user' => 'current_user',
113 $new_variant->set('selection_criteria', [$anonymous_selection]);
114 $new_variant->save();
116 // Clear caches to load the right metatags.
117 drupal_flush_all_caches();
119 $this->drupalGet('/metatag-test');
120 $this->assertSession->statusCodeEquals(200);
122 // Confirm what the page title looks like by default.
123 $this->assertSession->titleEquals('Metatag Page | Drupal');
125 // Create the Metatag object through the UI to check the custom label.
127 'id' => 'page_variant__metatag_page_manager_variant_test',
128 'title' => 'My title',
131 $this->drupalPostForm('/admin/config/search/metatag/add', $edit, 'Save');
132 $this->assertSession->pageTextContains('Page Variant: Metatag Page: Metatag Variant');
134 // Clear caches to load the right metatags.
135 drupal_flush_all_caches();
137 $this->drupalGet('/metatag-test');
138 $this->assertSession->statusCodeEquals(200);
140 // Confirm what the page title is overridden.
141 $this->assertSession->titleEquals('My title');
143 // Visiting page as anon user, should get the default title.
144 $this->drupalLogout();
145 $this->drupalGet('/metatag-test');
146 $this->assertSession->statusCodeEquals(200);
148 // Confirm what the page title looks like by default.
149 $this->assertSession->titleEquals('Metatag Page | Drupal');
151 // Login and add custom metatag for anonymous user variant.
153 // Create the Metatag object through the UI to check the custom label.
155 'id' => 'page_variant__metatag_page_manager_multiple_variant_test',
156 'title' => 'My title anonymous',
159 $this->drupalPostForm('/admin/config/search/metatag/add', $edit, 'Save');
160 $this->assertSession->pageTextContains('Page Variant: Metatag Page: Metatag Multiple Variant');
162 // Clear caches to load the right metatags.
163 drupal_flush_all_caches();
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');
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');