Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / DefaultRevisionStateTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\workflows\Entity\Workflow;
10
11 /**
12  * Tests the correct default revision is set.
13  *
14  * @group content_moderation
15  */
16 class DefaultRevisionStateTest extends KernelTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = [
22     'entity_test',
23     'node',
24     'block_content',
25     'content_moderation',
26     'user',
27     'system',
28     'language',
29     'content_translation',
30     'text',
31     'workflows',
32   ];
33
34   /**
35    * @var \Drupal\Core\Entity\EntityTypeManager
36    */
37   protected $entityTypeManager;
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
44
45     $this->installSchema('node', 'node_access');
46     $this->installEntitySchema('node');
47     $this->installEntitySchema('user');
48     $this->installEntitySchema('entity_test_with_bundle');
49     $this->installEntitySchema('entity_test_rev');
50     $this->installEntitySchema('entity_test_mulrevpub');
51     $this->installEntitySchema('block_content');
52     $this->installEntitySchema('content_moderation_state');
53     $this->installConfig('content_moderation');
54
55     $this->entityTypeManager = $this->container->get('entity_type.manager');
56   }
57
58   /**
59    * Tests a translatable Node.
60    */
61   public function testMultilingual() {
62     // Enable French.
63     ConfigurableLanguage::createFromLangcode('fr')->save();
64     $node_type = NodeType::create([
65       'type' => 'example',
66     ]);
67     $node_type->save();
68
69     $this->container->get('content_translation.manager')->setEnabled('node', 'example', TRUE);
70
71     $workflow = Workflow::load('editorial');
72     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
73     $workflow->save();
74
75     $english_node = Node::create([
76       'type' => 'example',
77       'title' => 'Test title',
78     ]);
79     // Revision 1 (en).
80     $english_node
81       ->setUnpublished()
82       ->save();
83     $this->assertEquals('draft', $english_node->moderation_state->value);
84     $this->assertFalse($english_node->isPublished());
85     $this->assertTrue($english_node->isDefaultRevision());
86     $this->assertModerationState($english_node->getRevisionId(), $english_node->language()->getId(), 'draft');
87
88     // Revision 2 (fr)
89     $french_node = $english_node->addTranslation('fr', ['title' => 'French title']);
90     $french_node->moderation_state->value = 'published';
91     $french_node->save();
92     $this->assertTrue($french_node->isPublished());
93     $this->assertTrue($french_node->isDefaultRevision());
94     $this->assertModerationState($french_node->getRevisionId(), $french_node->language()->getId(), 'published');
95
96     // Revision 3 (fr)
97     $node = Node::load($english_node->id())->getTranslation('fr');
98     $node->moderation_state->value = 'draft';
99     $node->save();
100     $this->assertFalse($node->isPublished());
101     $this->assertFalse($node->isDefaultRevision());
102     $this->assertModerationState($node->getRevisionId(), $node->language()->getId(), 'draft');
103
104     // Revision 4 (en)
105     $latest_revision = $this->entityTypeManager->getStorage('node')->loadRevision(3);
106     $latest_revision->moderation_state->value = 'draft';
107     $latest_revision->save();
108     $this->assertFalse($latest_revision->isPublished());
109     $this->assertFalse($latest_revision->isDefaultRevision());
110     $this->assertModerationState($latest_revision->getRevisionId(), $latest_revision->language()->getId(), 'draft');
111   }
112
113   /**
114    * Verifies the expected moderation state revision exists.
115    *
116    * @param int $revision_id
117    *   The revision ID of the host entity.
118    * @param string $langcode
119    *   The language code of the host entity to check.
120    * @param string $expected_state
121    *   The state the content moderation state revision should be in.
122    * @param string $expected_workflow
123    *   The workflow the content moderation state revision should be using.
124    */
125   protected function assertModerationState($revision_id, $langcode, $expected_state, $expected_workflow = 'editorial') {
126     $moderation_state_storage = $this->entityTypeManager->getStorage('content_moderation_state');
127
128     $query = $moderation_state_storage->getQuery();
129     $results = $query->allRevisions()
130       ->condition('content_entity_revision_id', $revision_id)
131       ->condition('langcode', $langcode)
132       ->execute();
133     $this->assertCount(1, $results);
134
135     $moderation_state = $moderation_state_storage
136       ->loadRevision(key($results))
137       ->getTranslation($langcode);
138     $this->assertEquals($expected_state, $moderation_state->get('moderation_state')->value);
139     $this->assertEquals($expected_workflow, $moderation_state->get('workflow')->target_id);
140   }
141
142 }