476653839285115b2b42d7b965af3dbef47b0f74
[yaffs-website] / web / core / modules / history / src / Tests / Views / HistoryTimestampTest.php
1 <?php
2
3 namespace Drupal\history\Tests\Views;
4
5 use Drupal\views\Views;
6 use Drupal\views\Tests\ViewTestBase;
7
8 /**
9  * Tests the history timestamp handlers.
10  *
11  * @group history
12  * @see \Drupal\history\Plugin\views\field\HistoryTimestamp.
13  * @see \Drupal\history\Plugin\views\filter\HistoryTimestamp.
14  */
15 class HistoryTimestampTest extends ViewTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['history', 'node'];
23
24   /**
25    * Views used by this test.
26    *
27    * @var array
28    */
29   public static $testViews = ['test_history'];
30
31   /**
32    * Tests the handlers.
33    */
34   public function testHandlers() {
35     $nodes = [];
36     $nodes[] = $this->drupalCreateNode();
37     $nodes[] = $this->drupalCreateNode();
38
39     $account = $this->drupalCreateUser();
40     $this->drupalLogin($account);
41     \Drupal::currentUser()->setAccount($account);
42
43     db_insert('history')
44       ->fields([
45         'uid' => $account->id(),
46         'nid' => $nodes[0]->id(),
47         'timestamp' => REQUEST_TIME - 100,
48       ])->execute();
49
50     db_insert('history')
51       ->fields([
52         'uid' => $account->id(),
53         'nid' => $nodes[1]->id(),
54         'timestamp' => REQUEST_TIME + 100,
55       ])->execute();
56
57
58     $column_map = [
59       'nid' => 'nid',
60     ];
61
62     // Test the history field.
63     $view = Views::getView('test_history');
64     $view->setDisplay('page_1');
65     $this->executeView($view);
66     $this->assertEqual(count($view->result), 2);
67     $output = $view->preview();
68     $this->setRawContent(\Drupal::service('renderer')->renderRoot($output));
69     $result = $this->xpath('//span[@class=:class]', [':class' => 'marker']);
70     $this->assertEqual(count($result), 1, 'Just one node is marked as new');
71
72     // Test the history filter.
73     $view = Views::getView('test_history');
74     $view->setDisplay('page_2');
75     $this->executeView($view);
76     $this->assertEqual(count($view->result), 1);
77     $this->assertIdenticalResultset($view, [['nid' => $nodes[0]->id()]], $column_map);
78
79     // Install Comment module and make sure that content types without comment
80     // field will not break the view.
81     // See \Drupal\history\Plugin\views\filter\HistoryUserTimestamp::query()
82     \Drupal::service('module_installer')->install(['comment']);
83     $view = Views::getView('test_history');
84     $view->setDisplay('page_2');
85     $this->executeView($view);
86
87   }
88
89 }