Chris@0: drupalCreateContentType([ Chris@0: 'type' => 'article', Chris@0: ]); Chris@0: // Create the vocabulary for the tag field. Chris@0: $this->vocabulary = Vocabulary::create([ Chris@0: 'name' => 'quickedit testing tags', Chris@0: 'vid' => 'quickedit_testing_tags', Chris@0: ]); Chris@0: $this->vocabulary->save(); Chris@0: $this->fieldName = 'field_' . $this->vocabulary->id(); Chris@0: Chris@0: $handler_settings = [ Chris@0: 'target_bundles' => [ Chris@0: $this->vocabulary->id() => $this->vocabulary->id(), Chris@0: ], Chris@0: 'auto_create' => TRUE, Chris@0: ]; Chris@0: $this->createEntityReferenceField('node', 'article', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); Chris@0: Chris@0: entity_get_form_display('node', 'article', 'default') Chris@0: ->setComponent($this->fieldName, [ Chris@0: 'type' => 'entity_reference_autocomplete_tags', Chris@0: 'weight' => -4, Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@0: entity_get_display('node', 'article', 'default') Chris@0: ->setComponent($this->fieldName, [ Chris@0: 'type' => 'entity_reference_label', Chris@0: 'weight' => 10, Chris@0: ]) Chris@0: ->save(); Chris@0: entity_get_display('node', 'article', 'teaser') Chris@0: ->setComponent($this->fieldName, [ Chris@0: 'type' => 'entity_reference_label', Chris@0: 'weight' => 10, Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@0: $this->term1 = $this->createTerm(); Chris@0: $this->term2 = $this->createTerm(); Chris@0: Chris@0: $node = []; Chris@0: $node['type'] = 'article'; Chris@0: $node[$this->fieldName][]['target_id'] = $this->term1->id(); Chris@0: $node[$this->fieldName][]['target_id'] = $this->term2->id(); Chris@0: $this->node = $this->drupalCreateNode($node); Chris@0: Chris@0: $this->editorUser = $this->drupalCreateUser(['access content', 'create article content', 'edit any article content', 'access in-place editing']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests Quick Edit autocomplete term behavior. Chris@0: */ Chris@0: public function testAutocompleteQuickEdit() { Chris@0: $this->drupalLogin($this->editorUser); Chris@0: Chris@0: $quickedit_uri = 'quickedit/form/node/' . $this->node->id() . '/' . $this->fieldName . '/' . $this->node->language()->getId() . '/full'; Chris@0: $post = ['nocssjs' => 'true'] + $this->getAjaxPageStatePostData(); Chris@0: $response = $this->drupalPost($quickedit_uri, '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]); Chris@0: $ajax_commands = Json::decode($response); Chris@0: Chris@0: // Prepare form values for submission. drupalPostAJAX() is not suitable for Chris@0: // handling pages with JSON responses, so we need our own solution here. Chris@0: $form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match); Chris@0: $this->assertTrue($form_tokens_found, 'Form tokens found in output.'); Chris@0: Chris@0: if ($form_tokens_found) { Chris@0: $post = [ Chris@0: 'form_id' => 'quickedit_field_form', Chris@0: 'form_token' => $token_match[1], Chris@0: 'form_build_id' => $build_id_match[1], Chris@0: $this->fieldName . '[target_id]' => implode(', ', [$this->term1->getName(), 'new term', $this->term2->getName()]), Chris@0: 'op' => t('Save'), Chris@0: ]; Chris@0: Chris@0: // Submit field form and check response. Should render back all the terms. Chris@0: $response = $this->drupalPost($quickedit_uri, '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]); Chris@0: $this->assertResponse(200); Chris@0: $ajax_commands = Json::decode($response); Chris@0: $this->setRawContent($ajax_commands[0]['data']); Chris@0: $this->assertLink($this->term1->getName()); Chris@0: $this->assertLink($this->term2->getName()); Chris@0: $this->assertText('new term'); Chris@0: $this->assertNoLink('new term'); Chris@0: Chris@0: // Load the form again, which should now get it back from Chris@0: // PrivateTempStore. Chris@0: $quickedit_uri = 'quickedit/form/node/' . $this->node->id() . '/' . $this->fieldName . '/' . $this->node->language()->getId() . '/full'; Chris@0: $post = ['nocssjs' => 'true'] + $this->getAjaxPageStatePostData(); Chris@0: $response = $this->drupalPost($quickedit_uri, '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]); Chris@0: $ajax_commands = Json::decode($response); Chris@0: Chris@0: // The AjaxResponse's first command is an InsertCommand which contains Chris@0: // the form to edit the taxonomy term field, it should contain all three Chris@0: // taxonomy terms, including the one that has just been newly created and Chris@0: // which is not yet stored. Chris@0: $this->setRawContent($ajax_commands[0]['data']); Chris@0: $expected = [ Chris@0: $this->term1->getName() . ' (' . $this->term1->id() . ')', Chris@0: 'new term', Chris@0: $this->term2->getName() . ' (' . $this->term2->id() . ')', Chris@0: ]; Chris@0: $this->assertFieldByName($this->fieldName . '[target_id]', implode(', ', $expected)); Chris@0: Chris@0: // Save the entity. Chris@0: $post = ['nocssjs' => 'true']; Chris@0: $response = $this->drupalPostWithFormat('quickedit/entity/node/' . $this->node->id(), 'json', $post); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // The full node display should now link to all entities, with the new Chris@0: // one created in the database as well. Chris@0: $this->drupalGet('node/' . $this->node->id()); Chris@0: $this->assertLink($this->term1->getName()); Chris@0: $this->assertLink($this->term2->getName()); Chris@0: $this->assertLink('new term'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a new term with random name and description in $this->vocabulary. Chris@0: * Chris@0: * @return \Drupal\taxonomy\TermInterface Chris@0: * The created taxonomy term. Chris@0: */ Chris@0: protected function createTerm() { Chris@0: $filter_formats = filter_formats(); Chris@0: $format = array_pop($filter_formats); Chris@0: $term = Term::create([ Chris@0: 'name' => $this->randomMachineName(), Chris@0: 'description' => $this->randomMachineName(), Chris@0: // Use the first available text format. Chris@0: 'format' => $format->id(), Chris@0: 'vid' => $this->vocabulary->id(), Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: ]); Chris@0: $term->save(); Chris@0: return $term; Chris@0: } Chris@0: Chris@0: }