function ArgumentDefaultTest::testArgumentDefaultNode

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Functional/Plugin/ArgumentDefaultTest.php \Drupal\Tests\views\Functional\Plugin\ArgumentDefaultTest::testArgumentDefaultNode()
  2. 8.9.x core/modules/views/tests/src/Functional/Plugin/ArgumentDefaultTest.php \Drupal\Tests\views\Functional\Plugin\ArgumentDefaultTest::testArgumentDefaultNode()
  3. 11.x core/modules/views/tests/src/Functional/Plugin/ArgumentDefaultTest.php \Drupal\Tests\views\Functional\Plugin\ArgumentDefaultTest::testArgumentDefaultNode()

Tests node default argument.

File

core/modules/views/tests/src/Functional/Plugin/ArgumentDefaultTest.php, line 142

Class

ArgumentDefaultTest
Tests pluggable argument_default for views.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testArgumentDefaultNode() : void {
    // Create a user that has permission to place a view block.
    $permissions = [
        'administer views',
        'administer blocks',
        'bypass node access',
        'access user profiles',
        'view all revisions',
    ];
    $views_admin = $this->drupalCreateUser($permissions);
    $this->drupalLogin($views_admin);
    // Create nodes where should show themselves again as view block.
    $node_type = NodeType::create([
        'type' => 'page',
        'name' => 'Page',
    ]);
    $node_type->save();
    $node1 = Node::create([
        'title' => 'Test node 1',
        'type' => 'page',
    ]);
    $node1->save();
    $node2 = Node::create([
        'title' => 'Test node 2',
        'type' => 'page',
    ]);
    $node2->save();
    // Place the block, visit the pages that display the block, and check that
    // the nodes we expect appear in the respective pages.
    $id = 'view-block-id';
    $this->drupalPlaceBlock("views_block:test_argument_default_node-block_1", [
        'id' => 'view_block_id',
    ]);
    $this->drupalGet('node/' . $node1->id());
    $this->assertSession()
        ->elementTextContains('xpath', '//*[@id="block-' . $id . '"]', $node1->getTitle());
    $this->drupalGet('node/' . $node2->id());
    $this->assertSession()
        ->elementTextContains('xpath', '//*[@id="block-' . $id . '"]', $node2->getTitle());
    // Check the view from node preview page.
    $node3 = $this->drupalCreateNode([
        'title' => 'Title 1',
        'type' => 'page',
    ]);
    $this->drupalGet($node3->toUrl('edit-form'));
    $this->submitForm([
        'title[0][value]' => 'Title 2',
    ], 'Preview');
    $this->assertSession()
        ->elementTextContains('xpath', '//*[@id="block-' . $id . '"]', $node3->getTitle());
}

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