function ModerationActionsTest::testNodeStatusActions

Same name in other branches
  1. 9 core/modules/content_moderation/tests/src/Functional/ModerationActionsTest.php \Drupal\Tests\content_moderation\Functional\ModerationActionsTest::testNodeStatusActions()
  2. 10 core/modules/content_moderation/tests/src/Functional/ModerationActionsTest.php \Drupal\Tests\content_moderation\Functional\ModerationActionsTest::testNodeStatusActions()
  3. 11.x core/modules/content_moderation/tests/src/Functional/ModerationActionsTest.php \Drupal\Tests\content_moderation\Functional\ModerationActionsTest::testNodeStatusActions()

Test the node status actions report moderation status to users correctly.

@dataProvider nodeStatusActionsTestCases

File

core/modules/content_moderation/tests/src/Functional/ModerationActionsTest.php, line 64

Class

ModerationActionsTest
Test the content moderation actions.

Namespace

Drupal\Tests\content_moderation\Functional

Code

public function testNodeStatusActions($action, $bundle, $warning_appears, $starting_status, $final_status) {
    // Create and run an action on a node.
    $node = Node::create([
        'type' => $bundle,
        'title' => $this->randomString(),
        'status' => $starting_status,
    ]);
    if ($bundle == 'moderated_bundle') {
        $node->moderation_state->value = $starting_status ? 'published' : 'draft';
    }
    $node->save();
    $this->drupalPostForm('admin/content', [
        'node_bulk_form[0]' => TRUE,
        'action' => $action,
    ], 'Apply to selected items');
    if ($warning_appears) {
        if ($action == 'node_publish_action') {
            $this->assertSession()
                ->elementContains('css', '.messages--warning', node_get_type_label($node) . ' content items were skipped as they are under moderation and may not be directly published.');
        }
        else {
            $this->assertSession()
                ->elementContains('css', '.messages--warning', node_get_type_label($node) . ' content items were skipped as they are under moderation and may not be directly unpublished.');
        }
    }
    else {
        $this->assertSession()
            ->elementNotExists('css', '.messages--warning');
    }
    // Ensure after the action has run, the node matches the expected status.
    $node = Node::load($node->id());
    $this->assertEquals($node->isPublished(), $final_status);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.