You are here

public function WorkflowTest::testGetAllowedTransitions in State Machine 8

@covers ::getAllowedTransitions

File

tests/src/Unit/Plugin/Workflow/WorkflowTest.php, line 148

Class

WorkflowTest
@coversDefaultClass \Drupal\state_machine\Plugin\Workflow\Workflow @group state_machine

Namespace

Drupal\Tests\state_machine\Unit\Plugin\Workflow

Code

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));
}