You are here

public function DisplayBlockTest::testBlockContextualLinks in Drupal 8

Same name and namespace in other branches
  1. 9 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()
  $this
    ->assertRaw('<div' . new Attribute([
    'data-contextual-id' => $id,
    'data-contextual-token' => $id_token,
  ]) . '></div>', new FormattableMarkup('Contextual link placeholder with id @id exists.', [
    '@id' => $id,
  ]));
  $this
    ->assertRaw('<div' . new Attribute([
    'data-contextual-id' => $cached_id,
    'data-contextual-token' => $cached_id_token,
  ]) . '></div>', new FormattableMarkup('Contextual link placeholder with id @id exists.', [
    '@id' => $cached_id,
  ]));

  // 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
    ->assertIdentical($json[$id], '<ul class="contextual-links"><li class="block-configure"><a href="' . base_path() . 'admin/structure/block/manage/' . $block
    ->id() . '">Configure 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>');
  $this
    ->assertIdentical($json[$cached_id], '<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="entityviewedit-form"><a href="' . base_path() . 'admin/structure/views/view/test_view_block/edit/block_1">Edit view</a></li></ul>');
}