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 ], t('Save and Create New Draft'));
32 $node = $this->getNodeByTitle('moderated content');
34 $this->fail('Test node was not saved correctly.');
36 $this->assertEqual('draft', $node->moderation_state->value);
38 $path = 'node/' . $node->id() . '/edit';
39 // Set up published revision.
40 $this->drupalPostForm($path, [], t('Save and Publish'));
41 \Drupal::entityTypeManager()->getStorage('node')->resetCache([$node->id()]);
42 /* @var \Drupal\node\NodeInterface $node */
43 $node = \Drupal::entityTypeManager()->getStorage('node')->load($node->id());
44 $this->assertTrue($node->isPublished());
45 $this->assertEqual('published', $node->moderation_state->value);
47 // Verify that the state field is not shown.
48 $this->assertNoText('Published');
51 $this->drupalPostForm('node/' . $node->id() . '/delete', [], t('Delete'));
52 $this->assertText(t('The Moderated content moderated content has been deleted.'));
54 // Disable content moderation.
55 $this->drupalPostForm('admin/structure/types/manage/moderated_content/moderation', ['workflow' => ''], t('Save'));
56 $this->drupalGet('admin/structure/types/manage/moderated_content/moderation');
57 $this->assertOptionSelected('edit-workflow', '');
58 // Ensure the parent environment is up-to-date.
59 // @see content_moderation_workflow_insert()
60 \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
61 \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
64 $this->drupalPostForm('node/add/moderated_content', [
65 'title[0][value]' => 'non-moderated content',
66 ], t('Save and publish'));
68 $node = $this->getNodeByTitle('non-moderated content');
70 $this->fail('Non-moderated test node was not saved correctly.');
72 $this->assertEqual(NULL, $node->moderation_state->value);
76 * Tests edit form destinations.
78 public function testFormSaveDestination() {
79 // Create new moderated content in draft.
80 $this->drupalPostForm('node/add/moderated_content', [
81 'title[0][value]' => 'Some moderated content',
82 'body[0][value]' => 'First version of the content.',
83 ], t('Save and Create New Draft'));
85 $node = $this->drupalGetNodeByTitle('Some moderated content');
86 $edit_path = sprintf('node/%d/edit', $node->id());
88 // After saving, we should be at the canonical URL and viewing the first
90 $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
91 $this->assertText('First version of the content.');
93 // Create a new draft; after saving, we should still be on the canonical
94 // URL, but viewing the second revision.
95 $this->drupalPostForm($edit_path, [
96 'body[0][value]' => 'Second version of the content.',
97 ], t('Save and Create New Draft'));
98 $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
99 $this->assertText('Second version of the content.');
101 // Make a new published revision; after saving, we should be at the
103 $this->drupalPostForm($edit_path, [
104 'body[0][value]' => 'Third version of the content.',
105 ], t('Save and Publish'));
106 $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
107 $this->assertText('Third version of the content.');
109 // Make a new forward revision; after saving, we should be on the "Latest
111 $this->drupalPostForm($edit_path, [
112 'body[0][value]' => 'Fourth version of the content.',
113 ], t('Save and Create New Draft'));
114 $this->assertUrl(Url::fromRoute('entity.node.latest_version', ['node' => $node->id()]));
115 $this->assertText('Fourth version of the content.');
119 * Tests pagers aren't broken by content_moderation.
121 public function testPagers() {
122 // Create 51 nodes to force the pager.
123 foreach (range(1, 51) as $delta) {
125 'type' => 'moderated_content',
126 'uid' => $this->adminUser->id(),
127 'title' => 'Node ' . $delta,
129 'moderation_state' => 'published',
132 $this->drupalLogin($this->adminUser);
133 $this->drupalGet('admin/content');
134 $element = $this->cssSelect('nav.pager li.is-active a');
135 $url = $element[0]->getAttribute('href');
137 parse_str(parse_url($url, PHP_URL_QUERY), $query);
138 $this->assertEqual(0, $query['page']);