View source
<?php
namespace Drupal\Tests\roleassign\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
class RoleAssignPermissionTest extends BrowserTestBase {
protected $testAccount;
protected $restrictedUser;
protected $adminUser;
protected $defaultTheme = 'stark';
public static $modules = [
'roleassign',
];
protected function setUp() {
parent::setUp();
$this
->drupalCreateRole([], 'editor', 'Editor');
$this
->drupalCreateRole([
'administer users',
'assign roles',
], 'webmaster', 'Webmaster');
$this
->drupalCreateRole([
'administer users',
'administer permissions',
], 'siteadmin', 'SiteAdmin');
$this
->config('roleassign.settings')
->set('roleassign_roles', [
'editor' => 'editor',
'webmaster' => 'webmaster',
])
->save();
$this->testAccount = $this
->drupalCreateUser();
$this->restrictedUser = $this
->drupalCreateUser([
'administer users',
'assign roles',
]);
$this->adminUser = $this
->drupalCreateUser([
'administer users',
'administer permissions',
]);
}
public function testRoleAssignSettings() {
$assignable_roles = array_filter(\Drupal::config('roleassign.settings')
->get('roleassign_roles'));
$this
->assertIdentical([
'editor' => 'editor',
'webmaster' => 'webmaster',
], $assignable_roles);
}
public function testRoleAssignRestrictedUser() {
$this
->drupalLogin($this->restrictedUser);
$this
->drupalGet('user/' . $this->testAccount
->id() . '/edit');
$this
->assertText(t('Assignable roles'));
$this
->assertNoFieldChecked('edit-roles-editor');
$this
->assertNoFieldChecked('edit-roles-webmaster');
$this
->assertNoField('edit-roles-siteadmin');
$this
->drupalPostForm('user/' . $this->testAccount
->id() . '/edit', [
"roles[editor]" => "editor",
], t('Save'));
$this
->assertText(t('The changes have been saved.'));
$this
->assertFieldChecked('edit-roles-editor', 'Role editor is assigned.');
$this
->assertNoFieldChecked('edit-roles-webmaster');
$this
->assertNoField('edit-roles-siteadmin');
$this
->userLoadAndCheckRoleAssigned($this->testAccount, 'editor');
$this
->userLoadAndCheckRoleAssigned($this->testAccount, RoleInterface::AUTHENTICATED_ID);
$this
->drupalPostForm('user/' . $this->testAccount
->id() . '/edit', [
"roles[editor]" => FALSE,
], t('Save'));
$this
->assertText(t('The changes have been saved.'));
$this
->assertNoFieldChecked('edit-roles-editor', 'Role editor is removed.');
$this
->assertNoFieldChecked('edit-roles-webmaster');
$this
->assertNoField('edit-roles-siteadmin');
$this
->userLoadAndCheckRoleAssigned($this->testAccount, 'editor', FALSE);
$this
->userLoadAndCheckRoleAssigned($this->testAccount, RoleInterface::AUTHENTICATED_ID);
$values = [
'name' => $this
->randomString(),
'roles' => [
'editor',
'siteadmin',
],
];
$code_account = User::create($values);
$code_account
->save();
$this
->assertTrue($code_account
->hasRole('editor'));
$this
->assertFalse($code_account
->hasRole('siteadmin'));
}
public function testRoleAssignAdminUser() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('user/' . $this->testAccount
->id() . '/edit');
$this
->assertText(t('Roles'));
$this
->assertNoFieldChecked('edit-roles-editor');
$this
->assertNoFieldChecked('edit-roles-webmaster');
$this
->assertNoFieldChecked('edit-roles-siteadmin');
$this
->drupalPostForm('user/' . $this->testAccount
->id() . '/edit', [
"roles[siteadmin]" => "siteadmin",
], t('Save'));
$this
->assertText(t('The changes have been saved.'));
$this
->assertFieldChecked('edit-roles-siteadmin', 'Role siteadmin is assigned.');
$this
->userLoadAndCheckRoleAssigned($this->testAccount, 'siteadmin');
$this
->userLoadAndCheckRoleAssigned($this->testAccount, RoleInterface::AUTHENTICATED_ID);
$this
->drupalLogin($this->restrictedUser);
$this
->drupalPostForm('user/' . $this->testAccount
->id() . '/edit', [
"roles[editor]" => "editor",
], t('Save'));
$this
->assertText(t('The changes have been saved.'));
$this
->assertFieldChecked('edit-roles-editor', 'Role editor is assigned.');
$this
->assertNoField('edit-roles-siteadmin');
$this
->userLoadAndCheckRoleAssigned($this->testAccount, 'editor');
$this
->userLoadAndCheckRoleAssigned($this->testAccount, RoleInterface::AUTHENTICATED_ID);
$this
->userLoadAndCheckRoleAssigned($this->testAccount, 'siteadmin');
}
private function userLoadAndCheckRoleAssigned($account, $rid, $is_assigned = TRUE) {
$user_storage = \Drupal::entityTypeManager()
->getStorage('user');
$user_storage
->resetCache([
$account
->id(),
]);
$account = $user_storage
->load($account
->id());
if ($is_assigned) {
$this
->assertFalse(array_search($rid, $account
->getRoles()) === FALSE, 'The role is present in the user object.');
}
else {
$this
->assertTrue(array_search($rid, $account
->getRoles()) === FALSE, 'The role is not present in the user object.');
}
}
}