View source
<?php
namespace Drupal\Tests\user\Functional\Views;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
use Drupal\views\Views;
class BulkFormTest extends UserTestBase {
protected static $modules = [
'views_ui',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_user_bulk_form',
'test_user_bulk_form_combine_filter',
];
public function testBulkForm() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer permissions',
]));
$user_storage = $this->container
->get('entity_type.manager')
->getStorage('user');
$this
->drupalLogin($this
->drupalCreateUser([
'administer users',
]));
$this
->drupalGet('test-user-bulk-form');
$this
->assertNotEmpty($this
->cssSelect('#edit-action option'));
$edit = [
'action' => 'user_block_user_action',
];
$this
->drupalGet('test-user-bulk-form');
$this
->submitForm($edit, 'Apply to selected items');
$this
->assertSession()
->pageTextContains('No users selected.');
$account = $user_storage
->load($this->users[0]
->id());
$roles = user_role_names(TRUE);
unset($roles[RoleInterface::AUTHENTICATED_ID]);
$role = key($roles);
$this
->assertFalse($account
->hasRole($role), 'The user currently does not have a custom role.');
$edit = [
'user_bulk_form[1]' => TRUE,
'action' => 'user_add_role_action.' . $role,
];
$this
->submitForm($edit, 'Apply to selected items');
$user_storage
->resetCache([
$account
->id(),
]);
$account = $user_storage
->load($account
->id());
$this
->assertTrue($account
->hasRole($role), 'The user now has the custom role.');
$edit = [
'user_bulk_form[1]' => TRUE,
'action' => 'user_remove_role_action.' . $role,
];
$this
->submitForm($edit, 'Apply to selected items');
$user_storage
->resetCache([
$account
->id(),
]);
$account = $user_storage
->load($account
->id());
$this
->assertFalse($account
->hasRole($role), 'The user no longer has the custom role.');
$this
->assertTrue($account
->isActive(), 'The user is not blocked.');
$this
->assertSession()
->pageTextContains($account
->label());
$edit = [
'user_bulk_form[1]' => TRUE,
'action' => 'user_block_user_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$user_storage
->resetCache([
$account
->id(),
]);
$account = $user_storage
->load($account
->id());
$this
->assertTrue($account
->isBlocked(), 'The user is blocked.');
$this
->assertSession()
->pageTextNotContains($account
->label());
$view = Views::getView('test_user_bulk_form');
$view
->removeHandler('default', 'filter', 'status');
$view->storage
->save();
$this
->drupalGet('test-user-bulk-form');
$this
->assertSession()
->pageTextContains($this
->config('user.settings')
->get('anonymous'));
$edit = [
'user_bulk_form[0]' => TRUE,
'action' => 'user_block_user_action',
];
$this
->submitForm($edit, 'Apply to selected items');
$anonymous_account = $user_storage
->load(0);
$this
->assertTrue($anonymous_account
->isBlocked(), 'Ensure the anonymous user got blocked.');
$this
->drupalLogin($this
->drupalCreateUser([
'administer permissions',
'administer views',
'administer users',
]));
$action_id = 'user_add_role_action.' . $role;
$edit = [
'options[include_exclude]' => 'exclude',
"options[selected_actions][{$action_id}]" => $action_id,
];
$this
->drupalGet('admin/structure/views/nojs/handler/test_user_bulk_form/default/field/user_bulk_form');
$this
->submitForm($edit, 'Apply');
$this
->submitForm([], 'Save');
$this
->drupalGet('test-user-bulk-form');
$this
->assertSession()
->optionNotExists('edit-action', $action_id);
$edit['options[include_exclude]'] = 'include';
$this
->drupalGet('admin/structure/views/nojs/handler/test_user_bulk_form/default/field/user_bulk_form');
$this
->submitForm($edit, 'Apply');
$this
->submitForm([], 'Save');
$this
->drupalGet('test-user-bulk-form');
$this
->assertSession()
->optionExists('edit-action', $action_id);
}
public function testBulkFormCombineFilter() {
User::load($this->users[0]
->id());
$view = Views::getView('test_user_bulk_form_combine_filter');
$errors = $view
->validate();
$this
->assertEquals(t('Field %field set in %filter is not usable for this filter type. Combined field filter only works for simple fields.', [
'%field' => 'User: Bulk update',
'%filter' => 'Global: Combine fields filter',
]), reset($errors['default']));
}
}