View source
<?php
namespace Drupal\Tests\domain\Functional;
use Drupal\domain\DomainInterface;
use Drupal\user\UserInterface;
class DomainAdminElementTest extends DomainTestBase {
public static $modules = [
'domain',
'field',
'field_ui',
'user',
];
protected function setUp() {
parent::setUp();
$this
->domainCreateTestDomains(5);
}
public function testDomainAccessElement() {
$admin = $this
->drupalCreateUser([
'bypass node access',
'administer content types',
'administer users',
'administer domains',
]);
$this
->drupalLogin($admin);
$this
->drupalGet('admin/people/create');
$this
->assertSession()
->statusCodeEquals(200);
$this
->fillField('name', 'testuser');
$this
->fillField('mail', 'test@example.com');
$this
->fillField('pass[pass1]', 'test');
$this
->fillField('pass[pass2]', 'test');
$domains = \Drupal::entityTypeManager()
->getStorage('domain')
->loadMultiple();
$count = 0;
$ids = [
'example_com',
'one_example_com',
'two_example_com',
];
foreach ($domains as $domain) {
$locator = DomainInterface::DOMAIN_ADMIN_FIELD . '[' . $domain
->id() . ']';
$this
->findField($locator);
if (in_array($domain
->id(), $ids)) {
$this
->checkField($locator);
}
}
$this
->pressButton('edit-submit');
$this
->assertSession()
->statusCodeEquals(200);
$storage = \Drupal::entityTypeManager()
->getStorage('user');
$user = $storage
->load(3);
$manager = \Drupal::service('domain.element_manager');
$values = $manager
->getFieldValues($user, DomainInterface::DOMAIN_ADMIN_FIELD);
$this
->assert(count($values) == 3, 'User saved with three domain records.');
$account = $this
->drupalCreateUser([
'administer users',
'assign domain administrators',
]);
$ids = [
'example_com',
'one_example_com',
];
$this
->addDomainsToEntity('user', $account
->id(), $ids, DomainInterface::DOMAIN_ADMIN_FIELD);
$tester = $storage
->load($account
->id());
$values = $manager
->getFieldValues($tester, DomainInterface::DOMAIN_ADMIN_FIELD);
$this
->assert(count($values) == 2, 'User saved with two domain records.');
$storage
->resetCache([
$account
->id(),
]);
$this
->drupalLogin($account);
$this
->drupalGet('user/' . $user
->id() . '/edit');
$this
->assertSession()
->statusCodeEquals(200);
foreach ($domains as $domain) {
$locator = DomainInterface::DOMAIN_ADMIN_FIELD . '[' . $domain
->id() . ']';
$this
->findField($locator);
if ($domain
->id() == 'example_com') {
$this
->checkField($locator);
}
elseif ($domain
->id() == 'one_example_com') {
$this
->uncheckField($locator);
}
else {
$this
->assertSession()
->fieldNotExists($locator);
}
}
$this
->pressButton('edit-submit');
$this
->assertSession()
->statusCodeEquals(200);
$storage
->resetCache([
$user
->id(),
]);
$user = $storage
->load($user
->id());
$values = $manager
->getFieldValues($user, DomainInterface::DOMAIN_ADMIN_FIELD);
$this
->assert(count($values) == 2, 'User saved with two domain records.');
$config = \Drupal::configFactory()
->getEditable('user.settings');
$config
->set('verify_mail', 0);
$config
->set('register', UserInterface::REGISTER_VISITORS);
$config
->save();
$this
->drupalLogout();
$this
->drupalGet('user/register');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseNotContains('Domain administrator');
foreach ($domains as $domain) {
$locator = DomainInterface::DOMAIN_ADMIN_FIELD . '[' . $domain
->id() . ']';
$this
->assertSession()
->fieldNotExists($locator);
}
$this
->fillField('name', 'testuser2');
$this
->fillField('mail', 'test2@example.com');
if (!empty($this
->findField('pass[pass1]'))) {
$this
->fillField('pass[pass1]', 'test');
$this
->fillField('pass[pass2]', 'test');
}
$this
->pressButton('edit-submit');
$this
->assertSession()
->statusCodeEquals(200);
}
}