You are here

public function AccessTest::testDynamicRoutes in Entity Construction Kit (ECK) 8

Tests if the access to dynamic routes is properly checked.

File

tests/src/Functional/AccessTest.php, line 85

Class

AccessTest
Tests eck's access control.

Namespace

Drupal\Tests\eck\Functional

Code

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));

      // Anonymous users can not access the route.
      $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));

      // Users with the correct permission can access the route.
      $this
        ->assertSession()
        ->statusCodeEquals(200);
    }
  }
}