1589a3ebd9d6db5b2ff578297f5d1b3af07582d5
[yaffs-website] / web / core / modules / content_moderation / tests / src / Functional / ModerationStateTestBase.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Functional;
4
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\user\Entity\Role;
8
9 /**
10  * Defines a base class for moderation state tests.
11  */
12 abstract class ModerationStateTestBase extends BrowserTestBase {
13
14   /**
15    * Profile to use.
16    */
17   protected $profile = 'testing';
18
19   /**
20    * Admin user.
21    *
22    * @var \Drupal\Core\Session\AccountInterface
23    */
24   protected $adminUser;
25
26   /**
27    * Permissions to grant admin user.
28    *
29    * @var array
30    */
31   protected $permissions = [
32     'administer content moderation',
33     'access administration pages',
34     'administer content types',
35     'administer nodes',
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',
41   ];
42
43   /**
44    * Modules to enable.
45    *
46    * @var array
47    */
48   public static $modules = [
49     'content_moderation',
50     'block',
51     'block_content',
52     'node',
53     'entity_test',
54   ];
55
56   /**
57    * Sets the test up.
58    */
59   protected function setUp() {
60     parent::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']);
65   }
66
67   /**
68    * Gets the permission machine name for a transition.
69    *
70    * @param string $workflow_id
71    *   The workflow ID.
72    * @param string $transition_id
73    *   The transition ID.
74    *
75    * @return string
76    *   The permission machine name for a transition.
77    */
78   protected function getWorkflowTransitionPermission($workflow_id, $transition_id) {
79     return 'use ' . $workflow_id . ' transition ' . $transition_id;
80   }
81
82   /**
83    * Creates a content-type from the UI.
84    *
85    * @param string $content_type_name
86    *   Content type human name.
87    * @param string $content_type_id
88    *   Machine name.
89    * @param bool $moderated
90    *   TRUE if should be moderated.
91    * @param string $workflow_id
92    *   The workflow to attach to the bundle.
93    */
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');
97     $edit = [
98       'name' => $content_type_name,
99       'type' => $content_type_id,
100     ];
101     $this->drupalPostForm(NULL, $edit, t('Save content type'));
102
103     if ($moderated) {
104       $this->enableModerationThroughUi($content_type_id, $workflow_id);
105     }
106   }
107
108   /**
109    * Enable moderation for a specified content type, using the UI.
110    *
111    * @param string $content_type_id
112    *   Machine name.
113    * @param string $workflow_id
114    *   The workflow to attach to the bundle.
115    */
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();
123   }
124
125   /**
126    * Grants given user permission to create content of given type.
127    *
128    * @param \Drupal\Core\Session\AccountInterface $account
129    *   User to grant permission to.
130    * @param string $content_type_id
131    *   Content type ID.
132    */
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));
141     $role->save();
142   }
143
144 }