View source
<?php
namespace Drupal\Tests\dblog\Kernel\Views;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;
use Drupal\views\Tests\ViewTestData;
class ViewsIntegrationTest extends ViewsKernelTestBase {
public static $testViews = [
'test_dblog',
'dblog_integration_test',
];
protected static $modules = [
'dblog',
'dblog_test_views',
'user',
];
protected $columnMap = [
'watchdog_message' => 'message',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installSchema('dblog', [
'watchdog',
]);
ViewTestData::createTestViews(static::class, [
'dblog_test_views',
]);
}
public function testMessages() {
$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');
if ($index == 2) {
$this
->assertNotEquals($entry['variables']['link'], $link_field);
}
$this
->assertEquals(Xss::filterAdmin($entry['variables']['link']), $link_field);
}
$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'));
}
}
public function testRelationship() {
$view = Views::getView('dblog_integration_test');
$view
->setDisplay('page_1');
$base_tables = $view
->getBaseTables();
$this
->assertArrayHasKey('users_field_data', $base_tables);
$this
->assertArrayNotHasKey('users', $base_tables);
$this
->assertArrayHasKey('watchdog', $base_tables);
}
public function testFiltering() {
$this->container
->get('database')
->truncate('watchdog')
->execute();
$this
->createLogEntries();
$view = Views::getView('dblog_integration_test');
$filters = [
'severity' => [
'id' => 'severity',
'table' => 'watchdog',
'field' => 'severity',
'relationship' => 'none',
'group_type' => 'group',
'admin_label' => '',
'operator' => 'in',
'value' => [
RfcLogLevel::WARNING,
],
'group' => 1,
'exposed' => FALSE,
'plugin_id' => 'in_operator',
],
];
$view
->setDisplay('page_1');
$view->displayHandlers
->get('page_1')
->overrideOption('filters', $filters);
$view
->save();
$this
->executeView($view);
$resultset = [
[
'message' => 'Warning message',
],
];
$this
->assertIdenticalResultset($view, $resultset, $this->columnMap);
$view = Views::getView('dblog_integration_test');
$filters = [
'type' => [
'id' => 'type',
'table' => 'watchdog',
'field' => 'type',
'relationship' => 'none',
'group_type' => 'group',
'admin_label' => '',
'operator' => 'in',
'value' => [
'my-module' => 'my-module',
],
'group' => '1',
'exposed' => FALSE,
'is_grouped' => FALSE,
'plugin_id' => 'dblog_types',
],
];
$view
->setDisplay('page_1');
$view->displayHandlers
->get('page_1')
->overrideOption('filters', $filters);
$view
->save();
$this
->executeView($view);
$resultset = [
[
'message' => 'My module message',
],
];
$this
->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
protected function createLogEntries() {
$entries = [];
$entries[] = [
'message' => $this
->randomMachineName(),
'variables' => [
'link' => Link::fromTextAndUrl('Link', Url::fromRoute('<front>'))
->toString(),
],
];
$entries[] = [
'message' => '@token1',
'variables' => [
'@token1' => $this
->randomMachineName(),
'link' => Link::fromTextAndUrl('Link', Url::fromRoute('<front>'))
->toString(),
],
];
$entries[] = [
'message' => '@token1 @token2',
'variables' => [
'@token1' => $this
->randomMachineName(),
'@token2' => $this
->randomMachineName(),
'link' => '<a href="' . Url::fromRoute('<front>')
->toString() . '"><object>Link</object></a>',
],
];
$entries[] = [
'message' => 'Warning message',
'severity' => RfcLogLevel::WARNING,
];
$entries[] = [
'message' => 'My module message',
'severity' => RfcLogLevel::INFO,
'type' => 'my-module',
];
$logger_factory = $this->container
->get('logger.factory');
foreach ($entries as $entry) {
$entry += [
'type' => 'test-views',
'severity' => RfcLogLevel::NOTICE,
'variables' => [],
];
$logger_factory
->get($entry['type'])
->log($entry['severity'], $entry['message'], $entry['variables']);
}
return $entries;
}
}