ModerationStateNodeTypeTest.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Functional;
  3. /**
  4. * Tests moderation state node type integration.
  5. *
  6. * @group content_moderation
  7. */
  8. class ModerationStateNodeTypeTest extends ModerationStateTestBase {
  9. /**
  10. * A node type without moderation state disabled.
  11. *
  12. * @covers \Drupal\content_moderation\EntityTypeInfo::formAlter
  13. * @covers \Drupal\content_moderation\Entity\Handler\NodeModerationHandler::enforceRevisionsBundleFormAlter
  14. */
  15. public function testNotModerated() {
  16. $this->drupalLogin($this->adminUser);
  17. $this->createContentTypeFromUi('Not moderated', 'not_moderated');
  18. $this->assertText('The content type Not moderated has been added.');
  19. $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'not_moderated');
  20. $this->drupalGet('node/add/not_moderated');
  21. $this->assertRaw('Save');
  22. $this->drupalPostForm(NULL, [
  23. 'title[0][value]' => 'Test',
  24. ], t('Save'));
  25. $this->assertText('Not moderated Test has been created.');
  26. }
  27. /**
  28. * Tests enabling moderation on an existing node-type, with content.
  29. *
  30. * @covers \Drupal\content_moderation\EntityTypeInfo::formAlter
  31. * @covers \Drupal\content_moderation\Entity\Handler\NodeModerationHandler::enforceRevisionsBundleFormAlter
  32. */
  33. public function testEnablingOnExistingContent() {
  34. $editor_permissions = [
  35. 'administer workflows',
  36. 'access administration pages',
  37. 'administer content types',
  38. 'administer nodes',
  39. 'view latest version',
  40. 'view any unpublished content',
  41. 'access content overview',
  42. 'use editorial transition create_new_draft',
  43. ];
  44. $publish_permissions = array_merge($editor_permissions, ['use editorial transition publish']);
  45. $editor = $this->drupalCreateUser($editor_permissions);
  46. $editor_with_publish = $this->drupalCreateUser($publish_permissions);
  47. // Create a node type that is not moderated.
  48. $this->drupalLogin($editor);
  49. $this->createContentTypeFromUi('Not moderated', 'not_moderated');
  50. $this->grantUserPermissionToCreateContentOfType($editor, 'not_moderated');
  51. $this->grantUserPermissionToCreateContentOfType($editor_with_publish, 'not_moderated');
  52. // Create content.
  53. $this->drupalGet('node/add/not_moderated');
  54. $this->drupalPostForm(NULL, [
  55. 'title[0][value]' => 'Test',
  56. ], t('Save'));
  57. $this->assertText('Not moderated Test has been created.');
  58. // Now enable moderation state.
  59. $this->enableModerationThroughUi('not_moderated');
  60. // And make sure it works.
  61. $nodes = \Drupal::entityTypeManager()->getStorage('node')
  62. ->loadByProperties(['title' => 'Test']);
  63. if (empty($nodes)) {
  64. $this->fail('Could not load node with title Test');
  65. return;
  66. }
  67. $node = reset($nodes);
  68. $this->drupalGet('node/' . $node->id());
  69. $this->assertResponse(200);
  70. $this->assertLinkByHref('node/' . $node->id() . '/edit');
  71. $this->drupalGet('node/' . $node->id() . '/edit');
  72. $this->assertResponse(200);
  73. $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
  74. $this->assertSession()->optionNotExists('moderation_state[0][state]', 'published');
  75. $this->drupalLogin($editor_with_publish);
  76. $this->drupalGet('node/' . $node->id() . '/edit');
  77. $this->assertResponse(200);
  78. $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
  79. $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
  80. }
  81. }