You are here

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

Tests if the access to the default routes is properly checked.

File

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

Class

AccessTest
Tests eck's access control.

Namespace

Drupal\Tests\eck\Functional

Code

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

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

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