LatestRevisionFilterTest.php in Drupal 9
File
core/modules/views/tests/src/Kernel/Entity/LatestRevisionFilterTest.php
View source
<?php
namespace Drupal\Tests\views\Kernel\Entity;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
class LatestRevisionFilterTest extends ViewsKernelTestBase {
public static $testViews = [
'test_latest_revision_filter',
];
protected static $modules = [
'node',
];
public function testLatestRevisionFilter() {
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installSchema('node', [
'node_access',
]);
NodeType::create([
'type' => 'article',
])
->save();
$node = Node::create([
'title' => 'First node - v1 - default',
'type' => 'article',
]);
$node
->save();
$all_revisions[$node
->getRevisionId()] = $node;
$node
->setTitle('First node - v2 - pending');
$node
->setNewRevision(TRUE);
$node
->isDefaultRevision(FALSE);
$node
->save();
$all_revisions[$node
->getRevisionId()] = $node;
$node
->setTitle('First node - v3 - default');
$node
->setNewRevision(TRUE);
$node
->isDefaultRevision(TRUE);
$node
->save();
$all_revisions[$node
->getRevisionId()] = $node;
$node
->setTitle('First node - v4 - pending');
$node
->setNewRevision(TRUE);
$node
->isDefaultRevision(TRUE);
$node
->save();
$all_revisions[$node
->getRevisionId()] = $node;
$latest_revisions[$node
->getRevisionId()] = $node;
$node = Node::create([
'title' => 'Second node - v1 - default',
'type' => 'article',
]);
$node
->save();
$all_revisions[$node
->getRevisionId()] = $node;
$node
->setTitle('Second node - v2 - pending');
$node
->setNewRevision(TRUE);
$node
->isDefaultRevision(FALSE);
$node
->save();
$all_revisions[$node
->getRevisionId()] = $node;
$latest_revisions[$node
->getRevisionId()] = $node;
$node = Node::create([
'title' => 'Third node - v1 - default',
'type' => 'article',
]);
$node
->save();
$all_revisions[$node
->getRevisionId()] = $node;
$latest_revisions[$node
->getRevisionId()] = $node;
$node = Node::create([
'title' => 'Fourth node - v1 - pending',
'type' => 'article',
]);
$node
->isDefaultRevision(FALSE);
$node
->save();
$all_revisions[$node
->getRevisionId()] = $node;
$latest_revisions[$node
->getRevisionId()] = $node;
$view = Views::getView('test_latest_revision_filter');
$this
->executeView($view);
$this
->assertSameSize($latest_revisions, $view->result);
$expected = $not_expected = [];
foreach ($all_revisions as $revision_id => $revision) {
if (isset($latest_revisions[$revision_id])) {
$expected[] = [
'vid' => $revision_id,
'title' => $revision
->label(),
];
}
else {
$not_expected[] = $revision_id;
}
}
$this
->assertIdenticalResultset($view, $expected, [
'vid' => 'vid',
'title' => 'title',
], 'The test view only shows the latest revisions.');
$this
->assertNotInResultSet($view, $not_expected);
$view
->destroy();
}
protected function assertNotInResultSet(ViewExecutable $view, array $not_expected_revision_ids) {
$found_revision_ids = array_filter($view->result, function ($row) use ($not_expected_revision_ids) {
return in_array($row->vid, $not_expected_revision_ids);
});
$this
->assertEmpty($found_revision_ids);
}
}