View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\preview_link\Functional;
use Drupal\Core\Logger\LogMessageParser;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTestMulRevPub;
use Drupal\entity_test\Entity\EntityTestRevPub;
use Drupal\preview_link\Entity\PreviewLink;
use Drupal\preview_link_test_time\TimeMachine;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\RoleInterface;
class PreviewLinkSessionTokenTest extends BrowserTestBase {
protected $defaultTheme = 'classy';
protected static $modules = [
'dynamic_entity_reference',
'preview_link',
'entity_test',
'preview_link_test',
'preview_link_test_time',
'block',
];
protected function setUp() {
parent::setUp();
$timeMachine = \Drupal::service('datetime.time');
assert($timeMachine instanceof TimeMachine);
$currentTime = new \DateTime('14 May 2014 14:00:00');
$timeMachine
->setTime($currentTime);
$logger = \Drupal::service('logger.preview_link_test');
$logger
->cleanLogs();
}
public function testSessionToken() : void {
$entity1 = EntityTestRevPub::create([
'name' => 'test entity 1',
]);
$entity1
->save();
$entity2 = EntityTestRevPub::create([
'name' => 'test entity 2',
]);
$entity2
->save();
$this
->drupalGet($entity1
->toUrl());
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($entity2
->toUrl());
$this
->assertSession()
->statusCodeEquals(403);
$previewLink = PreviewLink::create()
->setEntities([
$entity1,
$entity2,
]);
$previewLink
->save();
$previewLinkUrl1 = Url::fromRoute('entity.entity_test_revpub.preview_link', [
$entity1
->getEntityTypeId() => $entity1
->id(),
'preview_token' => $previewLink
->getToken(),
]);
$this
->drupalGet($previewLinkUrl1);
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet($entity2
->toUrl());
$previewLinkUrl2 = Url::fromRoute('entity.entity_test_revpub.preview_link', [
$entity2
->getEntityTypeId() => $entity2
->id(),
'preview_token' => $previewLink
->getToken(),
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals($previewLinkUrl2);
$this
->assertSession()
->pageTextContains('You are viewing this page because a preview link granted you access. Click here to remove token.');
$this
->drupalGet($entity1
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals($previewLinkUrl1);
$this
->assertSession()
->pageTextContains('You are viewing this page because a preview link granted you access. Click here to remove token.');
$this
->drupalGet(Url::fromRoute('preview_link.session_tokens.remove'));
$this
->assertSession()
->pageTextContains('Removed preview link tokens.');
$this
->drupalGet($entity1
->toUrl());
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet($entity2
->toUrl());
$this
->assertSession()
->statusCodeEquals(403);
$logger = \Drupal::service('logger.preview_link_test');
$messages = array_map(function ($log) : string {
[
1 => $message,
2 => $messagePlaceholders,
3 => $context,
] = $log;
return empty($messagePlaceholders) ? $message : strtr($message, $messagePlaceholders);
}, $logger
->getLogs());
$channels = array_map(function ($log) : ?string {
return $log[3]['channel'] ?? NULL;
}, $logger
->getLogs());
$this
->assertContains('preview_link', $channels);
$this
->assertContains('Redirecting to preview link of test entity 2', $messages);
$this
->assertNotContains('php', $channels);
}
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);
$this
->drupalGet($entity
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals($previewLinkUrl);
$this
->drupalGet(Url::fromRoute('preview_link.session_tokens.remove'));
$this
->assertSession()
->pageTextContains('Removed preview link tokens.');
$this
->drupalGet($previewLinkUrl);
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet($entity
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals($previewLinkUrl);
}
public function testSessionTokenUnclaimDestination() : void {
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'view test entity' => TRUE,
]);
$entity = EntityTestRevPub::create();
$entity
->setPublished();
$entity
->save();
$this
->drupalGet($entity
->toUrl());
$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);
$this
->drupalGet($entity
->toUrl());
$this
->assertSession()
->pageTextContains('You are viewing this page because a preview link granted you access. Click here to remove token.');
$this
->assertSession()
->linkByHrefExists(Url::fromRoute('preview_link.session_tokens.remove', [], [
'query' => [
'destination' => $entity
->toUrl()
->toString(),
],
])
->toString());
}
public function testCanonicalAccessNoClaimedToken() : void {
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'view test entity' => TRUE,
]);
$claimedEntity = EntityTestRevPub::create();
$claimedEntity
->save();
$previewLink = PreviewLink::create()
->addEntity($claimedEntity);
$previewLink
->save();
$previewLinkUrl = Url::fromRoute('entity.entity_test_revpub.preview_link', [
$claimedEntity
->getEntityTypeId() => $claimedEntity
->id(),
'preview_token' => $previewLink
->getToken(),
]);
$this
->drupalGet($previewLinkUrl);
$this
->assertSession()
->statusCodeEquals(200);
$otherEntity = EntityTestRevPub::create();
$otherEntity
->setPublished();
$otherEntity
->save();
$this
->drupalGet($otherEntity
->toUrl());
$this
->assertSession()
->statusCodeEquals(200);
}
public function testRouteSimulateNoRedirect() : void {
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalLogin($this
->createUser([
'view test entity',
'administer entity_test content',
]));
$claimedEntity = EntityTestMulRevPub::create();
$claimedEntity
->save();
$previewLink = PreviewLink::create()
->addEntity($claimedEntity);
$previewLink
->save();
$previewLinkUrl = Url::fromRoute('entity.entity_test_mulrevpub.preview_link', [
$claimedEntity
->getEntityTypeId() => $claimedEntity
->id(),
'preview_token' => $previewLink
->getToken(),
]);
$this
->drupalGet($previewLinkUrl);
$this
->assertSession()
->statusCodeEquals(200);
$editUrl = $claimedEntity
->toUrl('edit-form');
$this
->drupalGet($editUrl);
$this
->assertSession()
->addressEquals($editUrl
->toString());
}
}