ModerationStateBlockTest.php 4.87 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\content_moderation\Functional;
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;

/**
 * Tests general content moderation workflow for blocks.
 *
 * @group content_moderation
 */
class ModerationStateBlockTest extends ModerationStateTestBase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Create the "basic" block type.
    $bundle = BlockContentType::create([
      'id' => 'basic',
      'label' => 'basic',
      'revision' => FALSE,
    ]);
    $bundle->save();

    // Add the body field to it.
    block_content_add_body_field($bundle->id());
  }

  /**
   * Tests moderating custom blocks.
   *
   * Blocks and any non-node-type-entities do not have a concept of
   * "published". As such, we must use the "default revision" to know what is
   * going to be "published", i.e. visible to the user.
   *
   * The one exception is a block that has never been "published". When a block
   * is first created, it becomes the "default revision". For each edit of the
   * block after that, Content Moderation checks the "default revision" to
   * see if it is set to a published moderation state. If it is not, the entity
   * being saved will become the "default revision".
   *
   * The test below is intended, in part, to make this behavior clear.
   *
   * @see \Drupal\content_moderation\EntityOperations::entityPresave
   * @see \Drupal\content_moderation\Tests\ModerationFormTest::testModerationForm
   */
  public function testCustomBlockModeration() {
    $this->drupalLogin($this->rootUser);

54 55 56 57 58 59
    $this->drupalGet('admin/structure/block/block-content/types');
    $this->assertLinkByHref('admin/structure/block/block-content/manage/basic/moderation');
    $this->drupalGet('admin/structure/block/block-content/manage/basic');
    $this->assertLinkByHref('admin/structure/block/block-content/manage/basic/moderation');
    $this->drupalGet('admin/structure/block/block-content/manage/basic/moderation');

60 61
    // Enable moderation for custom blocks at
    // admin/structure/block/block-content/manage/basic/moderation.
62
    $edit = ['workflow' => 'editorial'];
63
    $this->drupalPostForm(NULL, $edit, t('Save'));
64 65 66 67 68 69 70 71 72 73 74 75
    $this->assertText(t('Your settings have been saved.'));

    // Create a custom block at block/add and save it as draft.
    $body = 'Body of moderated block';
    $edit = [
      'info[0][value]' => 'Moderated block',
      'body[0][value]' => $body,
    ];
    $this->drupalPostForm('block/add', $edit, t('Save and Create New Draft'));
    $this->assertText(t('basic Moderated block has been created.'));

    // Place the block in the Sidebar First region.
76
    $instance = [
77 78 79
      'id' => 'moderated_block',
      'settings[label]' => $edit['info[0][value]'],
      'region' => 'sidebar_first',
80
    ];
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    $block = BlockContent::load(1);
    $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default');
    $this->drupalPostForm($url, $instance, t('Save block'));

    // Navigate to home page and check that the block is visible. It should be
    // visible because it is the default revision.
    $this->drupalGet('');
    $this->assertText($body);

    // Update the block.
    $updated_body = 'This is the new body value';
    $edit = [
      'body[0][value]' => $updated_body,
    ];
    $this->drupalPostForm('block/' . $block->id(), $edit, t('Save and Create New Draft'));
    $this->assertText(t('basic Moderated block has been updated.'));

    // Navigate to the home page and check that the block shows the updated
    // content. It should show the updated content because the block's default
    // revision is not a published moderation state.
    $this->drupalGet('');
    $this->assertText($updated_body);

    // Publish the block so we can create a forward revision.
    $this->drupalPostForm('block/' . $block->id(), [], t('Save and Publish'));

    // Create a forward revision.
    $forward_revision_body = 'This is the forward revision body value';
    $edit = [
      'body[0][value]' => $forward_revision_body,
    ];
    $this->drupalPostForm('block/' . $block->id(), $edit, t('Save and Create New Draft'));
    $this->assertText(t('basic Moderated block has been updated.'));

    // Navigate to home page and check that the forward revision doesn't show,
    // since it should not be set as the default revision.
    $this->drupalGet('');
    $this->assertText($updated_body);

    // Open the latest tab and publish the new draft.
    $edit = [
      'new_state' => 'published',
    ];
    $this->drupalPostForm('block/' . $block->id() . '/latest', $edit, t('Apply'));
    $this->assertText(t('The moderation state has been updated.'));

    // Navigate to home page and check that the forward revision is now the
    // default revision and therefore visible.
    $this->drupalGet('');
    $this->assertText($forward_revision_body);
  }

}