You are here

public function PreviewLinkSessionTokenTest::testSessionTokenUnclaimDestination in Preview Link 2.0.x

Same name and namespace in other branches
  1. 2.x tests/src/Functional/PreviewLinkSessionTokenTest.php \Drupal\Tests\preview_link\Functional\PreviewLinkSessionTokenTest::testSessionTokenUnclaimDestination()

Tests destination/redirect for unclaiming.

For when user has access to canonical route, without the token.

File

tests/src/Functional/PreviewLinkSessionTokenTest.php, line 169

Class

PreviewLinkSessionTokenTest
Tests tokens claimed against sessions.

Namespace

Drupal\Tests\preview_link\Functional

Code

public function testSessionTokenUnclaimDestination() : void {
  user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
    'view test entity' => TRUE,
  ]);
  $entity = EntityTestRevPub::create();

  // Must be published so session always has access.
  $entity
    ->setPublished();
  $entity
    ->save();

  // Make sure anon session can access canonical.
  $this
    ->drupalGet($entity
    ->toUrl());
  $previewLink = PreviewLink::create()
    ->addEntity($entity);
  $previewLink
    ->save();

  // Claim the token to the session.
  $previewLinkUrl = Url::fromRoute('entity.entity_test_revpub.preview_link', [
    $entity
      ->getEntityTypeId() => $entity
      ->id(),
    'preview_token' => $previewLink
      ->getToken(),
  ]);
  $this
    ->drupalGet($previewLinkUrl);
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Make the unclaim message appear by visiting the canonical page.
  $this
    ->drupalGet($entity
    ->toUrl());
  $this
    ->assertSession()
    ->pageTextContains('You are viewing this page because a preview link granted you access. Click here to remove token.');

  // Link should have the canonical URL as the destination.
  $this
    ->assertSession()
    ->linkByHrefExists(Url::fromRoute('preview_link.session_tokens.remove', [], [
    'query' => [
      'destination' => $entity
        ->toUrl()
        ->toString(),
    ],
  ])
    ->toString());
}