View source
<?php
namespace Drupal\Tests\domain\Functional;
class DomainEntityReferenceTest extends DomainTestBase {
public static $modules = [
'domain',
'field',
'field_ui',
];
public function testDomainField() {
$this->admin_user = $this
->drupalCreateUser([
'administer content types',
'administer node fields',
'administer node display',
'administer domains',
]);
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('admin/structure/types/manage/article/fields');
$this
->assertResponse(200, 'Manage fields page accessed.');
$this
->assertNoText('Domain test field', 'Domain form field not found.');
$this
->drupalGet('admin/structure/types/manage/article/display');
$this
->assertResponse(200, 'Manage field display page accessed.');
$this
->assertNoText('Domain test field', 'Domain form field not found.');
$this
->domainCreateTestField();
$this
->drupalGet('admin/structure/types/manage/article/fields');
$this
->assertText('Domain test field', 'Added a test field instance.');
$this
->drupalGet('admin/structure/types/manage/article/display');
$this
->assertText('Domain test field', 'Added a test field display instance.');
$this
->drupalGet('admin/config/people/accounts/fields/user.user.field_domain_access');
}
public function testDomainFieldStorage() {
$this->admin_user = $this
->drupalCreateUser([
'bypass node access',
'administer content types',
'administer node fields',
'administer node display',
'administer domains',
]);
$this
->drupalLogin($this->admin_user);
$this
->domainCreateTestField();
$this
->domainCreateTestDomains(5);
$this
->drupalGet('node/add/article');
$this
->assertResponse(200);
$this
->assertText('Domain test field', 'Found the domain field instance.');
$domains = \Drupal::entityTypeManager()
->getStorage('domain')
->loadMultiple();
foreach ($domains as $domain) {
$string = 'value="' . $domain
->id() . '"';
$this
->assertRaw($string, 'Found the domain option');
if (!isset($one)) {
$one = $domain
->id();
continue;
}
if (!isset($two)) {
$two = $domain
->id();
}
}
$edit['title[0][value]'] = 'Test node';
$edit["field_domain[{$one}]"] = TRUE;
$edit["field_domain[{$two}]"] = TRUE;
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$this
->assertResponse(200);
$node = \Drupal::entityTypeManager()
->getStorage('node')
->load(1);
$values = $node
->get('field_domain');
$this
->assertCount(2, $values, 'Node saved with two domain records.');
}
public function domainCreateTestField() {
$label = 'domain';
$name = 'field_' . $label;
$storage = [
'field_name' => $name,
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => -1,
'settings' => [
'target_type' => 'domain',
],
];
$field_storage_config = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->create($storage);
$field_storage_config
->save();
$field = [
'field_name' => $name,
'entity_type' => 'node',
'label' => 'Domain test field',
'bundle' => 'article',
'settings' => [
'handler_settings' => [
'sort' => [
'field' => 'weight',
'direction' => 'ASC',
],
],
],
];
$field_config = \Drupal::entityTypeManager()
->getStorage('field_config')
->create($field);
$field_config
->save();
if ($display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('node.article.default')) {
$display
->setComponent($name, [
'type' => 'options_buttons',
])
->save();
}
}
}