View source
<?php
namespace Drupal\Tests\userprotect\Functional;
use Drupal\user\Entity\User;
class UnsavedUserFieldAccessTest extends UserProtectBrowserTestBase {
protected $account;
protected function setUp() {
parent::setUp();
$this->account = $this
->drupalCreateUser([
'administer users',
'administer permissions',
]);
$this
->drupalLogin($this->account);
}
public function testUserCreate() {
$name = $this
->randomMachineName();
$edit = [
'name' => $name,
'mail' => $this
->randomMachineName() . '@example.com',
'pass[pass1]' => $pass = $this
->randomString(),
'pass[pass2]' => $pass,
'notify' => FALSE,
];
$this
->drupalPostForm('admin/people/create', $edit, t('Create new account'));
$this
->assertSession()
->pageTextContains(t('Created a new user account for @name. No email has been sent.', [
'@name' => $edit['name'],
]), 'User created');
$edit = [
'name' => $name,
'mail' => $this
->randomMachineName() . '@example.com',
'pass[pass1]' => $pass = $this
->randomString(),
'pass[pass2]' => $pass,
'notify' => FALSE,
];
$this
->drupalPostForm('admin/people/create', $edit, t('Create new account'));
$this
->assertSession()
->pageTextContains(t('The username @name is already taken.', [
'@name' => $edit['name'],
]));
}
public function testNameAccessForUnsavedUser() {
$module_handler = $this->container
->get('module_handler');
$module_installer = $this->container
->get('module_installer');
$unsavedUserEntity = User::create([]);
$this
->assertTrue($unsavedUserEntity
->isAnonymous(), 'Unsaved user is considered anonymous when userprotect is installed.');
$this
->assertTrue($unsavedUserEntity
->get('name')
->access('edit'), 'Logged in user is allowed to edit name field when userprotect is installed.');
$module_installer
->uninstall([
'userprotect_test',
'userprotect',
]);
$this
->rebuildContainer();
$module_handler = $this->container
->get('module_handler');
$this
->assertFalse($module_handler
->moduleExists('userprotect'), 'Userprotect uninstalled successfully.');
$this
->assertTrue($unsavedUserEntity
->isAnonymous(), 'Unsaved user is considered anonymous when userprotect is uninstalled.');
$this
->assertTrue($unsavedUserEntity
->get('name')
->access('edit'), 'Logged in user is allowed to edit name field when userprotect is uninstalled.');
}
}