View source
<?php
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Core\Link;
use Drupal\Core\Database\Database;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
class TermIndexTest extends TaxonomyTestBase {
public static $modules = [
'views',
];
protected $defaultTheme = 'stark';
protected $vocabulary;
protected $fieldName1;
protected $fieldName2;
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'administer taxonomy',
'bypass node access',
]));
$this->vocabulary = $this
->createVocabulary();
$this->fieldName1 = mb_strtolower($this
->randomMachineName());
$handler_settings = [
'target_bundles' => [
$this->vocabulary
->id() => $this->vocabulary
->id(),
],
'auto_create' => TRUE,
];
$this
->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('node', 'article')
->setComponent($this->fieldName1, [
'type' => 'options_select',
])
->save();
$display_repository
->getViewDisplay('node', 'article')
->setComponent($this->fieldName1, [
'type' => 'entity_reference_label',
])
->save();
$this->fieldName2 = mb_strtolower($this
->randomMachineName());
$this
->createEntityReferenceField('node', 'article', $this->fieldName2, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('node', 'article')
->setComponent($this->fieldName2, [
'type' => 'options_select',
])
->save();
$display_repository
->getViewDisplay('node', 'article')
->setComponent($this->fieldName2, [
'type' => 'entity_reference_label',
])
->save();
}
public function testTaxonomyIndex() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$term_1 = $this
->createTerm($this->vocabulary);
$term_2 = $this
->createTerm($this->vocabulary);
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName();
$edit['body[0][value]'] = $this
->randomMachineName();
$edit["{$this->fieldName1}[]"] = $term_1
->id();
$edit["{$this->fieldName2}[]"] = $term_1
->id();
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$connection = Database::getConnection();
$index_count = $connection
->select('taxonomy_index')
->condition('nid', $node
->id())
->condition('tid', $term_1
->id())
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 1 is indexed once.');
$edit["{$this->fieldName1}[]"] = $term_2
->id();
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, t('Save'));
$index_count = $connection
->select('taxonomy_index')
->condition('nid', $node
->id())
->condition('tid', $term_1
->id())
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 1 is indexed.');
$index_count = $connection
->select('taxonomy_index')
->condition('nid', $node
->id())
->condition('tid', $term_2
->id())
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 2 is indexed.');
$edit["{$this->fieldName2}[]"] = $term_2
->id();
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, t('Save'));
$index_count = $connection
->select('taxonomy_index')
->condition('nid', $node
->id())
->condition('tid', $term_1
->id())
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(0, $index_count, 'Term 1 is not indexed.');
$index_count = $connection
->select('taxonomy_index')
->condition('nid', $node
->id())
->condition('tid', $term_2
->id())
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 2 is indexed once.');
$node_storage
->resetCache([
$node
->id(),
]);
$node = $node_storage
->load($node
->id());
$node->title = $this
->randomMachineName();
$node
->save();
$index_count = $connection
->select('taxonomy_index')
->condition('nid', $node
->id())
->condition('tid', $term_1
->id())
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(0, $index_count, 'Term 1 is not indexed.');
$index_count = $connection
->select('taxonomy_index')
->condition('nid', $node
->id())
->condition('tid', $term_2
->id())
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 2 is indexed once.');
$node->{$this->fieldName1} = [
[
'target_id' => $term_1
->id(),
],
];
$node
->save();
$index_count = $connection
->select('taxonomy_index')
->condition('nid', $node
->id())
->condition('tid', $term_1
->id())
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 1 is indexed.');
$index_count = $connection
->select('taxonomy_index')
->condition('nid', $node
->id())
->condition('tid', $term_2
->id())
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 2 is indexed.');
$node->{$this->fieldName2} = [
[
'target_id' => $term_1
->id(),
],
];
$node
->save();
$index_count = $connection
->select('taxonomy_index')
->condition('nid', $node
->id())
->condition('tid', $term_1
->id())
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(1, $index_count, 'Term 1 is indexed once.');
$index_count = $connection
->select('taxonomy_index')
->condition('nid', $node
->id())
->condition('tid', $term_2
->id())
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(0, $index_count, 'Term 2 is not indexed.');
}
public function testTaxonomyTermHierarchyBreadcrumbs() {
$term1 = $this
->createTerm($this->vocabulary);
$term2 = $this
->createTerm($this->vocabulary);
$term1->parent = [
$term2
->id(),
];
$term1
->save();
$this
->drupalGet('taxonomy/term/' . $term1
->id());
$this
->assertRaw(Link::fromTextAndUrl($term2
->getName(), $term2
->toUrl('canonical', [
'language' => NULL,
]))
->toString(), 'Parent term link is displayed when viewing the node.');
}
}