Completed
Push — 8.x-1.x ( a94db3...a3ff3e )
by Janez
02:24
created

EntityFormWidgetTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 33
nc 1
nop 0
dl 0
loc 44
rs 8.8571
c 0
b 0
f 0
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
    $this->drupalGet('node/add/foo');
88
    $this->getSession()->getPage()->clickLink('Select entities');
89
    $this->getSession()->switchToIFrame('entity_browser_iframe_entity_browser_test_entity_form');
90
    $this->assertSession()->buttonExists('Save entity');
91
    /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
92
    $browser = $this->container->get('entity_type.manager')
93
      ->getStorage('entity_browser')
94
      ->load('entity_browser_test_entity_form');
95
    $entity_form_widget = $browser->getWidget('9c6ee4c0-4642-4203-b4bd-ec0bad068ad3');
96
    // Update submit text in widget settings.
97
    $entity_form_widget->setConfiguration([
98
      'settings' => [
99
        'entity_type' => 'node',
100
        'bundle' => 'article',
101
        'form_mode' => 'default',
102
        'submit_text' => 'Save node',
103
      ],
104
      'uuid' => '9c6ee4c0-4642-4203-b4bd-ec0bad068ad3',
105
      'weight' => 2,
106
      'label' => 'entity_form',
107
      'id' => 'entity_form',
108
    ]);
109
    $browser->save();
110
    $this->drupalGet('node/add/foo');
111
    $this->getSession()->getPage()->clickLink('Select entities');
112
    $this->getSession()->switchToIFrame('entity_browser_iframe_entity_browser_test_entity_form');
113
    // Assert changes in widget configuration is respected.
114
    $this->assertSession()->buttonNotExists('Save entity');
115
    $this->assertSession()->buttonExists('Save node');
116
  }
117
118
}
119