View source
<?php
namespace Drupal\Tests\field\FunctionalJavascript\EntityReference;
use Behat\Mink\Element\NodeElement;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class EntityReferenceAdminTest extends WebDriverTestBase {
use FieldUiTestTrait;
protected static $modules = [
'node',
'field_ui',
'path',
'taxonomy',
'block',
'views_ui',
];
protected $defaultTheme = 'stark';
protected $type;
protected $target_type;
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$type_name = strtolower($this
->randomMachineName(8)) . '_test';
$type = $this
->drupalCreateContentType([
'name' => $type_name,
'type' => $type_name,
]);
$this->type = $type
->id();
$type_name = strtolower($this
->randomMachineName(8)) . '_test';
$type = $this
->drupalCreateContentType([
'name' => $type_name,
'type' => $type_name,
]);
$this->target_type = $type
->id();
$fields = \Drupal::service('entity_field.manager')
->getFieldDefinitions('node', $type
->id());
$fields['title']
->getConfig($type
->id())
->setLabel($type
->id() . ' title')
->save();
FieldStorageConfig::create([
'field_name' => 'field_text',
'entity_type' => 'node',
'type' => 'text',
'entity_types' => [
'node',
],
])
->save();
FieldConfig::create([
'label' => 'Text Field',
'field_name' => 'field_text',
'entity_type' => 'node',
'bundle' => $this->target_type,
'settings' => [],
'required' => FALSE,
])
->save();
$admin_user = $this
->drupalCreateUser([
'access content',
'administer node fields',
'administer node display',
'administer views',
'create ' . $this->type . ' content',
'edit own ' . $this->type . ' content',
]);
$this
->drupalLogin($admin_user);
}
public function testFieldAdminHandler() {
$bundle_path = 'admin/structure/types/manage/' . $this->type;
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet($bundle_path . '/fields/add-field');
$this
->assertSession()
->optionExists('edit-new-storage-type', 'field_ui:entity_reference:node');
$this
->assertSession()
->optionExists('edit-new-storage-type', 'field_ui:entity_reference:user');
$page
->findField('new_storage_type')
->setValue('entity_reference');
$assert_session
->waitForField('label')
->setValue('Test');
$machine_name = $assert_session
->waitForElement('xpath', '//*[@id="edit-label-machine-name-suffix"]/span[contains(text(), "field_test")]');
$this
->assertNotEmpty($machine_name);
$page
->pressButton('Save and continue');
$this
->assertSession()
->fieldValueEquals('settings[target_type]', 'node');
$this
->assertFieldSelectOptions('settings[target_type]', array_keys(\Drupal::entityTypeManager()
->getDefinitions()));
$this
->submitForm([], 'Save field settings');
$this
->assertSession()
->fieldValueEquals('settings[handler]', 'default:node');
$entity_type_id = 'node';
$assert_session
->pageTextContains('Content type');
$sort_by = $page
->findField('settings[handler_settings][sort][field]');
$this
->assertNotEmpty($sort_by);
$this
->assertFalse($sort_by
->isVisible(), 'The "sort by" options are hidden.');
$bundles = $this->container
->get('entity_type.bundle.info')
->getBundleInfo($entity_type_id);
foreach ($bundles as $bundle_name => $bundle_info) {
$this
->assertSession()
->fieldExists('settings[handler_settings][target_bundles][' . $bundle_name . ']');
}
reset($bundles);
$this
->assertFieldSelectOptions('settings[handler_settings][sort][field]', [
'_none',
]);
$page
->findField('settings[handler_settings][target_bundles][' . $this->type . ']')
->setValue($this->type);
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'nid');
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'title');
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'body.value');
$assert_session
->optionNotExists('settings[handler_settings][sort][field]', 'field_text.value');
$title_options = $sort_by
->findAll('xpath', 'option[@value="title"]');
$this
->assertEquals(1, count($title_options));
$this
->assertEquals('Title', $title_options[0]
->getText());
$page
->findField('settings[handler_settings][target_bundles][' . $this->target_type . ']')
->setValue($this->target_type);
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'nid');
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'title');
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'body.value');
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'field_text.value');
$page
->findField('settings[handler_settings][target_bundles][' . $this->type . ']')
->uncheck();
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'nid');
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'title');
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'body.value');
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'field_text.value');
$title_options = $sort_by
->findAll('xpath', 'option[@value="title"]');
$this
->assertEquals(1, count($title_options));
$this
->assertEquals($this->target_type . ' title', $title_options[0]
->getText());
$this
->assertSession()
->fieldValueEquals('settings[handler_settings][sort][field]', '_none');
$sort_direction = $page
->findField('settings[handler_settings][sort][direction]');
$this
->assertFalse($sort_direction
->isVisible());
$sort_by
->setValue('nid');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertTrue($sort_direction
->isVisible());
$this
->assertSession()
->fieldValueEquals('settings[handler_settings][sort][direction]', 'ASC');
$labels = array_map(function (NodeElement $element) {
return $element
->getText();
}, $sort_by
->findAll('xpath', 'option'));
for ($i = count($labels) - 1, $sorted = TRUE; $i > 0; --$i) {
if ($labels[$i - 1] > $labels[$i]) {
$sorted = FALSE;
break;
}
}
$this
->assertTrue($sorted, 'The "sort by" options are sorted.');
$sort_by
->setValue('_none');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertFalse($sort_direction
->isVisible());
$sort_by
->setValue('nid');
$assert_session
->assertWaitOnAjaxRequest();
foreach ($bundles as $bundle_name => $bundle_info) {
$this
->assertSession()
->fieldExists('settings[handler_settings][target_bundles][' . $bundle_name . ']');
$page
->findField('settings[handler_settings][target_bundles][' . $bundle_name . ']')
->uncheck();
$assert_session
->assertWaitOnAjaxRequest();
}
$this
->assertFalse($sort_by
->isVisible(), 'The "sort by" options are hidden.');
$this
->assertFalse($sort_direction
->isVisible());
$page
->findField('settings[handler_settings][target_bundles][' . $this->target_type . ']')
->setValue($this->target_type);
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertTrue($sort_by
->isVisible(), 'The "sort by" options are visible.');
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'field_text.value');
$page
->findField('settings[handler_settings][target_bundles][' . $this->target_type . ']')
->uncheck();
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertFalse($sort_by
->isVisible(), 'The "sort by" options are hidden yet again.');
$this
->assertFieldSelectOptions('settings[handler_settings][sort][field]', [
'_none',
]);
$page
->findField('settings[handler_settings][target_bundles][' . $this->target_type . ']')
->setValue($this->target_type);
$assert_session
->assertWaitOnAjaxRequest();
$this
->submitForm([
'required' => '1',
], 'Save settings');
$this
->assertSession()
->elementTextContains('xpath', '//table[@id="field-overview"]//tr[@id="field-test"]/td[1]', "Test");
$this
->clickLink('Edit', 1);
$this
->submitForm([], 'Save settings');
$field_name = 'node.' . $this->type . '.field_test';
$edit = [
'settings[target_type]' => 'taxonomy_term',
];
$this
->drupalGet($bundle_path . '/fields/' . $field_name . '/storage');
$this
->submitForm($edit, 'Save field settings');
$this
->drupalGet($bundle_path . '/fields/' . $field_name);
$this
->assertSession()
->fieldExists('settings[handler_settings][auto_create]');
$field_name = 'node.' . $this->type . '.field_test';
$edit = [
'settings[target_type]' => 'user',
];
$this
->drupalGet($bundle_path . '/fields/' . $field_name . '/storage');
$this
->submitForm($edit, 'Save field settings');
$this
->drupalGet($bundle_path . '/fields/' . $field_name);
$this
->assertSession()
->fieldValueEquals('settings[handler_settings][filter][type]', '_none');
$this
->assertSession()
->fieldValueEquals('settings[handler_settings][sort][field]', '_none');
$assert_session
->optionNotExists('settings[handler_settings][sort][field]', 'nid');
$assert_session
->optionExists('settings[handler_settings][sort][field]', 'uid');
$sort_direction = $page
->findField('settings[handler_settings][sort][direction]');
$this
->assertFalse($sort_direction
->isVisible());
$sort_by
->setValue('name');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertTrue($sort_direction
->isVisible());
$field_name = 'node.' . $this->type . '.field_test';
$edit = [
'settings[target_type]' => 'node',
];
$this
->drupalGet($bundle_path . '/fields/' . $field_name . '/storage');
$this
->submitForm($edit, 'Save field settings');
$this
->drupalGet($bundle_path . '/fields/' . $field_name);
$page
->findField('settings[handler]')
->setValue('views');
$views_text = (string) new FormattableMarkup('No eligible views were found. <a href=":create">Create a view</a> with an <em>Entity Reference</em> display, or add such a display to an <a href=":existing">existing view</a>.', [
':create' => Url::fromRoute('views_ui.add')
->toString(),
':existing' => Url::fromRoute('entity.view.collection')
->toString(),
]);
$assert_session
->waitForElement('xpath', '//a[contains(text(), "Create a view")]');
$assert_session
->responseContains($views_text);
$this
->submitForm([], 'Save settings');
$assert_session
->pageTextContains('The views entity selection mode requires a view.');
$this->container
->get('module_installer')
->install([
'entity_reference_test',
]);
$this
->resetAll();
$this
->drupalGet($bundle_path . '/fields/' . $field_name);
$page
->findField('settings[handler]')
->setValue('views');
$assert_session
->waitForField('settings[handler_settings][view][view_and_display]')
->setValue('test_entity_reference:entity_reference_1');
$this
->submitForm([], 'Save settings');
$assert_session
->pageTextContains('Saved Test configuration.');
$edit = [
'settings[target_type]' => 'entity_test',
];
$this
->drupalGet($bundle_path . '/fields/' . $field_name . '/storage');
$this
->submitForm($edit, 'Save field settings');
$this
->drupalGet($bundle_path . '/fields/' . $field_name);
$page
->findField('settings[handler]')
->setValue('views');
$assert_session
->waitForField('settings[handler_settings][view][view_and_display]')
->setValue('test_entity_reference_entity_test:entity_reference_1');
$edit = [
'required' => FALSE,
];
$this
->submitForm($edit, 'Save settings');
$assert_session
->pageTextContains('Saved Test configuration.');
}
protected function assertFieldSelectOptions($name, array $expected_options) {
$field = $this
->assertSession()
->selectExists($name);
$options = $field
->findAll('xpath', 'option');
$optgroups = $field
->findAll('xpath', 'optgroup');
foreach ($optgroups as $optgroup) {
$options = array_merge($options, $optgroup
->findAll('xpath', 'option'));
}
array_walk($options, function (NodeElement &$option) {
$option = $option
->getAttribute('value');
});
$this
->assertEqualsCanonicalizing($expected_options, $options);
}
}