Upgraded drupal core with security updates
[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     $this->drupalGet('admin/structure/block/block-content/types');
55     $this->assertLinkByHref('admin/structure/block/block-content/manage/basic/moderation');
56     $this->drupalGet('admin/structure/block/block-content/manage/basic');
57     $this->assertLinkByHref('admin/structure/block/block-content/manage/basic/moderation');
58     $this->drupalGet('admin/structure/block/block-content/manage/basic/moderation');
59
60     // Enable moderation for custom blocks at
61     // admin/structure/block/block-content/manage/basic/moderation.
62     $edit = ['workflow' => 'editorial'];
63     $this->drupalPostForm(NULL, $edit, t('Save'));
64     $this->assertText(t('Your settings have been saved.'));
65
66     // Create a custom block at block/add and save it as draft.
67     $body = 'Body of moderated block';
68     $edit = [
69       'info[0][value]' => 'Moderated block',
70       'body[0][value]' => $body,
71     ];
72     $this->drupalPostForm('block/add', $edit, t('Save and Create New Draft'));
73     $this->assertText(t('basic Moderated block has been created.'));
74
75     // Place the block in the Sidebar First region.
76     $instance = [
77       'id' => 'moderated_block',
78       'settings[label]' => $edit['info[0][value]'],
79       'region' => 'sidebar_first',
80     ];
81     $block = BlockContent::load(1);
82     $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default');
83     $this->drupalPostForm($url, $instance, t('Save block'));
84
85     // Navigate to home page and check that the block is visible. It should be
86     // visible because it is the default revision.
87     $this->drupalGet('');
88     $this->assertText($body);
89
90     // Update the block.
91     $updated_body = 'This is the new body value';
92     $edit = [
93       'body[0][value]' => $updated_body,
94     ];
95     $this->drupalPostForm('block/' . $block->id(), $edit, t('Save and Create New Draft'));
96     $this->assertText(t('basic Moderated block has been updated.'));
97
98     // Navigate to the home page and check that the block shows the updated
99     // content. It should show the updated content because the block's default
100     // revision is not a published moderation state.
101     $this->drupalGet('');
102     $this->assertText($updated_body);
103
104     // Publish the block so we can create a forward revision.
105     $this->drupalPostForm('block/' . $block->id(), [], t('Save and Publish'));
106
107     // Create a forward revision.
108     $forward_revision_body = 'This is the forward revision body value';
109     $edit = [
110       'body[0][value]' => $forward_revision_body,
111     ];
112     $this->drupalPostForm('block/' . $block->id(), $edit, t('Save and Create New Draft'));
113     $this->assertText(t('basic Moderated block has been updated.'));
114
115     // Navigate to home page and check that the forward revision doesn't show,
116     // since it should not be set as the default revision.
117     $this->drupalGet('');
118     $this->assertText($updated_body);
119
120     // Open the latest tab and publish the new draft.
121     $edit = [
122       'new_state' => 'published',
123     ];
124     $this->drupalPostForm('block/' . $block->id() . '/latest', $edit, t('Apply'));
125     $this->assertText(t('The moderation state has been updated.'));
126
127     // Navigate to home page and check that the forward revision is now the
128     // default revision and therefore visible.
129     $this->drupalGet('');
130     $this->assertText($forward_revision_body);
131   }
132
133 }