3 namespace Drupal\Tests\content_moderation\Functional;
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
9 * Tests general content moderation workflow for blocks.
11 * @group content_moderation
13 class ModerationStateBlockTest extends ModerationStateTestBase {
18 protected function setUp() {
21 // Create the "basic" block type.
22 $bundle = BlockContentType::create([
29 // Add the body field to it.
30 block_content_add_body_field($bundle->id());
34 * Tests moderating custom blocks.
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.
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".
46 * The test below is intended, in part, to make this behavior clear.
48 * @see \Drupal\content_moderation\EntityOperations::entityPresave
49 * @see \Drupal\content_moderation\Tests\ModerationFormTest::testModerationForm
51 public function testCustomBlockModeration() {
52 $this->drupalLogin($this->rootUser);
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'));
58 // Create a custom block at block/add and save it as draft.
59 $body = 'Body of moderated block';
61 'info[0][value]' => 'Moderated block',
62 'moderation_state[0][state]' => 'draft',
63 'body[0][value]' => $body,
65 $this->drupalPostForm('block/add', $edit, t('Save'));
66 $this->assertText(t('basic Moderated block has been created.'));
68 // Place the block in the Sidebar First region.
70 'id' => 'moderated_block',
71 'settings[label]' => $edit['info[0][value]'],
72 'region' => 'sidebar_first',
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'));
78 // Navigate to home page and check that the block is visible. It should be
79 // visible because it is the default revision.
81 $this->assertText($body);
84 $updated_body = 'This is the new body value';
86 'body[0][value]' => $updated_body,
87 'moderation_state[0][state]' => 'draft',
89 $this->drupalPostForm('block/' . $block->id(), $edit, t('Save'));
90 $this->assertText(t('basic Moderated block has been updated.'));
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.
96 $this->assertText($updated_body);
98 // Publish the block so we can create a pending revision.
99 $this->drupalPostForm('block/' . $block->id(), [
100 'moderation_state[0][state]' => 'published',
103 // Create a pending revision.
104 $pending_revision_body = 'This is the pending revision body value';
106 'body[0][value]' => $pending_revision_body,
107 'moderation_state[0][state]' => 'draft',
109 $this->drupalPostForm('block/' . $block->id(), $edit, t('Save'));
110 $this->assertText(t('basic Moderated block has been updated.'));
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);
117 // Open the latest tab and publish the new draft.
119 'new_state' => 'published',
121 $this->drupalPostForm('block/' . $block->id() . '/latest', $edit, t('Apply'));
122 $this->assertText(t('The moderation state has been updated.'));
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);
129 // Check that revision is checked by default when content moderation is
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');