function LatestRevisionFilterTest::testLatestRevisionFilter

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/Entity/LatestRevisionFilterTest.php \Drupal\Tests\views\Kernel\Entity\LatestRevisionFilterTest::testLatestRevisionFilter()
  2. 10 core/modules/views/tests/src/Kernel/Entity/LatestRevisionFilterTest.php \Drupal\Tests\views\Kernel\Entity\LatestRevisionFilterTest::testLatestRevisionFilter()
  3. 11.x core/modules/views/tests/src/Kernel/Entity/LatestRevisionFilterTest.php \Drupal\Tests\views\Kernel\Entity\LatestRevisionFilterTest::testLatestRevisionFilter()

Tests the 'Latest revision' filter.

File

core/modules/views/tests/src/Kernel/Entity/LatestRevisionFilterTest.php, line 31

Class

LatestRevisionFilterTest
Tests the 'Latest revision' filter.

Namespace

Drupal\Tests\views\Kernel\Entity

Code

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([
        '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;
    // Create a node that has a default and a pending revision.
    $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;
    // Create a node that only has a default revision.
    $node = Node::create([
        'title' => 'Third node - v1 - default',
        'type' => 'article',
    ]);
    $node->save();
    $all_revisions[$node->getRevisionId()] = $node;
    $latest_revisions[$node->getRevisionId()] = $node;
    // Create a node that only has a pending revision.
    $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);
    // Check that we have all the results.
    $this->assertCount(count($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();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.