View source
<?php
namespace Drupal\Tests\content_moderation\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
use Drupal\user\Entity\Role;
abstract class ModerationStateTestBase extends BrowserTestBase {
use ContentModerationTestTrait;
protected $profile = 'testing';
protected $adminUser;
protected $permissions = [
'administer workflows',
'access administration pages',
'administer content types',
'administer nodes',
'view latest version',
'view any unpublished content',
'access content overview',
'use editorial transition create_new_draft',
'use editorial transition publish',
'use editorial transition archive',
'use editorial transition archived_draft',
'use editorial transition archived_published',
];
protected $workflow;
public static $modules = [
'content_moderation',
'block',
'block_content',
'node',
'entity_test',
];
protected function setUp() {
parent::setUp();
$this->workflow = $this
->createEditorialWorkflow();
$this->adminUser = $this
->drupalCreateUser($this->permissions);
$this
->drupalPlaceBlock('local_tasks_block', [
'id' => 'tabs_block',
]);
$this
->drupalPlaceBlock('page_title_block');
$this
->drupalPlaceBlock('local_actions_block', [
'id' => 'actions_block',
]);
}
protected function getWorkflowTransitionPermission($workflow_id, $transition_id) {
return 'use ' . $workflow_id . ' transition ' . $transition_id;
}
protected function createContentTypeFromUi($content_type_name, $content_type_id, $moderated = FALSE, $workflow_id = 'editorial') {
$this
->drupalGet('admin/structure/types');
$this
->clickLink('Add content type');
$edit = [
'name' => $content_type_name,
'type' => $content_type_id,
];
$this
->drupalPostForm(NULL, $edit, t('Save content type'));
$this
->assertTrue(NodeType::load($content_type_id)
->shouldCreateNewRevision());
if ($moderated) {
$this
->enableModerationThroughUi($content_type_id, $workflow_id);
}
}
public function enableModerationThroughUi($content_type_id, $workflow_id = 'editorial') {
$this
->drupalGet('/admin/config/workflow/workflows');
$this
->assertLinkByHref('admin/config/workflow/workflows/manage/' . $workflow_id);
$edit['bundles[' . $content_type_id . ']'] = TRUE;
$this
->drupalPostForm('admin/config/workflow/workflows/manage/' . $workflow_id . '/type/node', $edit, t('Save'));
\Drupal::service('entity_type.bundle.info')
->clearCachedBundles();
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
$router_builder = $this->container
->get('router.builder');
$router_builder
->rebuildIfNeeded();
}
protected function grantUserPermissionToCreateContentOfType(AccountInterface $account, $content_type_id) {
$role_ids = $account
->getRoles(TRUE);
$role_id = reset($role_ids);
$role = Role::load($role_id);
$role
->grantPermission(sprintf('create %s content', $content_type_id));
$role
->grantPermission(sprintf('edit any %s content', $content_type_id));
$role
->grantPermission(sprintf('delete any %s content', $content_type_id));
$role
->save();
}
}