function WorkflowTest::testGetState

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

@covers ::getState

File

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

Class

WorkflowTest
@coversDefaultClass \Drupal\workflows\Plugin\WorkflowTypeBase

Namespace

Drupal\Tests\workflows\Unit

Code

public function testGetState() : void {
    $workflow = new Workflow([
        'id' => 'test',
        'type' => 'test_type',
    ], 'workflow');
    // By default states are ordered in the order added.
    $workflow->getTypePlugin()
        ->addState('draft', 'Draft')
        ->addState('published', 'Published')
        ->addState('archived', 'Archived')
        ->addTransition('create_new_draft', 'Create new draft', [
        'draft',
    ], 'draft')
        ->addTransition('publish', 'Publish', [
        'draft',
    ], 'published');
    // Ensure we're returning state objects and they are set up correctly
    $this->assertInstanceOf(State::class, $workflow->getTypePlugin()
        ->getState('draft'));
    $this->assertEquals('archived', $workflow->getTypePlugin()
        ->getState('archived')
        ->id());
    $this->assertEquals('Archived', $workflow->getTypePlugin()
        ->getState('archived')
        ->label());
    $draft = $workflow->getTypePlugin()
        ->getState('draft');
    $this->assertTrue($draft->canTransitionTo('draft'));
    $this->assertTrue($draft->canTransitionTo('published'));
    $this->assertFalse($draft->canTransitionTo('archived'));
    $this->assertEquals('Publish', $draft->getTransitionTo('published')
        ->label());
    $this->assertEquals(0, $draft->weight());
    $this->assertEquals(1, $workflow->getTypePlugin()
        ->getState('published')
        ->weight());
    $this->assertEquals(2, $workflow->getTypePlugin()
        ->getState('archived')
        ->weight());
}

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