UserRoleAdminTest.php in Drupal 9
File
core/modules/user/tests/src/Functional/UserRoleAdminTest.php
View source
<?php
namespace Drupal\Tests\user\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class UserRoleAdminTest extends BrowserTestBase {
protected $adminUser;
protected static $modules = [
'block',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer permissions',
'administer users',
]);
$this
->drupalPlaceBlock('local_tasks_block');
}
public function testRoleAdministration() {
$this
->drupalLogin($this->adminUser);
$default_langcode = \Drupal::languageManager()
->getDefaultLanguage()
->getId();
$this
->drupalGet('admin/people/permissions');
$this
->assertSession()
->elementsCount('xpath', '//ul[@class="tabs primary" and //a[contains(., "Roles")]]', 1);
$role_name = '123';
$edit = [
'label' => $role_name,
'id' => $role_name,
];
$this
->drupalGet('admin/people/roles/add');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("Role 123 has been added.");
$role = Role::load($role_name);
$this
->assertIsObject($role);
$this
->assertEquals($default_langcode, $role
->language()
->getId());
$this
->drupalGet('admin/people/roles/add');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("The machine-readable name is already in use. It must be unique.");
$role_name = '456';
$edit = [
'label' => $role_name,
];
$this
->drupalGet("admin/people/roles/manage/{$role->id()}");
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("Role {$role_name} has been updated.");
\Drupal::entityTypeManager()
->getStorage('user_role')
->resetCache([
$role
->id(),
]);
$new_role = Role::load($role
->id());
$this
->assertEquals($role_name, $new_role
->label(), 'The role name has been successfully changed.');
$this
->drupalGet("admin/people/roles/manage/{$role->id()}");
$this
->clickLink('Delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains("Role {$role_name} has been deleted.");
$this
->assertSession()
->linkByHrefNotExists("admin/people/roles/manage/{$role->id()}", 'Role edit link removed.');
\Drupal::entityTypeManager()
->getStorage('user_role')
->resetCache([
$role
->id(),
]);
$this
->assertNull(Role::load($role
->id()), 'A deleted role can no longer be loaded.');
$this
->drupalGet('admin/people/roles/manage/' . RoleInterface::ANONYMOUS_ID);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Delete role');
$this
->drupalGet('admin/people/roles/manage/' . RoleInterface::AUTHENTICATED_ID);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Delete role');
}
public function testRoleWeightOrdering() {
$this
->drupalLogin($this->adminUser);
$roles = user_roles();
$weight = count($roles);
$new_role_weights = [];
$saved_rids = [];
$edit = [];
foreach ($roles as $role) {
$edit['entities[' . $role
->id() . '][weight]'] = $weight;
$new_role_weights[$role
->id()] = $weight;
$saved_rids[] = $role
->id();
$weight--;
}
$this
->drupalGet('admin/people/roles');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('The role settings have been updated.');
$roles = user_roles();
$rids = [];
foreach ($roles as $role) {
$this
->assertEquals($role
->getWeight(), $new_role_weights[$role
->id()]);
$rids[] = $role
->id();
}
$this
->assertSame(array_reverse($saved_rids), $rids);
}
}
Classes
Name |
Description |
UserRoleAdminTest |
Tests adding, editing and deleting user roles and changing role weights. |