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;
public static $modules = [
'block',
];
protected $defaultTheme = 'classy';
protected function setUp() {
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');
$tabs = $this
->xpath('//ul[@class=:classes and //a[contains(., :text)]]', [
':classes' => 'tabs primary',
':text' => 'Roles',
]);
$this
->assertCount(1, $tabs, 'Found roles tab');
$role_name = '123';
$edit = [
'label' => $role_name,
'id' => $role_name,
];
$this
->drupalPostForm('admin/people/roles/add', $edit, t('Save'));
$this
->assertRaw(t('Role %label has been added.', [
'%label' => 123,
]));
$role = Role::load($role_name);
$this
->assertIsObject($role);
$this
->assertEqual($role
->language()
->getId(), $default_langcode);
$this
->drupalPostForm('admin/people/roles/add', $edit, t('Save'));
$this
->assertRaw(t('The machine-readable name is already in use. It must be unique.'), 'Duplicate role warning displayed.');
$role_name = '456';
$edit = [
'label' => $role_name,
];
$this
->drupalPostForm("admin/people/roles/manage/{$role->id()}", $edit, t('Save'));
$this
->assertRaw(t('Role %label has been updated.', [
'%label' => $role_name,
]));
\Drupal::entityTypeManager()
->getStorage('user_role')
->resetCache([
$role
->id(),
]);
$new_role = Role::load($role
->id());
$this
->assertEqual($new_role
->label(), $role_name, 'The role name has been successfully changed.');
$this
->drupalGet("admin/people/roles/manage/{$role->id()}");
$this
->clickLink(t('Delete'));
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->assertRaw(t('The role %label has been deleted.', [
'%label' => $role_name,
]));
$this
->assertNoLinkByHref("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
->assertNoText(t('Delete role'), 'Delete button for the anonymous role is not present.');
$this
->drupalGet('admin/people/roles/manage/' . RoleInterface::AUTHENTICATED_ID);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertNoText(t('Delete role'), 'Delete button for the authenticated role is not present.');
}
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
->drupalPostForm('admin/people/roles', $edit, t('Save'));
$this
->assertText(t('The role settings have been updated.'), 'The role settings form submitted successfully.');
$roles = user_roles();
$rids = [];
foreach ($roles as $role) {
$this
->assertEqual($role
->getWeight(), $new_role_weights[$role
->id()]);
$rids[] = $role
->id();
}
$this
->assertIdentical($rids, array_reverse($saved_rids));
}
}