View source
<?php
namespace Drupal\Tests\field\FunctionalJavascript\EntityReference;
use Drupal\Core\Url;
use Behat\Mink\Element\NodeElement;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
class EntityReferenceAdminTest extends WebDriverTestBase {
use FieldUiTestTrait;
public static $modules = [
'node',
'field_ui',
'path',
'taxonomy',
'block',
'views_ui',
];
protected $defaultTheme = 'stark';
protected $type;
protected function setUp() {
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();
$admin_user = $this
->drupalCreateUser([
'access content',
'administer node fields',
'administer node display',
'administer views',
'create ' . $type_name . ' content',
'edit own ' . $type_name . ' 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
->assertOption('edit-new-storage-type', 'field_ui:entity_reference:node');
$this
->assertOption('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[2]/span[contains(text(), "field_test")]');
$this
->assertNotEmpty($machine_name);
$page
->pressButton('Save and continue');
$this
->assertFieldByName('settings[target_type]', 'node');
$this
->assertFieldSelectOptions('settings[target_type]', array_keys(\Drupal::entityTypeManager()
->getDefinitions()));
$this
->drupalPostForm(NULL, [], t('Save field settings'));
$this
->assertFieldByName('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
->assertFieldByName('settings[handler_settings][target_bundles][' . $bundle_name . ']');
$page
->findField('settings[handler_settings][target_bundles][' . $bundle_name . ']')
->setValue($bundle_name);
$assert_session
->assertWaitOnAjaxRequest();
}
reset($bundles);
$this
->assertFieldByName('settings[handler_settings][sort][field]', '_none');
$sort_by = $page
->findField('settings[handler_settings][sort][field]');
$this
->assertNoFieldByName('settings[handler_settings][sort][direction]');
$sort_by
->setValue('nid');
$assert_session
->waitForField('settings[handler_settings][sort][direction]');
$this
->assertFieldByName('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.');
$this
->assertFieldByXPath("//select[@name='settings[handler_settings][sort][field]']/option[@value='nid']");
$this
->assertFieldByXPath("//select[@name='settings[handler_settings][sort][field]']/option[@value='title']");
$this
->assertFieldByXPath("//select[@name='settings[handler_settings][sort][field]']/option[@value='body.value']");
$sort_by
->setValue('_none');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertNoFieldByName('settings[handler_settings][sort][direction]');
$this
->drupalPostForm(NULL, [
'required' => '1',
], t('Save settings'));
$this
->assertFieldByXPath('//table[@id="field-overview"]//tr[@id="field-test"]/td[1]', 'Test', 'Field was created and appears in the overview page.');
$this
->clickLink(t('Edit'), 1);
$this
->drupalPostForm(NULL, [], t('Save settings'));
$field_name = 'node.' . $this->type . '.field_test';
$edit = [
'settings[target_type]' => 'taxonomy_term',
];
$this
->drupalPostForm($bundle_path . '/fields/' . $field_name . '/storage', $edit, t('Save field settings'));
$this
->drupalGet($bundle_path . '/fields/' . $field_name);
$this
->assertFieldByName('settings[handler_settings][auto_create]');
$field_name = 'node.' . $this->type . '.field_test';
$edit = [
'settings[target_type]' => 'user',
];
$this
->drupalPostForm($bundle_path . '/fields/' . $field_name . '/storage', $edit, t('Save field settings'));
$this
->drupalGet($bundle_path . '/fields/' . $field_name);
$this
->assertFieldByName('settings[handler_settings][filter][type]', '_none');
$this
->assertFieldByName('settings[handler_settings][sort][field]', '_none');
$field_name = 'node.' . $this->type . '.field_test';
$edit = [
'settings[target_type]' => 'node',
];
$this
->drupalPostForm($bundle_path . '/fields/' . $field_name . '/storage', $edit, t('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
->drupalPostForm(NULL, [], t('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
->drupalPostForm(NULL, [], t('Save settings'));
$assert_session
->pageTextContains('Saved Test configuration.');
$edit = [
'settings[target_type]' => 'entity_test',
];
$this
->drupalPostForm($bundle_path . '/fields/' . $field_name . '/storage', $edit, t('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
->drupalPostForm(NULL, $edit, t('Save settings'));
$assert_session
->pageTextContains('Saved Test configuration.');
}
protected function assertFieldSelectOptions($name, array $expected_options) {
$xpath = $this
->buildXPathQuery('//select[@name=:name]', [
':name' => $name,
]);
$fields = $this
->xpath($xpath);
if ($fields) {
$field = $fields[0];
$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');
});
sort($options);
sort($expected_options);
$this
->assertIdentical($options, $expected_options);
}
else {
$this
->fail('Unable to find field ' . $name);
}
}
}