View source
<?php
namespace Drupal\Tests\userprotect\Functional\RoleDelegation;
use Drupal\Tests\BrowserTestBase;
use Drupal\userprotect\Entity\ProtectionRule;
class RoleDelegationIntegrationTest extends BrowserTestBase {
public static $modules = [
'userprotect',
'user',
'role_delegation',
];
protected $defaultTheme = 'stark';
protected $rid1;
protected $rid2;
protected $adminUser;
protected $roleDelegatedAdminUser;
protected $regularRolesAdminUser;
public function setUp() {
parent::setUp();
$admin_role = $this
->createAdminRole();
$this->adminUser = $this
->createUser();
$this->adminUser
->addRole($admin_role);
$this->adminUser
->save();
$this->rid1 = $this
->drupalCreateRole([]);
$this->rid2 = $this
->drupalCreateRole([]);
$this->roleDelegatedAdminUser = $this
->drupalCreateUser([
'administer users',
sprintf('assign %s role', $this->rid1),
sprintf('assign %s role', $this->rid2),
]);
$this->regularRolesAdminUser = $this
->drupalCreateUser([
'administer users',
'administer permissions',
]);
ProtectionRule::create([
'name' => 'protect_admin_role',
'label' => 'Protect admin role',
'protections' => [
'user_roles' => [
'status' => TRUE,
],
],
'protectedEntityTypeId' => 'user_role',
'protectedEntityId' => $admin_role,
])
->save();
}
public function testUserEditPage() {
$this
->drupalLogin($this->roleDelegatedAdminUser);
$this
->drupalGet(sprintf('/user/%s/edit', $this->roleDelegatedAdminUser
->id()));
$this
->assertSession()
->fieldNotExists(sprintf('roles[%s]', $this->rid1));
$this
->assertSession()
->fieldExists(sprintf('role_change[%s]', $this->rid1));
$this
->assertSession()
->fieldNotExists(sprintf('roles[%s]', $this->rid2));
$this
->assertSession()
->fieldExists(sprintf('role_change[%s]', $this->rid2));
$this
->drupalGet(sprintf('/user/%s/edit', $this->adminUser
->id()));
$this
->assertSession()
->fieldNotExists(sprintf('roles[%s]', $this->rid1));
$this
->assertSession()
->fieldNotExists(sprintf('role_change[%s]', $this->rid1));
$this
->assertSession()
->fieldNotExists(sprintf('roles[%s]', $this->rid2));
$this
->assertSession()
->fieldNotExists(sprintf('role_change[%s]', $this->rid2));
$this
->drupalLogin($this->regularRolesAdminUser);
$this
->drupalGet(sprintf('/user/%s/edit', $this->roleDelegatedAdminUser
->id()));
$this
->assertSession()
->fieldExists(sprintf('roles[%s]', $this->rid1));
$this
->assertSession()
->fieldNotExists(sprintf('role_change[%s]', $this->rid1));
$this
->assertSession()
->fieldExists(sprintf('roles[%s]', $this->rid2));
$this
->assertSession()
->fieldNotExists(sprintf('role_change[%s]', $this->rid2));
$this
->drupalGet(sprintf('/user/%s/edit', $this->adminUser
->id()));
$this
->assertSession()
->fieldNotExists(sprintf('role_change[%s]', $this->rid1));
$this
->assertSession()
->elementAttributeContains('css', sprintf('[name="roles[%s]"]', $this->rid1), 'disabled', 'disabled');
$this
->assertSession()
->fieldNotExists(sprintf('role_change[%s]', $this->rid2));
$this
->assertSession()
->elementAttributeContains('css', sprintf('[name="roles[%s]"]', $this->rid2), 'disabled', 'disabled');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet(sprintf('/user/%s/edit', $this->roleDelegatedAdminUser
->id()));
$this
->assertSession()
->fieldExists(sprintf('roles[%s]', $this->rid1));
$this
->assertSession()
->fieldNotExists(sprintf('role_change[%s]', $this->rid1));
$this
->assertSession()
->fieldExists(sprintf('roles[%s]', $this->rid2));
$this
->assertSession()
->fieldNotExists(sprintf('role_change[%s]', $this->rid2));
$this
->drupalGet(sprintf('/user/%s/edit', $this->adminUser
->id()));
$this
->assertSession()
->fieldExists(sprintf('roles[%s]', $this->rid1));
$this
->assertSession()
->fieldNotExists(sprintf('role_change[%s]', $this->rid1));
$this
->assertSession()
->fieldExists(sprintf('roles[%s]', $this->rid2));
$this
->assertSession()
->fieldNotExists(sprintf('role_change[%s]', $this->rid2));
}
public function testRolesPage() {
$this
->drupalGet('admin/config/people/userprotect/manage/protect_admin_role');
$this
->drupalLogin($this->roleDelegatedAdminUser);
$this
->drupalGet(sprintf('/user/%s/roles', $this->roleDelegatedAdminUser
->id()));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet(sprintf('/user/%s/roles', $this->adminUser
->id()));
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->regularRolesAdminUser);
$this
->drupalGet(sprintf('/user/%s/roles', $this->roleDelegatedAdminUser
->id()));
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet(sprintf('/user/%s/roles', $this->adminUser
->id()));
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet(sprintf('/user/%s/roles', $this->roleDelegatedAdminUser
->id()));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet(sprintf('/user/%s/roles', $this->adminUser
->id()));
$this
->assertSession()
->statusCodeEquals(200);
}
}