View source
<?php
namespace Drupal\Tests\state_machine\Kernel;
use Drupal\entity_test\Entity\EntityTestWithBundle;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
class StateItemTest extends FieldKernelTestBase {
public static $modules = [
'entity_test',
'field',
'user',
'state_machine',
'state_machine_test',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('entity_test_with_bundle');
$this
->installConfig([
'state_machine_test',
]);
}
public function testInvalidTransitionApply() {
$entity = EntityTestWithBundle::create([
'type' => 'first',
]);
$state_item = $entity
->get('field_state')
->first();
$state_item
->applyTransitionById('INVALID');
}
public function testField($initial_state, $allowed_transitions, $invalid_new_state, $valid_transition, $expected_new_state) {
$entity = EntityTestWithBundle::create([
'type' => 'second',
'field_state' => $initial_state,
]);
$this
->assertEquals($initial_state, $entity
->get('field_state')->value);
$state_item = $entity
->get('field_state')
->first();
$transitions = $state_item
->getTransitions();
$this
->assertCount(count($allowed_transitions), $transitions);
$this
->assertEquals($allowed_transitions, array_keys($transitions));
if (count($allowed_transitions) > 0) {
foreach ($allowed_transitions as $transition_id) {
$this
->assertTrue($state_item
->isTransitionAllowed($transition_id));
}
}
$this
->assertFalse($state_item
->isTransitionAllowed('foo'));
if ($invalid_new_state) {
$state_item->value = $invalid_new_state;
$this
->assertEquals($initial_state, $state_item
->getOriginalId());
$this
->assertEquals($invalid_new_state, $state_item
->getId());
$this
->assertFalse($state_item
->isValid());
}
$state_item->value = $initial_state;
$workflow = $state_item
->getWorkflow();
$all_transitions = $workflow
->getTransitions();
$invalid_transitions = array_diff_key($all_transitions, $allowed_transitions);
if ($invalid_transitions) {
$random_key = array_rand($invalid_transitions);
$this
->expectException(\InvalidArgumentException::class);
$state_item
->applyTransition($invalid_transitions[$random_key]);
$this
->expectException(\InvalidArgumentException::class);
$state_item
->applyTransitionById($random_key);
}
$state_item
->applyTransitionById($valid_transition);
$this
->assertEquals($initial_state, $state_item
->getOriginalId());
$this
->assertEquals($expected_new_state, $state_item
->getId());
$this
->assertTrue($state_item
->isValid());
}
public function providerTestField() {
$data = [];
$data['new->validation'] = [
'new',
[
'create',
'cancel',
],
'fulfillment',
'create',
'validation',
];
$data['new->canceled'] = [
'new',
[
'create',
'cancel',
],
'completed',
'cancel',
'canceled',
];
$data['validation->fulfillment'] = [
'validation',
[
'validate',
],
'completed',
'validate',
'fulfillment',
];
$data['fulfillment->completed'] = [
'fulfillment',
[
'fulfill',
],
'new',
'fulfill',
'completed',
];
return $data;
}
public function testSettableOptions($initial_state, $available_options) {
$entity = EntityTestWithBundle::create([
'type' => 'second',
'field_state' => $initial_state,
]);
$this
->assertEquals($initial_state, $entity
->get('field_state')->value);
$this
->assertEquals($available_options, $entity
->get('field_state')
->first()
->getSettableOptions());
}
public function providerSettableOptions() {
$data = [];
$data['new'] = [
'new',
[
'canceled' => 'Canceled',
'validation' => 'Validation',
'new' => 'New',
],
];
$data['invalid'] = [
'invalid',
[],
];
return $data;
}
public function testGenerateSampleValue() {
$entity = EntityTestWithBundle::create([
'type' => 'first',
]);
$entity->field_state
->generateSampleItems();
$state_item = $entity
->get('field_state')
->first();
$this
->assertEquals('default', $state_item
->getWorkflow()
->getId());
$this
->assertNotEmpty($state_item
->getId());
$this
->assertTrue(in_array($state_item
->getId(), array_keys($state_item
->getWorkflow()
->getStates())));
$this
->entityValidateAndSave($entity);
$entity = EntityTestWithBundle::create([
'type' => 'second',
]);
$entity->field_state
->generateSampleItems();
$state_item = $entity
->get('field_state')
->first();
$this
->assertNotEmpty($state_item
->getId());
$this
->assertTrue(in_array($state_item
->getId(), array_keys($state_item
->getWorkflow()
->getStates())));
$this
->entityValidateAndSave($entity);
$entity = EntityTestWithBundle::create([
'type' => 'third',
]);
$entity->field_state
->generateSampleItems();
$state_item = $entity
->get('field_state')
->first();
$this
->assertEquals('two_transitions', $state_item
->getWorkflow()
->getId());
$this
->assertNotEmpty($state_item
->getId());
$this
->assertTrue(in_array($state_item
->getId(), array_keys($state_item
->getWorkflow()
->getStates())));
$this
->entityValidateAndSave($entity);
}
}