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 {
17 protected $profile = 'testing';
22 * @var \Drupal\Core\Session\AccountInterface
27 * Permissions to grant admin user.
31 protected $permissions = [
32 'administer content moderation',
33 'access administration pages',
34 'administer content types',
36 'view latest version',
37 'view any unpublished content',
38 'access content overview',
39 'use editorial transition create_new_draft',
40 'use editorial transition publish',
48 public static $modules = [
59 protected function setUp() {
61 $this->adminUser = $this->drupalCreateUser($this->permissions);
62 $this->drupalPlaceBlock('local_tasks_block', ['id' => 'tabs_block']);
63 $this->drupalPlaceBlock('page_title_block');
64 $this->drupalPlaceBlock('local_actions_block', ['id' => 'actions_block']);
68 * Gets the permission machine name for a transition.
70 * @param string $workflow_id
72 * @param string $transition_id
76 * The permission machine name for a transition.
78 protected function getWorkflowTransitionPermission($workflow_id, $transition_id) {
79 return 'use ' . $workflow_id . ' transition ' . $transition_id;
83 * Creates a content-type from the UI.
85 * @param string $content_type_name
86 * Content type human name.
87 * @param string $content_type_id
89 * @param bool $moderated
90 * TRUE if should be moderated.
91 * @param string $workflow_id
92 * The workflow to attach to the bundle.
94 protected function createContentTypeFromUi($content_type_name, $content_type_id, $moderated = FALSE, $workflow_id = 'editorial') {
95 $this->drupalGet('admin/structure/types');
96 $this->clickLink('Add content type');
98 'name' => $content_type_name,
99 'type' => $content_type_id,
101 $this->drupalPostForm(NULL, $edit, t('Save content type'));
104 $this->enableModerationThroughUi($content_type_id, $workflow_id);
109 * Enable moderation for a specified content type, using the UI.
111 * @param string $content_type_id
113 * @param string $workflow_id
114 * The workflow to attach to the bundle.
116 protected function enableModerationThroughUi($content_type_id, $workflow_id = 'editorial') {
117 $edit['workflow'] = $workflow_id;
118 $this->drupalPostForm('admin/structure/types/manage/' . $content_type_id . '/moderation', $edit, t('Save'));
119 // Ensure the parent environment is up-to-date.
120 // @see content_moderation_workflow_insert()
121 \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
122 \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
126 * Grants given user permission to create content of given type.
128 * @param \Drupal\Core\Session\AccountInterface $account
129 * User to grant permission to.
130 * @param string $content_type_id
133 protected function grantUserPermissionToCreateContentOfType(AccountInterface $account, $content_type_id) {
134 $role_ids = $account->getRoles(TRUE);
135 /* @var \Drupal\user\RoleInterface $role */
136 $role_id = reset($role_ids);
137 $role = Role::load($role_id);
138 $role->grantPermission(sprintf('create %s content', $content_type_id));
139 $role->grantPermission(sprintf('edit any %s content', $content_type_id));
140 $role->grantPermission(sprintf('delete any %s content', $content_type_id));