Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / history / tests / src / Kernel / Views / HistoryTimestampTest.php
1 <?php
2
3 namespace Drupal\Tests\history\Kernel\Views;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
7 use Drupal\user\Entity\User;
8 use Drupal\views\Views;
9
10 /**
11  * Tests the history timestamp handlers.
12  *
13  * @group history
14  * @see \Drupal\history\Plugin\views\field\HistoryUserTimestamp
15  * @see \Drupal\history\Plugin\views\filter\HistoryUserTimestamp
16  */
17 class HistoryTimestampTest extends ViewsKernelTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['history', 'node'];
25
26   /**
27    * Views used by this test.
28    *
29    * @var array
30    */
31   public static $testViews = ['test_history'];
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp($import_test_views = TRUE) {
37     parent::setUp($import_test_views);
38
39     $this->installEntitySchema('node');
40     $this->installEntitySchema('user');
41     $this->installSchema('history', ['history']);
42     // Use classy theme because its marker is wrapped in a span so it can be
43     // easily targeted with xpath.
44     \Drupal::service('theme_handler')->install(['classy']);
45     \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('classy'));
46   }
47
48   /**
49    * Tests the handlers.
50    */
51   public function testHandlers() {
52     $nodes = [];
53     $node = Node::create([
54       'title' => 'n1',
55       'type' => 'default',
56     ]);
57     $node->save();
58     $nodes[] = $node;
59     $node = Node::create([
60       'title' => 'n2',
61       'type' => 'default',
62     ]);
63     $node->save();
64     $nodes[] = $node;
65
66     $account = User::create(['name' => 'admin']);
67     $account->save();
68     \Drupal::currentUser()->setAccount($account);
69
70     db_insert('history')
71       ->fields([
72         'uid' => $account->id(),
73         'nid' => $nodes[0]->id(),
74         'timestamp' => REQUEST_TIME - 100,
75       ])->execute();
76
77     db_insert('history')
78       ->fields([
79         'uid' => $account->id(),
80         'nid' => $nodes[1]->id(),
81         'timestamp' => REQUEST_TIME + 100,
82       ])->execute();
83
84     $column_map = [
85       'nid' => 'nid',
86     ];
87
88     // Test the history field.
89     $view = Views::getView('test_history');
90     $view->setDisplay('page_1');
91     $this->executeView($view);
92     $this->assertEqual(count($view->result), 2);
93     $output = $view->preview();
94     $this->setRawContent(\Drupal::service('renderer')->renderRoot($output));
95     $result = $this->xpath('//span[@class=:class]', [':class' => 'marker']);
96     $this->assertEqual(count($result), 1, 'Just one node is marked as new');
97
98     // Test the history filter.
99     $view = Views::getView('test_history');
100     $view->setDisplay('page_2');
101     $this->executeView($view);
102     $this->assertEqual(count($view->result), 1);
103     $this->assertIdenticalResultset($view, [['nid' => $nodes[0]->id()]], $column_map);
104
105     // Install Comment module and make sure that content types without comment
106     // field will not break the view.
107     // See \Drupal\history\Plugin\views\filter\HistoryUserTimestamp::query()
108     \Drupal::service('module_installer')->install(['comment']);
109     $view = Views::getView('test_history');
110     $view->setDisplay('page_2');
111     $this->executeView($view);
112   }
113
114 }