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',
80 $this->drupalLogin($account);
84 * Test if save button is appears on form.
86 public function testEntityForm() {
87 /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
88 $browser = $this->container->get('entity_type.manager')
89 ->getStorage('entity_browser')
90 ->load('entity_browser_test_entity_form');
92 // Make sure that the "Save entities" button exists.
93 $this->drupalGet('entity-browser/iframe/entity_browser_test_entity_form');
94 $this->assertSession()->buttonExists('Save entity');
96 // Change save button's text and make sure that the change was respected.
97 $config = $browser->getWidget('9c6ee4c0-4642-4203-b4bd-ec0bad068ad3')->getConfiguration();
98 $config['settings']['submit_text'] = 'Save node';
99 $browser->getWidget('9c6ee4c0-4642-4203-b4bd-ec0bad068ad3')->setConfiguration($config);
101 $this->drupalGet('entity-browser/iframe/entity_browser_test_entity_form');
102 $this->assertSession()->buttonNotExists('Save entity');
103 $this->assertSession()->buttonExists('Save node');
105 // Make sure that the widget works correctly with the field widget
106 $this->drupalGet('node/add/foo');
107 $this->getSession()->getPage()->clickLink('Select entities');
108 $this->getSession()->switchToIFrame('entity_browser_iframe_entity_browser_test_entity_form');
109 $this->getSession()->getPage()->fillField('inline_entity_form[title][0][value]', 'War is peace');
110 $this->getSession()->getPage()->pressButton('Save node');
112 // Switch back to the main page.
113 $this->getSession()->switchToIFrame();
114 $this->assertSession()->assertWaitOnAjaxRequest();
116 $this->assertSession()->pageTextContains('War is peace');
117 $this->getSession()->getPage()->fillField('title[0][value]', 'Freedom is slavery');
118 $this->getSession()->getPage()->pressButton('Save');
120 $parent_node = $this->container->get('entity_type.manager')
122 ->loadByProperties(['title' => 'Freedom is slavery']);
123 $parent_node = current($parent_node);
124 $this->assertEquals(1, $parent_node->get('field_reference')->count(), 'There is one child node.');
125 $this->assertEquals('War is peace', $parent_node->field_reference->entity->label(), 'Child node has correct title.');
127 // Now try using Multi value selection display and make sure there is only
128 // one node created by the Entity browser.
129 $browser->setSelectionDisplay('multi_step_display')->save();
130 $this->drupalGet('node/add/foo');
131 $this->getSession()->getPage()->clickLink('Select entities');
132 $this->getSession()->switchToIFrame('entity_browser_iframe_entity_browser_test_entity_form');
133 $this->getSession()->getPage()->fillField('inline_entity_form[title][0][value]', 'War is peace');
134 $this->getSession()->getPage()->pressButton('Save node');
135 $this->getSession()->getPage()->pressButton('Use selected');
137 // Switch back to the main page.
138 $this->getSession()->switchToIFrame();
139 $this->assertSession()->assertWaitOnAjaxRequest();
141 $this->assertSession()->pageTextContains('War is peace');
142 $this->getSession()->getPage()->fillField('title[0][value]', 'Ignorance is strength');
143 $this->getSession()->getPage()->pressButton('Save');
145 $parent_node = $this->container->get('entity_type.manager')
147 ->loadByProperties(['title' => 'Ignorance is strength']);
148 $parent_node = current($parent_node);
149 $this->assertEquals(1, $parent_node->get('field_reference')->count(), 'There is one child node.');
150 $this->assertEquals('War is peace', $parent_node->field_reference->entity->label(), 'Child node has correct title.');