3 namespace Drupal\Tests\content_moderation\Functional;
6 use Drupal\node\Entity\Node;
9 * Tests general content moderation workflow for nodes.
11 * @group content_moderation
13 class ModerationStateNodeTest extends ModerationStateTestBase {
18 protected function setUp() {
20 $this->drupalLogin($this->adminUser);
21 $this->createContentTypeFromUi('Moderated content', 'moderated_content', TRUE);
22 $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content');
26 * Tests creating and deleting content.
28 public function testCreatingContent() {
29 $this->drupalPostForm('node/add/moderated_content', [
30 'title[0][value]' => 'moderated content',
31 'moderation_state[0][state]' => 'draft',
33 $node = $this->getNodeByTitle('moderated content');
35 $this->fail('Test node was not saved correctly.');
37 $this->assertEqual('draft', $node->moderation_state->value);
39 $path = 'node/' . $node->id() . '/edit';
40 // Set up published revision.
41 $this->drupalPostForm($path, [
42 'moderation_state[0][state]' => 'published',
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);
50 // Verify that the state field is not shown.
51 $this->assertNoText('Published');
54 $this->drupalPostForm('node/' . $node->id() . '/delete', [], t('Delete'));
55 $this->assertText(t('The Moderated content moderated content has been deleted.'));
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();
66 $this->drupalPostForm('node/add/moderated_content', [
67 'title[0][value]' => 'non-moderated content',
70 $node = $this->getNodeByTitle('non-moderated content');
72 $this->fail('Non-moderated test node was not saved correctly.');
74 $this->assertEqual(NULL, $node->moderation_state->value);
78 * Tests edit form destinations.
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',
88 $node = $this->drupalGetNodeByTitle('Some moderated content');
89 $edit_path = sprintf('node/%d/edit', $node->id());
91 // After saving, we should be at the canonical URL and viewing the first
93 $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
94 $this->assertText('First version of the content.');
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',
102 $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
103 $this->assertText('Second version of the content.');
105 // Make a new published revision; after saving, we should be at the
107 $this->drupalPostForm($edit_path, [
108 'body[0][value]' => 'Third version of the content.',
109 'moderation_state[0][state]' => 'published',
111 $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
112 $this->assertText('Third version of the content.');
114 // Make a new pending revision; after saving, we should be on the "Latest
116 $this->drupalPostForm($edit_path, [
117 'body[0][value]' => 'Fourth version of the content.',
118 'moderation_state[0][state]' => 'draft',
120 $this->assertUrl(Url::fromRoute('entity.node.latest_version', ['node' => $node->id()]));
121 $this->assertText('Fourth version of the content.');
125 * Tests pagers aren't broken by content_moderation.
127 public function testPagers() {
128 // Create 51 nodes to force the pager.
129 foreach (range(1, 51) as $delta) {
131 'type' => 'moderated_content',
132 'uid' => $this->adminUser->id(),
133 'title' => 'Node ' . $delta,
135 'moderation_state' => 'published',
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');
143 parse_str(parse_url($url, PHP_URL_QUERY), $query);
144 $this->assertEqual(0, $query['page']);
148 * Tests the workflow when a user has no Content Moderation permissions.
150 public function testNoContentModerationPermissions() {
151 $session_assert = $this->assertSession();
153 // Create a user with quite advanced node permissions but no content
154 // moderation permissions.
155 $limited_user = $this->createUser([
157 'bypass node access',
159 $this->drupalLogin($limited_user);
161 // Check the user can add content, but can't see the moderation state
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',
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';
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');
183 // When saving they should still be on the edit form, and see the validation
185 $session_assert->pageTextContains('Edit Moderated content moderated content');
186 $session_assert->pageTextContains('Invalid state transition from Archived to Archived');