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 $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');
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.'));
66 // Create a custom block at block/add and save it as draft.
67 $body = 'Body of moderated block';
69 'info[0][value]' => 'Moderated block',
70 'body[0][value]' => $body,
72 $this->drupalPostForm('block/add', $edit, t('Save and Create New Draft'));
73 $this->assertText(t('basic Moderated block has been created.'));
75 // Place the block in the Sidebar First region.
77 'id' => 'moderated_block',
78 'settings[label]' => $edit['info[0][value]'],
79 'region' => 'sidebar_first',
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'));
85 // Navigate to home page and check that the block is visible. It should be
86 // visible because it is the default revision.
88 $this->assertText($body);
91 $updated_body = 'This is the new body value';
93 'body[0][value]' => $updated_body,
95 $this->drupalPostForm('block/' . $block->id(), $edit, t('Save and Create New Draft'));
96 $this->assertText(t('basic Moderated block has been updated.'));
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);
104 // Publish the block so we can create a forward revision.
105 $this->drupalPostForm('block/' . $block->id(), [], t('Save and Publish'));
107 // Create a forward revision.
108 $forward_revision_body = 'This is the forward revision body value';
110 'body[0][value]' => $forward_revision_body,
112 $this->drupalPostForm('block/' . $block->id(), $edit, t('Save and Create New Draft'));
113 $this->assertText(t('basic Moderated block has been updated.'));
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);
120 // Open the latest tab and publish the new draft.
122 'new_state' => 'published',
124 $this->drupalPostForm('block/' . $block->id() . '/latest', $edit, t('Apply'));
125 $this->assertText(t('The moderation state has been updated.'));
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);