You are here

public function ModerationStateNodeTest::testCreatingContent in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/tests/src/Functional/ModerationStateNodeTest.php \Drupal\Tests\content_moderation\Functional\ModerationStateNodeTest::testCreatingContent()

Tests creating and deleting content.

File

core/modules/content_moderation/tests/src/Functional/ModerationStateNodeTest.php, line 33

Class

ModerationStateNodeTest
Tests general content moderation workflow for nodes.

Namespace

Drupal\Tests\content_moderation\Functional

Code

public function testCreatingContent() {
  $this
    ->drupalPostForm('node/add/moderated_content', [
    'title[0][value]' => 'moderated content',
    'moderation_state[0][state]' => 'draft',
  ], t('Save'));
  $node = $this
    ->getNodeByTitle('moderated content');
  if (!$node) {
    $this
      ->fail('Test node was not saved correctly.');
  }
  $this
    ->assertEqual('draft', $node->moderation_state->value);
  $path = 'node/' . $node
    ->id() . '/edit';

  // Set up published revision.
  $this
    ->drupalPostForm($path, [
    'moderation_state[0][state]' => 'published',
  ], t('Save'));
  \Drupal::entityTypeManager()
    ->getStorage('node')
    ->resetCache([
    $node
      ->id(),
  ]);

  /* @var \Drupal\node\NodeInterface $node */
  $node = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->load($node
    ->id());
  $this
    ->assertTrue($node
    ->isPublished());
  $this
    ->assertEqual('published', $node->moderation_state->value);

  // Verify that the state field is not shown.
  $this
    ->assertNoText('Published');

  // Delete the node.
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/delete', [], t('Delete'));
  $this
    ->assertText(t('The Moderated content moderated content has been deleted.'));

  // Disable content moderation.
  $edit['bundles[moderated_content]'] = FALSE;
  $this
    ->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/node', $edit, t('Save'));

  // Ensure the parent environment is up-to-date.
  // @see content_moderation_workflow_insert()
  \Drupal::service('entity_type.bundle.info')
    ->clearCachedBundles();
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();

  // Create a new node.
  $this
    ->drupalPostForm('node/add/moderated_content', [
    'title[0][value]' => 'non-moderated content',
  ], t('Save'));
  $node = $this
    ->getNodeByTitle('non-moderated content');
  if (!$node) {
    $this
      ->fail('Non-moderated test node was not saved correctly.');
  }
  $this
    ->assertFalse($node
    ->hasField('moderation_state'));
}