82feca34627b02c28e3f80e969245347b9b47054
[yaffs-website] / web / core / modules / workflows / tests / src / Unit / StateTest.php
1 <?php
2
3 namespace Drupal\Tests\workflows\Unit;
4
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;
13
14 /**
15  * @coversDefaultClass \Drupal\workflows\State
16  *
17  * @group workflows
18  */
19 class StateTest extends UnitTestCase {
20
21   /**
22    * Sets up the Workflow Type manager so that workflow entities can be used.
23    */
24   protected function setUp() {
25     parent::setUp();
26     // Create a container so that the plugin manager and workflow type can be
27     // mocked.
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);
38   }
39
40   /**
41    * @covers ::__construct
42    * @covers ::id
43    * @covers ::label
44    * @covers ::weight
45    */
46   public function testGetters() {
47     $state = new State(
48       $this->prophesize(WorkflowInterface::class)->reveal(),
49       'draft',
50       'Draft',
51       3
52     );
53     $this->assertEquals('draft', $state->id());
54     $this->assertEquals('Draft', $state->label());
55     $this->assertEquals(3, $state->weight());
56   }
57
58   /**
59    * @covers ::canTransitionTo
60    */
61   public function testCanTransitionTo() {
62     $workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
63     $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'));
70
71     $workflow->deleteTransition('publish');
72     $this->assertFalse($state->canTransitionTo('published'));
73   }
74
75   /**
76    * @covers ::getTransitionTo
77    */
78   public function testGetTransitionTo() {
79     $workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
80     $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());
87   }
88
89   /**
90    * @covers ::getTransitionTo
91    */
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');
98   }
99
100   /**
101    * @covers ::getTransitions
102    */
103   public function testGetTransitions() {
104     $workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
105     $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());
117   }
118
119   /**
120    * @covers ::labelCallback
121    */
122   public function testLabelCallback() {
123     $workflow = $this->prophesize(WorkflowInterface::class)->reveal();
124     $states = [
125       new State($workflow, 'draft', 'Draft'),
126       new State($workflow, 'published', 'Published'),
127     ];
128     $this->assertEquals(['Draft', 'Published'], array_map([State::class, 'labelCallback'], $states));
129   }
130
131 }