diff --git a/tests/modules/state_machine_test/src/Guard/FulfillmentGuard.php b/tests/modules/state_machine_test/src/Guard/FulfillmentGuard.php index b5b184639e3807923185075ac875cd57e75f7fb6..9687a8a69a30e25d3495526895dc5199d673d78f 100644 --- a/tests/modules/state_machine_test/src/Guard/FulfillmentGuard.php +++ b/tests/modules/state_machine_test/src/Guard/FulfillmentGuard.php @@ -18,8 +18,8 @@ class FulfillmentGuard implements GuardInterface { * {@inheritdoc} */ public function allowed(WorkflowTransition $transition, WorkflowInterface $workflow, EntityInterface $entity) { - // @todo Add an additional condition here that makes sense for tests. - if ($transition->getId() == 'fulfill') { + // Don't allow entities in fulfillment to be cancelled. + if ($transition->getId() == 'cancel' && $entity->test_state->first()->value == 'fulfillment') { return FALSE; } } diff --git a/tests/modules/state_machine_test/state_machine_test.services.yml b/tests/modules/state_machine_test/state_machine_test.services.yml index 3b7c1e09386ea33d72e53bddf15056fe606fbe51..1aa07b3e6034698144e3ca9d8ee8b0e0729532f9 100644 --- a/tests/modules/state_machine_test/state_machine_test.services.yml +++ b/tests/modules/state_machine_test/state_machine_test.services.yml @@ -1,3 +1,4 @@ +services: state_machine_test.fulfillment_guard: class: Drupal\state_machine_test\Guard\FulfillmentGuard tags: diff --git a/tests/modules/state_machine_test/state_machine_test.workflows.yml b/tests/modules/state_machine_test/state_machine_test.workflows.yml index 3c85e70a7bcdca933c1618ff99a8ec22577e80d0..d097f9e59f59cb054dafadc051e1a5f309c7c95d 100644 --- a/tests/modules/state_machine_test/state_machine_test.workflows.yml +++ b/tests/modules/state_machine_test/state_machine_test.workflows.yml @@ -22,5 +22,5 @@ default: to: completed cancel: label: Cancel - from: [new, validation, fulfillment] + from: [new, fulfillment] to: canceled diff --git a/tests/src/Kernel/StateItemTest.php b/tests/src/Kernel/StateItemTest.php new file mode 100644 index 0000000000000000000000000000000000000000..fbe67d0ce7b9fc1c570d567e4ca3030e6ba4faf2 --- /dev/null +++ b/tests/src/Kernel/StateItemTest.php @@ -0,0 +1,88 @@ +<?php + +/** + * @file + * Contains \Drupal\Tests\state_machine\StateItem\StateItemTest. + */ + +namespace Drupal\Tests\state_machine\StateItem; + +use Drupal\entity_test\Entity\EntityTest; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; +use Drupal\KernelTests\KernelTestBase; +use Drupal\state_machine\Plugin\Workflow\WorkflowTransition; + +/** + * @coversDefaultClass \Drupal\state_machine\Plugin\Field\FieldType\StateItem + * @group state_machine + */ +class StateItemTest extends KernelTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = ['entity_test', 'state_machine', 'field', 'user', 'state_machine_test']; + + protected function setUp() { + parent::setUp(); + + $this->installEntitySchema('entity_test'); + $this->installEntitySchema('user'); + + $field_storage = FieldStorageConfig::create([ + 'field_name' => 'test_state', + 'entity_type' => 'entity_test', + 'type' => 'state', + ]); + $field_storage->save(); + + $field = FieldConfig::create([ + 'field_name' => 'test_state', + 'entity_type' => 'entity_test', + 'bundle' => 'entity_test', + 'settings' => [ + 'workflow' => 'default', + ] + ]); + $field->save(); + } + + /** + * @dataProvider providerTestField + */ + public function testField($initial_state, $allowed_transitions, $invalid_new_state, $valid_transition, $expected_new_state) { + $entity = EntityTest::create(['test_state' => ['value' => $initial_state]]); + // Ensure that the first state of a workflow is chosen automatically. + $this->assertEquals($initial_state, $entity->test_state->value); + $this->assertFalse($entity->test_state->isEmpty()); + + $result = $entity->test_state->first()->getTransitions(); + $this->assertCount(count($allowed_transitions), $result); + $this->assertEquals($allowed_transitions, array_keys($result)); + + if ($invalid_new_state) { + $entity->test_state->value = $invalid_new_state; + $this->assertFalse($entity->test_state->first()->isValid()); + } + + /** @var \Drupal\state_machine\WorkflowManagerInterface $workflow_manager */ + $workflow_manager = \Drupal::service('plugin.manager.workflow'); + /** @var \Drupal\state_machine\Plugin\Workflow\Workflow $workflow */ + $workflow = $workflow_manager->createInstance('default'); + $transition = $workflow->getTransition($valid_transition); + $entity->test_state->first()->applyTransition($transition); + $this->assertEquals($expected_new_state, $entity->test_state->value); + } + + public function providerTestField() { + $data = []; + $data['new->fulfillment'] = ['new', ['create', 'cancel'], 'completed', 'create', 'fulfillment']; + $data['new->canceled'] = ['new', ['create', 'cancel'], 'completed', 'cancel', 'canceled']; + $data['fulfillment->completed'] = ['fulfillment', ['fulfill'], 'new', 'fulfill', 'completed']; + // A transition to canceled is forbidden by the FulfillmentGuard. + + return $data; + } + +} diff --git a/tests/src/Unit/WorkflowGroupManagerTest.php b/tests/src/Unit/WorkflowGroupManagerTest.php index 8d62aa518c13ceada984a3aceb6d0443bda5a55d..7fe9dff33babf1d4d71e9c1f2d8f12af38616902 100644 --- a/tests/src/Unit/WorkflowGroupManagerTest.php +++ b/tests/src/Unit/WorkflowGroupManagerTest.php @@ -100,15 +100,15 @@ class WorkflowGroupManagerTest extends UnitTestCase { * Workflow group configuration that will be translated into YAML. * * @covers ::processDefinition - * @dataProvider invalidConfigWorkflowGroups + * @dataProvider configWorkflowGroups */ public function testProcessIncompleteDefinitions($group_config) { vfsStream::setup('root'); $file = Yaml::encode($group_config); vfsStream::create([ - 'state_machine_test' => [ - 'state_machine_test.workflow_groups.yml' => $file, - ]] + 'state_machine_test' => [ + 'state_machine_test.workflow_groups.yml' => $file, + ]] ); $discovery = new YamlDiscovery('workflow_groups', ['state_machine_test' => vfsStream::url('root/state_machine_test')]);