View source
<?php
namespace Drupal\Tests\workbench_moderation\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\FunctionalTests\AssertLegacyTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\workbench_moderation\Entity\ModerationState;
abstract class ModerationStateTestBase extends BrowserTestBase {
use AssertLegacyTrait;
protected $defaultTheme = 'stark';
protected $profile = 'testing';
protected $adminUser;
protected $permissions = [
'administer moderation states',
'administer moderation state transitions',
'use draft_draft transition',
'use draft_needs_review transition',
'use published_draft transition',
'use needs_review_published transition',
'access administration pages',
'administer content types',
'administer nodes',
'view latest version',
'view any unpublished content',
'access content overview',
];
public static $modules = [
'workbench_moderation',
'block',
'block_content',
'publishing_dropbutton',
'node',
'views',
'options',
'user',
];
protected function setUp() {
parent::setUp();
$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 createContentTypeFromUi($content_type_name, $content_type_id, $moderated = FALSE, array $allowed_states = [], $default_state = NULL) {
$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'));
if ($moderated) {
$this
->enableModerationThroughUi($content_type_id, $allowed_states, $default_state);
}
}
protected function enableModerationThroughUi($content_type_id, array $allowed_states, $default_state) {
$this
->drupalGet('admin/structure/types/manage/' . $content_type_id . '/moderation');
$this
->assertFieldByName('enable_moderation_state');
$this
->assertNoFieldChecked('edit-enable-moderation-state');
$edit['enable_moderation_state'] = 1;
foreach (ModerationState::loadMultiple() as $id => $state) {
$key = $state
->isPublishedState() ? 'allowed_moderation_states_published[' . $state
->id() . ']' : 'allowed_moderation_states_unpublished[' . $state
->id() . ']';
$edit[$key] = (int) in_array($id, $allowed_states);
}
$edit['default_moderation_state'] = $default_state;
$this
->drupalPostForm(NULL, $edit, t('Save'));
}
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();
}
}