7b63394d52b8ca0c0b003330f6a4528fc8b592d1
[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    * Views used by this test.
22    *
23    * @var array
24    */
25   public static $testViews = ['test_dblog'];
26
27   /**
28    * Modules to enable.
29    *
30    * @var array
31    */
32   public static $modules = ['dblog', 'dblog_test_views', 'user'];
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp($import_test_views = TRUE) {
38     parent::setUp();
39
40     // Rebuild the router, otherwise we can't generate links.
41     $this->container->get('router.builder')->rebuild();
42
43     $this->installSchema('dblog', ['watchdog']);
44
45     ViewTestData::createTestViews(get_class($this), ['dblog_test_views']);
46   }
47
48   /**
49    * Tests the integration.
50    */
51   public function testIntegration() {
52
53     // Remove the watchdog entries added by the potential batch process.
54     $this->container->get('database')->truncate('watchdog')->execute();
55
56     $entries = [];
57     // Setup a watchdog entry without tokens.
58     $entries[] = [
59       'message' => $this->randomMachineName(),
60       'variables' => ['link' => \Drupal::l('Link', new Url('<front>'))],
61     ];
62     // Setup a watchdog entry with one token.
63     $entries[] = [
64       'message' => '@token1',
65       'variables' => ['@token1' => $this->randomMachineName(), 'link' => \Drupal::l('Link', new Url('<front>'))],
66     ];
67     // Setup a watchdog entry with two tokens.
68     $entries[] = [
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.
73       'variables' => [
74         '@token1' => $this->randomMachineName(),
75         '@token2' => $this->randomMachineName(),
76         'link' => '<a href="' . \Drupal::url('<front>') . '"><object>Link</object></a>',
77       ],
78     ];
79     $logger_factory = $this->container->get('logger.factory');
80     foreach ($entries as $entry) {
81       $entry += [
82         'type' => 'test-views',
83         'severity' => RfcLogLevel::NOTICE,
84       ];
85       $logger_factory->get($entry['type'])->log($entry['severity'], $entry['message'], $entry['variables']);
86     }
87
88     $view = Views::getView('test_dblog');
89     $this->executeView($view);
90     $view->initStyle();
91
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.
96       if ($index == 2) {
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']);
100       }
101       $this->assertEqual($link_field, Xss::filterAdmin($entry['variables']['link']));
102     }
103
104     // Disable replacing variables and check that the tokens aren't replaced.
105     $view->destroy();
106     $view->storage->invalidateCaches();
107     $view->initHandlers();
108     $this->executeView($view);
109     $view->initStyle();
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']);
113     }
114   }
115
116 }