Pull merge.
[yaffs-website] / web / core / modules / views_ui / tests / src / Functional / PreviewTest.php
1 <?php
2
3 namespace Drupal\Tests\views_ui\Functional;
4
5 /**
6  * Tests the UI preview functionality.
7  *
8  * @group views_ui
9  */
10 class PreviewTest extends UITestBase {
11
12   /**
13    * Views used by this test.
14    *
15    * @var array
16    */
17   public static $testViews = ['test_preview', 'test_preview_error', 'test_pager_full', 'test_mini_pager', 'test_click_sort'];
18
19   /**
20    * Tests contextual links in the preview form.
21    */
22   public function testPreviewContextual() {
23     \Drupal::service('module_installer')->install(['contextual']);
24     $this->resetAll();
25
26     $this->drupalGet('admin/structure/views/view/test_preview/edit');
27     $this->assertResponse(200);
28     $this->drupalPostForm(NULL, $edit = [], t('Update preview'));
29
30     $elements = $this->xpath('//div[@id="views-live-preview"]//ul[contains(@class, :ul-class)]/li[contains(@class, :li-class)]', [':ul-class' => 'contextual-links', ':li-class' => 'filter-add']);
31     $this->assertEqual(count($elements), 1, 'The contextual link to add a new field is shown.');
32
33     $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview'));
34
35     // Test that area text and exposed filters are present and rendered.
36     $this->assertFieldByName('id', NULL, 'ID exposed filter field found.');
37     $this->assertText('Test header text', 'Rendered header text found');
38     $this->assertText('Test footer text', 'Rendered footer text found.');
39     $this->assertText('Test empty text', 'Rendered empty text found.');
40   }
41
42   /**
43    * Tests arguments in the preview form.
44    */
45   public function testPreviewUI() {
46     $this->drupalGet('admin/structure/views/view/test_preview/edit');
47     $this->assertResponse(200);
48
49     $this->drupalPostForm(NULL, $edit = [], t('Update preview'));
50
51     $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]');
52     $this->assertEqual(count($elements), 5);
53
54     // Filter just the first result.
55     $this->drupalPostForm(NULL, $edit = ['view_args' => '1'], t('Update preview'));
56
57     $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]');
58     $this->assertEqual(count($elements), 1);
59
60     // Filter for no results.
61     $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview'));
62
63     $elements = $this->xpath('//div[@class = "view-content"]/div[contains(@class, views-row)]');
64     $this->assertEqual(count($elements), 0);
65
66     // Test that area text and exposed filters are present and rendered.
67     $this->assertFieldByName('id', NULL, 'ID exposed filter field found.');
68     $this->assertText('Test header text', 'Rendered header text found');
69     $this->assertText('Test footer text', 'Rendered footer text found.');
70     $this->assertText('Test empty text', 'Rendered empty text found.');
71
72     // Test feed preview.
73     $view = [];
74     $view['label'] = $this->randomMachineName(16);
75     $view['id'] = strtolower($this->randomMachineName(16));
76     $view['page[create]'] = 1;
77     $view['page[title]'] = $this->randomMachineName(16);
78     $view['page[path]'] = $this->randomMachineName(16);
79     $view['page[feed]'] = 1;
80     $view['page[feed_properties][path]'] = $this->randomMachineName(16);
81     $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
82     $this->clickLink(t('Feed'));
83     $this->drupalPostForm(NULL, [], t('Update preview'));
84     $result = $this->xpath('//div[@id="views-live-preview"]/pre');
85     $this->assertTrue(strpos($result[0]->getText(), '<title>' . $view['page[title]'] . '</title>'), 'The Feed RSS preview was rendered.');
86
87     // Test the non-default UI display options.
88     // Statistics only, no query.
89     $settings = \Drupal::configFactory()->getEditable('views.settings');
90     $settings->set('ui.show.performance_statistics', TRUE)->save();
91     $this->drupalGet('admin/structure/views/view/test_preview/edit');
92     $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview'));
93     $this->assertText(t('Query build time'));
94     $this->assertText(t('Query execute time'));
95     $this->assertText(t('View render time'));
96     $this->assertNoRaw('<strong>Query</strong>');
97
98     // Statistics and query.
99     $settings->set('ui.show.sql_query.enabled', TRUE)->save();
100     $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview'));
101     $this->assertText(t('Query build time'));
102     $this->assertText(t('Query execute time'));
103     $this->assertText(t('View render time'));
104     $this->assertRaw('<strong>Query</strong>');
105     $query_string = <<<SQL
106 SELECT views_test_data.name AS views_test_data_name
107 FROM
108 {views_test_data} views_test_data
109 WHERE (views_test_data.id = '100')
110 SQL;
111     $this->assertEscaped($query_string);
112
113     // Test that the statistics and query are rendered above the preview.
114     $this->assertTrue(strpos($this->getSession()->getPage()->getContent(), 'views-query-info') < strpos($this->getSession()->getPage()->getContent(), 'view-test-preview'), 'Statistics shown above the preview.');
115
116     // Test that statistics and query rendered below the preview.
117     $settings->set('ui.show.sql_query.where', 'below')->save();
118     $this->drupalPostForm(NULL, $edit = ['view_args' => '100'], t('Update preview'));
119     $this->assertTrue(strpos($this->getSession()->getPage()->getContent(), 'view-test-preview') < strpos($this->getSession()->getPage()->getContent(), 'views-query-info'), 'Statistics shown below the preview.');
120
121     // Test that the preview title isn't double escaped.
122     $this->drupalPostForm("admin/structure/views/nojs/display/test_preview/default/title", $edit = ['title' => 'Double & escaped'], t('Apply'));
123     $this->drupalPostForm(NULL, [], t('Update preview'));
124     $elements = $this->xpath('//div[@id="views-live-preview"]/div[contains(@class, views-query-info)]//td[text()=:text]', [':text' => 'Double & escaped']);
125     $this->assertEqual(1, count($elements));
126   }
127
128   /**
129    * Tests the additional information query info area.
130    */
131   public function testPreviewAdditionalInfo() {
132     \Drupal::service('module_installer')->install(['views_ui_test']);
133     $this->resetAll();
134
135     $this->drupalGet('admin/structure/views/view/test_preview/edit');
136     $this->assertResponse(200);
137
138     $this->drupalPostForm(NULL, $edit = [], t('Update preview'));
139
140     // Check for implementation of hook_views_preview_info_alter().
141     // @see views_ui_test.module
142     $elements = $this->xpath('//div[@id="views-live-preview"]/div[contains(@class, views-query-info)]//td[text()=:text]', [':text' => 'Test row count']);
143     $this->assertEqual(count($elements), 1, 'Views Query Preview Info area altered.');
144     // Check that additional assets are attached.
145     $this->assertTrue(strpos($this->getDrupalSettings()['ajaxPageState']['libraries'], 'views_ui_test/views_ui_test.test') !== FALSE, 'Attached library found.');
146     $this->assertRaw('css/views_ui_test.test.css', 'Attached CSS asset found.');
147   }
148
149   /**
150    * Tests view validation error messages in the preview.
151    */
152   public function testPreviewError() {
153     $this->drupalGet('admin/structure/views/view/test_preview_error/edit');
154     $this->assertResponse(200);
155
156     $this->drupalPostForm(NULL, $edit = [], t('Update preview'));
157
158     $this->assertText('Unable to preview due to validation errors.', 'Preview error text found.');
159   }
160
161 }