c9ef7151d771cf05865cad5304b5a7e41f895be7
[yaffs-website] / web / core / modules / workflows / tests / src / Kernel / RequiredStatesTest.php
1 <?php
2
3 namespace Drupal\Tests\workflows\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\workflows\Entity\Workflow;
7 use Drupal\workflows\Exception\RequiredStateMissingException;
8
9 /**
10  * Tests Workflow type's required states and configuration initialization.
11  *
12  * @coversDefaultClass \Drupal\workflows\Plugin\WorkflowTypeBase
13  *
14  * @group workflows
15  */
16 class RequiredStatesTest extends KernelTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = ['workflows', 'workflow_type_test'];
22
23   /**
24    * @covers ::getRequiredStates
25    * @covers ::initializeWorkflow
26    * @covers ::__construct
27    */
28   public function testGetRequiredStates() {
29     $workflow = new Workflow([
30       'id' => 'test',
31       'type' => 'workflow_type_required_state_test',
32     ], 'workflow');
33     $workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
34     $workflow->save();
35     $this->assertEquals(['fresh', 'rotten'], $workflow->getTypePlugin()
36       ->getRequiredStates());
37
38     // Ensure that the workflow has the default configuration.
39     $this->assertTrue($workflow->hasState('rotten'));
40     $this->assertTrue($workflow->hasState('fresh'));
41     $this->assertTrue($workflow->hasTransitionFromStateToState('fresh', 'rotten'));
42   }
43
44   /**
45    * @covers \Drupal\workflows\Entity\Workflow::preSave
46    */
47   public function testDeleteRequiredStateAPI() {
48     $workflow = new Workflow([
49       'id' => 'test',
50       'type' => 'workflow_type_required_state_test',
51     ], 'workflow');
52     $workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
53     $workflow->save();
54     // Ensure that required states can't be deleted.
55     $this->setExpectedException(RequiredStateMissingException::class, "Required State Type Test' requires states with the ID 'fresh' in workflow 'test'");
56     $workflow->deleteState('fresh')->save();
57   }
58
59   /**
60    * @covers \Drupal\workflows\Entity\Workflow::preSave
61    */
62   public function testNoStatesRequiredStateAPI() {
63     $workflow = new Workflow([
64       'id' => 'test',
65       'type' => 'workflow_type_required_state_test',
66     ], 'workflow');
67     $this->setExpectedException(RequiredStateMissingException::class, "Required State Type Test' requires states with the ID 'fresh', 'rotten' in workflow 'test'");
68     $workflow->save();
69   }
70
71   /**
72    * Ensures that initialized configuration can be changed.
73    */
74   public function testChangeRequiredStateAPI() {
75     $workflow = new Workflow([
76       'id' => 'test',
77       'type' => 'workflow_type_required_state_test',
78     ], 'workflow');
79     $workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
80     $workflow->save();
81
82     // Ensure states added by default configuration can be changed.
83     $this->assertEquals('Fresh', $workflow->getState('fresh')->label());
84     $workflow
85       ->setStateLabel('fresh', 'Fresher')
86       ->save();
87     $this->assertEquals('Fresher', $workflow->getState('fresh')->label());
88
89     // Ensure transitions can be altered.
90     $workflow
91       ->addState('cooked', 'Cooked')
92       ->setTransitionFromStates('rot', ['fresh', 'cooked'])
93       ->save();
94     $this->assertTrue($workflow->hasTransitionFromStateToState('fresh', 'rotten'));
95     $this->assertTrue($workflow->hasTransitionFromStateToState('cooked', 'rotten'));
96
97     $workflow
98       ->setTransitionFromStates('rot', ['cooked'])
99       ->save();
100     $this->assertFalse($workflow->hasTransitionFromStateToState('fresh', 'rotten'));
101     $this->assertTrue($workflow->hasTransitionFromStateToState('cooked', 'rotten'));
102
103     // Ensure the default configuration does not cause ordering issues.
104     $workflow->addTransition('cook', 'Cook', ['fresh'], 'cooked')->save();
105     $this->assertSame([
106       'cooked',
107       'fresh',
108       'rotten',
109     ], array_keys($workflow->get('states')));
110     $this->assertSame([
111       'cook',
112       'rot',
113     ], array_keys($workflow->get('transitions')));
114
115     // Ensure that transitions can be deleted.
116     $workflow->deleteTransition('rot')->save();
117     $this->assertFalse($workflow->hasTransition('rot'));
118   }
119
120 }