Chris@0: vocabulary = $this->createVocabulary(); Chris@0: Chris@0: // Create 11 terms, which have some sub-string in common, in a Chris@0: // non-alphabetical order, so that we will have more than 10 matches later Chris@0: // when we test the correct number of results is returned, and we can test Chris@0: // the order of the results. The location of the sub-string to match varies Chris@0: // also, since it should not be necessary to start with the sub-string to Chris@0: // match it. Save term IDs to reuse later. Chris@0: $termNames = [ Chris@0: 'aaa 20 bbb', Chris@0: 'aaa 70 bbb', Chris@0: 'aaa 10 bbb', Chris@0: 'aaa 12 bbb', Chris@0: 'aaa 40 bbb', Chris@0: 'aaa 11 bbb', Chris@0: 'aaa 30 bbb', Chris@0: 'aaa 50 bbb', Chris@0: 'aaa 80', Chris@0: 'aaa 90', Chris@0: 'bbb 60 aaa', Chris@0: ]; Chris@0: foreach ($termNames as $termName) { Chris@0: $term = $this->createTerm($this->vocabulary, ['name' => $termName]); Chris@0: $this->termIds[$termName] = $term->id(); Chris@0: } Chris@0: Chris@0: // Create a taxonomy_term_reference field on the article Content Type that Chris@0: // uses a taxonomy_autocomplete widget. Chris@0: $this->fieldName = Unicode::strtolower($this->randomMachineName()); Chris@0: FieldStorageConfig::create([ Chris@0: 'field_name' => $this->fieldName, Chris@0: 'entity_type' => 'node', Chris@0: 'type' => 'entity_reference', Chris@0: 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, Chris@0: 'settings' => [ Chris@0: 'target_type' => 'taxonomy_term', Chris@0: ], Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'field_name' => $this->fieldName, Chris@0: 'bundle' => 'article', Chris@0: 'entity_type' => 'node', Chris@0: 'settings' => [ Chris@0: 'handler' => 'default', Chris@0: 'handler_settings' => [ Chris@0: // Restrict selection of terms to a single vocabulary. Chris@0: 'target_bundles' => [ Chris@0: $this->vocabulary->id() => $this->vocabulary->id(), Chris@0: ], Chris@0: ], Chris@0: ], Chris@0: ])->save(); Chris@0: EntityFormDisplay::load('node.article.default') Chris@0: ->setComponent($this->fieldName, [ Chris@0: 'type' => 'entity_reference_autocomplete', Chris@0: ]) Chris@0: ->save(); Chris@0: EntityViewDisplay::load('node.article.default') Chris@0: ->setComponent($this->fieldName, [ Chris@0: 'type' => 'entity_reference_label', Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@0: // Create a user and then login. Chris@0: $this->adminUser = $this->drupalCreateUser(['create article content']); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Retrieve the autocomplete url. Chris@0: $this->drupalGet('node/add/article'); Chris@0: $result = $this->xpath('//input[@name="' . $this->fieldName . '[0][target_id]"]'); Chris@0: $this->autocompleteUrl = $this->getAbsoluteUrl($result[0]['data-autocomplete-path']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that the autocomplete method returns the good number of results. Chris@0: * Chris@0: * @see \Drupal\taxonomy\Controller\TermAutocompleteController::autocomplete() Chris@0: */ Chris@0: public function testAutocompleteCountResults() { Chris@0: // Test that no matching term found. Chris@0: $data = $this->drupalGetJSON( Chris@0: $this->autocompleteUrl, Chris@0: ['query' => ['q' => 'zzz']] Chris@0: ); Chris@0: $this->assertTrue(empty($data), 'Autocomplete returned no results'); Chris@0: Chris@0: // Test that only one matching term found, when only one matches. Chris@0: $data = $this->drupalGetJSON( Chris@0: $this->autocompleteUrl, Chris@0: ['query' => ['q' => 'aaa 10']] Chris@0: ); Chris@0: $this->assertEqual(1, count($data), 'Autocomplete returned 1 result'); Chris@0: Chris@0: // Test the correct number of matches when multiple are partial matches. Chris@0: $data = $this->drupalGetJSON( Chris@0: $this->autocompleteUrl, Chris@0: ['query' => ['q' => 'aaa 1']] Chris@0: ); Chris@0: $this->assertEqual(3, count($data), 'Autocomplete returned 3 results'); Chris@0: Chris@0: // Tests that only 10 results are returned, even if there are more than 10 Chris@0: // matches. Chris@0: $data = $this->drupalGetJSON( Chris@0: $this->autocompleteUrl, Chris@0: ['query' => ['q' => 'aaa']] Chris@0: ); Chris@0: $this->assertEqual(10, count($data), 'Autocomplete returned only 10 results (for over 10 matches)'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that the autocomplete method returns properly ordered results. Chris@0: * Chris@0: * @see \Drupal\taxonomy\Controller\TermAutocompleteController::autocomplete() Chris@0: */ Chris@0: public function testAutocompleteOrderedResults() { Chris@0: $expectedResults = [ Chris@0: 'aaa 10 bbb', Chris@0: 'aaa 11 bbb', Chris@0: 'aaa 12 bbb', Chris@0: 'aaa 20 bbb', Chris@0: 'aaa 30 bbb', Chris@0: 'aaa 40 bbb', Chris@0: 'aaa 50 bbb', Chris@0: 'aaa 70 bbb', Chris@0: 'bbb 60 aaa', Chris@0: ]; Chris@0: // Build $expected to match the autocomplete results. Chris@0: $expected = []; Chris@0: foreach ($expectedResults as $termName) { Chris@0: $expected[] = [ Chris@0: 'value' => $termName . ' (' . $this->termIds[$termName] . ')', Chris@0: 'label' => $termName Chris@0: ]; Chris@0: } Chris@0: Chris@0: $data = $this->drupalGetJSON( Chris@0: $this->autocompleteUrl, Chris@0: ['query' => ['q' => 'bbb']] Chris@0: ); Chris@0: Chris@0: $this->assertIdentical($expected, $data); Chris@0: } Chris@0: Chris@0: }