Skip to content
Snippets Groups Projects
Unverified Commit 1cb3315f authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3041786 by claudiu.cristea, Lendude: Convert LatestRevisionFilterTest to a kernel test

parent d88a43d4
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
<?php
namespace Drupal\Tests\views\Functional\Entity;
namespace Drupal\Tests\views\Kernel\Entity;
use Drupal\node\Entity\Node;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
......@@ -12,43 +13,26 @@
*
* @group views
*/
class LatestRevisionFilterTest extends ViewTestBase {
class LatestRevisionFilterTest extends ViewsKernelTestBase {
/**
* An array of node revisions.
*
* @var \Drupal\node\NodeInterface[]
*/
protected $allRevisions = [];
/**
* An array of node revisions.
*
* @var \Drupal\node\NodeInterface[]
*/
protected $latestRevisions = [];
/**
* Views used by this test.
*
* @var array
* {@inheritdoc}
*/
public static $testViews = ['test_latest_revision_filter'];
/**
* Modules to enable.
*
* @var array
* {@inheritdoc}
*/
public static $modules = ['node'];
/**
* {@inheritdoc}
* Tests the 'Latest revision' filter.
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp();
$this->drupalCreateContentType(['type' => 'article']);
public function testLatestRevisionFilter() {
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installSchema('node', ['node_access']);
NodeType::create(['type' => 'article'])->save();
// Create a node that goes through various default/pending revision stages.
$node = Node::create([
......@@ -56,26 +40,26 @@ protected function setUp($import_test_views = TRUE) {
'type' => 'article',
]);
$node->save();
$this->allRevisions[$node->getRevisionId()] = $node;
$all_revisions[$node->getRevisionId()] = $node;
$node->setTitle('First node - v2 - pending');
$node->setNewRevision(TRUE);
$node->isDefaultRevision(FALSE);
$node->save();
$this->allRevisions[$node->getRevisionId()] = $node;
$all_revisions[$node->getRevisionId()] = $node;
$node->setTitle('First node - v3 - default');
$node->setNewRevision(TRUE);
$node->isDefaultRevision(TRUE);
$node->save();
$this->allRevisions[$node->getRevisionId()] = $node;
$all_revisions[$node->getRevisionId()] = $node;
$node->setTitle('First node - v4 - pending');
$node->setNewRevision(TRUE);
$node->isDefaultRevision(TRUE);
$node->save();
$this->allRevisions[$node->getRevisionId()] = $node;
$this->latestRevisions[$node->getRevisionId()] = $node;
$all_revisions[$node->getRevisionId()] = $node;
$latest_revisions[$node->getRevisionId()] = $node;
// Create a node that has a default and a pending revision.
$node = Node::create([
......@@ -83,14 +67,14 @@ protected function setUp($import_test_views = TRUE) {
'type' => 'article',
]);
$node->save();
$this->allRevisions[$node->getRevisionId()] = $node;
$all_revisions[$node->getRevisionId()] = $node;
$node->setTitle('Second node - v2 - pending');
$node->setNewRevision(TRUE);
$node->isDefaultRevision(FALSE);
$node->save();
$this->allRevisions[$node->getRevisionId()] = $node;
$this->latestRevisions[$node->getRevisionId()] = $node;
$all_revisions[$node->getRevisionId()] = $node;
$latest_revisions[$node->getRevisionId()] = $node;
// Create a node that only has a default revision.
$node = Node::create([
......@@ -98,8 +82,8 @@ protected function setUp($import_test_views = TRUE) {
'type' => 'article',
]);
$node->save();
$this->allRevisions[$node->getRevisionId()] = $node;
$this->latestRevisions[$node->getRevisionId()] = $node;
$all_revisions[$node->getRevisionId()] = $node;
$latest_revisions[$node->getRevisionId()] = $node;
// Create a node that only has a pending revision.
$node = Node::create([
......@@ -108,24 +92,19 @@ protected function setUp($import_test_views = TRUE) {
]);
$node->isDefaultRevision(FALSE);
$node->save();
$this->allRevisions[$node->getRevisionId()] = $node;
$this->latestRevisions[$node->getRevisionId()] = $node;
}
$all_revisions[$node->getRevisionId()] = $node;
$latest_revisions[$node->getRevisionId()] = $node;
/**
* Tests the 'Latest revision' filter.
*/
public function testLatestRevisionFilter() {
$view = Views::getView('test_latest_revision_filter');
$this->executeView($view);
// Check that we have all the results.
$this->assertCount(count($this->latestRevisions), $view->result);
$this->assertCount(count($latest_revisions), $view->result);
$expected = $not_expected = [];
foreach ($this->allRevisions as $revision_id => $revision) {
if (isset($this->latestRevisions[$revision_id])) {
foreach ($all_revisions as $revision_id => $revision) {
if (isset($latest_revisions[$revision_id])) {
$expected[] = [
'vid' => $revision_id,
'title' => $revision->label(),
......@@ -136,25 +115,23 @@ public function testLatestRevisionFilter() {
}
}
$this->assertIdenticalResultset($view, $expected, ['vid' => 'vid', 'title' => 'title'], 'The test view only shows the latest revisions.');
$this->assertNotInResultSet($view, $not_expected, 'Non-latest revisions are not shown by the view.');
$this->assertNotInResultSet($view, $not_expected);
$view->destroy();
}
/**
* Verifies that a list of revision IDs are not in the result.
* Asserts that a list of revision IDs are not in the result.
*
* @param \Drupal\views\ViewExecutable $view
* An executed View.
* @param array $not_expected_revision_ids
* An array of revision IDs which should not be part of the result set.
* @param string $message
* (optional) A custom message to display with the assertion.
*/
protected function assertNotInResultSet(ViewExecutable $view, array $not_expected_revision_ids, $message = '') {
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->assertFalse($found_revision_ids, $message);
$this->assertFalse($found_revision_ids);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment