View source
<?php
namespace Drupal\Tests\user\Functional\Views;
use Drupal\user\Entity\User;
class BulkFormAccessTest extends UserTestBase {
protected static $modules = [
'user_access_test',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_user_bulk_form',
];
public function testUserEditAccess() {
$no_edit_user = $this
->drupalCreateUser([], 'no_edit');
$this
->assertFalse($no_edit_user
->isBlocked(), 'The user is not blocked.');
$admin_user = $this
->drupalCreateUser([
'administer users',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('user/' . $no_edit_user
->id() . '/edit');
$this
->assertFalse($no_edit_user
->access('update', $admin_user));
$this
->assertSession()
->statusCodeEquals(403);
$edit = [
'user_bulk_form[' . ($no_edit_user
->id() - 1) . ']' => TRUE,
'action' => 'user_block_user_action',
];
$this
->drupalGet('test-user-bulk-form');
$this
->submitForm($edit, 'Apply to selected items');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains("No access to execute Block the selected user(s) on the User {$no_edit_user->label()}.");
$no_edit_user = User::load($no_edit_user
->id());
$this
->assertFalse($no_edit_user
->isBlocked(), 'The user is not blocked.');
$normal_user = $this
->drupalCreateUser();
$this
->assertTrue($normal_user
->access('update', $admin_user));
$edit = [
'user_bulk_form[' . ($normal_user
->id() - 1) . ']' => TRUE,
'action' => 'user_block_user_action',
];
$this
->drupalGet('test-user-bulk-form');
$this
->submitForm($edit, 'Apply to selected items');
$normal_user = User::load($normal_user
->id());
$this
->assertTrue($normal_user
->isBlocked(), 'The user is blocked.');
$this
->drupalLogin($this
->drupalCreateUser());
$edit = [
'user_bulk_form[' . ($normal_user
->id() - 1) . ']' => TRUE,
'action' => 'user_unblock_user_action',
];
$this
->drupalGet('test-user-bulk-form');
$this
->submitForm($edit, 'Apply to selected items');
$normal_user = User::load($normal_user
->id());
$this
->assertTrue($normal_user
->isBlocked(), 'The user is still blocked.');
}
public function testUserDeleteAccess() {
$account = $this
->drupalCreateUser([], 'no_delete');
$account2 = $this
->drupalCreateUser([], 'may_delete');
$this
->drupalLogin($this
->drupalCreateUser([
'administer users',
]));
$this
->drupalGet('user/' . $account
->id() . '/cancel');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('user/' . $account2
->id() . '/cancel');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'user_bulk_form[' . ($account
->id() - 1) . ']' => TRUE,
'user_bulk_form[' . ($account2
->id() - 1) . ']' => TRUE,
'action' => 'user_cancel_user_action',
];
$this
->drupalGet('test-user-bulk-form');
$this
->submitForm($edit, 'Apply to selected items');
$edit = [
'user_cancel_method' => 'user_cancel_delete',
];
$this
->submitForm($edit, 'Confirm');
$account = User::load($account
->id());
$this
->assertNotNull($account, 'The user "no_delete" is not deleted.');
$account = User::load($account2
->id());
$this
->assertNull($account, 'The user "may_delete" is deleted.');
}
}