Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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\node\Entity\NodeType;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
9 use Drupal\user\Entity\Role;
10
11 /**
12  * Defines a base class for moderation state tests.
13  */
14 abstract class ModerationStateTestBase extends BrowserTestBase {
15
16   use ContentModerationTestTrait;
17
18   /**
19    * Profile to use.
20    *
21    * @var string
22    */
23   protected $profile = 'testing';
24
25   /**
26    * Admin user.
27    *
28    * @var \Drupal\Core\Session\AccountInterface
29    */
30   protected $adminUser;
31
32   /**
33    * Permissions to grant admin user.
34    *
35    * @var array
36    */
37   protected $permissions = [
38     'administer workflows',
39     'access administration pages',
40     'administer content types',
41     'administer nodes',
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',
50   ];
51
52   /**
53    * The editorial workflow entity.
54    *
55    * @var \Drupal\workflows\Entity\Workflow
56    */
57   protected $workflow;
58
59   /**
60    * Modules to enable.
61    *
62    * @var array
63    */
64   public static $modules = [
65     'content_moderation',
66     'block',
67     'block_content',
68     'node',
69     'entity_test',
70   ];
71
72   /**
73    * Sets the test up.
74    */
75   protected function setUp() {
76     parent::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']);
82   }
83
84   /**
85    * Gets the permission machine name for a transition.
86    *
87    * @param string $workflow_id
88    *   The workflow ID.
89    * @param string $transition_id
90    *   The transition ID.
91    *
92    * @return string
93    *   The permission machine name for a transition.
94    */
95   protected function getWorkflowTransitionPermission($workflow_id, $transition_id) {
96     return 'use ' . $workflow_id . ' transition ' . $transition_id;
97   }
98
99   /**
100    * Creates a content-type from the UI.
101    *
102    * @param string $content_type_name
103    *   Content type human name.
104    * @param string $content_type_id
105    *   Machine name.
106    * @param bool $moderated
107    *   TRUE if should be moderated.
108    * @param string $workflow_id
109    *   The workflow to attach to the bundle.
110    */
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');
114
115     // Check that the 'Create new revision' checkbox is checked and disabled.
116     $this->assertSession()->checkboxChecked('options[revision]');
117     $this->assertSession()->fieldDisabled('options[revision]');
118
119     $edit = [
120       'name' => $content_type_name,
121       'type' => $content_type_id,
122     ];
123     $this->drupalPostForm(NULL, $edit, t('Save content type'));
124
125     // Check the content type has been set to create new revisions.
126     $this->assertTrue(NodeType::load($content_type_id)->isNewRevision());
127
128     if ($moderated) {
129       $this->enableModerationThroughUi($content_type_id, $workflow_id);
130     }
131   }
132
133   /**
134    * Enable moderation for a specified content type, using the UI.
135    *
136    * @param string $content_type_id
137    *   Machine name.
138    * @param string $workflow_id
139    *   The workflow to attach to the bundle.
140    */
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();
153   }
154
155   /**
156    * Grants given user permission to create content of given type.
157    *
158    * @param \Drupal\Core\Session\AccountInterface $account
159    *   User to grant permission to.
160    * @param string $content_type_id
161    *   Content type ID.
162    */
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));
171     $role->save();
172   }
173
174 }