Commit 4fce22fb authored by dawehner's avatar dawehner Committed by bojanz

More test work; Initial StateItem tests.

parent 5015933c
......@@ -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;
}
}
......
services:
state_machine_test.fulfillment_guard:
class: Drupal\state_machine_test\Guard\FulfillmentGuard
tags:
......
......@@ -22,5 +22,5 @@ default:
to: completed
cancel:
label: Cancel
from: [new, validation, fulfillment]
from: [new, fulfillment]
to: canceled
<?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;
}
}
......@@ -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')]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment