You are here

public function NidArgumentTest::testNidArgument in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/Kernel/Views/NidArgumentTest.php \Drupal\Tests\node\Kernel\Views\NidArgumentTest::testNidArgument()

Tests the nid argument.

File

core/modules/node/tests/src/Kernel/Views/NidArgumentTest.php, line 53

Class

NidArgumentTest
Tests the nid argument handler.

Namespace

Drupal\Tests\node\Kernel\Views

Code

public function testNidArgument() {
  $view = Views::getView('test_nid_argument');
  $view
    ->setDisplay();
  $node1 = Node::create([
    'type' => 'default',
    'title' => $this
      ->randomMachineName(),
  ]);
  $node1
    ->save();
  $node2 = Node::create([
    'type' => 'default',
    'title' => $this
      ->randomMachineName(),
  ]);
  $node2
    ->save();
  $view
    ->preview();
  $this
    ->assertCount(2, $view->result, 'Found the expected number of results.');

  // Set the second node id as an argument.
  $view
    ->destroy();
  $view
    ->preview('default', [
    $node2
      ->id(),
  ]);

  // Verify that the title is overridden.
  $this
    ->assertEquals($node2
    ->getTitle(), $view
    ->getTitle());

  // Verify that the argument filtering works.
  $this
    ->assertCount(1, $view->result, 'Found the expected number of results.');
  $this
    ->assertEquals($node2
    ->id(), (string) $view->style_plugin
    ->getField(0, 'nid'), 'Found the correct nid.');

  // Verify that setting a non-existing id as argument results in no nodes
  // being shown.
  $view
    ->destroy();
  $view
    ->preview('default', [
    22,
  ]);
  $this
    ->assertCount(0, $view->result, 'Found the expected number of results.');
}