3 namespace Drupal\Tests\workflows\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\workflows\Entity\Workflow;
7 use Drupal\workflows\Exception\RequiredStateMissingException;
10 * Tests Workflow type's required states and configuration initialization.
12 * @coversDefaultClass \Drupal\workflows\Plugin\WorkflowTypeBase
16 class RequiredStatesTest extends KernelTestBase {
21 public static $modules = ['workflows', 'workflow_type_test'];
24 * @covers ::getRequiredStates
25 * @covers ::__construct
27 public function testGetRequiredStates() {
28 $workflow = new Workflow([
30 'type' => 'workflow_type_required_state_test',
33 $this->assertEquals(['fresh', 'rotten'], $workflow->getTypePlugin()
34 ->getRequiredStates());
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'));
43 * @covers \Drupal\workflows\Entity\Workflow::preSave
45 public function testDeleteRequiredStateAPI() {
46 $workflow = new Workflow([
48 'type' => 'workflow_type_required_state_test',
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');
58 * @covers \Drupal\workflows\Entity\Workflow::preSave
60 public function testNoStatesRequiredStateAPI() {
61 $workflow = new Workflow([
63 'type' => 'workflow_type_required_state_test',
68 $this->setExpectedException(RequiredStateMissingException::class, "Required State Type Test' requires states with the ID 'fresh', 'rotten' in workflow 'test'");
73 * Ensures that initialized configuration can be changed.
75 public function testChangeRequiredStateAPI() {
76 $workflow = new Workflow([
78 'type' => 'workflow_type_required_state_test',
82 // Ensure states added by default configuration can be changed.
83 $this->assertEquals('Fresh', $workflow->getTypePlugin()->getState('fresh')->label());
86 ->setStateLabel('fresh', 'Fresher');
88 $this->assertEquals('Fresher', $workflow->getTypePlugin()->getState('fresh')->label());
90 // Ensure transitions can be altered.
93 ->addState('cooked', 'Cooked')
94 ->setTransitionFromStates('rot', ['fresh', 'cooked']);
96 $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('fresh', 'rotten'));
97 $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('cooked', 'rotten'));
101 ->setTransitionFromStates('rot', ['cooked']);
103 $this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('fresh', 'rotten'));
104 $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('cooked', 'rotten'));
106 // Ensure the default configuration does not cause ordering issues.
107 $workflow->getTypePlugin()->addTransition('cook', 'Cook', ['fresh'], 'cooked');
113 ], array_keys($workflow->getTypePlugin()->getConfiguration()['states']));
117 ], array_keys($workflow->getTypePlugin()->getConfiguration()['transitions']));
119 // Ensure that transitions can be deleted.
120 $workflow->getTypePlugin()->deleteTransition('rot');
122 $this->assertFalse($workflow->getTypePlugin()->hasTransition('rot'));