You are here

public function PreviewLinkSessionTokenTest::testSessionTokenReclaimAttempt 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::testSessionTokenReclaimAttempt()

Tests trying to claim a token multiple times.

Tests that trying to re-claim a preview token doesnt return a cached response which doesnt end up claiming a token to the session.

File

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

Class

PreviewLinkSessionTokenTest
Tests tokens claimed against sessions.

Namespace

Drupal\Tests\preview_link\Functional

Code

public function testSessionTokenReclaimAttempt() : void {
  $entity = EntityTestRevPub::create();
  $entity
    ->save();
  $previewLink = PreviewLink::create()
    ->addEntity($entity);
  $previewLink
    ->save();
  $previewLinkUrl = Url::fromRoute('entity.entity_test_revpub.preview_link', [
    $entity
      ->getEntityTypeId() => $entity
      ->id(),
    'preview_token' => $previewLink
      ->getToken(),
  ]);
  $this
    ->drupalGet($previewLinkUrl);
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Should redirect to preview link.
  $this
    ->drupalGet($entity
    ->toUrl());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->addressEquals($previewLinkUrl);

  // Remove session tokens.
  $this
    ->drupalGet(Url::fromRoute('preview_link.session_tokens.remove'));
  $this
    ->assertSession()
    ->pageTextContains('Removed preview link tokens.');

  // Try to re-claim.
  // If this fails [with a 403] then something isnt allowing us to claim the
  // token to the session.
  $this
    ->drupalGet($previewLinkUrl);
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Should redirect to preview link again.
  $this
    ->drupalGet($entity
    ->toUrl());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->addressEquals($previewLinkUrl);
}