Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Functional / Entity / LatestRevisionFilterTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Entity;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7 use Drupal\views\ViewExecutable;
8 use Drupal\views\Views;
9
10 /**
11  * Tests the 'Latest revision' filter.
12  *
13  * @group views
14  */
15 class LatestRevisionFilterTest extends ViewTestBase {
16
17   /**
18    * An array of node revisions.
19    *
20    * @var \Drupal\node\NodeInterface[]
21    */
22   protected $allRevisions = [];
23
24   /**
25    * An array of node revisions.
26    *
27    * @var \Drupal\node\NodeInterface[]
28    */
29   protected $latestRevisions = [];
30
31   /**
32    * Views used by this test.
33    *
34    * @var array
35    */
36   public static $testViews = ['test_latest_revision_filter'];
37
38   /**
39    * Modules to enable.
40    *
41    * @var array
42    */
43   public static $modules = ['node'];
44
45   /**
46    * {@inheritdoc}
47    */
48   protected function setUp($import_test_views = TRUE) {
49     parent::setUp();
50
51     $this->drupalCreateContentType(['type' => 'article']);
52
53     // Create a node that goes through various default/pending revision stages.
54     $node = Node::create([
55       'title' => 'First node - v1 - default',
56       'type' => 'article',
57     ]);
58     $node->save();
59     $this->allRevisions[$node->getRevisionId()] = $node;
60
61     $node->setTitle('First node - v2 - pending');
62     $node->setNewRevision(TRUE);
63     $node->isDefaultRevision(FALSE);
64     $node->save();
65     $this->allRevisions[$node->getRevisionId()] = $node;
66
67     $node->setTitle('First node - v3 - default');
68     $node->setNewRevision(TRUE);
69     $node->isDefaultRevision(TRUE);
70     $node->save();
71     $this->allRevisions[$node->getRevisionId()] = $node;
72
73     $node->setTitle('First node - v4 - pending');
74     $node->setNewRevision(TRUE);
75     $node->isDefaultRevision(TRUE);
76     $node->save();
77     $this->allRevisions[$node->getRevisionId()] = $node;
78     $this->latestRevisions[$node->getRevisionId()] = $node;
79
80     // Create a node that has a default and a pending revision.
81     $node = Node::create([
82       'title' => 'Second node - v1 - default',
83       'type' => 'article',
84     ]);
85     $node->save();
86     $this->allRevisions[$node->getRevisionId()] = $node;
87
88     $node->setTitle('Second node - v2 - pending');
89     $node->setNewRevision(TRUE);
90     $node->isDefaultRevision(FALSE);
91     $node->save();
92     $this->allRevisions[$node->getRevisionId()] = $node;
93     $this->latestRevisions[$node->getRevisionId()] = $node;
94
95     // Create a node that only has a default revision.
96     $node = Node::create([
97       'title' => 'Third node - v1 - default',
98       'type' => 'article',
99     ]);
100     $node->save();
101     $this->allRevisions[$node->getRevisionId()] = $node;
102     $this->latestRevisions[$node->getRevisionId()] = $node;
103
104     // Create a node that only has a pending revision.
105     $node = Node::create([
106       'title' => 'Fourth node - v1 - pending',
107       'type' => 'article',
108     ]);
109     $node->isDefaultRevision(FALSE);
110     $node->save();
111     $this->allRevisions[$node->getRevisionId()] = $node;
112     $this->latestRevisions[$node->getRevisionId()] = $node;
113   }
114
115   /**
116    * Tests the 'Latest revision' filter.
117    */
118   public function testLatestRevisionFilter() {
119     $view = Views::getView('test_latest_revision_filter');
120
121     $this->executeView($view);
122
123     // Check that we have all the results.
124     $this->assertCount(count($this->latestRevisions), $view->result);
125
126     $expected = $not_expected = [];
127     foreach ($this->allRevisions as $revision_id => $revision) {
128       if (isset($this->latestRevisions[$revision_id])) {
129         $expected[] = [
130           'vid' => $revision_id,
131           'title' => $revision->label(),
132         ];
133       }
134       else {
135         $not_expected[] = $revision_id;
136       }
137     }
138     $this->assertIdenticalResultset($view, $expected, ['vid' => 'vid', 'title' => 'title'], 'The test view only shows the latest revisions.');
139     $this->assertNotInResultSet($view, $not_expected, 'Non-latest revisions are not shown by the view.');
140     $view->destroy();
141   }
142
143   /**
144    * Verifies that a list of revision IDs are not in the result.
145    *
146    * @param \Drupal\views\ViewExecutable $view
147    *   An executed View.
148    * @param array $not_expected_revision_ids
149    *   An array of revision IDs which should not be part of the result set.
150    * @param string $message
151    *   (optional) A custom message to display with the assertion.
152    */
153   protected function assertNotInResultSet(ViewExecutable $view, array $not_expected_revision_ids, $message = '') {
154     $found_revision_ids = array_filter($view->result, function ($row) use ($not_expected_revision_ids) {
155       return in_array($row->vid, $not_expected_revision_ids);
156     });
157     $this->assertFalse($found_revision_ids, $message);
158   }
159
160 }