ModerationStateNodeTest.php 6.2 KB

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