annotate core/modules/quickedit/tests/src/FunctionalJavascript/QuickEditAutocompleteTermTest.php @ 19:fa3358dc1485 tip

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