ModerationStateTestBase.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Functional;
  3. use Drupal\Core\Session\AccountInterface;
  4. use Drupal\node\Entity\NodeType;
  5. use Drupal\Tests\BrowserTestBase;
  6. use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
  7. use Drupal\user\Entity\Role;
  8. /**
  9. * Defines a base class for moderation state tests.
  10. */
  11. abstract class ModerationStateTestBase extends BrowserTestBase {
  12. use ContentModerationTestTrait;
  13. /**
  14. * Profile to use.
  15. *
  16. * @var string
  17. */
  18. protected $profile = 'testing';
  19. /**
  20. * Admin user.
  21. *
  22. * @var \Drupal\Core\Session\AccountInterface
  23. */
  24. protected $adminUser;
  25. /**
  26. * Permissions to grant admin user.
  27. *
  28. * @var array
  29. */
  30. protected $permissions = [
  31. 'administer workflows',
  32. 'access administration pages',
  33. 'administer content types',
  34. 'administer nodes',
  35. 'view latest version',
  36. 'view any unpublished content',
  37. 'access content overview',
  38. 'use editorial transition create_new_draft',
  39. 'use editorial transition publish',
  40. 'use editorial transition archive',
  41. 'use editorial transition archived_draft',
  42. 'use editorial transition archived_published',
  43. ];
  44. /**
  45. * The editorial workflow entity.
  46. *
  47. * @var \Drupal\workflows\Entity\Workflow
  48. */
  49. protected $workflow;
  50. /**
  51. * Modules to enable.
  52. *
  53. * @var array
  54. */
  55. public static $modules = [
  56. 'content_moderation',
  57. 'block',
  58. 'block_content',
  59. 'node',
  60. 'entity_test',
  61. ];
  62. /**
  63. * Sets the test up.
  64. */
  65. protected function setUp() {
  66. parent::setUp();
  67. $this->workflow = $this->createEditorialWorkflow();
  68. $this->adminUser = $this->drupalCreateUser($this->permissions);
  69. $this->drupalPlaceBlock('local_tasks_block', ['id' => 'tabs_block']);
  70. $this->drupalPlaceBlock('page_title_block');
  71. $this->drupalPlaceBlock('local_actions_block', ['id' => 'actions_block']);
  72. }
  73. /**
  74. * Gets the permission machine name for a transition.
  75. *
  76. * @param string $workflow_id
  77. * The workflow ID.
  78. * @param string $transition_id
  79. * The transition ID.
  80. *
  81. * @return string
  82. * The permission machine name for a transition.
  83. */
  84. protected function getWorkflowTransitionPermission($workflow_id, $transition_id) {
  85. return 'use ' . $workflow_id . ' transition ' . $transition_id;
  86. }
  87. /**
  88. * Creates a content-type from the UI.
  89. *
  90. * @param string $content_type_name
  91. * Content type human name.
  92. * @param string $content_type_id
  93. * Machine name.
  94. * @param bool $moderated
  95. * TRUE if should be moderated.
  96. * @param string $workflow_id
  97. * The workflow to attach to the bundle.
  98. */
  99. protected function createContentTypeFromUi($content_type_name, $content_type_id, $moderated = FALSE, $workflow_id = 'editorial') {
  100. $this->drupalGet('admin/structure/types');
  101. $this->clickLink('Add content type');
  102. // Check that the 'Create new revision' checkbox is checked and disabled.
  103. $this->assertSession()->checkboxChecked('options[revision]');
  104. $this->assertSession()->fieldDisabled('options[revision]');
  105. $edit = [
  106. 'name' => $content_type_name,
  107. 'type' => $content_type_id,
  108. ];
  109. $this->drupalPostForm(NULL, $edit, t('Save content type'));
  110. // Check the content type has been set to create new revisions.
  111. $this->assertTrue(NodeType::load($content_type_id)->isNewRevision());
  112. if ($moderated) {
  113. $this->enableModerationThroughUi($content_type_id, $workflow_id);
  114. }
  115. }
  116. /**
  117. * Enable moderation for a specified content type, using the UI.
  118. *
  119. * @param string $content_type_id
  120. * Machine name.
  121. * @param string $workflow_id
  122. * The workflow to attach to the bundle.
  123. */
  124. public function enableModerationThroughUi($content_type_id, $workflow_id = 'editorial') {
  125. $this->drupalGet('/admin/config/workflow/workflows');
  126. $this->assertLinkByHref('admin/config/workflow/workflows/manage/' . $workflow_id);
  127. $edit['bundles[' . $content_type_id . ']'] = TRUE;
  128. $this->drupalPostForm('admin/config/workflow/workflows/manage/' . $workflow_id . '/type/node', $edit, t('Save'));
  129. // Ensure the parent environment is up-to-date.
  130. // @see content_moderation_workflow_insert()
  131. \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
  132. \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
  133. /** @var \Drupal\Core\Routing\RouteBuilderInterface $router_builder */
  134. $router_builder = $this->container->get('router.builder');
  135. $router_builder->rebuildIfNeeded();
  136. }
  137. /**
  138. * Grants given user permission to create content of given type.
  139. *
  140. * @param \Drupal\Core\Session\AccountInterface $account
  141. * User to grant permission to.
  142. * @param string $content_type_id
  143. * Content type ID.
  144. */
  145. protected function grantUserPermissionToCreateContentOfType(AccountInterface $account, $content_type_id) {
  146. $role_ids = $account->getRoles(TRUE);
  147. /* @var \Drupal\user\RoleInterface $role */
  148. $role_id = reset($role_ids);
  149. $role = Role::load($role_id);
  150. $role->grantPermission(sprintf('create %s content', $content_type_id));
  151. $role->grantPermission(sprintf('edit any %s content', $content_type_id));
  152. $role->grantPermission(sprintf('delete any %s content', $content_type_id));
  153. $role->save();
  154. }
  155. }