c760cce7d762026455e5265eec7c264f6e4646da
[yaffs-website] / web / modules / contrib / entity_browser / tests / src / FunctionalJavascript / EntityReferenceWidgetTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\entity_browser\Element\EntityBrowserElement;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\node\Entity\Node;
10 use Drupal\user\Entity\Role;
11
12 /**
13  * Tests the Entity Reference Widget.
14  *
15  * @group entity_browser
16  */
17 class EntityReferenceWidgetTest extends EntityBrowserJavascriptTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function setUp() {
23     parent::setUp();
24
25     /** @var \Drupal\user\RoleInterface $role */
26     $role = Role::load('authenticated');
27     $this->grantPermissions($role, ['access test_entity_browser_iframe_node_view entity browser pages']);
28     $this->grantPermissions($role, ['bypass node access']);
29
30   }
31
32   /**
33    * Tests Entity Reference widget.
34    */
35   public function testEntityReferenceWidget() {
36
37     $page = $this->getSession()->getPage();
38     $assert_session = $this->assertSession();
39
40     // Create an entity_reference field to test the widget.
41     FieldStorageConfig::create([
42       'field_name' => 'field_entity_reference1',
43       'type' => 'entity_reference',
44       'entity_type' => 'node',
45       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
46       'settings' => [
47         'target_type' => 'node',
48       ],
49     ])->save();
50
51     FieldConfig::create([
52       'field_name' => 'field_entity_reference1',
53       'entity_type' => 'node',
54       'bundle' => 'article',
55       'label' => 'Referenced articles',
56       'settings' => [],
57     ])->save();
58
59     /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
60     $form_display = $this->container->get('entity_type.manager')
61       ->getStorage('entity_form_display')
62       ->load('node.article.default');
63
64     $form_display->setComponent('field_entity_reference1', [
65       'type' => 'entity_browser_entity_reference',
66       'settings' => [
67         'entity_browser' => 'test_entity_browser_iframe_node_view',
68         'open' => TRUE,
69         'field_widget_edit' => TRUE,
70         'field_widget_remove' => TRUE,
71         'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
72         'field_widget_display' => 'label',
73         'field_widget_display_settings' => [],
74       ],
75     ])->save();
76
77     // Create a dummy node that will be used as target.
78     $target_node = Node::create([
79       'title' => 'Target example node 1',
80       'type' => 'article',
81     ]);
82     $target_node->save();
83
84     $this->drupalGet('/node/add/article');
85     $page->fillField('title[0][value]', 'Referencing node 1');
86     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_node_view');
87     $this->waitForAjaxToFinish();
88     $page->checkField('edit-entity-browser-select-node1');
89     $page->pressButton('Select entities');
90     $this->getSession()->switchToIFrame();
91     $this->waitForAjaxToFinish();
92     $page->pressButton('Save');
93
94     $assert_session->pageTextContains('Article Referencing node 1 has been created.');
95     $nid = $this->container->get('entity.query')->get('node')->condition('title', 'Referencing node 1')->execute();
96     $nid = reset($nid);
97
98     $this->drupalGet('node/' . $nid . '/edit');
99     $assert_session->pageTextContains('Target example node 1');
100     // Make sure both "Edit" and "Remove" buttons are visible.
101     $assert_session->buttonExists('edit-field-entity-reference1-current-items-0-remove-button');
102     $assert_session->buttonExists('edit-field-entity-reference1-current-items-0-edit-button');
103
104     // Test whether changing these definitions on the browser config effectively
105     // change the visibility of the buttons.
106     $form_display->setComponent('field_entity_reference1', [
107       'type' => 'entity_browser_entity_reference',
108       'settings' => [
109         'entity_browser' => 'test_entity_browser_iframe_node_view',
110         'open' => TRUE,
111         'field_widget_edit' => FALSE,
112         'field_widget_remove' => FALSE,
113         'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
114         'field_widget_display' => 'label',
115         'field_widget_display_settings' => [],
116       ],
117     ])->save();
118     $this->drupalGet('node/' . $nid . '/edit');
119     $assert_session->buttonNotExists('edit-field-entity-reference1-current-items-0-remove-button');
120     $assert_session->buttonNotExists('edit-field-entity-reference1-current-items-0-edit-button');
121
122     // Set them to visible again.
123     $form_display->setComponent('field_entity_reference1', [
124       'type' => 'entity_browser_entity_reference',
125       'settings' => [
126         'entity_browser' => 'test_entity_browser_iframe_node_view',
127         'open' => TRUE,
128         'field_widget_edit' => TRUE,
129         'field_widget_remove' => TRUE,
130         'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
131         'field_widget_display' => 'label',
132         'field_widget_display_settings' => [],
133       ],
134     ])->save();
135     $this->drupalGet('node/' . $nid . '/edit');
136     $assert_session->buttonExists('edit-field-entity-reference1-current-items-0-remove-button');
137     $assert_session->buttonExists('edit-field-entity-reference1-current-items-0-edit-button');
138
139     // Test the "Remove" button on the widget works.
140     $page->pressButton('Remove');
141     $this->waitForAjaxToFinish();
142     $assert_session->pageTextNotContains('Target example node 1');
143
144     // Verify that if the user cannot edit the entity, the "Edit" button does
145     // not show up, even if configured to.
146     /** @var \Drupal\user\RoleInterface $role */
147     $role = Role::load('authenticated');
148     $role->revokePermission('bypass node access')->trustData()->save();
149     $this->drupalGet('node/add/article');
150     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_node_view');
151     $this->waitForAjaxToFinish();
152     $page->checkField('edit-entity-browser-select-node1');
153     $page->pressButton('Select entities');
154     $this->getSession()->switchToIFrame();
155     $this->waitForAjaxToFinish();
156     $assert_session->buttonNotExists('edit-field-entity-reference1-current-items-0-edit-button');
157
158   }
159
160 }