Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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\Tests\content_moderation\Traits\ContentModerationTestTrait;
9
10 /**
11  * Test the content moderation actions.
12  *
13  * @group content_moderation
14  */
15 class ModerationActionsTest extends BrowserTestBase {
16
17   use ContentTypeCreationTrait;
18   use ContentModerationTestTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = [
26     'content_moderation',
27     'node',
28     'views',
29   ];
30
31   /**
32    * {@inheritdoc}
33    */
34   public function setUp() {
35     parent::setUp();
36
37     $moderated_bundle = $this->createContentType(['type' => 'moderated_bundle']);
38     $moderated_bundle->save();
39     $standard_bundle = $this->createContentType(['type' => 'standard_bundle']);
40     $standard_bundle->save();
41
42     $workflow = $this->createEditorialWorkflow();
43     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'moderated_bundle');
44     $workflow->save();
45
46     $admin = $this->drupalCreateUser([
47       'access content overview',
48       'administer nodes',
49       'bypass node access',
50     ]);
51     $this->drupalLogin($admin);
52   }
53
54   /**
55    * Test the node status actions report moderation status to users correctly.
56    *
57    * @dataProvider nodeStatusActionsTestCases
58    */
59   public function testNodeStatusActions($action, $bundle, $warning_appears, $starting_status, $final_status) {
60     // Create and run an action on a node.
61     $node = Node::create([
62       'type' => $bundle,
63       'title' => $this->randomString(),
64       'status' => $starting_status,
65     ]);
66     if ($bundle == 'moderated_bundle') {
67       $node->moderation_state->value = $starting_status ? 'published' : 'draft';
68     }
69     $node->save();
70
71     $this->drupalPostForm('admin/content', [
72       'node_bulk_form[0]' => TRUE,
73       'action' => $action,
74     ], 'Apply to selected items');
75
76     if ($warning_appears) {
77       if ($action == 'node_publish_action') {
78         $this->assertSession()
79           ->elementContains('css', '.messages--warning', node_get_type_label($node) . ' content items were skipped as they are under moderation and may not be directly published.');
80       }
81       else {
82         $this->assertSession()
83           ->elementContains('css', '.messages--warning', node_get_type_label($node) . ' content items were skipped as they are under moderation and may not be directly unpublished.');
84       }
85     }
86     else {
87       $this->assertSession()->elementNotExists('css', '.messages--warning');
88     }
89
90     // Ensure after the action has run, the node matches the expected status.
91     $node = Node::load($node->id());
92     $this->assertEquals($node->isPublished(), $final_status);
93   }
94
95   /**
96    * Test cases for ::testNodeStatusActions.
97    *
98    * @return array
99    *   An array of test cases.
100    */
101   public function nodeStatusActionsTestCases() {
102     return [
103       'Moderated bundle shows warning (publish action)' => [
104         'node_publish_action',
105         'moderated_bundle',
106         TRUE,
107         // If the node starts out unpublished, the action should not work.
108         FALSE,
109         FALSE,
110       ],
111       'Moderated bundle shows warning (unpublish action)' => [
112         'node_unpublish_action',
113         'moderated_bundle',
114         TRUE,
115         // If the node starts out published, the action should not work.
116         TRUE,
117         TRUE,
118       ],
119       'Normal bundle works (publish action)' => [
120         'node_publish_action',
121         'standard_bundle',
122         FALSE,
123         // If the node starts out unpublished, the action should work.
124         FALSE,
125         TRUE,
126       ],
127       'Normal bundle works (unpublish action)' => [
128         'node_unpublish_action',
129         'standard_bundle',
130         FALSE,
131         // If the node starts out published, the action should work.
132         TRUE,
133         FALSE,
134       ],
135     ];
136   }
137
138 }