3 namespace Drupal\Tests\views\Functional\Entity;
5 use Drupal\node\Entity\Node;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7 use Drupal\views\ViewExecutable;
8 use Drupal\views\Views;
11 * Tests the 'Latest revision' filter.
15 class LatestRevisionFilterTest extends ViewTestBase {
18 * An array of node revisions.
20 * @var \Drupal\node\NodeInterface[]
22 protected $allRevisions = [];
25 * An array of node revisions.
27 * @var \Drupal\node\NodeInterface[]
29 protected $latestRevisions = [];
32 * Views used by this test.
36 public static $testViews = ['test_latest_revision_filter'];
43 public static $modules = ['node'];
48 protected function setUp($import_test_views = TRUE) {
51 $this->drupalCreateContentType(['type' => 'article']);
53 // Create a node that goes through various default/pending revision stages.
54 $node = Node::create([
55 'title' => 'First node - v1 - default',
59 $this->allRevisions[$node->getRevisionId()] = $node;
61 $node->setTitle('First node - v2 - pending');
62 $node->setNewRevision(TRUE);
63 $node->isDefaultRevision(FALSE);
65 $this->allRevisions[$node->getRevisionId()] = $node;
67 $node->setTitle('First node - v3 - default');
68 $node->setNewRevision(TRUE);
69 $node->isDefaultRevision(TRUE);
71 $this->allRevisions[$node->getRevisionId()] = $node;
73 $node->setTitle('First node - v4 - pending');
74 $node->setNewRevision(TRUE);
75 $node->isDefaultRevision(TRUE);
77 $this->allRevisions[$node->getRevisionId()] = $node;
78 $this->latestRevisions[$node->getRevisionId()] = $node;
80 // Create a node that has a default and a pending revision.
81 $node = Node::create([
82 'title' => 'Second node - v1 - default',
86 $this->allRevisions[$node->getRevisionId()] = $node;
88 $node->setTitle('Second node - v2 - pending');
89 $node->setNewRevision(TRUE);
90 $node->isDefaultRevision(FALSE);
92 $this->allRevisions[$node->getRevisionId()] = $node;
93 $this->latestRevisions[$node->getRevisionId()] = $node;
95 // Create a node that only has a default revision.
96 $node = Node::create([
97 'title' => 'Third node - v1 - default',
101 $this->allRevisions[$node->getRevisionId()] = $node;
102 $this->latestRevisions[$node->getRevisionId()] = $node;
104 // Create a node that only has a pending revision.
105 $node = Node::create([
106 'title' => 'Fourth node - v1 - pending',
109 $node->isDefaultRevision(FALSE);
111 $this->allRevisions[$node->getRevisionId()] = $node;
112 $this->latestRevisions[$node->getRevisionId()] = $node;
116 * Tests the 'Latest revision' filter.
118 public function testLatestRevisionFilter() {
119 $view = Views::getView('test_latest_revision_filter');
121 $this->executeView($view);
123 // Check that we have all the results.
124 $this->assertCount(count($this->latestRevisions), $view->result);
126 $expected = $not_expected = [];
127 foreach ($this->allRevisions as $revision_id => $revision) {
128 if (isset($this->latestRevisions[$revision_id])) {
130 'vid' => $revision_id,
131 'title' => $revision->label(),
135 $not_expected[] = $revision_id;
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.');
144 * Verifies that a list of revision IDs are not in the result.
146 * @param \Drupal\views\ViewExecutable $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.
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);
157 $this->assertFalse($found_revision_ids, $message);