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 pending revisions.
18 class PendingRevisionTest 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 pending revisions.
40 public function testTaxonomyIndexWithPendingRevision() {
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 pending revision does not affect the taxonomy index.
96 $node->setNewRevision(TRUE);
97 $node->isDefaultRevision(FALSE);
98 $node->field_tags->target_id = $term->id();
101 $taxonomy_index = $this->getTaxonomyIndex();
102 $this->assertEquals($term2->id(), $taxonomy_index[$node->id()]->tid);
104 // Check that making the previously created pending revision the default
105 // revision updates the taxonomy index correctly.
106 $node->isDefaultRevision(TRUE);
109 $taxonomy_index = $this->getTaxonomyIndex();
110 $this->assertEquals($term->id(), $taxonomy_index[$node->id()]->tid);
113 protected function getTaxonomyIndex() {
114 return \Drupal::database()->select('taxonomy_index')
115 ->fields('taxonomy_index')
117 ->fetchAllAssoc('nid');