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 {
21 * Views used by this test.
25 public static $testViews = ['test_dblog'];
32 public static $modules = ['dblog', 'dblog_test_views', 'user'];
37 protected function setUp($import_test_views = TRUE) {
40 // Rebuild the router, otherwise we can't generate links.
41 $this->container->get('router.builder')->rebuild();
43 $this->installSchema('dblog', ['watchdog']);
45 ViewTestData::createTestViews(get_class($this), ['dblog_test_views']);
49 * Tests the integration.
51 public function testIntegration() {
53 // Remove the watchdog entries added by the potential batch process.
54 $this->container->get('database')->truncate('watchdog')->execute();
57 // Setup a watchdog entry without tokens.
59 'message' => $this->randomMachineName(),
60 'variables' => ['link' => \Drupal::l('Link', new Url('<front>'))],
62 // Setup a watchdog entry with one token.
64 'message' => '@token1',
65 'variables' => ['@token1' => $this->randomMachineName(), 'link' => \Drupal::l('Link', new Url('<front>'))],
67 // Setup a watchdog entry with two tokens.
69 'message' => '@token1 @token2',
70 // Setup a link with a tag which is filtered by
71 // \Drupal\Component\Utility\Xss::filterAdmin() in order to make sure
72 // that strings which are not marked as safe get filtered.
74 '@token1' => $this->randomMachineName(),
75 '@token2' => $this->randomMachineName(),
76 'link' => '<a href="' . \Drupal::url('<front>') . '"><object>Link</object></a>',
79 $logger_factory = $this->container->get('logger.factory');
80 foreach ($entries as $entry) {
82 'type' => 'test-views',
83 'severity' => RfcLogLevel::NOTICE,
85 $logger_factory->get($entry['type'])->log($entry['severity'], $entry['message'], $entry['variables']);
88 $view = Views::getView('test_dblog');
89 $this->executeView($view);
92 foreach ($entries as $index => $entry) {
93 $this->assertEqual($view->style_plugin->getField($index, 'message'), SafeMarkup::format($entry['message'], $entry['variables']));
94 $link_field = $view->style_plugin->getField($index, 'link');
95 // The 3rd entry contains some unsafe markup that needs to get filtered.
97 // Make sure that unsafe link differs from the rendered link, so we know
98 // that some filtering actually happened.
99 $this->assertNotEqual($link_field, $entry['variables']['link']);
101 $this->assertEqual($link_field, Xss::filterAdmin($entry['variables']['link']));
104 // Disable replacing variables and check that the tokens aren't replaced.
106 $view->storage->invalidateCaches();
107 $view->initHandlers();
108 $this->executeView($view);
110 $view->field['message']->options['replace_variables'] = FALSE;
111 foreach ($entries as $index => $entry) {
112 $this->assertEqual($view->style_plugin->getField($index, 'message'), $entry['message']);