You are here

public function HistoryTimestampTest::testHandlers in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/history/tests/src/Kernel/Views/HistoryTimestampTest.php \Drupal\Tests\history\Kernel\Views\HistoryTimestampTest::testHandlers()
  2. 9 core/modules/history/tests/src/Kernel/Views/HistoryTimestampTest.php \Drupal\Tests\history\Kernel\Views\HistoryTimestampTest::testHandlers()

Tests the handlers.

File

core/modules/history/tests/src/Kernel/Views/HistoryTimestampTest.php, line 52

Class

HistoryTimestampTest
Tests the history timestamp handlers.

Namespace

Drupal\Tests\history\Kernel\Views

Code

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);
}