function DisplayBlockTest::testBlockContextualLinks

Same name in other branches
  1. 9 core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php \Drupal\Tests\block\Functional\Views\DisplayBlockTest::testBlockContextualLinks()
  2. 8.9.x core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php \Drupal\Tests\block\Functional\Views\DisplayBlockTest::testBlockContextualLinks()
  3. 11.x core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php \Drupal\Tests\block\Functional\Views\DisplayBlockTest::testBlockContextualLinks()

Tests the contextual links on a Views block.

File

core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php, line 380

Class

DisplayBlockTest
Tests the block display plugin.

Namespace

Drupal\Tests\block\Functional\Views

Code

public function testBlockContextualLinks() : void {
    $this->drupalLogin($this->drupalCreateUser([
        'administer views',
        'access contextual links',
        'administer blocks',
    ]));
    $block = $this->drupalPlaceBlock('views_block:test_view_block-block_1');
    $cached_block = $this->drupalPlaceBlock('views_block:test_view_block-block_1');
    $this->drupalGet('test-page');
    $id = 'block:block=' . $block->id() . ':langcode=en|entity.view.edit_form:view=test_view_block:location=block&name=test_view_block&display_id=block_1&langcode=en';
    $id_token = Crypt::hmacBase64($id, Settings::getHashSalt() . $this->container
        ->get('private_key')
        ->get());
    $cached_id = 'block:block=' . $cached_block->id() . ':langcode=en|entity.view.edit_form:view=test_view_block:location=block&name=test_view_block&display_id=block_1&langcode=en';
    $cached_id_token = Crypt::hmacBase64($cached_id, Settings::getHashSalt() . $this->container
        ->get('private_key')
        ->get());
    // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:assertContextualLinkPlaceHolder()
    // Check existence of the contextual link placeholders.
    $this->assertSession()
        ->responseContains('<div' . new Attribute([
        'data-contextual-id' => $id,
        'data-contextual-token' => $id_token,
        'data-drupal-ajax-container' => '',
    ]) . '></div>');
    $this->assertSession()
        ->responseContains('<div' . new Attribute([
        'data-contextual-id' => $cached_id,
        'data-contextual-token' => $cached_id_token,
        'data-drupal-ajax-container' => '',
    ]) . '></div>');
    // Get server-rendered contextual links.
    // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:renderContextualLinks()
    $post = [
        'ids[0]' => $id,
        'ids[1]' => $cached_id,
        'tokens[0]' => $id_token,
        'tokens[1]' => $cached_id_token,
    ];
    $url = 'contextual/render?_format=json,destination=test-page';
    $this->getSession()
        ->getDriver()
        ->getClient()
        ->request('POST', $url, $post);
    $this->assertSession()
        ->statusCodeEquals(200);
    $json = Json::decode($this->getSession()
        ->getPage()
        ->getContent());
    $this->assertSame('<ul class="contextual-links"><li><a href="' . base_path() . 'admin/structure/block/manage/' . $block->id() . '">Configure block</a></li><li><a href="' . base_path() . 'admin/structure/block/manage/' . $block->id() . '/delete">Remove block</a></li><li><a href="' . base_path() . 'admin/structure/views/view/test_view_block/edit/block_1">Edit view</a></li></ul>', $json[$id]);
    $this->assertSame('<ul class="contextual-links"><li><a href="' . base_path() . 'admin/structure/block/manage/' . $cached_block->id() . '">Configure block</a></li><li><a href="' . base_path() . 'admin/structure/block/manage/' . $cached_block->id() . '/delete">Remove block</a></li><li><a href="' . base_path() . 'admin/structure/views/view/test_view_block/edit/block_1">Edit view</a></li></ul>', $json[$cached_id]);
}

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