View source
<?php
namespace Drupal\Tests\domain_access\Functional;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\domain\Functional\DomainTestBase;
use Drupal\domain_access\DomainAccessManagerInterface;
class DomainAccessFieldTest extends DomainTestBase {
public static $modules = [
'domain',
'domain_access',
'field',
'field_ui',
'user',
];
protected function setUp() {
parent::setUp();
$this
->domainCreateTestDomains(5);
}
public function testDomainAccessFields() {
$label = 'Send to all affiliates';
$user1 = $this
->drupalCreateUser([
'create article content',
'publish to any domain',
]);
$this
->drupalLogin($user1);
$this
->drupalGet('node/add/article');
$this
->assertResponse(200, 'Article creation found.');
$domains = \Drupal::entityTypeManager()
->getStorage('domain')
->loadMultiple();
foreach ($domains as $domain) {
$this
->assertText($domain
->label(), 'Domain form item found.');
}
$this
->assertText($label, 'All affiliates field found.');
$user2 = $this
->drupalCreateUser([
'create article content',
'publish to any assigned domain',
]);
$active_domain = array_rand($domains, 1);
$this
->addDomainsToEntity('user', $user2
->id(), $active_domain, DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD);
$this
->drupalLogin($user2);
$this
->drupalGet('node/add/article');
$this
->assertResponse(200, 'Article creation found.');
foreach ($domains as $domain) {
if ($domain
->id() == $active_domain) {
$this
->assertRaw('>' . $domain
->label() . '</label>', 'Domain form item found.');
}
else {
$this
->assertNoRaw('>' . $domain
->label() . '</label>', 'Domain form item not found.');
}
}
$this
->assertNoText($label, 'All affiliates field not found.');
$user3 = $this
->drupalCreateUser([
'create article content',
]);
$this
->drupalLogin($user3);
$this
->drupalGet('node/add/article');
$this
->assertResponse(200, 'Article creation found.');
foreach ($domains as $domain) {
$this
->assertNoText($domain
->label(), 'Domain form item not found.');
}
$this
->assertNoText($label, 'All affiliates field not found.');
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName(8);
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertNotNull($node, 'Node found in database.');
$user4 = $this
->drupalCreateUser([
'administer users',
'assign editors to any domain',
]);
$this
->drupalLogin($user4);
$this
->drupalGet('admin/people/create');
$this
->assertResponse(200, 'User creation found.');
foreach ($domains as $domain) {
$this
->assertText($domain
->label(), 'Domain form item found.');
}
$user5 = $this
->drupalCreateUser([
'administer users',
'assign domain editors',
]);
$active_domain = array_rand($domains, 1);
$this
->addDomainsToEntity('user', $user5
->id(), $active_domain, DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD);
$this
->drupalLogin($user5);
$this
->drupalGet('admin/people/create');
$this
->assertResponse(200, 'User creation found.');
foreach ($domains as $domain) {
if ($domain
->id() == $active_domain) {
$this
->assertRaw('>' . $domain
->label() . '</label>', 'Domain form item found.');
}
else {
$this
->assertNoRaw('>' . $domain
->label() . '</label>', 'Domain form item not found.');
}
}
$user6 = $this
->drupalCreateUser([
'administer users',
]);
$this
->drupalLogin($user6);
$this
->drupalGet('admin/people/create');
$this
->assertResponse(200, 'User creation found.');
foreach ($domains as $domain) {
$this
->assertNoText($domain
->label(), 'Domain form item not found.');
}
$user7 = $this
->drupalCreateUser([
'bypass node access',
'publish to any domain',
]);
$this
->drupalLogin($user7);
$type = $this
->drupalCreateContentType();
$type_exists = (bool) NodeType::load($type
->id());
$this
->assertTrue($type_exists, 'The new content type has been created in the database.');
domain_access_confirm_fields('node', $type
->id());
$this
->drupalGet('node/add/' . $type
->id());
$this
->assertResponse(200, $type
->id() . ' creation found.');
$domains = \Drupal::entityTypeManager()
->getStorage('domain')
->loadMultiple();
foreach ($domains as $domain) {
$this
->assertText($domain
->label(), 'Domain form item found.');
}
$this
->assertText($label, 'All affiliates field found.');
$user8 = $this
->drupalCreateUser([
'change own username',
]);
$this
->drupalLogin($user8);
$user_edit_page = 'user/' . $user8
->id() . '/edit';
$this
->drupalGet($user_edit_page);
$domains = \Drupal::entityTypeManager()
->getStorage('domain')
->loadMultiple();
foreach ($domains as $domain) {
$this
->assertNoText($domain
->label(), 'Domain form item not found.');
}
$this
->assertNoText($label, 'All affiliates field not found.');
$edit = [];
$edit['name'] = $this
->randomMachineName();
$this
->drupalGet($user_edit_page);
$this
->submitForm($edit, 'Save');
}
}