Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / content_moderation / tests / src / Functional / ModerationActionsTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Functional;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\simpletest\ContentTypeCreationTrait;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\workflows\Entity\Workflow;
9
10 /**
11  * Test the content moderation actions.
12  *
13  * @group content_moderation
14  */
15 class ModerationActionsTest extends BrowserTestBase {
16
17   use ContentTypeCreationTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = [
25     'content_moderation',
26     'node',
27     'views',
28   ];
29
30   /**
31    * {@inheritdoc}
32    */
33   public function setUp() {
34     parent::setUp();
35
36     $moderated_bundle = $this->createContentType(['type' => 'moderated_bundle']);
37     $moderated_bundle->save();
38     $standard_bundle = $this->createContentType(['type' => 'standard_bundle']);
39     $standard_bundle->save();
40
41     $workflow = Workflow::load('editorial');
42     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'moderated_bundle');
43     $workflow->save();
44
45     $admin = $this->drupalCreateUser([
46       'access content overview',
47       'administer nodes',
48       'bypass node access',
49     ]);
50     $this->drupalLogin($admin);
51   }
52
53   /**
54    * Test the node status actions report moderation status to users correctly.
55    *
56    * @dataProvider nodeStatusActionsTestCases
57    */
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([
61       'type' => $bundle,
62       'title' => $this->randomString(),
63       'status' => $starting_status,
64     ]);
65     if ($bundle == 'moderated_bundle') {
66       $node->moderation_state->value = $starting_status ? 'published' : 'draft';
67     }
68     $node->save();
69
70     $this->drupalPostForm('admin/content', [
71       'node_bulk_form[0]' => TRUE,
72       'action' => $action,
73     ], 'Apply to selected items');
74
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.');
79       }
80       else {
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.');
83       }
84     }
85     else {
86       $this->assertSession()->elementNotExists('css', '.messages--warning');
87     }
88
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);
92   }
93
94   /**
95    * Test cases for ::testNodeStatusActions.
96    *
97    * @return array
98    *   An array of test cases.
99    */
100   public function nodeStatusActionsTestCases() {
101     return [
102       'Moderated bundle shows warning (publish action)' => [
103         'node_publish_action',
104         'moderated_bundle',
105         TRUE,
106         // If the node starts out unpublished, the action should not work.
107         FALSE,
108         FALSE,
109       ],
110       'Moderated bundle shows warning (unpublish action)' => [
111         'node_unpublish_action',
112         'moderated_bundle',
113         TRUE,
114         // If the node starts out published, the action should not work.
115         TRUE,
116         TRUE,
117       ],
118       'Normal bundle works (publish action)' => [
119         'node_publish_action',
120         'standard_bundle',
121         FALSE,
122         // If the node starts out unpublished, the action should work.
123         FALSE,
124         TRUE,
125       ],
126       'Normal bundle works (unpublish action)' => [
127         'node_unpublish_action',
128         'standard_bundle',
129         FALSE,
130         // If the node starts out published, the action should work.
131         TRUE,
132         FALSE,
133       ],
134     ];
135   }
136
137 }