comparison core/modules/quickedit/tests/src/FunctionalJavascript/QuickEditAutocompleteTermTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php
2
3 namespace Drupal\Tests\quickedit\FunctionalJavascript;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8 use Drupal\taxonomy\Entity\Vocabulary;
9 use Drupal\taxonomy\Entity\Term;
10 use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
11 use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
12
13 /**
14 * Tests in-place editing of autocomplete tags.
15 *
16 * @group quickedit
17 */
18 class QuickEditAutocompleteTermTest extends WebDriverTestBase {
19
20 use EntityReferenceTestTrait;
21 use ContextualLinkClickTrait;
22
23 /**
24 * {@inheritdoc}
25 */
26 public static $modules = [
27 'node',
28 'taxonomy',
29 'quickedit',
30 'contextual',
31 'ckeditor',
32 ];
33
34 /**
35 * Stores the node used for the tests.
36 *
37 * @var \Drupal\node\NodeInterface
38 */
39 protected $node;
40
41 /**
42 * Stores the vocabulary used in the tests.
43 *
44 * @var \Drupal\taxonomy\VocabularyInterface
45 */
46 protected $vocabulary;
47
48 /**
49 * Stores the first term used in the tests.
50 *
51 * @var \Drupal\taxonomy\TermInterface
52 */
53 protected $term1;
54
55 /**
56 * Stores the second term used in the tests.
57 *
58 * @var \Drupal\taxonomy\TermInterface
59 */
60 protected $term2;
61
62 /**
63 * Stores the field name for the autocomplete field.
64 *
65 * @var string
66 */
67 protected $fieldName;
68
69 /**
70 * An user with permissions to access in-place editor.
71 *
72 * @var \Drupal\user\UserInterface
73 */
74 protected $editorUser;
75
76 /**
77 * {@inheritdoc}
78 */
79 protected function setUp() {
80 parent::setUp();
81
82 $this->drupalCreateContentType([
83 'type' => 'article',
84 ]);
85 $this->vocabulary = Vocabulary::create([
86 'name' => 'quickedit testing tags',
87 'vid' => 'quickedit_testing_tags',
88 ]);
89 $this->vocabulary->save();
90 $this->fieldName = 'field_' . $this->vocabulary->id();
91
92 $handler_settings = [
93 'target_bundles' => [
94 $this->vocabulary->id() => $this->vocabulary->id(),
95 ],
96 'auto_create' => TRUE,
97 ];
98 $this->createEntityReferenceField('node', 'article', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
99
100 entity_get_form_display('node', 'article', 'default')
101 ->setComponent($this->fieldName, [
102 'type' => 'entity_reference_autocomplete_tags',
103 'weight' => -4,
104 ])
105 ->save();
106
107 entity_get_display('node', 'article', 'default')
108 ->setComponent($this->fieldName, [
109 'type' => 'entity_reference_label',
110 'weight' => 10,
111 ])
112 ->save();
113 entity_get_display('node', 'article', 'teaser')
114 ->setComponent($this->fieldName, [
115 'type' => 'entity_reference_label',
116 'weight' => 10,
117 ])
118 ->save();
119
120 $this->term1 = $this->createTerm();
121 $this->term2 = $this->createTerm();
122
123 $node = [];
124 $node['type'] = 'article';
125 $node[$this->fieldName][]['target_id'] = $this->term1->id();
126 $node[$this->fieldName][]['target_id'] = $this->term2->id();
127 $this->node = $this->drupalCreateNode($node);
128
129 $this->editorUser = $this->drupalCreateUser([
130 'access content',
131 'create article content',
132 'edit any article content',
133 'administer nodes',
134 'access contextual links',
135 'access in-place editing',
136 ]);
137 }
138
139 /**
140 * Tests Quick Edit autocomplete term behavior.
141 */
142 public function testAutocompleteQuickEdit() {
143 $page = $this->getSession()->getPage();
144 $assert = $this->assertSession();
145
146 $this->drupalLogin($this->editorUser);
147 $this->drupalGet('node/' . $this->node->id());
148
149 // Wait "Quick edit" button for node.
150 $assert->waitForElement('css', '[data-quickedit-entity-id="node/' . $this->node->id() . '"] .contextual .quickedit');
151 // Click by "Quick edit".
152 $this->clickContextualLink('[data-quickedit-entity-id="node/' . $this->node->id() . '"]', 'Quick edit');
153 // Switch to body field.
154 $page->find('css', '[data-quickedit-field-id="node/' . $this->node->id() . '/' . $this->fieldName . '/' . $this->node->language()->getId() . '/full"]')->click();
155
156 // Open Quick Edit.
157 $quickedit_field_locator = '[name="field_quickedit_testing_tags[target_id]"]';
158 $tag_field = $assert->waitForElementVisible('css', $quickedit_field_locator);
159 $tag_field->focus();
160 $tags = $tag_field->getValue();
161
162 // Check existing terms.
163 $this->assertTrue(strpos($tags, $this->term1->label()) !== FALSE);
164 $this->assertTrue(strpos($tags, $this->term2->label()) !== FALSE);
165
166 // Add new term.
167 $new_tag = $this->randomMachineName();
168 $tags .= ', ' . $new_tag;
169 $assert->waitForElementVisible('css', $quickedit_field_locator)->setValue($tags);
170 $assert->waitOnAutocomplete();
171 // Wait and click by "Save" button after body field was changed.
172 $assert->waitForElementVisible('css', '.quickedit-toolgroup.ops [type="submit"][aria-hidden="false"]')->click();
173 $assert->waitOnAutocomplete();
174
175 // Reload the page and check new term.
176 $this->drupalGet('node/' . $this->node->id());
177 $link = $assert->waitForLink($new_tag);
178 $this->assertNotEmpty($link);
179 }
180
181 /**
182 * Returns a new term with random name and description in $this->vocabulary.
183 *
184 * @return \Drupal\Core\Entity\EntityInterface|\Drupal\taxonomy\Entity\Term
185 * The created taxonomy term.
186 *
187 * @throws \Drupal\Core\Entity\EntityStorageException
188 */
189 protected function createTerm() {
190 $filter_formats = filter_formats();
191 $format = array_pop($filter_formats);
192 $term = Term::create([
193 'name' => $this->randomMachineName(),
194 'description' => $this->randomMachineName(),
195 // Use the first available text format.
196 'format' => $format->id(),
197 'vid' => $this->vocabulary->id(),
198 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
199 ]);
200 $term->save();
201 return $term;
202 }
203
204 }