You are here

public function WorkflowTest::testGetTransitions in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/workflows/tests/src/Unit/WorkflowTest.php \Drupal\Tests\workflows\Unit\WorkflowTest::testGetTransitions()

@covers ::getTransitions @covers ::setTransitionWeight

File

core/modules/workflows/tests/src/Unit/WorkflowTest.php, line 393

Class

WorkflowTest
@coversDefaultClass \Drupal\workflows\Plugin\WorkflowTypeBase

Namespace

Drupal\Tests\workflows\Unit

Code

public function testGetTransitions() {
  $workflow = new Workflow([
    'id' => 'test',
    'type' => 'test_type',
  ], 'workflow');

  // Getting transitions works when there are none.
  $this
    ->assertArrayEquals([], array_keys($workflow
    ->getTypePlugin()
    ->getTransitions()));
  $this
    ->assertArrayEquals([], array_keys($workflow
    ->getTypePlugin()
    ->getTransitions([])));

  // By default states are ordered in the order added.
  $workflow
    ->getTypePlugin()
    ->addState('a', 'A')
    ->addState('b', 'B')
    ->addTransition('a_b', 'A to B', [
    'a',
  ], 'b')
    ->addTransition('a_a', 'A to A', [
    'a',
  ], 'a');

  // Transitions are stored in alphabetical key order in configuration.
  $this
    ->assertArrayEquals([
    'a_a',
    'a_b',
  ], array_keys($workflow
    ->getTypePlugin()
    ->getConfiguration()['transitions']));

  // Ensure we're returning transition objects.
  $this
    ->assertInstanceOf(Transition::class, $workflow
    ->getTypePlugin()
    ->getTransitions()['a_a']);

  // Passing in no IDs returns all transitions.
  $this
    ->assertArrayEquals([
    'a_b',
    'a_a',
  ], array_keys($workflow
    ->getTypePlugin()
    ->getTransitions()));

  // The order of states is by weight.
  $workflow
    ->getTypePlugin()
    ->setTransitionWeight('a_a', -1);
  $this
    ->assertArrayEquals([
    'a_a',
    'a_b',
  ], array_keys($workflow
    ->getTypePlugin()
    ->getTransitions()));

  // If all weights are equal it will fallback to labels.
  $workflow
    ->getTypePlugin()
    ->setTransitionWeight('a_a', 0);
  $this
    ->assertArrayEquals([
    'a_a',
    'a_b',
  ], array_keys($workflow
    ->getTypePlugin()
    ->getTransitions()));
  $workflow
    ->getTypePlugin()
    ->setTransitionLabel('a_b', 'A B');
  $this
    ->assertArrayEquals([
    'a_b',
    'a_a',
  ], array_keys($workflow
    ->getTypePlugin()
    ->getTransitions()));

  // You can limit the states returned by passing in states IDs.
  $this
    ->assertArrayEquals([
    'a_a',
  ], array_keys($workflow
    ->getTypePlugin()
    ->getTransitions([
    'a_a',
  ])));

  // An empty array does not load all states.
  $this
    ->assertArrayEquals([], array_keys($workflow
    ->getTypePlugin()
    ->getTransitions([])));
}