You are here

public function WorkflowUiTest::testSorting in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workflows/tests/src/Functional/WorkflowUiTest.php \Drupal\Tests\workflows\Functional\WorkflowUiTest::testSorting()

Tests the sorting of states and transitions by weight and label.

File

core/modules/workflows/tests/src/Functional/WorkflowUiTest.php, line 342

Class

WorkflowUiTest
Tests workflow creation UI.

Namespace

Drupal\Tests\workflows\Functional

Code

public function testSorting() {
  $workflow = Workflow::create([
    'id' => 'test',
    'type' => 'workflow_type_complex_test',
    'label' => 'Test',
  ]);
  $workflow
    ->getTypePlugin()
    ->setConfiguration([
    'states' => [
      'two_a' => [
        'label' => 'two a',
        'weight' => 2,
      ],
      'three' => [
        'label' => 'three',
        'weight' => 3,
      ],
      'two_b' => [
        'label' => 'two b',
        'weight' => 2,
      ],
      'one' => [
        'label' => 'one',
        'weight' => 1,
      ],
    ],
    'transitions' => [
      'three' => [
        'label' => 'three',
        'from' => [
          'three',
        ],
        'to' => 'three',
        'weight' => 3,
      ],
      'two_a' => [
        'label' => 'two a',
        'from' => [
          'two_a',
        ],
        'to' => 'two_a',
        'weight' => 2,
      ],
      'one' => [
        'label' => 'one',
        'from' => [
          'one',
        ],
        'to' => 'one',
        'weight' => 1,
      ],
      'two_b' => [
        'label' => 'two b',
        'from' => [
          'two_b',
        ],
        'to' => 'two_b',
        'weight' => 2,
      ],
    ],
  ]);
  $workflow
    ->save();
  $this
    ->drupalLogin($this
    ->createUser([
    'administer workflows',
  ]));
  $this
    ->drupalGet('admin/config/workflow/workflows/manage/test');
  $expected_states = [
    'one',
    'two a',
    'two b',
    'three',
  ];
  $elements = $this
    ->xpath('//details[@id="edit-states-container"]//table/tbody/tr');
  foreach ($elements as $key => $element) {
    $this
      ->assertEquals($expected_states[$key], $element
      ->find('xpath', 'td')
      ->getText());
  }
  $expected_transitions = [
    'one',
    'two a',
    'two b',
    'three',
  ];
  $elements = $this
    ->xpath('//details[@id="edit-transitions-container"]//table/tbody/tr');
  foreach ($elements as $key => $element) {
    $this
      ->assertEquals($expected_transitions[$key], $element
      ->find('xpath', 'td')
      ->getText());
  }

  // Ensure that there are enough weights to satisfy the potential number of
  // states and transitions.
  $this
    ->assertSession()
    ->selectExists('states[three][weight]')
    ->selectOption('2');
  $this
    ->assertSession()
    ->selectExists('states[three][weight]')
    ->selectOption('-2');
  $this
    ->assertSession()
    ->selectExists('transitions[three][weight]')
    ->selectOption('2');
  $this
    ->assertSession()
    ->selectExists('transitions[three][weight]')
    ->selectOption('-2');
}