3 namespace Drupal\Tests\workflows\Unit;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\workflows\Entity\Workflow;
8 use Drupal\workflows\State;
9 use Drupal\workflows\WorkflowInterface;
10 use Drupal\workflows\WorkflowTypeInterface;
11 use Drupal\workflows\WorkflowTypeManager;
12 use Prophecy\Argument;
15 * @coversDefaultClass \Drupal\workflows\State
19 class StateTest extends UnitTestCase {
22 * Sets up the Workflow Type manager so that workflow entities can be used.
24 protected function setUp() {
26 // Create a container so that the plugin manager and workflow type can be
28 $container = new ContainerBuilder();
29 $workflow_type = $this->prophesize(WorkflowTypeInterface::class);
30 $workflow_type->decorateState(Argument::any())->willReturnArgument(0);
31 $workflow_type->decorateTransition(Argument::any())->willReturnArgument(0);
32 $workflow_type->deleteState(Argument::any())->willReturn(NULL);
33 $workflow_type->deleteTransition(Argument::any())->willReturn(NULL);
34 $workflow_manager = $this->prophesize(WorkflowTypeManager::class);
35 $workflow_manager->createInstance('test_type', Argument::any())->willReturn($workflow_type->reveal());
36 $container->set('plugin.manager.workflows.type', $workflow_manager->reveal());
37 \Drupal::setContainer($container);
41 * @covers ::__construct
46 public function testGetters() {
48 $this->prophesize(WorkflowInterface::class)->reveal(),
53 $this->assertEquals('draft', $state->id());
54 $this->assertEquals('Draft', $state->label());
55 $this->assertEquals(3, $state->weight());
59 * @covers ::canTransitionTo
61 public function testCanTransitionTo() {
62 $workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
64 ->addState('draft', 'Draft')
65 ->addState('published', 'Published')
66 ->addTransition('publish', 'Publish', ['draft'], 'published');
67 $state = $workflow->getState('draft');
68 $this->assertTrue($state->canTransitionTo('published'));
69 $this->assertFalse($state->canTransitionTo('some_other_state'));
71 $workflow->deleteTransition('publish');
72 $this->assertFalse($state->canTransitionTo('published'));
76 * @covers ::getTransitionTo
78 public function testGetTransitionTo() {
79 $workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
81 ->addState('draft', 'Draft')
82 ->addState('published', 'Published')
83 ->addTransition('publish', 'Publish', ['draft'], 'published');
84 $state = $workflow->getState('draft');
85 $transition = $state->getTransitionTo('published');
86 $this->assertEquals('Publish', $transition->label());
90 * @covers ::getTransitionTo
92 public function testGetTransitionToException() {
93 $this->setExpectedException(\InvalidArgumentException::class, "Can not transition to 'published' state");
94 $workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
95 $workflow->addState('draft', 'Draft');
96 $state = $workflow->getState('draft');
97 $state->getTransitionTo('published');
101 * @covers ::getTransitions
103 public function testGetTransitions() {
104 $workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
106 ->addState('draft', 'Draft')
107 ->addState('published', 'Published')
108 ->addState('archived', 'Archived')
109 ->addTransition('create_new_draft', 'Create new draft', ['draft'], 'draft')
110 ->addTransition('publish', 'Publish', ['draft'], 'published')
111 ->addTransition('archive', 'Archive', ['published'], 'archived');
112 $state = $workflow->getState('draft');
113 $transitions = $state->getTransitions();
114 $this->assertCount(2, $transitions);
115 $this->assertEquals('Create new draft', $transitions['create_new_draft']->label());
116 $this->assertEquals('Publish', $transitions['publish']->label());
120 * @covers ::labelCallback
122 public function testLabelCallback() {
123 $workflow = $this->prophesize(WorkflowInterface::class)->reveal();
125 new State($workflow, 'draft', 'Draft'),
126 new State($workflow, 'published', 'Published'),
128 $this->assertEquals(['Draft', 'Published'], array_map([State::class, 'labelCallback'], $states));