function WorkflowTest::testGetStates

Same name and namespace in other branches
  1. 9 core/modules/workflows/tests/src/Unit/WorkflowTest.php \Drupal\Tests\workflows\Unit\WorkflowTest::testGetStates()
  2. 10 core/modules/workflows/tests/src/Unit/WorkflowTest.php \Drupal\Tests\workflows\Unit\WorkflowTest::testGetStates()
  3. 11.x core/modules/workflows/tests/src/Unit/WorkflowTest.php \Drupal\Tests\workflows\Unit\WorkflowTest::testGetStates()

@covers ::getStates

File

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

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 testGetStates() {
    $workflow = new Workflow([
        'id' => 'test',
        'type' => 'test_type',
    ], 'workflow');
    // Getting states works when there are none.
    $this->assertArrayEquals([], array_keys($workflow->getTypePlugin()
        ->getStates()));
    $this->assertArrayEquals([], array_keys($workflow->getTypePlugin()
        ->getStates([])));
    $workflow->getTypePlugin()
        ->addState('draft', 'Draft')
        ->addState('published', 'Published')
        ->addState('archived', 'Archived');
    // States are stored in alphabetical key order.
    $this->assertArrayEquals([
        'archived',
        'draft',
        'published',
    ], array_keys($workflow->getTypePlugin()
        ->getConfiguration()['states']));
    // Ensure we're returning state objects.
    $this->assertInstanceOf(State::class, $workflow->getTypePlugin()
        ->getStates()['draft']);
    // Passing in no IDs returns all states.
    $this->assertArrayEquals([
        'draft',
        'published',
        'archived',
    ], array_keys($workflow->getTypePlugin()
        ->getStates()));
    // The order of states is by weight.
    $workflow->getTypePlugin()
        ->setStateWeight('published', -1);
    $this->assertArrayEquals([
        'published',
        'draft',
        'archived',
    ], array_keys($workflow->getTypePlugin()
        ->getStates()));
    // The label is also used for sorting if weights are equal.
    $workflow->getTypePlugin()
        ->setStateWeight('archived', 0);
    $this->assertArrayEquals([
        'published',
        'archived',
        'draft',
    ], array_keys($workflow->getTypePlugin()
        ->getStates()));
    // You can limit the states returned by passing in states IDs.
    $this->assertArrayEquals([
        'archived',
        'draft',
    ], array_keys($workflow->getTypePlugin()
        ->getStates([
        'draft',
        'archived',
    ])));
    // An empty array does not load all states.
    $this->assertArrayEquals([], array_keys($workflow->getTypePlugin()
        ->getStates([])));
}

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