You are here

public function PreviewLinkAccessTest::testPreviewFakeToken in Preview Link 8

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

Test access with tokens.

File

tests/src/Functional/PreviewLinkAccessTest.php, line 34

Class

PreviewLinkAccessTest
Test access to preview pages with valid/invalid tokens.

Namespace

Drupal\Tests\preview_link\Functional

Code

public function testPreviewFakeToken() {
  $account = $this
    ->createUser([
    'view test entity',
  ]);
  $this
    ->drupalLogin($account);
  $entity = EntityTestRev::create();
  $entity
    ->save();
  $access = $entity
    ->access('view', $account, TRUE);

  // Make sure the current user has access to the entity.
  $this
    ->assertTrue($access
    ->isAllowed());

  // The entity needs a preview link otherwise the access checker quits early.
  $this
    ->getNewPreviewLinkForEntity($entity);

  // Create a temporary preview link entity to utilize whichever token
  // generation process is in use.
  $token = PreviewLink::create()
    ->getToken();

  // Make sure the token is set.
  $this
    ->assertIsString($token);
  $this
    ->assertTrue(strlen($token) > 0);
  $url = Url::fromRoute('entity.entity_test_rev.preview_link', [
    'entity_test_rev' => $entity
      ->id(),
    'preview_token' => $token,
  ]);
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(403);
}