Security update for Core, with self-updated composer
[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 ::__construct
26    */
27   public function testGetRequiredStates() {
28     $workflow = new Workflow([
29       'id' => 'test',
30       'type' => 'workflow_type_required_state_test',
31     ], 'workflow');
32     $workflow->save();
33     $this->assertEquals(['fresh', 'rotten'], $workflow->getTypePlugin()
34       ->getRequiredStates());
35
36     // Ensure that the workflow has the default configuration.
37     $this->assertTrue($workflow->getTypePlugin()->hasState('rotten'));
38     $this->assertTrue($workflow->getTypePlugin()->hasState('fresh'));
39     $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('fresh', 'rotten'));
40   }
41
42   /**
43    * @covers \Drupal\workflows\Entity\Workflow::preSave
44    */
45   public function testDeleteRequiredStateAPI() {
46     $workflow = new Workflow([
47       'id' => 'test',
48       'type' => 'workflow_type_required_state_test',
49     ], 'workflow');
50     $workflow->save();
51     // Ensure that required states can't be deleted.
52     $this->setExpectedException(RequiredStateMissingException::class, "Required State Type Test' requires states with the ID 'fresh' in workflow 'test'");
53     $workflow->getTypePlugin()->deleteState('fresh');
54     $workflow->save();
55   }
56
57   /**
58    * @covers \Drupal\workflows\Entity\Workflow::preSave
59    */
60   public function testNoStatesRequiredStateAPI() {
61     $workflow = new Workflow([
62       'id' => 'test',
63       'type' => 'workflow_type_required_state_test',
64       'type_settings' => [
65         'states' => [],
66       ],
67     ], 'workflow');
68     $this->setExpectedException(RequiredStateMissingException::class, "Required State Type Test' requires states with the ID 'fresh', 'rotten' in workflow 'test'");
69     $workflow->save();
70   }
71
72   /**
73    * Ensures that initialized configuration can be changed.
74    */
75   public function testChangeRequiredStateAPI() {
76     $workflow = new Workflow([
77       'id' => 'test',
78       'type' => 'workflow_type_required_state_test',
79     ], 'workflow');
80     $workflow->save();
81
82     // Ensure states added by default configuration can be changed.
83     $this->assertEquals('Fresh', $workflow->getTypePlugin()->getState('fresh')->label());
84     $workflow
85       ->getTypePlugin()
86       ->setStateLabel('fresh', 'Fresher');
87     $workflow->save();
88     $this->assertEquals('Fresher', $workflow->getTypePlugin()->getState('fresh')->label());
89
90     // Ensure transitions can be altered.
91     $workflow
92       ->getTypePlugin()
93       ->addState('cooked', 'Cooked')
94       ->setTransitionFromStates('rot', ['fresh', 'cooked']);
95     $workflow->save();
96     $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('fresh', 'rotten'));
97     $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('cooked', 'rotten'));
98
99     $workflow
100       ->getTypePlugin()
101       ->setTransitionFromStates('rot', ['cooked']);
102     $workflow->save();
103     $this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('fresh', 'rotten'));
104     $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('cooked', 'rotten'));
105
106     // Ensure the default configuration does not cause ordering issues.
107     $workflow->getTypePlugin()->addTransition('cook', 'Cook', ['fresh'], 'cooked');
108     $workflow->save();
109     $this->assertSame([
110       'cooked',
111       'fresh',
112       'rotten',
113     ], array_keys($workflow->getTypePlugin()->getConfiguration()['states']));
114     $this->assertSame([
115       'cook',
116       'rot',
117     ], array_keys($workflow->getTypePlugin()->getConfiguration()['transitions']));
118
119     // Ensure that transitions can be deleted.
120     $workflow->getTypePlugin()->deleteTransition('rot');
121     $workflow->save();
122     $this->assertFalse($workflow->getTypePlugin()->hasTransition('rot'));
123   }
124
125 }