11deaa72c061096ff003c5b935a59d5bf6deafa5
[yaffs-website] / web / core / modules / content_moderation / tests / src / Functional / ModerationStateNodeTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Functional;
4
5 use Drupal\Core\Url;
6 use Drupal\node\Entity\Node;
7
8 /**
9  * Tests general content moderation workflow for nodes.
10  *
11  * @group content_moderation
12  */
13 class ModerationStateNodeTest extends ModerationStateTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected function setUp() {
19     parent::setUp();
20     $this->drupalLogin($this->adminUser);
21     $this->createContentTypeFromUi('Moderated content', 'moderated_content', TRUE);
22     $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content');
23   }
24
25   /**
26    * Tests creating and deleting content.
27    */
28   public function testCreatingContent() {
29     $this->drupalPostForm('node/add/moderated_content', [
30       'title[0][value]' => 'moderated content',
31       'moderation_state[0][state]' => 'draft',
32     ], t('Save'));
33     $node = $this->getNodeByTitle('moderated content');
34     if (!$node) {
35       $this->fail('Test node was not saved correctly.');
36     }
37     $this->assertEqual('draft', $node->moderation_state->value);
38
39     $path = 'node/' . $node->id() . '/edit';
40     // Set up published revision.
41     $this->drupalPostForm($path, [
42       'moderation_state[0][state]' => 'published',
43     ], t('Save'));
44     \Drupal::entityTypeManager()->getStorage('node')->resetCache([$node->id()]);
45     /* @var \Drupal\node\NodeInterface $node */
46     $node = \Drupal::entityTypeManager()->getStorage('node')->load($node->id());
47     $this->assertTrue($node->isPublished());
48     $this->assertEqual('published', $node->moderation_state->value);
49
50     // Verify that the state field is not shown.
51     $this->assertNoText('Published');
52
53     // Delete the node.
54     $this->drupalPostForm('node/' . $node->id() . '/delete', [], t('Delete'));
55     $this->assertText(t('The Moderated content moderated content has been deleted.'));
56
57     // Disable content moderation.
58     $edit['bundles[moderated_content]'] = FALSE;
59     $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/node', $edit, t('Save'));;
60     // Ensure the parent environment is up-to-date.
61     // @see content_moderation_workflow_insert()
62     \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
63     \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
64
65     // Create a new node.
66     $this->drupalPostForm('node/add/moderated_content', [
67       'title[0][value]' => 'non-moderated content',
68     ], t('Save'));
69
70     $node = $this->getNodeByTitle('non-moderated content');
71     if (!$node) {
72       $this->fail('Non-moderated test node was not saved correctly.');
73     }
74     $this->assertEqual(NULL, $node->moderation_state->value);
75   }
76
77   /**
78    * Tests edit form destinations.
79    */
80   public function testFormSaveDestination() {
81     // Create new moderated content in draft.
82     $this->drupalPostForm('node/add/moderated_content', [
83       'title[0][value]' => 'Some moderated content',
84       'body[0][value]' => 'First version of the content.',
85       'moderation_state[0][state]' => 'draft',
86     ], t('Save'));
87
88     $node = $this->drupalGetNodeByTitle('Some moderated content');
89     $edit_path = sprintf('node/%d/edit', $node->id());
90
91     // After saving, we should be at the canonical URL and viewing the first
92     // revision.
93     $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
94     $this->assertText('First version of the content.');
95
96     // Create a new draft; after saving, we should still be on the canonical
97     // URL, but viewing the second revision.
98     $this->drupalPostForm($edit_path, [
99       'body[0][value]' => 'Second version of the content.',
100       'moderation_state[0][state]' => 'draft',
101     ], t('Save'));
102     $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
103     $this->assertText('Second version of the content.');
104
105     // Make a new published revision; after saving, we should be at the
106     // canonical URL.
107     $this->drupalPostForm($edit_path, [
108       'body[0][value]' => 'Third version of the content.',
109       'moderation_state[0][state]' => 'published',
110     ], t('Save'));
111     $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
112     $this->assertText('Third version of the content.');
113
114     // Make a new pending revision; after saving, we should be on the "Latest
115     // version" tab.
116     $this->drupalPostForm($edit_path, [
117       'body[0][value]' => 'Fourth version of the content.',
118       'moderation_state[0][state]' => 'draft',
119     ], t('Save'));
120     $this->assertUrl(Url::fromRoute('entity.node.latest_version', ['node' => $node->id()]));
121     $this->assertText('Fourth version of the content.');
122   }
123
124   /**
125    * Tests pagers aren't broken by content_moderation.
126    */
127   public function testPagers() {
128     // Create 51 nodes to force the pager.
129     foreach (range(1, 51) as $delta) {
130       Node::create([
131         'type' => 'moderated_content',
132         'uid' => $this->adminUser->id(),
133         'title' => 'Node ' . $delta,
134         'status' => 1,
135         'moderation_state' => 'published',
136       ])->save();
137     }
138     $this->drupalLogin($this->adminUser);
139     $this->drupalGet('admin/content');
140     $element = $this->cssSelect('nav.pager li.is-active a');
141     $url = $element[0]->getAttribute('href');
142     $query = [];
143     parse_str(parse_url($url, PHP_URL_QUERY), $query);
144     $this->assertEqual(0, $query['page']);
145   }
146
147   /**
148    * Tests the workflow when a user has no Content Moderation permissions.
149    */
150   public function testNoContentModerationPermissions() {
151     $session_assert = $this->assertSession();
152
153     // Create a user with quite advanced node permissions but no content
154     // moderation permissions.
155     $limited_user = $this->createUser([
156       'administer nodes',
157       'bypass node access',
158     ]);
159     $this->drupalLogin($limited_user);
160
161     // Check the user can add content, but can't see the moderation state
162     // select.
163     $this->drupalGet('node/add/moderated_content');
164     $session_assert->statusCodeEquals(200);
165     $session_assert->fieldNotExists('moderation_state[0][state]');
166     $this->drupalPostForm(NULL, [
167       'title[0][value]' => 'moderated content',
168     ], 'Save');
169
170     // Manually move the content to archived because the user doesn't have
171     // permission to do this.
172     $node = $this->getNodeByTitle('moderated content');
173     $node->moderation_state->value = 'archived';
174     $node->save();
175
176     // Check the user can see the current state but not the select.
177     $this->drupalGet('node/' . $node->id() . '/edit');
178     $session_assert->statusCodeEquals(200);
179     $session_assert->pageTextContains('Archived');
180     $session_assert->fieldNotExists('moderation_state[0][state]');
181     $this->drupalPostForm(NULL, [], 'Save');
182
183     // When saving they should still be on the edit form, and see the validation
184     // error message.
185     $session_assert->pageTextContains('Edit Moderated content moderated content');
186     $session_assert->pageTextContains('Invalid state transition from Archived to Archived');
187   }
188
189 }