3 namespace Drupal\Tests\entity_browser_entity_form\FunctionalJavascript;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
11 * Class for Entity browser entity form Javascript functional tests.
13 * @group entity_browser_entity_form
15 class EntityFormWidgetTest extends JavascriptTestBase {
22 public static $modules = [
23 'entity_browser_entity_form_test',
38 protected function setUp() {
41 $this->drupalCreateContentType(['type' => 'foo', 'name' => 'Foo']);
43 FieldStorageConfig::create([
44 'field_name' => 'field_reference',
45 'type' => 'entity_reference',
46 'entity_type' => 'node',
47 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
49 'target_type' => 'node',
54 'field_name' => 'field_reference',
55 'entity_type' => 'node',
57 'label' => 'Reference',
61 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
62 $form_display = $this->container->get('entity_type.manager')
63 ->getStorage('entity_form_display')
64 ->load('node.foo.default');
66 $form_display->setComponent('field_reference', [
67 'type' => 'entity_browser_entity_reference',
69 'entity_browser' => 'entity_browser_test_entity_form',
70 'field_widget_display' => 'label',
75 $account = $this->drupalCreateUser([
76 'access entity_browser_test_entity_form entity browser pages',
78 'create article content',
81 $this->drupalLogin($account);
85 * Test if save button is appears on form.
87 public function testEntityForm() {
88 /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
89 $browser = $this->container->get('entity_type.manager')
90 ->getStorage('entity_browser')
91 ->load('entity_browser_test_entity_form');
93 // Make sure that the "Save entities" button exists.
94 $this->drupalGet('entity-browser/iframe/entity_browser_test_entity_form');
95 $this->assertSession()->buttonExists('Save entity');
97 // Change save button's text and make sure that the change was respected.
98 $config = $browser->getWidget('9c6ee4c0-4642-4203-b4bd-ec0bad068ad3')->getConfiguration();
99 $config['settings']['submit_text'] = 'Save node';
100 $browser->getWidget('9c6ee4c0-4642-4203-b4bd-ec0bad068ad3')->setConfiguration($config);
102 $this->drupalGet('entity-browser/iframe/entity_browser_test_entity_form');
103 $this->assertSession()->buttonNotExists('Save entity');
104 $this->assertSession()->buttonExists('Save node');
106 // Make sure that the widget works correctly with the field widget
107 $this->drupalGet('node/add/foo');
108 $this->getSession()->getPage()->clickLink('Select entities');
109 $this->getSession()->switchToIFrame('entity_browser_iframe_entity_browser_test_entity_form');
110 $this->getSession()->getPage()->fillField('inline_entity_form[title][0][value]', 'War is peace');
111 $this->getSession()->getPage()->pressButton('Save node');
113 // Switch back to the main page.
114 $this->getSession()->switchToIFrame();
115 $this->assertSession()->assertWaitOnAjaxRequest();
117 $this->assertSession()->pageTextContains('War is peace');
118 $this->getSession()->getPage()->fillField('title[0][value]', 'Freedom is slavery');
119 $this->getSession()->getPage()->pressButton('Save');
121 $parent_node = $this->container->get('entity_type.manager')
123 ->loadByProperties(['title' => 'Freedom is slavery']);
124 $parent_node = current($parent_node);
125 $this->assertEquals(1, $parent_node->get('field_reference')->count(), 'There is one child node.');
126 $this->assertEquals('War is peace', $parent_node->field_reference->entity->label(), 'Child node has correct title.');
128 // Now try using Multi value selection display and make sure there is only
129 // one node created by the Entity browser.
130 $browser->setSelectionDisplay('multi_step_display')->save();
131 $this->drupalGet('node/add/foo');
132 $this->getSession()->getPage()->clickLink('Select entities');
133 $this->getSession()->switchToIFrame('entity_browser_iframe_entity_browser_test_entity_form');
134 $this->getSession()->getPage()->fillField('inline_entity_form[title][0][value]', 'War is peace');
135 $this->getSession()->getPage()->pressButton('Save node');
136 $this->getSession()->getPage()->pressButton('Use selected');
138 // Switch back to the main page.
139 $this->getSession()->switchToIFrame();
140 $this->assertSession()->assertWaitOnAjaxRequest();
142 $this->assertSession()->pageTextContains('War is peace');
143 $this->getSession()->getPage()->fillField('title[0][value]', 'Ignorance is strength');
144 $this->getSession()->getPage()->pressButton('Save');
146 $parent_node = $this->container->get('entity_type.manager')
148 ->loadByProperties(['title' => 'Ignorance is strength']);
149 $parent_node = current($parent_node);
150 $this->assertEquals(1, $parent_node->get('field_reference')->count(), 'There is one child node.');
151 $this->assertEquals('War is peace', $parent_node->field_reference->entity->label(), 'Child node has correct title.');
153 // Make sure entity create access is respected.
154 $account = $this->drupalCreateUser([
155 'access entity_browser_test_entity_form entity browser pages',
156 'create foo content',
159 $this->drupalLogin($account);
160 $this->drupalGet('entity-browser/iframe/entity_browser_test_entity_form');
161 $this->assertSession()->pageTextContains('No widgets are available.');