View source
<?php
namespace Drupal\Tests\eck\Functional;
use Drupal\Core\Url;
class AccessTest extends FunctionalTestBase {
protected $entityTypeInfo;
protected $bundleInfo;
public function setUp() {
parent::setUp();
$this->entityTypeInfo = $this
->createEntityType();
$this->bundleInfo = $this
->createEntityBundle($this->entityTypeInfo['id']);
$this
->drupalLogout();
}
public function testDefaultRoutes() {
$routes = [
'administer eck entity types' => [
'eck.entity_type.list',
'eck.entity_type.add',
'entity.eck_entity_type.edit_form',
'entity.eck_entity_type.delete_form',
],
"create {$this->entityTypeInfo['id']} entities" => [
'eck.entity.add_page',
'eck.entity.add',
],
];
$route_args = [
'eck_entity_type' => $this->entityTypeInfo['id'],
'eck_entity_bundle' => $this->bundleInfo['type'],
];
foreach ($routes as $route_names) {
foreach ($route_names as $route) {
$this
->drupalGet(Url::fromRoute($route, $route_args));
$this
->assertSession()
->statusCodeEquals(403);
}
}
\Drupal::entityTypeManager()
->clearCachedDefinitions();
foreach ($routes as $permission => $route_names) {
$this
->drupalLogin($this
->drupalCreateUser([
$permission,
]));
foreach ($route_names as $route) {
$this
->drupalGet(Url::fromRoute($route, $route_args));
$this
->assertSession()
->statusCodeEquals(200);
}
}
}
public function testDynamicRoutes() {
$routes = [
"access {$this->entityTypeInfo['id']} entity listing" => [
"eck.entity.{$this->entityTypeInfo['id']}.list",
],
'bypass eck entity access' => [
"eck.entity.{$this->entityTypeInfo['id']}.list",
],
'administer eck entity bundles' => [
"eck.entity.{$this->entityTypeInfo['id']}_type.list",
"eck.entity.{$this->entityTypeInfo['id']}_type.add",
"entity.{$this->entityTypeInfo['id']}_type.edit_form",
"entity.{$this->entityTypeInfo['id']}_type.delete_form",
],
];
$routeArguments = [
"{$this->entityTypeInfo['id']}_type" => $this->bundleInfo['type'],
];
foreach ($routes as $routeNames) {
foreach ($routeNames as $routeName) {
$this
->drupalGet(Url::fromRoute($routeName, $routeArguments));
$this
->assertSession()
->statusCodeEquals(403);
}
}
\Drupal::entityTypeManager()
->clearCachedDefinitions();
foreach ($routes as $permission => $routeNames) {
$this
->drupalLogin($this
->drupalCreateUser([
$permission,
]));
foreach ($routeNames as $routeName) {
$this
->drupalGet(Url::fromRoute($routeName, $routeArguments));
$this
->assertSession()
->statusCodeEquals(200);
}
}
}
public function testEntityAccess() {
$entityTypeName = $this->entityTypeInfo['id'];
$ownEntityPermissions = $anyEntityPermissions = [
"create {$entityTypeName} entities",
];
foreach ([
'view',
'edit',
'delete',
] as $op) {
$ownEntityPermissions[] = "{$op} own {$entityTypeName} entities";
$anyEntityPermissions[] = "{$op} any {$entityTypeName} entities";
}
$ownEntityUser = $this
->drupalCreateUser($ownEntityPermissions);
$anyEntityUser = $this
->drupalCreateUser($anyEntityPermissions);
$this
->drupalLogin($anyEntityUser);
$edit['title[0][value]'] = $this
->randomMachineName();
$route_args = [
'eck_entity_type' => $entityTypeName,
'eck_entity_bundle' => $this->bundleInfo['type'],
];
$this
->drupalGet(Url::fromRoute("eck.entity.add", $route_args));
$this
->submitForm($edit, 'Save');
$this
->drupalLogin($ownEntityUser);
$edit['title[0][value]'] = $this
->randomMachineName();
$route_args = [
'eck_entity_type' => $entityTypeName,
'eck_entity_bundle' => $this->bundleInfo['type'],
];
$this
->drupalGet(Url::fromRoute("eck.entity.add", $route_args));
$this
->submitForm($edit, 'Save');
$arguments = [
$entityTypeName => 1,
];
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.canonical", $arguments));
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.edit_form", $arguments));
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.delete_form", $arguments));
$this
->assertSession()
->statusCodeEquals(403);
$arguments = [
$entityTypeName => 2,
];
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.canonical", $arguments));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.edit_form", $arguments));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.delete_form", $arguments));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($anyEntityUser);
$arguments = [
$entityTypeName => 1,
];
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.canonical", $arguments));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.edit_form", $arguments));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.delete_form", $arguments));
$this
->assertSession()
->statusCodeEquals(200);
$arguments = [
$entityTypeName => 2,
];
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.canonical", $arguments));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.edit_form", $arguments));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.delete_form", $arguments));
$this
->assertSession()
->statusCodeEquals(200);
$this
->createEntity($entityTypeName, [
'type' => $this->bundleInfo['type'],
'title' => $this
->randomString(),
'status' => FALSE,
]);
$arguments = [
$entityTypeName => 3,
];
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.canonical", $arguments));
$this
->assertSession()
->statusCodeEquals(403);
$viewUnpublishedEntityUser = $this
->drupalCreateUser([
'view unpublished eck entities',
]);
$this
->drupalLogin($viewUnpublishedEntityUser);
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.canonical", $arguments));
$this
->assertSession()
->statusCodeEquals(200);
$viewUnpublishedAndAnyEntityUser = $this
->drupalCreateUser([
'view unpublished eck entities',
"view any {$entityTypeName} entities",
]);
$this
->drupalLogin($viewUnpublishedAndAnyEntityUser);
$this
->drupalGet(Url::fromRoute("entity.{$entityTypeName}.canonical", $arguments));
$this
->assertSession()
->statusCodeEquals(200);
}
}