public function BulkFormTest::testBulkForm

Same name in this branch
  1. 8.x drupal/core/modules/action/lib/Drupal/action/Tests/BulkFormTest.php \Drupal\action\Tests\BulkFormTest::testBulkForm()
  2. 8.x drupal/core/modules/node/lib/Drupal/node/Tests/Views/BulkFormTest.php \Drupal\node\Tests\Views\BulkFormTest::testBulkForm()
  3. 8.x drupal/core/modules/user/lib/Drupal/user/Tests/Views/BulkFormTest.php \Drupal\user\Tests\Views\BulkFormTest::testBulkForm()

Tests the user bulk form.

File

drupal/core/modules/user/lib/Drupal/user/Tests/Views/BulkFormTest.php, line 35
Contains \Drupal\user\Tests\Views\BulkFormTest.

Class

BulkFormTest
Tests the views bulk form test.

Namespace

Drupal\user\Tests\Views

Code

public function testBulkForm() {
  $this
    ->drupalLogin($this
    ->drupalCreateUser(array(
    'administer permissions',
  )));

  // Test submitting the page with no selection.
  $edit = array(
    'action' => 'user_block_user_action',
  );
  $this
    ->drupalPost('test-user-bulk-form', $edit, t('Apply'));

  // @todo Validation errors are only shown on page refresh.
  $this
    ->drupalGet('test-user-bulk-form');
  $this
    ->assertText(t('No users selected.'));

  // Assign a role to a user.
  $account = entity_load('user', $this->users[0]
    ->id());
  $roles = user_role_names(TRUE);
  unset($roles[DRUPAL_AUTHENTICATED_RID]);
  $role = key($roles);
  $this
    ->assertFalse($account
    ->hasRole($role), 'The user currently does not have a custom role.');
  $edit = array(
    'user_bulk_form[1]' => TRUE,
    'action' => 'user_add_role_action.' . $role,
  );
  $this
    ->drupalPost(NULL, $edit, t('Apply'));

  // Re-load the user and check their roles.
  $account = entity_load('user', $account
    ->id(), TRUE);
  $this
    ->assertTrue($account
    ->hasRole($role), 'The user now has the custom role.');
  $edit = array(
    'user_bulk_form[1]' => TRUE,
    'action' => 'user_remove_role_action.' . $role,
  );
  $this
    ->drupalPost(NULL, $edit, t('Apply'));

  // Re-load the user and check their roles.
  $account = entity_load('user', $account
    ->id(), TRUE);
  $this
    ->assertFalse($account
    ->hasRole($role), 'The user no longer has the custom role.');

  // Block a user using the bulk form.
  $this
    ->assertTrue($account->status->value, 'The user is not blocked.');
  $this
    ->assertRaw($account
    ->label(), 'The user is found in the table.');
  $edit = array(
    'user_bulk_form[1]' => TRUE,
    'action' => 'user_block_user_action',
  );
  $this
    ->drupalPost(NULL, $edit, t('Apply'));

  // Re-load the user and check their status.
  $account = entity_load('user', $account
    ->id(), TRUE);
  $this
    ->assertFalse($account->status->value, 'The user is blocked.');
  $this
    ->assertNoRaw($account
    ->label(), 'The user is not found in the table.');

  // Remove the user status filter from the view.
  $view = views_get_view('test_user_bulk_form');
  $view
    ->removeItem('default', 'filter', 'status');
  $view->storage
    ->save();

  // Ensure the anonymous user is found.
  $this
    ->drupalGet('test-user-bulk-form');
  $this
    ->assertText(config('user.settings')
    ->get('anonymous'));

  // Attempt to block the anonymous user.
  $edit = array(
    'user_bulk_form[0]' => TRUE,
    'action' => 'user_block_user_action',
  );
  $this
    ->drupalPost(NULL, $edit, t('Apply'));
  $anonymous_account = user_load(0);
  $this
    ->assertFalse($anonymous_account->status, 0, 'Ensure the anonymous user got blocked.');
}