You are here

public function WorkflowTest::testGetStates in Drupal 8

Same name and namespace in other branches
  1. 9 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
    ->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([])));
}