3 namespace Drupal\Tests\dblog\Kernel\Views;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Component\Utility\Xss;
7 use Drupal\Core\Logger\RfcLogLevel;
9 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
10 use Drupal\views\Views;
11 use Drupal\views\Tests\ViewTestData;
14 * Tests the views integration of dblog module.
18 class ViewsIntegrationTest extends ViewsKernelTestBase {
23 public static $testViews = ['test_dblog', 'dblog_integration_test'];
28 public static $modules = ['dblog', 'dblog_test_views', 'user'];
33 protected $columnMap = ['watchdog_message' => 'message'];
38 protected function setUp($import_test_views = TRUE) {
41 $this->installEntitySchema('user');
42 $this->installSchema('dblog', ['watchdog']);
44 ViewTestData::createTestViews(get_class($this), ['dblog_test_views']);
48 * Tests the messages escaping functionality.
50 public function testMessages() {
52 // Remove the watchdog entries added by the potential batch process.
53 $this->container->get('database')->truncate('watchdog')->execute();
55 $entries = $this->createLogEntries();
57 $view = Views::getView('test_dblog');
58 $this->executeView($view);
61 foreach ($entries as $index => $entry) {
62 if (!isset($entry['variables'])) {
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.
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']);
73 $this->assertEqual($link_field, Xss::filterAdmin($entry['variables']['link']));
76 // Disable replacing variables and check that the tokens aren't replaced.
78 $view->storage->invalidateCaches();
79 $view->initHandlers();
80 $this->executeView($view);
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']);
89 * Tests the relationship with the users_field_data table.
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));
102 * Test views can be filtered by severity and log type.
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();
109 $view = Views::getView('dblog_integration_test');
114 'table' => 'watchdog',
115 'field' => 'severity',
116 'relationship' => 'none',
117 'group_type' => 'group',
121 RfcLogLevel::WARNING,
125 'plugin_id' => 'in_operator',
129 $view->setDisplay('page_1');
130 $view->displayHandlers->get('page_1')->overrideOption('filters', $filters);
133 $this->executeView($view);
135 $resultset = [['message' => 'Warning message']];
136 $this->assertIdenticalResultset($view, $resultset, $this->columnMap);
138 $view = Views::getView('dblog_integration_test');
143 'table' => 'watchdog',
145 'relationship' => 'none',
146 'group_type' => 'group',
150 'my-module' => 'my-module',
154 'is_grouped' => FALSE,
155 'plugin_id' => 'dblog_types',
159 $view->setDisplay('page_1');
160 $view->displayHandlers->get('page_1')->overrideOption('filters', $filters);
163 $this->executeView($view);
165 $resultset = [['message' => 'My module message']];
166 $this->assertIdenticalResultset($view, $resultset, $this->columnMap);
170 * Create a set of log entries.
173 * An array of data used to create the log entries.
175 protected function createLogEntries() {
177 // Setup a watchdog entry without tokens.
179 'message' => $this->randomMachineName(),
180 'variables' => ['link' => \Drupal::l('Link', new Url('<front>'))],
182 // Setup a watchdog entry with one token.
184 'message' => '@token1',
185 'variables' => ['@token1' => $this->randomMachineName(), 'link' => \Drupal::l('Link', new Url('<front>'))],
187 // Setup a watchdog entry with two tokens.
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.
194 '@token1' => $this->randomMachineName(),
195 '@token2' => $this->randomMachineName(),
196 'link' => '<a href="' . \Drupal::url('<front>') . '"><object>Link</object></a>',
199 // Setup a watchdog entry with severity WARNING.
201 'message' => 'Warning message',
202 'severity' => RfcLogLevel::WARNING,
204 // Setup a watchdog entry with a different module.
206 'message' => 'My module message',
207 'severity' => RfcLogLevel::INFO,
208 'type' => 'my-module',
211 $logger_factory = $this->container->get('logger.factory');
212 foreach ($entries as $entry) {
214 'type' => 'test-views',
215 'severity' => RfcLogLevel::NOTICE,
218 $logger_factory->get($entry['type'])->log($entry['severity'], $entry['message'], $entry['variables']);