View source
<?php
namespace Drupal\dblog\Tests\Views;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url;
use Drupal\views\Views;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Tests\ViewKernelTestBase;
class ViewsIntegrationTest extends ViewKernelTestBase {
public static $testViews = array(
'test_dblog',
);
public static $modules = array(
'dblog',
'dblog_test_views',
'user',
);
protected function setUp() {
parent::setUp();
$this->container
->get('router.builder')
->rebuild();
$this
->installSchema('dblog', array(
'watchdog',
));
ViewTestData::createTestViews(get_class($this), array(
'dblog_test_views',
));
}
public function testIntegration() {
$this->container
->get('database')
->truncate('watchdog')
->execute();
$entries = array();
$entries[] = array(
'message' => $this
->randomMachineName(),
'variables' => array(
'link' => \Drupal::l('Link', new Url('<front>')),
),
);
$entries[] = array(
'message' => '@token1',
'variables' => array(
'@token1' => $this
->randomMachineName(),
'link' => \Drupal::l('Link', new Url('<front>')),
),
);
$entries[] = array(
'message' => '@token1 @token2',
'variables' => array(
'@token1' => $this
->randomMachineName(),
'@token2' => $this
->randomMachineName(),
'link' => '<a href="' . \Drupal::url('<front>') . '"><object>Link</object></a>',
),
);
$logger_factory = $this->container
->get('logger.factory');
foreach ($entries as $entry) {
$entry += array(
'type' => 'test-views',
'severity' => RfcLogLevel::NOTICE,
);
$logger_factory
->get($entry['type'])
->log($entry['severity'], $entry['message'], $entry['variables']);
}
$view = Views::getView('test_dblog');
$this
->executeView($view);
$view
->initStyle();
foreach ($entries as $index => $entry) {
$this
->assertEqual($view->style_plugin
->getField($index, 'message'), SafeMarkup::format($entry['message'], $entry['variables']));
$link_field = $view->style_plugin
->getField($index, 'link');
if ($index == 2) {
$this
->assertNotEqual($link_field, $entry['variables']['link']);
}
$this
->assertEqual($link_field, Xss::filterAdmin($entry['variables']['link']));
}
$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
->assertEqual($view->style_plugin
->getField($index, 'message'), $entry['message']);
}
}
}