View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\preview_link\Functional;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Url;
use Drupal\preview_link\Entity\PreviewLink;
use Drupal\preview_link\Entity\PreviewLinkInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\entity_test\Entity\EntityTestRev;
class PreviewLinkAccessTest extends BrowserTestBase {
protected $defaultTheme = 'classy';
public static $modules = [
'entity_test',
'preview_link',
'preview_link_test',
];
public function testPreviewFakeToken() : void {
$account = $this
->createUser([
'view test entity',
]);
$this
->drupalLogin($account);
$entity = EntityTestRev::create();
$entity
->save();
$access = $entity
->access('view', $account, TRUE);
$this
->assertTrue($access
->isAllowed());
$this
->getNewPreviewLinkForEntity($entity);
$token = PreviewLink::create()
->getToken();
$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);
}
public function testPreviewRealToken() : void {
$account = $this
->createUser([
'view test entity',
]);
$this
->drupalLogin($account);
$entity = EntityTestRev::create();
$entity
->save();
$access = $entity
->access('view', $account, TRUE);
$this
->assertTrue($access
->isAllowed());
$preview = $this
->getNewPreviewLinkForEntity($entity);
$token = $preview
->getToken();
$url = Url::fromRoute('entity.entity_test_rev.preview_link', [
'entity_test_rev' => $entity
->id(),
'preview_token' => $token,
]);
$this
->drupalGet($url);
$this
->assertSession()
->statusCodeEquals(200);
}
public function testPreviewLinkEnabledEntityTypesConfiguration() : void {
$config = $this
->config('preview_link.settings');
$account = $this
->createUser([
'view test entity',
]);
$this
->drupalLogin($account);
$entity = EntityTestRev::create();
$entity
->save();
$preview = $this
->getNewPreviewLinkForEntity($entity);
$token = $preview
->getToken();
$url = Url::fromRoute('entity.entity_test_rev.preview_link', [
'entity_test_rev' => $entity
->id(),
'preview_token' => $token,
]);
$config
->set('enabled_entity_types', [])
->save();
$this
->drupalGet($url);
$this
->assertSession()
->statusCodeEquals(200);
$config
->set('enabled_entity_types', [
'foo' => [],
])
->save();
$this
->drupalGet($url);
$this
->assertSession()
->statusCodeEquals(403);
$config
->set('enabled_entity_types', [
'foo' => [],
'entity_test_rev' => [],
])
->save();
$this
->drupalGet($url);
$this
->assertSession()
->statusCodeEquals(200);
$config
->set('enabled_entity_types', [
'foo' => [],
'entity_test_rev' => [
'foo',
],
])
->save();
$this
->drupalGet($url);
$this
->assertSession()
->statusCodeEquals(403);
$config
->set('enabled_entity_types', [
'foo' => [],
'entity_test_rev' => [
'foo',
'entity_test_rev',
],
])
->save();
$this
->drupalGet($url);
$this
->assertSession()
->statusCodeEquals(200);
}
protected function getNewPreviewLinkForEntity(ContentEntityInterface $entity) : ?PreviewLinkInterface {
$previewLink = PreviewLink::create()
->addEntity($entity);
$previewLink
->save();
return $previewLink;
}
}