function WorkflowTest::testGetStates

Same name in other branches
  1. 8.9.x 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 \Drupal\workflows\Plugin\WorkflowTypeBase

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->assertSame([], $workflow->getTypePlugin()
        ->getStates());
    $this->assertSame([], $workflow->getTypePlugin()
        ->getStates([]));
    $workflow->getTypePlugin()
        ->addState('draft', 'Draft')
        ->addState('published', 'Published')
        ->addState('archived', 'Archived');
    // States are stored in alphabetical key order.
    $this->assertEquals([
        '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->assertEquals([
        'draft',
        'published',
        'archived',
    ], array_keys($workflow->getTypePlugin()
        ->getStates()));
    // The order of states is by weight.
    $workflow->getTypePlugin()
        ->setStateWeight('published', -1);
    $this->assertEquals([
        '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->assertEquals([
        'published',
        'archived',
        'draft',
    ], array_keys($workflow->getTypePlugin()
        ->getStates()));
    // You can limit the states returned by passing in states IDs.
    $this->assertEquals([
        'archived',
        'draft',
    ], array_keys($workflow->getTypePlugin()
        ->getStates([
        'draft',
        'archived',
    ])));
    // An empty array does not load all states.
    $this->assertSame([], $workflow->getTypePlugin()
        ->getStates([]));
}

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