3 namespace Drupal\Tests\content_moderation\Functional;
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
9 use Drupal\user\Entity\Role;
12 * Defines a base class for moderation state tests.
14 abstract class ModerationStateTestBase extends BrowserTestBase {
16 use ContentModerationTestTrait;
23 protected $profile = 'testing';
28 * @var \Drupal\Core\Session\AccountInterface
33 * Permissions to grant admin user.
37 protected $permissions = [
38 'administer workflows',
39 'access administration pages',
40 'administer content types',
42 'view latest version',
43 'view any unpublished content',
44 'access content overview',
45 'use editorial transition create_new_draft',
46 'use editorial transition publish',
47 'use editorial transition archive',
48 'use editorial transition archived_draft',
49 'use editorial transition archived_published',
53 * The editorial workflow entity.
55 * @var \Drupal\workflows\Entity\Workflow
64 public static $modules = [
75 protected function setUp() {
77 $this->workflow = $this->createEditorialWorkflow();
78 $this->adminUser = $this->drupalCreateUser($this->permissions);
79 $this->drupalPlaceBlock('local_tasks_block', ['id' => 'tabs_block']);
80 $this->drupalPlaceBlock('page_title_block');
81 $this->drupalPlaceBlock('local_actions_block', ['id' => 'actions_block']);
85 * Gets the permission machine name for a transition.
87 * @param string $workflow_id
89 * @param string $transition_id
93 * The permission machine name for a transition.
95 protected function getWorkflowTransitionPermission($workflow_id, $transition_id) {
96 return 'use ' . $workflow_id . ' transition ' . $transition_id;
100 * Creates a content-type from the UI.
102 * @param string $content_type_name
103 * Content type human name.
104 * @param string $content_type_id
106 * @param bool $moderated
107 * TRUE if should be moderated.
108 * @param string $workflow_id
109 * The workflow to attach to the bundle.
111 protected function createContentTypeFromUi($content_type_name, $content_type_id, $moderated = FALSE, $workflow_id = 'editorial') {
112 $this->drupalGet('admin/structure/types');
113 $this->clickLink('Add content type');
115 // Check that the 'Create new revision' checkbox is checked and disabled.
116 $this->assertSession()->checkboxChecked('options[revision]');
117 $this->assertSession()->fieldDisabled('options[revision]');
120 'name' => $content_type_name,
121 'type' => $content_type_id,
123 $this->drupalPostForm(NULL, $edit, t('Save content type'));
125 // Check the content type has been set to create new revisions.
126 $this->assertTrue(NodeType::load($content_type_id)->isNewRevision());
129 $this->enableModerationThroughUi($content_type_id, $workflow_id);
134 * Enable moderation for a specified content type, using the UI.
136 * @param string $content_type_id
138 * @param string $workflow_id
139 * The workflow to attach to the bundle.
141 public function enableModerationThroughUi($content_type_id, $workflow_id = 'editorial') {
142 $this->drupalGet('/admin/config/workflow/workflows');
143 $this->assertLinkByHref('admin/config/workflow/workflows/manage/' . $workflow_id);
144 $edit['bundles[' . $content_type_id . ']'] = TRUE;
145 $this->drupalPostForm('admin/config/workflow/workflows/manage/' . $workflow_id . '/type/node', $edit, t('Save'));
146 // Ensure the parent environment is up-to-date.
147 // @see content_moderation_workflow_insert()
148 \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
149 \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
150 /** @var \Drupal\Core\Routing\RouteBuilderInterface $router_builder */
151 $router_builder = $this->container->get('router.builder');
152 $router_builder->rebuildIfNeeded();
156 * Grants given user permission to create content of given type.
158 * @param \Drupal\Core\Session\AccountInterface $account
159 * User to grant permission to.
160 * @param string $content_type_id
163 protected function grantUserPermissionToCreateContentOfType(AccountInterface $account, $content_type_id) {
164 $role_ids = $account->getRoles(TRUE);
165 /* @var \Drupal\user\RoleInterface $role */
166 $role_id = reset($role_ids);
167 $role = Role::load($role_id);
168 $role->grantPermission(sprintf('create %s content', $content_type_id));
169 $role->grantPermission(sprintf('edit any %s content', $content_type_id));
170 $role->grantPermission(sprintf('delete any %s content', $content_type_id));