View source
<?php
namespace Drupal\Tests\apigee_api_catalog\Kernel;
use Drupal\apigee_api_catalog\Entity\ApiDoc;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
class ApidocEntityRevisionsAccessTest extends KernelTestBase {
use UserCreationTrait;
protected $apidoc;
protected $apidocV1Id;
protected $apidocV2Id;
protected $entityTypeManager;
protected $entityTypeStorage;
protected static $modules = [
'system',
'user',
'text',
'file',
'options',
'file_link',
'apigee_edge',
'key',
'apigee_api_catalog',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('apidoc');
$this
->installSchema('system', [
'sequences',
]);
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->entityTypeStorage = $this->entityTypeManager
->getStorage('apidoc');
$apidoc = $this->entityTypeStorage
->create([
'name' => 'API 1',
'description' => 'Test API v1',
'spec' => NULL,
'api_product' => NULL,
'status' => 1,
]);
$apidoc
->save();
$this->apidocV1Id = $apidoc
->getRevisionId();
$apidoc
->setDescription('Test API v2');
$apidoc
->setRevisionLogMessage('v2');
$apidoc
->setNewRevision();
$apidoc
->save();
$this->apidocV2Id = $apidoc
->getRevisionId();
$this->apidoc = $apidoc;
$this
->createUser();
}
public function testApiDocRevisionsAccessAnon() {
$entity_v1 = $this->entityTypeStorage
->loadRevision($this->apidocV1Id);
$tests = [
'view' => 'Anonymous should not be able to view an unpublished revision.',
'update' => 'Anonymous should not be able to update a revision.',
];
foreach ($tests as $op => $message) {
$this
->assertFalse($entity_v1
->access($op), $message);
}
}
public function testApiDocRevisionsAccessLoggedIn() {
$user = $this
->createUser([]);
$this->container
->get('account_switcher')
->switchTo($user);
$entity_v1 = $this->entityTypeStorage
->loadRevision($this->apidocV1Id);
$tests = [
'view' => 'LoggedIn should not be able to view an unpublished revision.',
'update' => 'LoggedIn should not be able to update a revision.',
];
foreach ($tests as $op => $message) {
$this
->assertFalse($entity_v1
->access($op, $user), $message);
}
}
public function testApiDocRevisionsAccessPermissions() {
$user = $this
->createUser([
'view published apidoc entities',
'view unpublished apidoc entities',
'view apidoc revisions',
'edit apidoc entities',
'revert apidoc revisions',
]);
$this->container
->get('account_switcher')
->switchTo($user);
$entity_v1 = $this->entityTypeStorage
->loadRevision($this->apidocV1Id);
$tests = [
'view' => 'User should be able to view an unpublished revision.',
'update' => 'User should be able to update a revision.',
];
foreach ($tests as $op => $message) {
$this
->assertTrue($entity_v1
->access($op, $user), $message);
}
}
public function testApiDocRevisionsAccessAdmin() {
$user = $this
->createUser([
'administer apigee api catalog',
]);
$this->container
->get('account_switcher')
->switchTo($user);
$entity_v1 = $this->entityTypeStorage
->loadRevision($this->apidocV1Id);
$tests = [
'view' => 'User should be able to view an unpublished revision.',
'update' => 'User should be able to update a revision.',
];
foreach ($tests as $op => $message) {
$this
->assertTrue($entity_v1
->access($op, $user), $message);
}
}
}