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 ::initializeWorkflow
26 * @covers ::__construct
28 public function testGetRequiredStates() {
29 $workflow = new Workflow([
31 'type' => 'workflow_type_required_state_test',
33 $workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
35 $this->assertEquals(['fresh', 'rotten'], $workflow->getTypePlugin()
36 ->getRequiredStates());
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'));
45 * @covers \Drupal\workflows\Entity\Workflow::preSave
47 public function testDeleteRequiredStateAPI() {
48 $workflow = new Workflow([
50 'type' => 'workflow_type_required_state_test',
52 $workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
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();
60 * @covers \Drupal\workflows\Entity\Workflow::preSave
62 public function testNoStatesRequiredStateAPI() {
63 $workflow = new Workflow([
65 'type' => 'workflow_type_required_state_test',
67 $this->setExpectedException(RequiredStateMissingException::class, "Required State Type Test' requires states with the ID 'fresh', 'rotten' in workflow 'test'");
72 * Ensures that initialized configuration can be changed.
74 public function testChangeRequiredStateAPI() {
75 $workflow = new Workflow([
77 'type' => 'workflow_type_required_state_test',
79 $workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
82 // Ensure states added by default configuration can be changed.
83 $this->assertEquals('Fresh', $workflow->getState('fresh')->label());
85 ->setStateLabel('fresh', 'Fresher')
87 $this->assertEquals('Fresher', $workflow->getState('fresh')->label());
89 // Ensure transitions can be altered.
91 ->addState('cooked', 'Cooked')
92 ->setTransitionFromStates('rot', ['fresh', 'cooked'])
94 $this->assertTrue($workflow->hasTransitionFromStateToState('fresh', 'rotten'));
95 $this->assertTrue($workflow->hasTransitionFromStateToState('cooked', 'rotten'));
98 ->setTransitionFromStates('rot', ['cooked'])
100 $this->assertFalse($workflow->hasTransitionFromStateToState('fresh', 'rotten'));
101 $this->assertTrue($workflow->hasTransitionFromStateToState('cooked', 'rotten'));
103 // Ensure the default configuration does not cause ordering issues.
104 $workflow->addTransition('cook', 'Cook', ['fresh'], 'cooked')->save();
109 ], array_keys($workflow->get('states')));
113 ], array_keys($workflow->get('transitions')));
115 // Ensure that transitions can be deleted.
116 $workflow->deleteTransition('rot')->save();
117 $this->assertFalse($workflow->hasTransition('rot'));