function RequiredStatesTest::testChangeRequiredStateAPI

Same name and namespace in other branches
  1. 9 core/modules/workflows/tests/src/Kernel/RequiredStatesTest.php \Drupal\Tests\workflows\Kernel\RequiredStatesTest::testChangeRequiredStateAPI()
  2. 8.9.x core/modules/workflows/tests/src/Kernel/RequiredStatesTest.php \Drupal\Tests\workflows\Kernel\RequiredStatesTest::testChangeRequiredStateAPI()
  3. 11.x core/modules/workflows/tests/src/Kernel/RequiredStatesTest.php \Drupal\Tests\workflows\Kernel\RequiredStatesTest::testChangeRequiredStateAPI()

Ensures that initialized configuration can be changed.

File

core/modules/workflows/tests/src/Kernel/RequiredStatesTest.php, line 81

Class

RequiredStatesTest
Tests Workflow type's required states and configuration initialization.

Namespace

Drupal\Tests\workflows\Kernel

Code

public function testChangeRequiredStateAPI() : void {
  $workflow = Workflow::create([
    'id' => 'test',
    'label' => 'Test workflow',
    'type' => 'workflow_type_required_state_test',
  ]);
  $workflow->save();
  // Ensure states added by default configuration can be changed.
  $this->assertEquals('Fresh', $workflow->getTypePlugin()
    ->getState('fresh')
    ->label());
  $workflow->getTypePlugin()
    ->setStateLabel('fresh', 'Fresher');
  $workflow->save();
  $this->assertEquals('Fresher', $workflow->getTypePlugin()
    ->getState('fresh')
    ->label());
  // Ensure transitions can be altered.
  $workflow->getTypePlugin()
    ->addState('cooked', 'Cooked')
    ->setTransitionFromStates('rot', [
    'fresh',
    'cooked',
  ]);
  $workflow->save();
  $this->assertTrue($workflow->getTypePlugin()
    ->hasTransitionFromStateToState('fresh', 'rotten'));
  $this->assertTrue($workflow->getTypePlugin()
    ->hasTransitionFromStateToState('cooked', 'rotten'));
  $workflow->getTypePlugin()
    ->setTransitionFromStates('rot', [
    'cooked',
  ]);
  $workflow->save();
  $this->assertFalse($workflow->getTypePlugin()
    ->hasTransitionFromStateToState('fresh', 'rotten'));
  $this->assertTrue($workflow->getTypePlugin()
    ->hasTransitionFromStateToState('cooked', 'rotten'));
  // Ensure the default configuration does not cause ordering issues.
  $workflow->getTypePlugin()
    ->addTransition('cook', 'Cook', [
    'fresh',
  ], 'cooked');
  $workflow->save();
  $this->assertSame([
    'cooked',
    'fresh',
    'rotten',
  ], array_keys($workflow->getTypePlugin()
    ->getConfiguration()['states']));
  $this->assertSame([
    'cook',
    'rot',
  ], array_keys($workflow->getTypePlugin()
    ->getConfiguration()['transitions']));
  // Ensure that transitions can be deleted.
  $workflow->getTypePlugin()
    ->deleteTransition('rot');
  $workflow->save();
  $this->assertFalse($workflow->getTypePlugin()
    ->hasTransition('rot'));
}

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