92edbb9f334bd9446564a24bd4236986a28bb7ae
[yaffs-website] / web / core / modules / workflows / tests / src / Unit / StateTest.php
1 <?php
2
3 namespace Drupal\Tests\workflows\Unit;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\workflow_type_test\Plugin\WorkflowType\TestType;
7 use Drupal\workflows\State;
8 use Drupal\workflows\WorkflowTypeInterface;
9
10 /**
11  * @coversDefaultClass \Drupal\workflows\State
12  *
13  * @group workflows
14  */
15 class StateTest extends UnitTestCase {
16
17   /**
18    * @covers ::__construct
19    * @covers ::id
20    * @covers ::label
21    * @covers ::weight
22    */
23   public function testGetters() {
24     $state = new State(
25       $this->prophesize(WorkflowTypeInterface::class)->reveal(),
26       'draft',
27       'Draft',
28       3
29     );
30     $this->assertEquals('draft', $state->id());
31     $this->assertEquals('Draft', $state->label());
32     $this->assertEquals(3, $state->weight());
33   }
34
35   /**
36    * @covers ::canTransitionTo
37    */
38   public function testCanTransitionTo() {
39     $workflow_type = new TestType([], '', []);
40     $workflow_type
41       ->addState('draft', 'Draft')
42       ->addState('published', 'Published')
43       ->addTransition('publish', 'Publish', ['draft'], 'published');
44     $state = $workflow_type->getState('draft');
45     $this->assertTrue($state->canTransitionTo('published'));
46     $this->assertFalse($state->canTransitionTo('some_other_state'));
47
48     $workflow_type->deleteTransition('publish');
49     $this->assertFalse($state->canTransitionTo('published'));
50   }
51
52   /**
53    * @covers ::getTransitionTo
54    */
55   public function testGetTransitionTo() {
56     $workflow_type = new TestType([], '', []);
57     $workflow_type
58       ->addState('draft', 'Draft')
59       ->addState('published', 'Published')
60       ->addTransition('publish', 'Publish', ['draft'], 'published');
61     $state = $workflow_type->getState('draft');
62     $transition = $state->getTransitionTo('published');
63     $this->assertEquals('Publish', $transition->label());
64   }
65
66   /**
67    * @covers ::getTransitionTo
68    */
69   public function testGetTransitionToException() {
70     $this->setExpectedException(\InvalidArgumentException::class, "Can not transition to 'published' state");
71     $workflow_type = new TestType([], '', []);
72     $workflow_type->addState('draft', 'Draft');
73     $state = $workflow_type->getState('draft');
74     $state->getTransitionTo('published');
75   }
76
77   /**
78    * @covers ::getTransitions
79    */
80   public function testGetTransitions() {
81     $workflow_type = new TestType([], '', []);
82     $workflow_type
83       ->addState('draft', 'Draft')
84       ->addState('published', 'Published')
85       ->addState('archived', 'Archived')
86       ->addTransition('create_new_draft', 'Create new draft', ['draft'], 'draft')
87       ->addTransition('publish', 'Publish', ['draft'], 'published')
88       ->addTransition('archive', 'Archive', ['published'], 'archived');
89     $state = $workflow_type->getState('draft');
90     $transitions = $state->getTransitions();
91     $this->assertCount(2, $transitions);
92     $this->assertEquals('Create new draft', $transitions['create_new_draft']->label());
93     $this->assertEquals('Publish', $transitions['publish']->label());
94   }
95
96   /**
97    * @covers ::labelCallback
98    */
99   public function testLabelCallback() {
100     $workflow_type = $this->prophesize(WorkflowTypeInterface::class)->reveal();
101     $states = [
102       new State($workflow_type, 'draft', 'Draft'),
103       new State($workflow_type, 'published', 'Published'),
104     ];
105     $this->assertEquals(['Draft', 'Published'], array_map([State::class, 'labelCallback'], $states));
106   }
107
108 }