function WorkflowTest::testAddAndHasState

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

@covers ::addState @covers ::hasState

File

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

Class

WorkflowTest
@coversDefaultClass \Drupal\workflows\Plugin\WorkflowTypeBase

Namespace

Drupal\Tests\workflows\Unit

Code

public function testAddAndHasState() {
    $workflow = new Workflow([
        'id' => 'test',
        'type' => 'test_type',
    ], 'workflow');
    $this->assertFalse($workflow->getTypePlugin()
        ->hasState('draft'));
    // By default states are ordered in the order added.
    $workflow->getTypePlugin()
        ->addState('draft', 'Draft');
    $this->assertTrue($workflow->getTypePlugin()
        ->hasState('draft'));
    $this->assertFalse($workflow->getTypePlugin()
        ->hasState('published'));
    $this->assertEquals(0, $workflow->getTypePlugin()
        ->getState('draft')
        ->weight());
    // Adding a state does not set up a transition to itself.
    $this->assertFalse($workflow->getTypePlugin()
        ->hasTransitionFromStateToState('draft', 'draft'));
    // New states are added with a new weight 1 more than the current highest
    // weight.
    $workflow->getTypePlugin()
        ->addState('published', 'Published');
    $this->assertEquals(1, $workflow->getTypePlugin()
        ->getState('published')
        ->weight());
}

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