Skip to content

Commit

Permalink
[#297] Updated RoleTrait.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Oct 30, 2024
1 parent 83a39e1 commit 92439ee
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 73 deletions.
4 changes: 4 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ A migration map of the step definitions available in v2 to v3.
| `Then I :can visit :path with HTTP credentials :user :pass` | `Given the basic authentication with the username :username and the password :password` |
| `When I visit :path then the final URL should be :alias` | Removed. Use `When I visit :path` and `Then the path should be :path` |
|   | |
| **[`RoleTrait`](src/RoleTrait.php) ([example](tests/behat/features/role.feature))** | |
| `Given role :name with permissions :permissions` | `Given the role :role with the permissions :permissions` |
| `Given roles:` | `Given the following roles:` |
|   | |
| **[`SearchApiTrait`](src/SearchApiTrait.php) ([example](tests/behat/features/search.feature))** | |
| `When I index :type :title for search` | `When I add the :content_type content with the title :title to the search index` |
| `When I index :limit Search API items` | `When I run search indexing for :count item(s)` |
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ For migration from v2 to v3, see [MIGRATION.md](MIGRATION.md).
| `Then response header :name does not contain :value` | Assert that the response header does not contain the specified value. |
|   | |
| **[`RoleTrait`](src/RoleTrait.php) ([example](tests/behat/features/role.feature))** | |
| `Given role :name with permissions :permissions` | Create a single role with the specified permissions. |
| `Given roles:` | Create multiple roles from the specified table. |
| `Given the role :role with the permissions :permissions` | Create a single role with the specified permissions. |
| `Given the following roles:` | Create multiple roles from the specified table. |
|   | |
| **[`SelectTrait`](src/SelectTrait.php) ([example](tests/behat/features/select.feature))** | |
| `Then select :select should have an option :option` | Assert that the specified select element has the specified option. |
Expand Down
43 changes: 7 additions & 36 deletions src/RoleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace DrevOps\BehatSteps;

use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Drupal\user\Entity\Role;

Expand All @@ -17,23 +16,16 @@
*/
trait RoleTrait {

/**
* Roles ids.
*
* @var array<string,string>
*/
protected array $rolesIds = [];

/**
* Create a single role with specified permissions.
*
* @Given role :name with permissions :permissions
* @Given the role :role_name with the permissions :permissions
*/
public function roleCreateSingle(string $name, string $permissions): void {
public function roleCreateSingle(string $role_name, string $permissions): void {
$permissions = array_map(trim(...), explode(',', $permissions));

$rid = strtolower($name);
$name = trim($name);
$rid = strtolower($role_name);
$role_name = trim($role_name);

$existing_role = Role::load($rid);
if ($existing_role) {
Expand All @@ -43,22 +35,22 @@ public function roleCreateSingle(string $name, string $permissions): void {
/** @var \Drupal\user\RoleInterface $role */
$role = \Drupal::entityTypeManager()->getStorage('user_role')->create([
'id' => $rid,
'label' => $name,
'label' => $role_name,
]);
$saved = $role->save();

if ($saved !== SAVED_NEW) {
throw new \RuntimeException(sprintf('Failed to create a role with "%s" permission(s).', implode(', ', $permissions)));
}
$this->rolesIds[(string) $role->id()] = (string) $role->id();
$this->roles[(string) $role->id()] = (string) $role->id();

user_role_grant_permissions($role->id(), $permissions);
}

/**
* Create multiple roles from the specified table.
*
* @Given roles:
* @Given the following roles:
*/
public function roleCreateMultiple(TableNode $table): void {
foreach ($table->getHash() as $hash) {
Expand All @@ -71,25 +63,4 @@ public function roleCreateMultiple(TableNode $table): void {
}
}

/**
* Remove all roles after scenario run.
*
* @AfterScenario
*/
public function roleCleanAll(AfterScenarioScope $scope): void {
// Allow to skip this by adding a tag.
if ($scope->getScenario()->hasTag('behat-steps-skip:' . __FUNCTION__)) {
return;
}

foreach ($this->rolesIds as $rid) {
$role = Role::load($rid);
if ($role) {
$role->delete();
}
}

$this->rolesIds = [];
}

}
39 changes: 4 additions & 35 deletions tests/behat/features/role.feature
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Feature: Check that RoleTrait works

@api
Scenario: Assert "Given role :name with permissions :permissions"
Given role "customrole" with permissions "access administration pages, administer filters"
Scenario: Assert "Given the role :role_name with the permissions :permissions" works
Given the role "customrole" with the permissions "access administration pages, administer filters"
And I am logged in as a user with the "customrole" role

When I go to "/admin"
Expand All @@ -11,40 +11,9 @@ Feature: Check that RoleTrait works
When I go to "/admin/config/content/formats"
Then I should get a 200 HTTP response

When I go to "/admin/structure/types"
Then I should get a 403 HTTP response

@api
Scenario: Assert "Given role :name with permissions :permissions" with existing role
Given role "customrole" with permissions "administer site configuration"
Then I am logged in as a user with the "customrole" role
Then I go to "/admin/config/system/site-information"
Then I should get a 200 HTTP response
Then I go to "/admin/config/content/formats"
Then I should get a 403 HTTP response
Then I go to "/admin/structure/types"
Then I should get a 403 HTTP response

Then role "customrole" with permissions "administer site configuration, access administration pages, administer filters"
Then I log out
# We should not need clear cache at here. Re-check later.
Then I am logged in as a user with the "administrator" role
Then I visit "/admin/config/development/performance"
Then I press the "Clear all cache" button
Then I log out

Then I am logged in as a user with the "customrole" role

Then I go to "/admin/config/system/site-information"
Then I should get a 200 HTTP response
Then I go to "/admin/config/content/formats"
Then I should get a 200 HTTP response
Then I go to "/admin/structure/types"
Then I should get a 403 HTTP response

@api
Scenario: Assert create multiple roles from the specified table.
Given roles:
Scenario: Assert "Given the following roles:" works
Given the following roles:
| name | permissions |
| test-role1 | access administration pages |
| test-role2 | access administration pages, administer filters |
Expand Down

0 comments on commit 92439ee

Please sign in to comment.