View source
<?php
namespace Drupal\Tests\history\Kernel\Views;
use Drupal\Core\Database\Database;
use Drupal\node\Entity\Node;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\user\Entity\User;
use Drupal\views\Views;
class HistoryTimestampTest extends ViewsKernelTestBase {
protected static $modules = [
'history',
'node',
];
public static $testViews = [
'test_history',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installSchema('history', [
'history',
]);
\Drupal::service('theme_installer')
->install([
'classy',
]);
\Drupal::theme()
->setActiveTheme(\Drupal::service('theme.initialization')
->initTheme('classy'));
}
public function testHandlers() {
$nodes = [];
$node = Node::create([
'title' => 'n1',
'type' => 'default',
]);
$node
->save();
$nodes[] = $node;
$node = Node::create([
'title' => 'n2',
'type' => 'default',
]);
$node
->save();
$nodes[] = $node;
$account = User::create([
'name' => 'admin',
]);
$account
->save();
\Drupal::currentUser()
->setAccount($account);
$connection = Database::getConnection();
$connection
->insert('history')
->fields([
'uid' => $account
->id(),
'nid' => $nodes[0]
->id(),
'timestamp' => REQUEST_TIME - 100,
])
->execute();
$connection
->insert('history')
->fields([
'uid' => $account
->id(),
'nid' => $nodes[1]
->id(),
'timestamp' => REQUEST_TIME + 100,
])
->execute();
$column_map = [
'nid' => 'nid',
];
$view = Views::getView('test_history');
$view
->setDisplay('page_1');
$this
->executeView($view);
$this
->assertCount(2, $view->result);
$output = $view
->preview();
$this
->setRawContent(\Drupal::service('renderer')
->renderRoot($output));
$result = $this
->xpath('//span[@class=:class]', [
':class' => 'marker',
]);
$this
->assertCount(1, $result, 'Just one node is marked as new');
$view = Views::getView('test_history');
$view
->setDisplay('page_2');
$this
->executeView($view);
$this
->assertCount(1, $view->result);
$this
->assertIdenticalResultset($view, [
[
'nid' => $nodes[0]
->id(),
],
], $column_map);
\Drupal::service('module_installer')
->install([
'comment',
]);
$view = Views::getView('test_history');
$view
->setDisplay('page_2');
$this
->executeView($view);
}
}