1<?php 2 3namespace Drupal\Tests\content_moderation\Functional; 4 5use Drupal\Core\Session\AccountInterface; 6use Drupal\node\Entity\NodeType; 7use Drupal\Tests\BrowserTestBase; 8use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait; 9use Drupal\user\Entity\Role; 10 11/** 12 * Defines a base class for moderation state tests. 13 */ 14abstract 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 protected 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 $edit = [ 116 'name' => $content_type_name, 117 'type' => $content_type_id, 118 ]; 119 $this->submitForm($edit, 'Save content type'); 120 121 // Check the content type has been set to create new revisions. 122 $this->assertTrue(NodeType::load($content_type_id)->shouldCreateNewRevision()); 123 124 if ($moderated) { 125 $this->enableModerationThroughUi($content_type_id, $workflow_id); 126 } 127 } 128 129 /** 130 * Enable moderation for a specified content type, using the UI. 131 * 132 * @param string $content_type_id 133 * Machine name. 134 * @param string $workflow_id 135 * The workflow to attach to the bundle. 136 */ 137 public function enableModerationThroughUi($content_type_id, $workflow_id = 'editorial') { 138 $this->drupalGet('/admin/config/workflow/workflows'); 139 $this->assertSession()->linkByHrefExists('admin/config/workflow/workflows/manage/' . $workflow_id); 140 $edit['bundles[' . $content_type_id . ']'] = TRUE; 141 $this->drupalGet('admin/config/workflow/workflows/manage/' . $workflow_id . '/type/node'); 142 $this->submitForm($edit, 'Save'); 143 // Ensure the parent environment is up-to-date. 144 // @see content_moderation_workflow_insert() 145 \Drupal::service('entity_type.bundle.info')->clearCachedBundles(); 146 \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); 147 /** @var \Drupal\Core\Routing\RouteBuilderInterface $router_builder */ 148 $router_builder = $this->container->get('router.builder'); 149 $router_builder->rebuildIfNeeded(); 150 } 151 152 /** 153 * Grants given user permission to create content of given type. 154 * 155 * @param \Drupal\Core\Session\AccountInterface $account 156 * User to grant permission to. 157 * @param string $content_type_id 158 * Content type ID. 159 */ 160 protected function grantUserPermissionToCreateContentOfType(AccountInterface $account, $content_type_id) { 161 $role_ids = $account->getRoles(TRUE); 162 /** @var \Drupal\user\RoleInterface $role */ 163 $role_id = reset($role_ids); 164 $role = Role::load($role_id); 165 $role->grantPermission(sprintf('create %s content', $content_type_id)); 166 $role->grantPermission(sprintf('edit any %s content', $content_type_id)); 167 $role->grantPermission(sprintf('delete any %s content', $content_type_id)); 168 $role->save(); 169 } 170 171} 172