Chris@0: drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access'])); Chris@0: Chris@0: // Create a vocabulary and add two term reference fields to article nodes. Chris@0: $this->vocabulary = $this->createVocabulary(); Chris@0: Chris@17: $this->fieldName1 = mb_strtolower($this->randomMachineName()); 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->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); Chris@0: Chris@0: entity_get_form_display('node', 'article', 'default') Chris@0: ->setComponent($this->fieldName1, [ Chris@0: 'type' => 'options_select', Chris@0: ]) Chris@0: ->save(); Chris@0: entity_get_display('node', 'article', 'default') Chris@0: ->setComponent($this->fieldName1, [ Chris@0: 'type' => 'entity_reference_label', Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@17: $this->fieldName2 = mb_strtolower($this->randomMachineName()); Chris@0: $this->createEntityReferenceField('node', 'article', $this->fieldName2, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); Chris@0: Chris@0: entity_get_form_display('node', 'article', 'default') Chris@0: ->setComponent($this->fieldName2, [ Chris@0: 'type' => 'options_select', Chris@0: ]) Chris@0: ->save(); Chris@0: entity_get_display('node', 'article', 'default') Chris@0: ->setComponent($this->fieldName2, [ Chris@0: 'type' => 'entity_reference_label', Chris@0: ]) Chris@0: ->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that the taxonomy index is maintained properly. Chris@0: */ Chris@0: public function testTaxonomyIndex() { Chris@0: $node_storage = $this->container->get('entity.manager')->getStorage('node'); Chris@0: // Create terms in the vocabulary. Chris@0: $term_1 = $this->createTerm($this->vocabulary); Chris@0: $term_2 = $this->createTerm($this->vocabulary); Chris@0: Chris@0: // Post an article. Chris@0: $edit = []; Chris@0: $edit['title[0][value]'] = $this->randomMachineName(); Chris@0: $edit['body[0][value]'] = $this->randomMachineName(); Chris@0: $edit["{$this->fieldName1}[]"] = $term_1->id(); Chris@0: $edit["{$this->fieldName2}[]"] = $term_1->id(); Chris@0: $this->drupalPostForm('node/add/article', $edit, t('Save')); Chris@0: Chris@0: // Check that the term is indexed, and only once. Chris@0: $node = $this->drupalGetNodeByTitle($edit['title[0][value]']); Chris@0: $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [ Chris@0: ':nid' => $node->id(), Chris@0: ':tid' => $term_1->id(), Chris@0: ])->fetchField(); Chris@0: $this->assertEqual(1, $index_count, 'Term 1 is indexed once.'); Chris@0: Chris@0: // Update the article to change one term. Chris@0: $edit["{$this->fieldName1}[]"] = $term_2->id(); Chris@0: $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); Chris@0: Chris@0: // Check that both terms are indexed. Chris@0: $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [ Chris@0: ':nid' => $node->id(), Chris@0: ':tid' => $term_1->id(), Chris@0: ])->fetchField(); Chris@0: $this->assertEqual(1, $index_count, 'Term 1 is indexed.'); Chris@0: $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [ Chris@0: ':nid' => $node->id(), Chris@0: ':tid' => $term_2->id(), Chris@0: ])->fetchField(); Chris@0: $this->assertEqual(1, $index_count, 'Term 2 is indexed.'); Chris@0: Chris@0: // Update the article to change another term. Chris@0: $edit["{$this->fieldName2}[]"] = $term_2->id(); Chris@0: $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); Chris@0: Chris@0: // Check that only one term is indexed. Chris@0: $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [ Chris@0: ':nid' => $node->id(), Chris@0: ':tid' => $term_1->id(), Chris@0: ])->fetchField(); Chris@0: $this->assertEqual(0, $index_count, 'Term 1 is not indexed.'); Chris@0: $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [ Chris@0: ':nid' => $node->id(), Chris@0: ':tid' => $term_2->id(), Chris@0: ])->fetchField(); Chris@0: $this->assertEqual(1, $index_count, 'Term 2 is indexed once.'); Chris@0: Chris@0: // Redo the above tests without interface. Chris@0: $node_storage->resetCache([$node->id()]); Chris@0: $node = $node_storage->load($node->id()); Chris@0: $node->title = $this->randomMachineName(); Chris@0: Chris@0: // Update the article with no term changed. Chris@0: $node->save(); Chris@0: Chris@0: // Check that the index was not changed. Chris@0: $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [ Chris@0: ':nid' => $node->id(), Chris@0: ':tid' => $term_1->id(), Chris@0: ])->fetchField(); Chris@0: $this->assertEqual(0, $index_count, 'Term 1 is not indexed.'); Chris@0: $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [ Chris@0: ':nid' => $node->id(), Chris@0: ':tid' => $term_2->id(), Chris@0: ])->fetchField(); Chris@0: $this->assertEqual(1, $index_count, 'Term 2 is indexed once.'); Chris@0: Chris@0: // Update the article to change one term. Chris@0: $node->{$this->fieldName1} = [['target_id' => $term_1->id()]]; Chris@0: $node->save(); Chris@0: Chris@0: // Check that both terms are indexed. Chris@0: $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [ Chris@0: ':nid' => $node->id(), Chris@0: ':tid' => $term_1->id(), Chris@0: ])->fetchField(); Chris@0: $this->assertEqual(1, $index_count, 'Term 1 is indexed.'); Chris@0: $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [ Chris@0: ':nid' => $node->id(), Chris@0: ':tid' => $term_2->id(), Chris@0: ])->fetchField(); Chris@0: $this->assertEqual(1, $index_count, 'Term 2 is indexed.'); Chris@0: Chris@0: // Update the article to change another term. Chris@0: $node->{$this->fieldName2} = [['target_id' => $term_1->id()]]; Chris@0: $node->save(); Chris@0: Chris@0: // Check that only one term is indexed. Chris@0: $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [ Chris@0: ':nid' => $node->id(), Chris@0: ':tid' => $term_1->id(), Chris@0: ])->fetchField(); Chris@0: $this->assertEqual(1, $index_count, 'Term 1 is indexed once.'); Chris@0: $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [ Chris@0: ':nid' => $node->id(), Chris@0: ':tid' => $term_2->id(), Chris@0: ])->fetchField(); Chris@0: $this->assertEqual(0, $index_count, 'Term 2 is not indexed.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that there is a link to the parent term on the child term page. Chris@0: */ Chris@0: public function testTaxonomyTermHierarchyBreadcrumbs() { Chris@0: // Create two taxonomy terms and set term2 as the parent of term1. Chris@0: $term1 = $this->createTerm($this->vocabulary); Chris@0: $term2 = $this->createTerm($this->vocabulary); Chris@0: $term1->parent = [$term2->id()]; Chris@0: $term1->save(); Chris@0: Chris@0: // Verify that the page breadcrumbs include a link to the parent term. Chris@0: $this->drupalGet('taxonomy/term/' . $term1->id()); Chris@0: // Breadcrumbs are not rendered with a language, prevent the term Chris@0: // language from being added to the options. Chris@18: $this->assertRaw(\Drupal::l($term2->getName(), $term2->toUrl('canonical', ['language' => NULL])), 'Parent term link is displayed when viewing the node.'); Chris@0: } Chris@0: Chris@0: }