Security update for Core, with self-updated composer
[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    * @var string
18    */
19   protected $profile = 'testing';
20
21   /**
22    * Admin user.
23    *
24    * @var \Drupal\Core\Session\AccountInterface
25    */
26   protected $adminUser;
27
28   /**
29    * Permissions to grant admin user.
30    *
31    * @var array
32    */
33   protected $permissions = [
34     'administer workflows',
35     'access administration pages',
36     'administer content types',
37     'administer nodes',
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',
46   ];
47
48   /**
49    * Modules to enable.
50    *
51    * @var array
52    */
53   public static $modules = [
54     'content_moderation',
55     'block',
56     'block_content',
57     'node',
58     'entity_test',
59   ];
60
61   /**
62    * Sets the test up.
63    */
64   protected function setUp() {
65     parent::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']);
70   }
71
72   /**
73    * Gets the permission machine name for a transition.
74    *
75    * @param string $workflow_id
76    *   The workflow ID.
77    * @param string $transition_id
78    *   The transition ID.
79    *
80    * @return string
81    *   The permission machine name for a transition.
82    */
83   protected function getWorkflowTransitionPermission($workflow_id, $transition_id) {
84     return 'use ' . $workflow_id . ' transition ' . $transition_id;
85   }
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
103     // Check that the 'Create new revision' checkbox is checked and disabled.
104     $this->assertSession()->checkboxChecked('options[revision]');
105     $this->assertSession()->fieldDisabled('options[revision]');
106
107     $edit = [
108       'name' => $content_type_name,
109       'type' => $content_type_id,
110     ];
111     $this->drupalPostForm(NULL, $edit, t('Save content type'));
112
113     if ($moderated) {
114       $this->enableModerationThroughUi($content_type_id, $workflow_id);
115     }
116   }
117
118   /**
119    * Enable moderation for a specified content type, using the UI.
120    *
121    * @param string $content_type_id
122    *   Machine name.
123    * @param string $workflow_id
124    *   The workflow to attach to the bundle.
125    */
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();
135   }
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
156 }