WorkflowUiTest.php 19.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php

namespace Drupal\Tests\workflows\Functional;

use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\workflows\Entity\Workflow;

/**
 * Tests workflow creation UI.
 *
 * @group workflows
 */
class WorkflowUiTest extends BrowserTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
21
  public static $modules = ['workflows', 'workflow_type_test', 'block'];
22

23 24 25 26 27
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    // We're testing local actions.
    $this->drupalPlaceBlock('local_actions_block');
  }

  /**
   * Tests route access/permissions.
   */
  public function testAccess() {
    // Create a minimal workflow for testing.
    $workflow = Workflow::create(['id' => 'test', 'type' => 'workflow_type_test']);
    $workflow
44
      ->getTypePlugin()
45 46
      ->addState('draft', 'Draft')
      ->addState('published', 'Published')
47 48
      ->addTransition('publish', 'Publish', ['draft', 'published'], 'published');
    $workflow->save();
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    $paths = [
      'admin/config/workflow/workflows',
      'admin/config/workflow/workflows/add',
      'admin/config/workflow/workflows/manage/test',
      'admin/config/workflow/workflows/manage/test/delete',
      'admin/config/workflow/workflows/manage/test/add_state',
      'admin/config/workflow/workflows/manage/test/state/published',
      'admin/config/workflow/workflows/manage/test/state/published/delete',
      'admin/config/workflow/workflows/manage/test/add_transition',
      'admin/config/workflow/workflows/manage/test/transition/publish',
      'admin/config/workflow/workflows/manage/test/transition/publish/delete',
    ];

    foreach ($paths as $path) {
      $this->drupalGet($path);
      // No access.
      $this->assertSession()->statusCodeEquals(403);
    }
    $this->drupalLogin($this->createUser(['administer workflows']));
    foreach ($paths as $path) {
      $this->drupalGet($path);
      // User has access.
      $this->assertSession()->statusCodeEquals(200);
    }

75 76 77 78 79 80
    // Ensure that default states can not be deleted.
    \Drupal::state()->set('workflow_type_test.required_states', ['published']);
    $this->drupalGet('admin/config/workflow/workflows/manage/test/state/published/delete');
    $this->assertSession()->statusCodeEquals(403);
    \Drupal::state()->set('workflow_type_test.required_states', []);

81 82 83
    // Delete one of the states and ensure the other test cannot be deleted.
    $this->drupalGet('admin/config/workflow/workflows/manage/test/state/published/delete');
    $this->submitForm([], 'Delete');
84
    $this->drupalGet('admin/config/workflow/workflows/manage/test/state/draft/delete');
85 86 87
    $this->assertSession()->statusCodeEquals(403);
  }

88 89 90 91 92 93 94 95 96 97
  /**
   * Test the machine name validation of the state add form.
   */
  public function testStateMachineNameValidation() {
    Workflow::create([
      'id' => 'test_workflow',
      'type' => 'workflow_type_test',
    ])->save();

    $this->drupalLogin($this->createUser(['administer workflows']));
98

99 100 101 102 103 104
    $this->drupalPostForm('admin/config/workflow/workflows/manage/test_workflow/add_state', [
      'label' => 'Test State',
      'id' => 'Invalid ID',
    ], 'Save');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('The machine-readable name must contain only lowercase letters, numbers, and underscores.');
105 106 107 108 109 110

    $this->drupalPostForm('admin/config/workflow/workflows/manage/test_workflow/add_transition', [
      'label' => 'Test Transition',
      'id' => 'Invalid ID',
    ], 'Save');
    $this->assertSession()->pageTextContains('The machine-readable name must contain only lowercase letters, numbers, and underscores.');
111 112
  }

113 114 115 116 117 118 119 120 121 122
  /**
   * Tests the creation of a workflow through the UI.
   */
  public function testWorkflowCreation() {
    $workflow_storage = $this->container->get('entity_type.manager')->getStorage('workflow');
    /** @var \Drupal\workflows\WorkflowInterface $workflow */
    $this->drupalLogin($this->createUser(['access administration pages', 'administer workflows']));
    $this->drupalGet('admin/config/workflow');
    $this->assertSession()->linkByHrefExists('admin/config/workflow/workflows');
    $this->clickLink('Workflows');
123
    $this->assertSession()->pageTextContains('Workflows');
124
    $this->assertSession()->pageTextContains('There are no workflows yet.');
125 126 127 128 129 130 131 132 133 134 135
    $this->clickLink('Add workflow');
    $this->submitForm(['label' => 'Test', 'id' => 'test', 'workflow_type' => 'workflow_type_test'], 'Save');
    $this->assertSession()->pageTextContains('Created the Test Workflow.');
    $this->assertSession()->addressEquals('admin/config/workflow/workflows/manage/test/add_state');
    $this->drupalGet('/admin/config/workflow/workflows/manage/test');
    $this->assertSession()->pageTextContains('This workflow has no states and will be disabled until there is at least one, add a new state.');
    $this->assertSession()->pageTextContains('There are no states yet.');
    $this->clickLink('Add a new state');
    $this->submitForm(['label' => 'Published', 'id' => 'published'], 'Save');
    $this->assertSession()->pageTextContains('Created Published state.');
    $workflow = $workflow_storage->loadUnchanged('test');
136
    $this->assertFalse($workflow->getTypePlugin()->getState('published')->canTransitionTo('published'), 'No default transition from published to published exists.');
137 138 139 140 141 142

    $this->clickLink('Add a new state');
    // Don't create a draft to draft transition by default.
    $this->submitForm(['label' => 'Draft', 'id' => 'draft'], 'Save');
    $this->assertSession()->pageTextContains('Created Draft state.');
    $workflow = $workflow_storage->loadUnchanged('test');
143
    $this->assertFalse($workflow->getTypePlugin()->getState('draft')->canTransitionTo('draft'), 'Can not transition from draft to draft');
144 145 146 147 148

    $this->clickLink('Add a new transition');
    $this->submitForm(['id' => 'publish', 'label' => 'Publish', 'from[draft]' => 'draft', 'to' => 'published'], 'Save');
    $this->assertSession()->pageTextContains('Created Publish transition.');
    $workflow = $workflow_storage->loadUnchanged('test');
149
    $this->assertTrue($workflow->getTypePlugin()->getState('draft')->canTransitionTo('published'), 'Can transition from draft to published');
150 151

    $this->clickLink('Add a new transition');
152 153
    $this->assertCount(2, $this->cssSelect('input[name="to"][type="radio"]'));
    $this->assertCount(0, $this->cssSelect('input[name="to"][checked="checked"][type="radio"]'));
154 155 156
    $this->submitForm(['id' => 'create_new_draft', 'label' => 'Create new draft', 'from[draft]' => 'draft', 'to' => 'draft'], 'Save');
    $this->assertSession()->pageTextContains('Created Create new draft transition.');
    $workflow = $workflow_storage->loadUnchanged('test');
157
    $this->assertTrue($workflow->getTypePlugin()->getState('draft')->canTransitionTo('draft'), 'Can transition from draft to draft');
158 159 160 161 162 163 164 165 166 167 168 169 170

    // The fist state to edit on the page should be published.
    $this->clickLink('Edit');
    $this->assertSession()->fieldValueEquals('label', 'Published');
    // Change the label.
    $this->submitForm(['label' => 'Live'], 'Save');
    $this->assertSession()->pageTextContains('Saved Live state.');

    // Allow published to draft.
    $this->clickLink('Edit', 3);
    $this->submitForm(['from[published]' => 'published'], 'Save');
    $this->assertSession()->pageTextContains('Saved Create new draft transition.');
    $workflow = $workflow_storage->loadUnchanged('test');
171
    $this->assertTrue($workflow->getTypePlugin()->getState('published')->canTransitionTo('draft'), 'Can transition from published to draft');
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

    // Try creating a duplicate transition.
    $this->clickLink('Add a new transition');
    $this->submitForm(['id' => 'create_new_draft', 'label' => 'Create new draft', 'from[published]' => 'published', 'to' => 'draft'], 'Save');
    $this->assertSession()->pageTextContains('The machine-readable name is already in use. It must be unique.');
    // Try creating a transition which duplicates the states of another.
    $this->submitForm(['id' => 'create_new_draft2', 'label' => 'Create new draft again', 'from[published]' => 'published', 'to' => 'draft'], 'Save');
    $this->assertSession()->pageTextContains('The transition from Live to Draft already exists.');

    // Create a new transition.
    $this->submitForm(['id' => 'save_and_publish', 'label' => 'Save and publish', 'from[published]' => 'published', 'to' => 'published'], 'Save');
    $this->assertSession()->pageTextContains('Created Save and publish transition.');
    // Edit the new transition and try to add an existing transition.
    $this->clickLink('Edit', 4);
    $this->submitForm(['from[draft]' => 'draft'], 'Save');
    $this->assertSession()->pageTextContains('The transition from Draft to Live already exists.');

    // Delete the transition.
    $workflow = $workflow_storage->loadUnchanged('test');
191
    $this->assertTrue($workflow->getTypePlugin()->hasTransitionFromStateToState('published', 'published'), 'Can transition from published to published');
192 193 194 195
    $this->clickLink('Delete');
    $this->assertSession()->pageTextContains('Are you sure you want to delete Save and publish from Test?');
    $this->submitForm([], 'Delete');
    $workflow = $workflow_storage->loadUnchanged('test');
196
    $this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('published', 'published'), 'Cannot transition from published to published');
197 198 199 200 201 202 203 204 205

    // Try creating a duplicate state.
    $this->drupalGet('admin/config/workflow/workflows/manage/test');
    $this->clickLink('Add a new state');
    $this->submitForm(['label' => 'Draft', 'id' => 'draft'], 'Save');
    $this->assertSession()->pageTextContains('The machine-readable name is already in use. It must be unique.');

    // Ensure that weight changes the state ordering.
    $workflow = $workflow_storage->loadUnchanged('test');
206
    $this->assertEquals('published', $workflow->getTypePlugin()->getInitialState()->id());
207 208 209
    $this->drupalGet('admin/config/workflow/workflows/manage/test');
    $this->submitForm(['states[draft][weight]' => '-1'], 'Save');
    $workflow = $workflow_storage->loadUnchanged('test');
210
    $this->assertEquals('draft', $workflow->getTypePlugin()->getInitialState()->id());
211

212 213
    // Verify that we are still on the workflow edit page.
    $this->assertSession()->addressEquals('admin/config/workflow/workflows/manage/test');
214 215

    // Ensure that weight changes the transition ordering.
216
    $this->assertEquals(['publish', 'create_new_draft'], array_keys($workflow->getTypePlugin()->getTransitions()));
217 218 219
    $this->drupalGet('admin/config/workflow/workflows/manage/test');
    $this->submitForm(['transitions[create_new_draft][weight]' => '-1'], 'Save');
    $workflow = $workflow_storage->loadUnchanged('test');
220
    $this->assertEquals(['create_new_draft', 'publish'], array_keys($workflow->getTypePlugin()->getTransitions()));
221

222 223
    // Verify that we are still on the workflow edit page.
    $this->assertSession()->addressEquals('admin/config/workflow/workflows/manage/test');
224 225 226 227 228

    // Ensure that a delete link for the published state exists before deleting
    // the draft state.
    $published_delete_link = Url::fromRoute('entity.workflow.delete_state_form', [
      'workflow' => $workflow->id(),
229 230 231 232 233
      'workflow_state' => 'published',
    ])->toString();
    $draft_delete_link = Url::fromRoute('entity.workflow.delete_state_form', [
      'workflow' => $workflow->id(),
      'workflow_state' => 'draft',
234
    ])->toString();
235
    $this->assertSession()->elementContains('css', 'tr[data-drupal-selector="edit-states-published"]', 'Delete');
236
    $this->assertSession()->linkByHrefExists($published_delete_link);
237 238 239 240 241 242 243 244 245 246 247 248 249 250
    $this->assertSession()->linkByHrefExists($draft_delete_link);

    // Make the published state a default state and ensure it is no longer
    // linked.
    \Drupal::state()->set('workflow_type_test.required_states', ['published']);
    $this->getSession()->reload();
    $this->assertSession()->linkByHrefNotExists($published_delete_link);
    $this->assertSession()->linkByHrefExists($draft_delete_link);
    $this->assertSession()->elementNotContains('css', 'tr[data-drupal-selector="edit-states-published"]', 'Delete');
    \Drupal::state()->set('workflow_type_test.required_states', []);
    $this->getSession()->reload();
    $this->assertSession()->elementContains('css', 'tr[data-drupal-selector="edit-states-published"]', 'Delete');
    $this->assertSession()->linkByHrefExists($published_delete_link);
    $this->assertSession()->linkByHrefExists($draft_delete_link);
251 252 253 254 255 256 257

    // Delete the Draft state.
    $this->clickLink('Delete');
    $this->assertSession()->pageTextContains('Are you sure you want to delete Draft from Test?');
    $this->submitForm([], 'Delete');
    $this->assertSession()->pageTextContains('State Draft deleted.');
    $workflow = $workflow_storage->loadUnchanged('test');
258 259
    $this->assertFalse($workflow->getTypePlugin()->hasState('draft'), 'Draft state deleted');
    $this->assertTrue($workflow->getTypePlugin()->hasState('published'), 'Workflow still has published state');
260 261 262 263 264 265 266 267

    // The last state cannot be deleted so the only delete link on the page will
    // be for the workflow.
    $this->assertSession()->linkByHrefNotExists($published_delete_link);
    $this->clickLink('Delete');
    $this->assertSession()->pageTextContains('Are you sure you want to delete Test?');
    $this->submitForm([], 'Delete');
    $this->assertSession()->pageTextContains('Workflow Test deleted.');
268
    $this->assertSession()->pageTextContains('There are no workflows yet.');
269
    $this->assertNull($workflow_storage->loadUnchanged('test'), 'The test workflow has been deleted');
270

271 272
    // Ensure that workflow types with default configuration are initialized
    // correctly.
273 274 275 276 277
    $this->drupalGet('admin/config/workflow/workflows');
    $this->clickLink('Add workflow');
    $this->submitForm(['label' => 'Test 2', 'id' => 'test2', 'workflow_type' => 'workflow_type_required_state_test'], 'Save');
    $this->assertSession()->addressEquals('admin/config/workflow/workflows/manage/test2');
    $workflow = $workflow_storage->loadUnchanged('test2');
278 279 280
    $this->assertTrue($workflow->getTypePlugin()->hasState('fresh'), 'The workflow has the "fresh" state');
    $this->assertTrue($workflow->getTypePlugin()->hasState('rotten'), 'The workflow has the "rotten" state');
    $this->assertTrue($workflow->getTypePlugin()->hasTransition('rot'), 'The workflow has the "rot" transition');
281 282
    $this->assertSession()->pageTextContains('Fresh');
    $this->assertSession()->pageTextContains('Rotten');
283 284
  }

285 286 287 288 289 290
  /**
   * Test the workflow configuration form.
   */
  public function testWorkflowConfigurationForm() {
    $workflow = Workflow::create(['id' => 'test', 'type' => 'workflow_type_complex_test', 'label' => 'Test']);
    $workflow
291
      ->getTypePlugin()
292
      ->addState('published', 'Published')
293 294
      ->addTransition('publish', 'Publish', ['published'], 'published');
    $workflow->save();
295 296 297 298 299 300 301 302 303 304 305 306 307

    $this->drupalLogin($this->createUser(['administer workflows']));

    // Add additional information to the workflow via the configuration form.
    $this->drupalGet('admin/config/workflow/workflows/manage/test');
    $this->assertSession()->pageTextContains('Example global workflow setting');
    $this->submitForm(['type_settings[example_setting]' => 'Extra global settings'], 'Save');

    $workflow_storage = $this->container->get('entity_type.manager')->getStorage('workflow');
    $workflow = $workflow_storage->loadUnchanged('test');
    $this->assertEquals('Extra global settings', $workflow->getTypePlugin()->getConfiguration()['example_setting']);
  }

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  /**
   * Test a workflow, state, and transition can have a numeric ID and label.
   */
  public function testNumericIds() {
    $this->drupalLogin($this->createUser(['administer workflows']));
    $this->drupalGet('admin/config/workflow/workflows');
    $this->clickLink('Add workflow');
    $this->submitForm(['label' => 123, 'id' => 123, 'workflow_type' => 'workflow_type_complex_test'], 'Save');

    $this->assertSession()->addressEquals('admin/config/workflow/workflows/manage/123/add_state');

    $this->submitForm(['label' => 456, 'id' => 456], 'Save');
    $this->assertSession()->pageTextContains('Created 456 state.');

    $this->clickLink('Add a new state');
    $this->submitForm(['label' => 789, 'id' => 789], 'Save');
    $this->assertSession()->pageTextContains('Created 789 state.');

    $this->clickLink('Add a new transition');
    $this->submitForm(['id' => 101112, 'label' => 101112, 'from[456]' => 456, 'to' => 789], 'Save');
    $this->assertSession()->pageTextContains('Created 101112 transition.');

    $workflow = $this->container->get('entity_type.manager')->getStorage('workflow')->loadUnchanged(123);
    $this->assertEquals(123, $workflow->id());
    $this->assertEquals(456, $workflow->getTypePlugin()->getState(456)->id());
    $this->assertEquals(101112, $workflow->getTypePlugin()->getTransition(101112)->id());
    $this->assertEquals(789, $workflow->getTypePlugin()->getTransition(101112)->to()->id());
  }

  /**
   * Test the sorting of states and transitions by weight and label.
   */
  public function testSorting() {
    $workflow = Workflow::create(['id' => 'test', 'type' => 'workflow_type_complex_test', 'label' => 'Test']);
    $workflow
      ->getTypePlugin()
      ->setConfiguration([
        'states' => [
          'twoa' => [
            'label' => 'twoa',
            'weight' => 2,
          ],
          'three' => [
            'label' => 'three',
            'weight' => 3,
          ],
          'twob' => [
            'label' => 'twob',
            'weight' => 2,
          ],
          'one' => [
            'label' => 'one',
            'weight' => 1,
          ],
        ],
        'transitions' => [
          'three' => [
            'label' => 'three',
            'from' => ['three'],
            'to' => 'three',
            'weight' => 3,
          ],
          'twoa' => [
            'label' => 'twoa',
            'from' => ['twoa'],
            'to' => 'twoa',
            'weight' => 2,
          ],
          'one' => [
            'label' => 'one',
            'from' => ['one'],
            'to' => 'one',
            'weight' => 1,
          ],
          'twob' => [
            'label' => 'twob',
            'from' => ['twob'],
            'to' => 'twob',
            'weight' => 2,
          ],
        ],
      ]);
    $workflow->save();

    $this->drupalLogin($this->createUser(['administer workflows']));
    $this->drupalGet('admin/config/workflow/workflows/manage/test');
    $expected_states = ['one', 'twoa', 'twob', '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', 'twoa', 'twob', '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());
    }
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

    // 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');
419 420
  }

421
}