3 namespace Drupal\Tests\taxonomy\Kernel;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\node\Entity\Node;
9 use Drupal\node\Entity\NodeType;
10 use Drupal\taxonomy\Entity\Term;
11 use Drupal\taxonomy\Entity\Vocabulary;
14 * Kernel tests for taxonomy forward revisions.
18 class ForwardRevisionTest extends KernelTestBase {
23 public static $modules = ['taxonomy', 'node', 'user', 'text', 'field', 'system'];
28 protected function setUp() {
31 $this->installEntitySchema('user');
32 $this->installEntitySchema('node');
33 $this->installEntitySchema('taxonomy_term');
34 $this->installSchema('node', 'node_access');
38 * Tests that the taxonomy index work correctly with forward revisions.
40 public function testTaxonomyIndexWithForwardRevision() {
41 \Drupal::configFactory()->getEditable('taxonomy.settings')->set('maintain_index_table', TRUE)->save();
47 $term = Term::create([
52 $term2 = Term::create([
62 FieldStorageConfig::create([
63 'entity_type' => 'node',
64 'field_name' => 'field_tags',
65 'type' => 'entity_reference',
67 'target_type' => 'taxonomy_term',
72 'field_name' => 'field_tags',
73 'entity_type' => 'node',
76 $node = Node::create([
78 'title' => 'test_title',
79 'field_tags' => [$term->id()],
83 $taxonomy_index = $this->getTaxonomyIndex();
84 $this->assertEquals($term->id(), $taxonomy_index[$node->id()]->tid);
86 // Normal new revision.
87 $node->setNewRevision(TRUE);
88 $node->isDefaultRevision(TRUE);
89 $node->field_tags->target_id = $term2->id();
92 $taxonomy_index = $this->getTaxonomyIndex();
93 $this->assertEquals($term2->id(), $taxonomy_index[$node->id()]->tid);
95 // Check that saving a forward (non-default) revision does not affect the
97 $node->setNewRevision(TRUE);
98 $node->isDefaultRevision(FALSE);
99 $node->field_tags->target_id = $term->id();
102 $taxonomy_index = $this->getTaxonomyIndex();
103 $this->assertEquals($term2->id(), $taxonomy_index[$node->id()]->tid);
105 // Check that making the previously created forward-revision the default
106 // revision updates the taxonomy index correctly.
107 $node->isDefaultRevision(TRUE);
110 $taxonomy_index = $this->getTaxonomyIndex();
111 $this->assertEquals($term->id(), $taxonomy_index[$node->id()]->tid);
114 protected function getTaxonomyIndex() {
115 return \Drupal::database()->select('taxonomy_index')
116 ->fields('taxonomy_index')
118 ->fetchAllAssoc('nid');