You are here

public function DisplayBlockTest::testBlockContextualLinks in Drupal 9

Same name and namespace in other branches
  1. 8 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 373

Class

DisplayBlockTest
Tests the block display plugin.

Namespace

Drupal\Tests\block\Functional\Views

Code

public function testBlockContextualLinks() {
  $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,
  ]) . '></div>');
  $this
    ->assertSession()
    ->responseContains('<div' . new Attribute([
    'data-contextual-id' => $cached_id,
    'data-contextual-token' => $cached_id_token,
  ]) . '></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 class="block-configure"><a href="' . base_path() . 'admin/structure/block/manage/' . $block
    ->id() . '">Configure block</a></li><li class="block-remove"><a href="' . base_path() . 'admin/structure/block/manage/' . $block
    ->id() . '/delete">Remove block</a></li><li class="entityviewedit-form"><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 class="block-configure"><a href="' . base_path() . 'admin/structure/block/manage/' . $cached_block
    ->id() . '">Configure block</a></li><li class="block-remove"><a href="' . base_path() . 'admin/structure/block/manage/' . $cached_block
    ->id() . '/delete">Remove block</a></li><li class="entityviewedit-form"><a href="' . base_path() . 'admin/structure/views/view/test_view_block/edit/block_1">Edit view</a></li></ul>', $json[$cached_id]);
}