class HistoryTimestampTest
Same name and namespace in other branches
- 11.x core/modules/history/tests/src/Kernel/Views/HistoryTimestampTest.php \Drupal\Tests\history\Kernel\Views\HistoryTimestampTest
Tests the history timestamp handlers.
@group history
Hierarchy
- class \Drupal\KernelTests\KernelTestBase extends \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\Tests\views\Kernel\ViewsKernelTestBase uses \Drupal\views\Tests\ViewResultAssertionTrait implements \Drupal\KernelTests\KernelTestBase
- class \Drupal\Tests\history\Kernel\Views\HistoryTimestampTest implements \Drupal\Tests\views\Kernel\ViewsKernelTestBase
- class \Drupal\Tests\views\Kernel\ViewsKernelTestBase uses \Drupal\views\Tests\ViewResultAssertionTrait implements \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of HistoryTimestampTest
See also
\Drupal\history\Plugin\views\field\HistoryUserTimestamp
\Drupal\history\Plugin\views\filter\HistoryUserTimestamp
File
-
core/
modules/ history/ tests/ src/ Kernel/ Views/ HistoryTimestampTest.php, line 18
Namespace
Drupal\Tests\history\Kernel\ViewsView source
class HistoryTimestampTest extends ViewsKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'history',
'node',
];
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = [
'test_history',
];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this->installEntitySchema('node');
$this->installEntitySchema('user');
$this->installSchema('history', [
'history',
]);
// Use history_test_theme because its marker is wrapped in a span so it can
// be easily targeted with xpath.
\Drupal::service('theme_installer')->install([
'history_test_theme',
]);
\Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('history_test_theme'));
}
/**
* Tests the handlers.
*/
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',
];
// Test the history field.
$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');
// Test the history filter.
$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);
// Install Comment module and make sure that content types without comment
// field will not break the view.
// See \Drupal\history\Plugin\views\filter\HistoryUserTimestamp::query()
\Drupal::service('module_installer')->install([
'comment',
]);
$view = Views::getView('test_history');
$view->setDisplay('page_2');
$this->executeView($view);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.