Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / entity_browser / modules / entity_form / tests / src / FunctionalJavascript / EntityFormWidgetTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_browser_entity_form\FunctionalJavascript;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
9
10 /**
11  * Class for Entity browser entity form Javascript functional tests.
12  *
13  * @group entity_browser_entity_form
14  */
15 class EntityFormWidgetTest extends JavascriptTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = [
23     'entity_browser_entity_form_test',
24     'ctools',
25     'views',
26     'block',
27     'node',
28     'file',
29     'image',
30     'field_ui',
31     'views_ui',
32     'system',
33   ];
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUp() {
39     parent::setUp();
40
41     $this->drupalCreateContentType(['type' => 'foo', 'name' => 'Foo']);
42
43     FieldStorageConfig::create([
44       'field_name' => 'field_reference',
45       'type' => 'entity_reference',
46       'entity_type' => 'node',
47       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
48       'settings' => [
49         'target_type' => 'node',
50       ],
51     ])->save();
52
53     FieldConfig::create([
54       'field_name' => 'field_reference',
55       'entity_type' => 'node',
56       'bundle' => 'foo',
57       'label' => 'Reference',
58       'settings' => [],
59     ])->save();
60
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');
65
66     $form_display->setComponent('field_reference', [
67       'type' => 'entity_browser_entity_reference',
68       'settings' => [
69         'entity_browser' => 'entity_browser_test_entity_form',
70         'field_widget_display' => 'label',
71         'open' => TRUE,
72       ],
73     ])->save();
74
75     $account = $this->drupalCreateUser([
76       'access entity_browser_test_entity_form entity browser pages',
77       'create foo content',
78       'access content',
79     ]);
80     $this->drupalLogin($account);
81   }
82
83   /**
84    * Test if save button is appears on form.
85    */
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');
91
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');
95
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);
100     $browser->save();
101     $this->drupalGet('entity-browser/iframe/entity_browser_test_entity_form');
102     $this->assertSession()->buttonNotExists('Save entity');
103     $this->assertSession()->buttonExists('Save node');
104
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');
111
112     // Switch back to the main page.
113     $this->getSession()->switchToIFrame();
114     $this->assertSession()->assertWaitOnAjaxRequest();
115
116     $this->assertSession()->pageTextContains('War is peace');
117     $this->getSession()->getPage()->fillField('title[0][value]', 'Freedom is slavery');
118     $this->getSession()->getPage()->pressButton('Save');
119
120     $parent_node = $this->container->get('entity_type.manager')
121       ->getStorage('node')
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.');
126
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');
136
137     // Switch back to the main page.
138     $this->getSession()->switchToIFrame();
139     $this->assertSession()->assertWaitOnAjaxRequest();
140
141     $this->assertSession()->pageTextContains('War is peace');
142     $this->getSession()->getPage()->fillField('title[0][value]', 'Ignorance is strength');
143     $this->getSession()->getPage()->pressButton('Save');
144
145     $parent_node = $this->container->get('entity_type.manager')
146       ->getStorage('node')
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.');
151   }
152
153 }