StateItemTest.php 2.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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;
  }

}