You are here

public function HistoryTimestampTest::testHandlers in Zircon Profile 8

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

Tests the handlers.

File

core/modules/history/src/Tests/Views/HistoryTimestampTest.php, line 39
Contains \Drupal\history\Tests\Views\HistoryTimestampTest.

Class

HistoryTimestampTest
Tests the history timestamp handlers.

Namespace

Drupal\history\Tests\Views

Code

public function testHandlers() {
  $nodes = array();
  $nodes[] = $this
    ->drupalCreateNode();
  $nodes[] = $this
    ->drupalCreateNode();
  $account = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($account);
  \Drupal::currentUser()
    ->setAccount($account);
  db_insert('history')
    ->fields(array(
    'uid' => $account
      ->id(),
    'nid' => $nodes[0]
      ->id(),
    'timestamp' => REQUEST_TIME - 100,
  ))
    ->execute();
  db_insert('history')
    ->fields(array(
    'uid' => $account
      ->id(),
    'nid' => $nodes[1]
      ->id(),
    'timestamp' => REQUEST_TIME + 100,
  ))
    ->execute();
  $column_map = array(
    'nid' => 'nid',
  );

  // Test the history field.
  $view = Views::getView('test_history');
  $view
    ->setDisplay('page_1');
  $this
    ->executeView($view);
  $this
    ->assertEqual(count($view->result), 2);
  $output = $view
    ->preview();
  $this
    ->setRawContent(\Drupal::service('renderer')
    ->renderRoot($output));
  $result = $this
    ->xpath('//span[@class=:class]', array(
    ':class' => 'marker',
  ));
  $this
    ->assertEqual(count($result), 1, 'Just one node is marked as new');

  // Test the history filter.
  $view = Views::getView('test_history');
  $view
    ->setDisplay('page_2');
  $this
    ->executeView($view);
  $this
    ->assertEqual(count($view->result), 1);
  $this
    ->assertIdenticalResultset($view, array(
    array(
      '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);
}