3 namespace Drupal\Tests\content_moderation\Functional;
5 use Drupal\node\Entity\Node;
6 use Drupal\simpletest\ContentTypeCreationTrait;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\workflows\Entity\Workflow;
11 * Test the content moderation actions.
13 * @group content_moderation
15 class ModerationActionsTest extends BrowserTestBase {
17 use ContentTypeCreationTrait;
24 public static $modules = [
33 public function setUp() {
36 $moderated_bundle = $this->createContentType(['type' => 'moderated_bundle']);
37 $moderated_bundle->save();
38 $standard_bundle = $this->createContentType(['type' => 'standard_bundle']);
39 $standard_bundle->save();
41 $workflow = Workflow::load('editorial');
42 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'moderated_bundle');
45 $admin = $this->drupalCreateUser([
46 'access content overview',
50 $this->drupalLogin($admin);
54 * Test the node status actions report moderation status to users correctly.
56 * @dataProvider nodeStatusActionsTestCases
58 public function testNodeStatusActions($action, $bundle, $warning_appears, $starting_status, $final_status) {
59 // Create and run an action on a node.
60 $node = Node::create([
62 'title' => $this->randomString(),
63 'status' => $starting_status,
65 if ($bundle == 'moderated_bundle') {
66 $node->moderation_state->value = $starting_status ? 'published' : 'draft';
70 $this->drupalPostForm('admin/content', [
71 'node_bulk_form[0]' => TRUE,
73 ], 'Apply to selected items');
75 if ($warning_appears) {
76 if ($action == 'node_publish_action') {
77 $this->assertSession()
78 ->elementContains('css', '.messages--warning', node_get_type_label($node) . ' content items were skipped as they are under moderation and may not be directly published.');
81 $this->assertSession()
82 ->elementContains('css', '.messages--warning', node_get_type_label($node) . ' content items were skipped as they are under moderation and may not be directly unpublished.');
86 $this->assertSession()->elementNotExists('css', '.messages--warning');
89 // Ensure after the action has run, the node matches the expected status.
90 $node = Node::load($node->id());
91 $this->assertEquals($node->isPublished(), $final_status);
95 * Test cases for ::testNodeStatusActions.
98 * An array of test cases.
100 public function nodeStatusActionsTestCases() {
102 'Moderated bundle shows warning (publish action)' => [
103 'node_publish_action',
106 // If the node starts out unpublished, the action should not work.
110 'Moderated bundle shows warning (unpublish action)' => [
111 'node_unpublish_action',
114 // If the node starts out published, the action should not work.
118 'Normal bundle works (publish action)' => [
119 'node_publish_action',
122 // If the node starts out unpublished, the action should work.
126 'Normal bundle works (unpublish action)' => [
127 'node_unpublish_action',
130 // If the node starts out published, the action should work.