Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / content_moderation / tests / src / Functional / ModerationStateBlockTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Functional;
4
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
7
8 /**
9  * Tests general content moderation workflow for blocks.
10  *
11  * @group content_moderation
12  */
13 class ModerationStateBlockTest extends ModerationStateTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected function setUp() {
19     parent::setUp();
20
21     // Create the "basic" block type.
22     $bundle = BlockContentType::create([
23       'id' => 'basic',
24       'label' => 'basic',
25       'revision' => FALSE,
26     ]);
27     $bundle->save();
28
29     // Add the body field to it.
30     block_content_add_body_field($bundle->id());
31   }
32
33   /**
34    * Tests moderating custom blocks.
35    *
36    * Blocks and any non-node-type-entities do not have a concept of
37    * "published". As such, we must use the "default revision" to know what is
38    * going to be "published", i.e. visible to the user.
39    *
40    * The one exception is a block that has never been "published". When a block
41    * is first created, it becomes the "default revision". For each edit of the
42    * block after that, Content Moderation checks the "default revision" to
43    * see if it is set to a published moderation state. If it is not, the entity
44    * being saved will become the "default revision".
45    *
46    * The test below is intended, in part, to make this behavior clear.
47    *
48    * @see \Drupal\content_moderation\EntityOperations::entityPresave
49    * @see \Drupal\content_moderation\Tests\ModerationFormTest::testModerationForm
50    */
51   public function testCustomBlockModeration() {
52     $this->drupalLogin($this->rootUser);
53
54     // Enable moderation for custom blocks.
55     $edit['bundles[basic]'] = TRUE;
56     $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/block_content', $edit, t('Save'));
57
58     // Create a custom block at block/add and save it as draft.
59     $body = 'Body of moderated block';
60     $edit = [
61       'info[0][value]' => 'Moderated block',
62       'moderation_state[0][state]' => 'draft',
63       'body[0][value]' => $body,
64     ];
65     $this->drupalPostForm('block/add', $edit, t('Save'));
66     $this->assertText(t('basic Moderated block has been created.'));
67
68     // Place the block in the Sidebar First region.
69     $instance = [
70       'id' => 'moderated_block',
71       'settings[label]' => $edit['info[0][value]'],
72       'region' => 'sidebar_first',
73     ];
74     $block = BlockContent::load(1);
75     $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default');
76     $this->drupalPostForm($url, $instance, t('Save block'));
77
78     // Navigate to home page and check that the block is visible. It should be
79     // visible because it is the default revision.
80     $this->drupalGet('');
81     $this->assertText($body);
82
83     // Update the block.
84     $updated_body = 'This is the new body value';
85     $edit = [
86       'body[0][value]' => $updated_body,
87       'moderation_state[0][state]' => 'draft',
88     ];
89     $this->drupalPostForm('block/' . $block->id(), $edit, t('Save'));
90     $this->assertText(t('basic Moderated block has been updated.'));
91
92     // Navigate to the home page and check that the block shows the updated
93     // content. It should show the updated content because the block's default
94     // revision is not a published moderation state.
95     $this->drupalGet('');
96     $this->assertText($updated_body);
97
98     // Publish the block so we can create a pending revision.
99     $this->drupalPostForm('block/' . $block->id(), [
100       'moderation_state[0][state]' => 'published',
101     ], t('Save'));
102
103     // Create a pending revision.
104     $pending_revision_body = 'This is the pending revision body value';
105     $edit = [
106       'body[0][value]' => $pending_revision_body,
107       'moderation_state[0][state]' => 'draft',
108     ];
109     $this->drupalPostForm('block/' . $block->id(), $edit, t('Save'));
110     $this->assertText(t('basic Moderated block has been updated.'));
111
112     // Navigate to home page and check that the pending revision doesn't show,
113     // since it should not be set as the default revision.
114     $this->drupalGet('');
115     $this->assertText($updated_body);
116
117     // Open the latest tab and publish the new draft.
118     $edit = [
119       'new_state' => 'published',
120     ];
121     $this->drupalPostForm('block/' . $block->id() . '/latest', $edit, t('Apply'));
122     $this->assertText(t('The moderation state has been updated.'));
123
124     // Navigate to home page and check that the pending revision is now the
125     // default revision and therefore visible.
126     $this->drupalGet('');
127     $this->assertText($pending_revision_body);
128
129     // Check that revision is checked by default when content moderation is
130     // enabled.
131     $this->drupalGet('/block/' . $block->id());
132     $this->assertSession()->checkboxChecked('revision');
133     $this->assertText('Revisions must be required when moderation is enabled.');
134     $this->assertSession()->fieldDisabled('revision');
135   }
136
137 }