View source
<?php
namespace Drupal\Tests\domain\Functional;
use Drupal\domain\DomainInterface;
use Drupal\domain_access\DomainAccessManagerInterface;
class DomainReferencesTest extends DomainTestBase {
public static $modules = [
'domain',
'domain_access',
'field',
'field_ui',
'user',
];
protected function setUp() {
parent::setUp();
$this
->domainCreateTestDomains(5);
}
public function testDomainReferences() {
$admin = $this
->drupalCreateUser([
'bypass node access',
'administer content types',
'administer users',
'administer domains',
'assign domain editors',
]);
$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();
$ids = [
'example_com',
'one_example_com',
'two_example_com',
];
$edit_ids = [
'example_com',
'one_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);
}
$locator = DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD . '[' . $domain
->id() . ']';
$this
->findField($locator);
if (in_array($domain
->id(), $edit_ids)) {
$this
->checkField($locator);
}
}
$locator = DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD . '[value]';
$this
->findField($locator);
$this
->pressButton('edit-submit');
$this
->assertSession()
->statusCodeEquals(200);
$storage = \Drupal::entityTypeManager()
->getStorage('user');
$testuser = $storage
->load(3);
$manager = \Drupal::service('domain.element_manager');
$values = $manager
->getFieldValues($testuser, DomainInterface::DOMAIN_ADMIN_FIELD);
$this
->assert(count($values) == 3, 'User saved with three domain admin records.');
$values = $manager
->getFieldValues($testuser, DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD);
$this
->assert(count($values) == 2, 'User saved with two domain access 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);
$limited_admin = $storage
->load($account
->id());
$values = $manager
->getFieldValues($limited_admin, DomainInterface::DOMAIN_ADMIN_FIELD);
$this
->assert(count($values) == 2, 'User saved with two domain admin records.');
$values = $manager
->getFieldValues($limited_admin, DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD);
$this
->assert(count($values) == 0, 'User saved with no domain access records.');
$this
->drupalLogin($account);
$this
->drupalGet('user/' . $testuser
->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);
}
$locator = DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD . '[' . $domain
->id() . ']';
$this
->assertSession()
->fieldNotExists($locator);
}
$locator = DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD . '[value]';
$this
->assertSession()
->fieldNotExists($locator);
$this
->pressButton('edit-submit');
$this
->assertSession()
->statusCodeEquals(200);
$storage
->resetCache([
$testuser
->id(),
]);
$testuser = $storage
->load($testuser
->id());
$values = $manager
->getFieldValues($testuser, DomainInterface::DOMAIN_ADMIN_FIELD);
$this
->assert(count($values) == 2, 'User saved with two domain admin records.');
$values = $manager
->getFieldValues($testuser, DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD);
$this
->assert(count($values) == 2, 'User saved with two domain access records.');
$new_account = $this
->drupalCreateUser([
'administer users',
'assign domain administrators',
'assign domain editors',
]);
$ids = [
'example_com',
'one_example_com',
];
$new_ids = [
'one_example_com',
'four_example_com',
];
$this
->addDomainsToEntity('user', $new_account
->id(), $ids, DomainInterface::DOMAIN_ADMIN_FIELD);
$this
->addDomainsToEntity('user', $new_account
->id(), $new_ids, DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD);
$new_admin = $storage
->load($new_account
->id());
$values = $manager
->getFieldValues($new_admin, DomainInterface::DOMAIN_ADMIN_FIELD);
$this
->assert(count($values) == 2, 'User saved with two domain admin records.');
$values = $manager
->getFieldValues($new_admin, DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD);
$this
->assert(count($values) == 2, 'User saved with two domain access records.');
$storage
->resetCache([
$new_admin
->id(),
]);
$this
->drupalLogin($new_account);
$this
->drupalGet('user/' . $testuser
->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);
}
$locator = DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD . '[' . $domain
->id() . ']';
if (in_array($domain
->id(), $new_ids)) {
$this
->findField($locator);
$this
->checkField($locator);
}
else {
$this
->assertSession()
->fieldNotExists($locator);
}
}
$locator = DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD . '[value]';
$this
->assertSession()
->fieldNotExists($locator);
$this
->pressButton('edit-submit');
$this
->assertSession()
->statusCodeEquals(200);
$storage
->resetCache([
$testuser
->id(),
]);
$testuser = $storage
->load($testuser
->id());
$values = $manager
->getFieldValues($testuser, DomainInterface::DOMAIN_ADMIN_FIELD);
$this
->assert(count($values) == 2, 'User saved with two domain admin records.');
$values = $manager
->getFieldValues($testuser, DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD);
$this
->assert(count($values) == 3, 'User saved with three domain access records.');
}
}