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