3 namespace Drupal\Tests\content_moderation\Functional;
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\user\Entity\Role;
10 * Defines a base class for moderation state tests.
12 abstract class ModerationStateTestBase extends BrowserTestBase {
19 protected $profile = 'testing';
24 * @var \Drupal\Core\Session\AccountInterface
29 * Permissions to grant admin user.
33 protected $permissions = [
34 'administer workflows',
35 'access administration pages',
36 'administer content types',
38 'view latest version',
39 'view any unpublished content',
40 'access content overview',
41 'use editorial transition create_new_draft',
42 'use editorial transition publish',
43 'use editorial transition archive',
44 'use editorial transition archived_draft',
45 'use editorial transition archived_published',
53 public static $modules = [
64 protected function setUp() {
66 $this->adminUser = $this->drupalCreateUser($this->permissions);
67 $this->drupalPlaceBlock('local_tasks_block', ['id' => 'tabs_block']);
68 $this->drupalPlaceBlock('page_title_block');
69 $this->drupalPlaceBlock('local_actions_block', ['id' => 'actions_block']);
73 * Gets the permission machine name for a transition.
75 * @param string $workflow_id
77 * @param string $transition_id
81 * The permission machine name for a transition.
83 protected function getWorkflowTransitionPermission($workflow_id, $transition_id) {
84 return 'use ' . $workflow_id . ' transition ' . $transition_id;
88 * Creates a content-type from the UI.
90 * @param string $content_type_name
91 * Content type human name.
92 * @param string $content_type_id
94 * @param bool $moderated
95 * TRUE if should be moderated.
96 * @param string $workflow_id
97 * The workflow to attach to the bundle.
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');
103 // Check that the 'Create new revision' checkbox is checked and disabled.
104 $this->assertSession()->checkboxChecked('options[revision]');
105 $this->assertSession()->fieldDisabled('options[revision]');
108 'name' => $content_type_name,
109 'type' => $content_type_id,
111 $this->drupalPostForm(NULL, $edit, t('Save content type'));
114 $this->enableModerationThroughUi($content_type_id, $workflow_id);
119 * Enable moderation for a specified content type, using the UI.
121 * @param string $content_type_id
123 * @param string $workflow_id
124 * The workflow to attach to the bundle.
126 public function enableModerationThroughUi($content_type_id, $workflow_id = 'editorial') {
127 $this->drupalGet('/admin/config/workflow/workflows');
128 $this->assertLinkByHref('admin/config/workflow/workflows/manage/' . $workflow_id);
129 $edit['bundles[' . $content_type_id . ']'] = TRUE;
130 $this->drupalPostForm('admin/config/workflow/workflows/manage/' . $workflow_id . '/type/node', $edit, t('Save'));
131 // Ensure the parent environment is up-to-date.
132 // @see content_moderation_workflow_insert()
133 \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
134 \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
138 * Grants given user permission to create content of given type.
140 * @param \Drupal\Core\Session\AccountInterface $account
141 * User to grant permission to.
142 * @param string $content_type_id
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));