View source
<?php
namespace Drupal\Tests\content_moderation\Unit;
use Drupal\content_moderation\ModerationInformationInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\content_moderation\StateTransitionValidation;
use Drupal\Tests\UnitTestCase;
use Drupal\workflow_type_test\Plugin\WorkflowType\TestType;
use Drupal\workflows\Entity\Workflow;
use Drupal\workflows\State;
use Drupal\workflows\WorkflowTypeManager;
use Prophecy\Argument;
class StateTransitionValidationTest extends UnitTestCase {
protected $workflow;
protected function setUp() {
parent::setUp();
$container = new ContainerBuilder();
$workflow_manager = $this
->prophesize(WorkflowTypeManager::class);
$workflow_manager
->createInstance('content_moderation', Argument::any())
->willReturn(new TestType([], '', []));
$container
->set('plugin.manager.workflows.type', $workflow_manager
->reveal());
\Drupal::setContainer($container);
$this->workflow = new Workflow([
'id' => 'process',
'type' => 'content_moderation',
], 'workflow');
$this->workflow
->getTypePlugin()
->addState('draft', 'draft')
->addState('needs_review', 'needs_review')
->addState('published', 'published')
->addTransition('draft', 'draft', [
'draft',
], 'draft')
->addTransition('review', 'review', [
'draft',
], 'needs_review')
->addTransition('publish', 'publish', [
'needs_review',
'published',
], 'published');
}
public function testUserSensitiveValidTransitions($from_id, $to_id, $permission, $allowed, $result) {
$user = $this
->prophesize(AccountInterface::class);
$user
->hasPermission($permission)
->willReturn($allowed);
$user
->hasPermission(Argument::type('string'))
->willReturn(FALSE);
$entity = $this
->prophesize(ContentEntityInterface::class);
$entity = $entity
->reveal();
$entity->moderation_state = new \stdClass();
$entity->moderation_state->value = $from_id;
$moderation_info = $this
->prophesize(ModerationInformationInterface::class);
$moderation_info
->getWorkflowForEntity($entity)
->willReturn($this->workflow);
$validator = new StateTransitionValidation($moderation_info
->reveal());
$has_transition = FALSE;
foreach ($validator
->getValidTransitions($entity, $user
->reveal()) as $transition) {
if ($transition
->to()
->id() === $to_id) {
$has_transition = TRUE;
break;
}
}
$this
->assertSame($result, $has_transition);
}
public function testDeprecatedEntityParameter() {
$moderation_info = $this
->prophesize(ModerationInformationInterface::class);
$state = new State($this->workflow
->getTypePlugin(), 'draft', 'draft');
$user = $this
->prophesize(AccountInterface::class);
$validator = new StateTransitionValidation($moderation_info
->reveal());
$validator
->isTransitionValid($this->workflow, $state, $state, $user
->reveal());
}
public function userTransitionsProvider() {
$ret[] = [
'draft',
'draft',
'use process transition draft',
TRUE,
TRUE,
];
$ret[] = [
'draft',
'draft',
'use process transition draft',
FALSE,
FALSE,
];
$ret[] = [
'draft',
'draft',
'use process transition review',
TRUE,
FALSE,
];
return $ret;
}
}