You are here

public function BulkFormAccessTest::testUserDeleteAccess in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Tests/Views/BulkFormAccessTest.php \Drupal\user\Tests\Views\BulkFormAccessTest::testUserDeleteAccess()

Tests if users that may not be deleted, can not be deleted in bulk.

File

core/modules/user/src/Tests/Views/BulkFormAccessTest.php, line 101
Contains \Drupal\user\Tests\Views\BulkFormAccessTest.

Class

BulkFormAccessTest
Tests if entity access is respected on a user bulk form.

Namespace

Drupal\user\Tests\Views

Code

public function testUserDeleteAccess() {

  // Create two authenticated users.
  $account = $this
    ->drupalCreateUser(array(), 'no_delete');
  $account2 = $this
    ->drupalCreateUser(array(), 'may_delete');

  // Login as user admin.
  $this
    ->drupalLogin($this
    ->drupalCreateUser(array(
    'administer users',
  )));

  // Ensure that the account "no_delete" can not be deleted.
  $this
    ->drupalGet('user/' . $account
    ->id() . '/cancel');
  $this
    ->assertResponse(403, 'The user "no_delete" may not be deleted.');

  // Ensure that the account "may_delete" *can* be deleted.
  $this
    ->drupalGet('user/' . $account2
    ->id() . '/cancel');
  $this
    ->assertResponse(200, 'The user "may_delete" may be deleted.');

  // Test deleting the accounts "no_delete" and "may_delete".
  $edit = array(
    'user_bulk_form[' . ($account
      ->id() - 1) . ']' => TRUE,
    'user_bulk_form[' . ($account2
      ->id() - 1) . ']' => TRUE,
    'action' => 'user_cancel_user_action',
  );
  $this
    ->drupalPostForm('test-user-bulk-form', $edit, t('Apply'));
  $edit = array(
    'user_cancel_method' => 'user_cancel_delete',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Cancel accounts'));

  // Ensure the account "no_delete" still exists.
  $account = User::load($account
    ->id());
  $this
    ->assertNotNull($account, 'The user "no_delete" is not deleted.');

  // Ensure the account "may_delete" no longer exists.
  $account = User::load($account2
    ->id());
  $this
    ->assertNull($account, 'The user "may_delete" is deleted.');
}