Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / dblog / tests / src / Kernel / Views / ViewsIntegrationTest.php
1 <?php
2
3 namespace Drupal\Tests\dblog\Kernel\Views;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Component\Utility\Xss;
7 use Drupal\Core\Logger\RfcLogLevel;
8 use Drupal\Core\Url;
9 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
10 use Drupal\views\Views;
11 use Drupal\views\Tests\ViewTestData;
12
13 /**
14  * Tests the views integration of dblog module.
15  *
16  * @group dblog
17  */
18 class ViewsIntegrationTest extends ViewsKernelTestBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $testViews = ['test_dblog', 'dblog_integration_test'];
24
25   /**
26    * {@inheritdoc}
27    */
28   public static $modules = ['dblog', 'dblog_test_views', 'user'];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected $columnMap = ['watchdog_message' => 'message'];
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUp($import_test_views = TRUE) {
39     parent::setUp();
40
41     $this->installEntitySchema('user');
42     $this->installSchema('dblog', ['watchdog']);
43
44     ViewTestData::createTestViews(get_class($this), ['dblog_test_views']);
45   }
46
47   /**
48    * Tests the messages escaping functionality.
49    */
50   public function testMessages() {
51
52     // Remove the watchdog entries added by the potential batch process.
53     $this->container->get('database')->truncate('watchdog')->execute();
54
55     $entries = $this->createLogEntries();
56
57     $view = Views::getView('test_dblog');
58     $this->executeView($view);
59     $view->initStyle();
60
61     foreach ($entries as $index => $entry) {
62       if (!isset($entry['variables'])) {
63         continue;
64       }
65       $this->assertEqual($view->style_plugin->getField($index, 'message'), SafeMarkup::format($entry['message'], $entry['variables']));
66       $link_field = $view->style_plugin->getField($index, 'link');
67       // The 3rd entry contains some unsafe markup that needs to get filtered.
68       if ($index == 2) {
69         // Make sure that unsafe link differs from the rendered link, so we know
70         // that some filtering actually happened.
71         $this->assertNotEqual($link_field, $entry['variables']['link']);
72       }
73       $this->assertEqual($link_field, Xss::filterAdmin($entry['variables']['link']));
74     }
75
76     // Disable replacing variables and check that the tokens aren't replaced.
77     $view->destroy();
78     $view->storage->invalidateCaches();
79     $view->initHandlers();
80     $this->executeView($view);
81     $view->initStyle();
82     $view->field['message']->options['replace_variables'] = FALSE;
83     foreach ($entries as $index => $entry) {
84       $this->assertEqual($view->style_plugin->getField($index, 'message'), $entry['message']);
85     }
86   }
87
88   /**
89    * Tests the relationship with the users_field_data table.
90    */
91   public function testRelationship() {
92     $view = Views::getView('dblog_integration_test');
93     $view->setDisplay('page_1');
94     // The uid relationship should now join to the {users_field_data} table.
95     $tables = array_keys($view->getBaseTables());
96     $this->assertTrue(in_array('users_field_data', $tables));
97     $this->assertFalse(in_array('users', $tables));
98     $this->assertTrue(in_array('watchdog', $tables));
99   }
100
101   /**
102    * Test views can be filtered by severity and log type.
103    */
104   public function testFiltering() {
105     // Remove the watchdog entries added by the potential batch process.
106     $this->container->get('database')->truncate('watchdog')->execute();
107     $this->createLogEntries();
108
109     $view = Views::getView('dblog_integration_test');
110
111     $filters = [
112       'severity' => [
113         'id' => 'severity',
114         'table' => 'watchdog',
115         'field' => 'severity',
116         'relationship' => 'none',
117         'group_type' => 'group',
118         'admin_label' => '',
119         'operator' => 'in',
120         'value' => [
121           RfcLogLevel::WARNING,
122         ],
123         'group' => 1,
124         'exposed' => FALSE,
125         'plugin_id' => 'in_operator',
126       ],
127     ];
128
129     $view->setDisplay('page_1');
130     $view->displayHandlers->get('page_1')->overrideOption('filters', $filters);
131     $view->save();
132
133     $this->executeView($view);
134
135     $resultset = [['message' => 'Warning message']];
136     $this->assertIdenticalResultset($view, $resultset, $this->columnMap);
137
138     $view = Views::getView('dblog_integration_test');
139
140     $filters = [
141       'type' => [
142         'id' => 'type',
143         'table' => 'watchdog',
144         'field' => 'type',
145         'relationship' => 'none',
146         'group_type' => 'group',
147         'admin_label' => '',
148         'operator' => 'in',
149         'value' => [
150           'my-module' => 'my-module',
151         ],
152         'group' => '1',
153         'exposed' => FALSE,
154         'is_grouped' => FALSE,
155         'plugin_id' => 'dblog_types',
156       ],
157     ];
158
159     $view->setDisplay('page_1');
160     $view->displayHandlers->get('page_1')->overrideOption('filters', $filters);
161     $view->save();
162
163     $this->executeView($view);
164
165     $resultset = [['message' => 'My module message']];
166     $this->assertIdenticalResultset($view, $resultset, $this->columnMap);
167   }
168
169   /**
170    * Create a set of log entries.
171    *
172    * @return array
173    *   An array of data used to create the log entries.
174    */
175   protected function createLogEntries() {
176     $entries = [];
177     // Setup a watchdog entry without tokens.
178     $entries[] = [
179       'message' => $this->randomMachineName(),
180       'variables' => ['link' => \Drupal::l('Link', new Url('<front>'))],
181     ];
182     // Setup a watchdog entry with one token.
183     $entries[] = [
184       'message' => '@token1',
185       'variables' => ['@token1' => $this->randomMachineName(), 'link' => \Drupal::l('Link', new Url('<front>'))],
186     ];
187     // Setup a watchdog entry with two tokens.
188     $entries[] = [
189       'message' => '@token1 @token2',
190       // Setup a link with a tag which is filtered by
191       // \Drupal\Component\Utility\Xss::filterAdmin() in order to make sure
192       // that strings which are not marked as safe get filtered.
193       'variables' => [
194         '@token1' => $this->randomMachineName(),
195         '@token2' => $this->randomMachineName(),
196         'link' => '<a href="' . \Drupal::url('<front>') . '"><object>Link</object></a>',
197       ],
198     ];
199     // Setup a watchdog entry with severity WARNING.
200     $entries[] = [
201       'message' => 'Warning message',
202       'severity' => RfcLogLevel::WARNING,
203     ];
204     // Setup a watchdog entry with a different module.
205     $entries[] = [
206       'message' => 'My module message',
207       'severity' => RfcLogLevel::INFO,
208       'type' => 'my-module',
209     ];
210
211     $logger_factory = $this->container->get('logger.factory');
212     foreach ($entries as $entry) {
213       $entry += [
214         'type' => 'test-views',
215         'severity' => RfcLogLevel::NOTICE,
216         'variables' => [],
217       ];
218       $logger_factory->get($entry['type'])->log($entry['severity'], $entry['message'], $entry['variables']);
219     }
220     return $entries;
221   }
222
223 }