EntityFormWidgetTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 150
rs 10
c 2
b 0
f 0
wmc 2
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 45 1
B testEntityForm() 0 76 1
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
      'create article content',
79
      'access content',
80
    ]);
81
    $this->drupalLogin($account);
82
  }
83
84
  /**
85
   * Test if save button is appears on form.
86
   */
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');
92
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');
96
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);
101
    $browser->save();
102
    $this->drupalGet('entity-browser/iframe/entity_browser_test_entity_form');
103
    $this->assertSession()->buttonNotExists('Save entity');
104
    $this->assertSession()->buttonExists('Save node');
105
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');
112
113
    // Switch back to the main page.
114
    $this->getSession()->switchToIFrame();
115
    $this->assertSession()->assertWaitOnAjaxRequest();
116
117
    $this->assertSession()->pageTextContains('War is peace');
118
    $this->getSession()->getPage()->fillField('title[0][value]', 'Freedom is slavery');
119
    $this->getSession()->getPage()->pressButton('Save');
120
121
    $parent_node = $this->container->get('entity_type.manager')
122
      ->getStorage('node')
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.');
127
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');
137
138
    // Switch back to the main page.
139
    $this->getSession()->switchToIFrame();
140
    $this->assertSession()->assertWaitOnAjaxRequest();
141
142
    $this->assertSession()->pageTextContains('War is peace');
143
    $this->getSession()->getPage()->fillField('title[0][value]', 'Ignorance is strength');
144
    $this->getSession()->getPage()->pressButton('Save');
145
146
    $parent_node = $this->container->get('entity_type.manager')
147
      ->getStorage('node')
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.');
152
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',
157
      'access content',
158
    ]);
159
    $this->drupalLogin($account);
160
    $this->drupalGet('entity-browser/iframe/entity_browser_test_entity_form');
161
    $this->assertSession()->pageTextContains('No widgets are available.');
162
  }
163
164
}
165