You are here

public function WorkflowTest::testGetState in Drupal 9

Same name and namespace in other branches
  1. 8 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 156

Class

WorkflowTest
@coversDefaultClass \Drupal\workflows\Plugin\WorkflowTypeBase

Namespace

Drupal\Tests\workflows\Unit

Code

public function testGetState() {
  $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());
}