function ViewsIntegrationTest::testMessages

Same name in other branches
  1. 8.9.x core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php \Drupal\Tests\dblog\Kernel\Views\ViewsIntegrationTest::testMessages()
  2. 10 core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php \Drupal\Tests\dblog\Kernel\Views\ViewsIntegrationTest::testMessages()
  3. 11.x core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php \Drupal\Tests\dblog\Kernel\Views\ViewsIntegrationTest::testMessages()

Tests the messages escaping functionality.

File

core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php, line 51

Class

ViewsIntegrationTest
Tests the views integration of dblog module.

Namespace

Drupal\Tests\dblog\Kernel\Views

Code

public function testMessages() {
    // Remove the watchdog entries added by the potential batch process.
    $this->container
        ->get('database')
        ->truncate('watchdog')
        ->execute();
    $entries = $this->createLogEntries();
    $view = Views::getView('test_dblog');
    $this->executeView($view);
    $view->initStyle();
    foreach ($entries as $index => $entry) {
        if (!isset($entry['variables'])) {
            continue;
        }
        $message_vars = $entry['variables'];
        unset($message_vars['link']);
        $this->assertEquals(new FormattableMarkup($entry['message'], $message_vars), $view->style_plugin
            ->getField($index, 'message'));
        $link_field = $view->style_plugin
            ->getField($index, 'link');
        // The 3rd entry contains some unsafe markup that needs to get filtered.
        if ($index == 2) {
            // Make sure that unsafe link differs from the rendered link, so we know
            // that some filtering actually happened.
            $this->assertNotEquals($entry['variables']['link'], $link_field);
        }
        $this->assertEquals(Xss::filterAdmin($entry['variables']['link']), $link_field);
    }
    // Disable replacing variables and check that the tokens aren't replaced.
    $view->destroy();
    $view->storage
        ->invalidateCaches();
    $view->initHandlers();
    $this->executeView($view);
    $view->initStyle();
    $view->field['message']->options['replace_variables'] = FALSE;
    foreach ($entries as $index => $entry) {
        $this->assertEquals($entry['message'], $view->style_plugin
            ->getField($index, 'message'));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.