Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / DisplayPageWebTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
5 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7 use Drupal\views\Views;
8
9 /**
10  * Tests the views page display plugin as webtest.
11  *
12  * @group views
13  */
14 class DisplayPageWebTest extends ViewTestBase {
15
16   use AssertPageCacheContextsAndTagsTrait;
17
18   /**
19    * Views used by this test.
20    *
21    * @var array
22    */
23   public static $testViews = ['test_page_display', 'test_page_display_arguments', 'test_page_display_menu', 'test_page_display_path'];
24
25   /**
26    * Modules to enable.
27    *
28    * @var array
29    */
30   public static $modules = ['menu_ui', 'block', 'views_ui'];
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUp($import_test_views = TRUE) {
36     parent::setUp($import_test_views);
37
38     $this->enableViewsTestModule();
39     $this->drupalPlaceBlock('local_tasks_block');
40   }
41
42   /**
43    * Tests arguments.
44    */
45   public function testArguments() {
46     $this->drupalGet('test_route_without_arguments');
47     $this->assertResponse(200);
48     $result = $this->xpath('//span[@class="field-content"]');
49     $this->assertEqual(count($result), 5, 'All entries was returned');
50
51     $this->drupalGet('test_route_without_arguments/1');
52     $this->assertResponse(404);
53
54     $this->drupalGet('test_route_with_argument/1');
55     $this->assertResponse(200);
56     $this->assertCacheContexts(['languages:language_interface', 'route', 'theme', 'url']);
57     $result = $this->xpath('//span[@class="field-content"]');
58     $this->assertEqual(count($result), 1, 'Ensure that just the filtered entry was returned.');
59     $this->assertEqual($result[0]->getText(), 1, 'The passed ID was returned.');
60
61     $this->drupalGet('test_route_with_suffix/1/suffix');
62     $this->assertResponse(200);
63     $result = $this->xpath('//span[@class="field-content"]');
64     $this->assertEqual(count($result), 1, 'Ensure that just the filtered entry was returned.');
65     $this->assertEqual($result[0]->getText(), 1, 'The passed ID was returned.');
66
67     $this->drupalGet('test_route_with_suffix_and_argument/1/suffix/2');
68     $this->assertResponse(200);
69     $result = $this->xpath('//span[@class="field-content"]');
70     $this->assertEqual(count($result), 0, 'No result was returned.');
71
72     $this->drupalGet('test_route_with_suffix_and_argument/1/suffix/1');
73     $this->assertResponse(200);
74     $result = $this->xpath('//span[@class="field-content"]');
75     $this->assertEqual(count($result), 1, 'Ensure that just the filtered entry was returned.');
76     $this->assertEqual($result[0]->getText(), 1, 'The passed ID was returned.');
77
78     $this->drupalGet('test_route_with_long_argument/1');
79     $this->assertResponse(200);
80     $result = $this->xpath('//span[@class="field-content"]');
81     $this->assertEqual(count($result), 1, 'Ensure that just the filtered entry was returned.');
82     $this->assertEqual($result[0]->getText(), 1, 'The passed ID was returned.');
83   }
84
85   /**
86    * Tests menu settings of page displays.
87    */
88   public function testPageDisplayMenu() {
89     // Check local tasks.
90     $this->drupalGet('test_page_display_menu');
91     $this->assertResponse(200);
92     $element = $this->xpath('//ul[contains(@class, :ul_class)]//a[contains(@class, :a_class)]/child::text()', [
93       ':ul_class' => 'tabs primary',
94       ':a_class' => 'is-active',
95     ]);
96     $this->assertEqual($element[0]->getText(), t('Test default tab'));
97     $this->assertTitle(t('Test default page | Drupal'));
98
99     $this->drupalGet('test_page_display_menu/default');
100     $this->assertResponse(404);
101
102     $this->drupalGet('test_page_display_menu/local');
103     $this->assertResponse(200);
104     $element = $this->xpath('//ul[contains(@class, :ul_class)]//a[contains(@class, :a_class)]/child::text()', [
105       ':ul_class' => 'tabs primary',
106       ':a_class' => 'is-active',
107     ]);
108     $this->assertEqual($element[0]->getText(), t('Test local tab'));
109     $this->assertTitle(t('Test local page | Drupal'));
110
111     // Check an ordinary menu link.
112     $admin_user = $this->drupalCreateUser(['administer menu']);
113     $this->drupalLogin($admin_user);
114     $this->drupalPlaceBlock('system_menu_block:tools');
115     $this->drupalGet('<front>');
116
117     $menu_link = $this->cssSelect('nav.block-menu ul.menu a');
118     $this->assertEqual($menu_link[0]->getText(), 'Test menu link');
119
120     // Update the menu link.
121     $this->drupalPostForm("admin/structure/menu/link/views_view:views.test_page_display_menu.page_3/edit", [
122       'title' => 'New title',
123     ], t('Save'));
124
125     $this->drupalGet('<front>');
126     $menu_link = $this->cssSelect('nav.block-menu ul.menu a');
127     $this->assertEqual($menu_link[0]->getText(), 'New title');
128   }
129
130   /**
131    * Tests the title is not displayed in the output.
132    */
133   public function testTitleOutput() {
134     $this->drupalGet('test_page_display_200');
135
136     $view = Views::getView('test_page_display');
137     $xpath = $this->cssSelect('div.view:contains("' . $view->getTitle() . '")');
138     $this->assertFalse($xpath, 'The view title was not displayed in the view markup.');
139   }
140
141   /**
142    * Tests the views page path functionality.
143    */
144   public function testPagePaths() {
145     $this->drupalLogin($this->rootUser);
146     $this->assertPagePath('0');
147     $this->assertPagePath('9999');
148   }
149
150   /**
151    * Tests that we can successfully change a view page display path.
152    *
153    * @param string $path
154    *   Path that will be set as the view page display path.
155    *
156    * @return bool
157    *   Assertion result.
158    */
159   public function assertPagePath($path) {
160     $view = Views::getView('test_page_display_path');
161     $view->initDisplay('page_1');
162     $view->displayHandlers->get('page_1')->overrideOption('path', $path);
163     $view->save();
164     $this->container->get('router.builder')->rebuild();
165     // Check if we successfully changed the path.
166     $this->drupalGet($path);
167     $success = $this->assertResponse(200);
168     // Check if we don't get any error on the view edit page.
169     $this->drupalGet('admin/structure/views/view/test_page_display_path');
170     return $success && $this->assertResponse(200);
171   }
172
173 }