View source
<?php
namespace Drupal\Tests\workflows\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\workflows\Entity\Workflow;
use Drupal\workflows\Exception\RequiredStateMissingException;
class RequiredStatesTest extends KernelTestBase {
public static $modules = [
'workflows',
'workflow_type_test',
];
public function testGetRequiredStates() {
$workflow = new Workflow([
'id' => 'test',
'type' => 'workflow_type_required_state_test',
], 'workflow');
$workflow
->save();
$this
->assertEquals([
'fresh',
'rotten',
], $workflow
->getTypePlugin()
->getRequiredStates());
$this
->assertTrue($workflow
->getTypePlugin()
->hasState('rotten'));
$this
->assertTrue($workflow
->getTypePlugin()
->hasState('fresh'));
$this
->assertTrue($workflow
->getTypePlugin()
->hasTransitionFromStateToState('fresh', 'rotten'));
}
public function testDeleteRequiredStateAPI() {
$workflow = new Workflow([
'id' => 'test',
'type' => 'workflow_type_required_state_test',
], 'workflow');
$workflow
->save();
$this
->expectException(RequiredStateMissingException::class);
$this
->expectExceptionMessage("Required State Type Test' requires states with the ID 'fresh' in workflow 'test'");
$workflow
->getTypePlugin()
->deleteState('fresh');
$workflow
->save();
}
public function testNoStatesRequiredStateAPI() {
$workflow = new Workflow([
'id' => 'test',
'type' => 'workflow_type_required_state_test',
'type_settings' => [
'states' => [],
],
], 'workflow');
$this
->expectException(RequiredStateMissingException::class);
$this
->expectExceptionMessage("Required State Type Test' requires states with the ID 'fresh', 'rotten' in workflow 'test'");
$workflow
->save();
}
public function testChangeRequiredStateAPI() {
$workflow = new Workflow([
'id' => 'test',
'type' => 'workflow_type_required_state_test',
], 'workflow');
$workflow
->save();
$this
->assertEquals('Fresh', $workflow
->getTypePlugin()
->getState('fresh')
->label());
$workflow
->getTypePlugin()
->setStateLabel('fresh', 'Fresher');
$workflow
->save();
$this
->assertEquals('Fresher', $workflow
->getTypePlugin()
->getState('fresh')
->label());
$workflow
->getTypePlugin()
->addState('cooked', 'Cooked')
->setTransitionFromStates('rot', [
'fresh',
'cooked',
]);
$workflow
->save();
$this
->assertTrue($workflow
->getTypePlugin()
->hasTransitionFromStateToState('fresh', 'rotten'));
$this
->assertTrue($workflow
->getTypePlugin()
->hasTransitionFromStateToState('cooked', 'rotten'));
$workflow
->getTypePlugin()
->setTransitionFromStates('rot', [
'cooked',
]);
$workflow
->save();
$this
->assertFalse($workflow
->getTypePlugin()
->hasTransitionFromStateToState('fresh', 'rotten'));
$this
->assertTrue($workflow
->getTypePlugin()
->hasTransitionFromStateToState('cooked', 'rotten'));
$workflow
->getTypePlugin()
->addTransition('cook', 'Cook', [
'fresh',
], 'cooked');
$workflow
->save();
$this
->assertSame([
'cooked',
'fresh',
'rotten',
], array_keys($workflow
->getTypePlugin()
->getConfiguration()['states']));
$this
->assertSame([
'cook',
'rot',
], array_keys($workflow
->getTypePlugin()
->getConfiguration()['transitions']));
$workflow
->getTypePlugin()
->deleteTransition('rot');
$workflow
->save();
$this
->assertFalse($workflow
->getTypePlugin()
->hasTransition('rot'));
}
}