Pull merge.
[yaffs-website] / web / core / modules / taxonomy / tests / src / Kernel / PendingRevisionTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Kernel;
4
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;
12
13 /**
14  * Kernel tests for taxonomy pending revisions.
15  *
16  * @group taxonomy
17  */
18 class PendingRevisionTest extends KernelTestBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $modules = ['taxonomy', 'node', 'user', 'text', 'field', 'system'];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30
31     $this->installEntitySchema('user');
32     $this->installEntitySchema('node');
33     $this->installEntitySchema('taxonomy_term');
34     $this->installSchema('node', 'node_access');
35   }
36
37   /**
38    * Tests that the taxonomy index work correctly with pending revisions.
39    */
40   public function testTaxonomyIndexWithPendingRevision() {
41     \Drupal::configFactory()->getEditable('taxonomy.settings')->set('maintain_index_table', TRUE)->save();
42
43     Vocabulary::create([
44       'name' => 'test',
45       'vid' => 'test',
46     ])->save();
47     $term = Term::create([
48       'name' => 'term1',
49       'vid' => 'test',
50     ]);
51     $term->save();
52     $term2 = Term::create([
53       'name' => 'term2',
54       'vid' => 'test',
55     ]);
56     $term2->save();
57
58     NodeType::create([
59       'type' => 'page',
60     ])->save();
61
62     FieldStorageConfig::create([
63       'entity_type' => 'node',
64       'field_name' => 'field_tags',
65       'type' => 'entity_reference',
66       'settings' => [
67         'target_type' => 'taxonomy_term',
68       ],
69     ])->save();
70
71     FieldConfig::create([
72       'field_name' => 'field_tags',
73       'entity_type' => 'node',
74       'bundle' => 'page',
75     ])->save();
76     $node = Node::create([
77       'type' => 'page',
78       'title' => 'test_title',
79       'field_tags' => [$term->id()],
80     ]);
81     $node->save();
82
83     $taxonomy_index = $this->getTaxonomyIndex();
84     $this->assertEquals($term->id(), $taxonomy_index[$node->id()]->tid);
85
86     // Normal new revision.
87     $node->setNewRevision(TRUE);
88     $node->isDefaultRevision(TRUE);
89     $node->field_tags->target_id = $term2->id();
90     $node->save();
91
92     $taxonomy_index = $this->getTaxonomyIndex();
93     $this->assertEquals($term2->id(), $taxonomy_index[$node->id()]->tid);
94
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();
99     $node->save();
100
101     $taxonomy_index = $this->getTaxonomyIndex();
102     $this->assertEquals($term2->id(), $taxonomy_index[$node->id()]->tid);
103
104     // Check that making the previously created pending revision the default
105     // revision updates the taxonomy index correctly.
106     $node->isDefaultRevision(TRUE);
107     $node->save();
108
109     $taxonomy_index = $this->getTaxonomyIndex();
110     $this->assertEquals($term->id(), $taxonomy_index[$node->id()]->tid);
111   }
112
113   protected function getTaxonomyIndex() {
114     return \Drupal::database()->select('taxonomy_index')
115       ->fields('taxonomy_index')
116       ->execute()
117       ->fetchAllAssoc('nid');
118   }
119
120 }