class WorkflowTest in State Machine 8
@coversDefaultClass \Drupal\state_machine\Plugin\Workflow\Workflow @group state_machine
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\state_machine\Unit\Plugin\Workflow\WorkflowTest
Expanded class hierarchy of WorkflowTest
File
- tests/
src/ Unit/ Plugin/ Workflow/ WorkflowTest.php, line 16
Namespace
Drupal\Tests\state_machine\Unit\Plugin\WorkflowView source
class WorkflowTest extends UnitTestCase {
/**
* @covers ::getId
*/
public function testGetId() {
$guard_factory = $this
->prophesize(GuardFactoryInterface::class);
$plugin_definition = [
'id' => 'test id',
'label' => 'test label',
'states' => [],
'transitions' => [],
];
$workflow = new Workflow([], 'test', $plugin_definition, $guard_factory
->reveal());
$this
->assertEquals('test id', $workflow
->getId());
}
/**
* @covers ::getLabel
*/
public function testGetLabel() {
$guard_factory = $this
->prophesize(GuardFactoryInterface::class);
$plugin_definition = [
'states' => [],
'transitions' => [],
'label' => 'test label',
];
$workflow = new Workflow([], 'test', $plugin_definition, $guard_factory
->reveal());
$this
->assertEquals('test label', $workflow
->getLabel());
}
/**
* @covers ::getStates
* @covers ::getState
*/
public function testGetStates() {
$guard_factory = $this
->prophesize(GuardFactoryInterface::class);
$plugin_definition = [
'states' => [
'draft' => [
'label' => 'Draft',
],
],
'transitions' => [],
];
$workflow = new Workflow([], 'test', $plugin_definition, $guard_factory
->reveal());
$state = $workflow
->getState('draft');
$this
->assertEquals('draft', $state
->getId());
$this
->assertEquals('Draft', $state
->getLabel());
$this
->assertEquals([
'draft' => $state,
], $workflow
->getStates());
}
/**
* @covers ::getTransitions
* @covers ::getTransition
*/
public function testGetTransitions() {
$guard_factory = $this
->prophesize(GuardFactoryInterface::class);
$plugin_definition = [
'states' => [
'draft' => [
'label' => 'Draft',
],
'published' => [
'label' => 'Published',
],
],
'transitions' => [
'publish' => [
'label' => 'Publish',
'from' => [
'draft',
],
'to' => 'published',
],
],
];
$workflow = new Workflow([], 'test', $plugin_definition, $guard_factory
->reveal());
$transition = $workflow
->getTransition('publish');
$this
->assertEquals('publish', $transition
->getId());
$this
->assertEquals('Publish', $transition
->getLabel());
$this
->assertEquals([
'draft' => $workflow
->getState('draft'),
], $transition
->getFromStates());
$this
->assertEquals($workflow
->getState('published'), $transition
->getToState());
$this
->assertEquals([
'publish' => $transition,
], $workflow
->getTransitions());
}
/**
* @covers ::getPossibleTransitions
*/
public function testGetPossibleTransitions() {
$guard_factory = $this
->prophesize(GuardFactoryInterface::class);
$plugin_definition = [
'states' => [
'draft' => [
'label' => 'Draft',
],
'review' => [
'label' => 'Review',
],
'published' => [
'label' => 'Published',
],
],
'transitions' => [
'send_to_review' => [
'label' => 'Send to review',
'from' => [
'draft',
],
'to' => 'review',
],
'publish' => [
'label' => 'Publish',
'from' => [
'review',
],
'to' => 'published',
],
],
];
$workflow = new Workflow([], 'test', $plugin_definition, $guard_factory
->reveal());
$transition = $workflow
->getTransition('send_to_review');
$this
->assertEquals([
'send_to_review' => $transition,
], $workflow
->getPossibleTransitions('draft'));
$transition = $workflow
->getTransition('publish');
$this
->assertEquals([
'publish' => $transition,
], $workflow
->getPossibleTransitions('review'));
$this
->assertEquals([], $workflow
->getPossibleTransitions('published'));
// Passing an empty state should return all transitions.
$this
->assertEquals($workflow
->getTransitions(), $workflow
->getPossibleTransitions(''));
}
/**
* @covers ::getAllowedTransitions
*/
public function testGetAllowedTransitions() {
$plugin_definition = [
'states' => [
'draft' => [
'label' => 'Draft',
],
'review' => [
'label' => 'Review',
],
'published' => [
'label' => 'Published',
],
],
'transitions' => [
'send_to_review' => [
'label' => 'Send to review',
'from' => [
'draft',
],
'to' => 'review',
],
'publish' => [
'label' => 'Publish',
'from' => [
'review',
],
'to' => 'published',
],
],
'group' => 'default',
];
$guard_allow = $this
->prophesize(GuardInterface::class);
$guard_allow
->allowed(Argument::cetera())
->willReturn(TRUE);
$guard_deny_publish = $this
->prophesize(GuardInterface::class);
$guard_deny_publish
->allowed(Argument::cetera())
->will(function ($args) {
// Allow only the send_to_review transition.
return $args[0]
->getId() == 'send_to_review';
});
$guard_factory = $this
->prophesize(GuardFactoryInterface::class);
$guard_factory
->get('default')
->willReturn([
$guard_allow
->reveal(),
$guard_deny_publish
->reveal(),
]);
$workflow = new Workflow([], 'test', $plugin_definition, $guard_factory
->reveal());
$entity = $this
->prophesize(EntityInterface::class)
->reveal();
$transition = $workflow
->getTransition('send_to_review');
$this
->assertEquals([
'send_to_review' => $transition,
], $workflow
->getAllowedTransitions('draft', $entity));
$this
->assertEquals([], $workflow
->getAllowedTransitions('review', $entity));
}
/**
* @covers ::findTransition
*/
public function testFindTransition() {
$guard_factory = $this
->prophesize(GuardFactoryInterface::class);
$plugin_definition = [
'states' => [
'draft' => [
'label' => 'Draft',
],
'review' => [
'label' => 'Review',
],
'published' => [
'label' => 'Published',
],
],
'transitions' => [
'send_to_review' => [
'label' => 'Send to review',
'from' => [
'draft',
],
'to' => 'review',
],
'publish' => [
'label' => 'Publish',
'from' => [
'review',
],
'to' => 'published',
],
],
];
$workflow = new Workflow([], 'test', $plugin_definition, $guard_factory
->reveal());
$transition = $workflow
->getTransition('send_to_review');
$this
->assertEquals($transition, $workflow
->findTransition('draft', 'review'));
$this
->assertNull($workflow
->findTransition('foo', 'bar'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 | |
WorkflowTest:: |
public | function | @covers ::findTransition | |
WorkflowTest:: |
public | function | @covers ::getAllowedTransitions | |
WorkflowTest:: |
public | function | @covers ::getId | |
WorkflowTest:: |
public | function | @covers ::getLabel | |
WorkflowTest:: |
public | function | @covers ::getPossibleTransitions | |
WorkflowTest:: |
public | function | @covers ::getStates @covers ::getState | |
WorkflowTest:: |
public | function | @covers ::getTransitions @covers ::getTransition |