function WorkflowTest::testGetTransitions

Same name and namespace in other branches
  1. 9 core/modules/workflows/tests/src/Unit/WorkflowTest.php \Drupal\Tests\workflows\Unit\WorkflowTest::testGetTransitions()
  2. 10 core/modules/workflows/tests/src/Unit/WorkflowTest.php \Drupal\Tests\workflows\Unit\WorkflowTest::testGetTransitions()
  3. 11.x 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 <a href="/api/drupal/core%21modules%21workflows%21src%21Plugin%21WorkflowTypeBase.php/class/WorkflowTypeBase/8.9.x" title="A base class for Workflow type plugins." class="local">\Drupal\workflows\Plugin\WorkflowTypeBase</a>

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([])));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.