RequiredStatesTest.php 3.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
<?php

namespace Drupal\Tests\workflows\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\workflows\Entity\Workflow;

/**
 * Tests Workflow type's required states and configuration initialization.
 *
 * @coversDefaultClass \Drupal\workflows\Plugin\WorkflowTypeBase
 *
 * @group workflows
 */
class RequiredStatesTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = ['workflows', 'workflow_type_test'];

  /**
   * @covers ::getRequiredStates
   * @covers ::initializeWorkflow
   * @covers ::__construct
   */
  public function testGetRequiredStates() {
    $workflow = new Workflow([
      'id' => 'test',
      'type' => 'workflow_type_required_state_test',
    ], 'workflow');
    $workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
    $workflow->save();
    $this->assertEquals(['fresh', 'rotten'], $workflow->getTypePlugin()
      ->getRequiredStates());

    // Ensure that the workflow has the default configuration.
    $this->assertTrue($workflow->hasState('rotten'));
    $this->assertTrue($workflow->hasState('fresh'));
    $this->assertTrue($workflow->hasTransitionFromStateToState('fresh', 'rotten'));
  }

  /**
   * @covers \Drupal\workflows\Entity\Workflow::preSave
   * @expectedException \Drupal\workflows\Exception\RequiredStateMissingException
   * @expectedExceptionMessage Required State Type Test' requires states with the ID 'fresh' in workflow 'test'
   */
  public function testDeleteRequiredStateAPI() {
    $workflow = new Workflow([
      'id' => 'test',
      'type' => 'workflow_type_required_state_test',
    ], 'workflow');
    $workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
    $workflow->save();
    // Ensure that required states can't be deleted.
    $workflow->deleteState('fresh')->save();
  }

  /**
   * @covers \Drupal\workflows\Entity\Workflow::preSave
   * @expectedException \Drupal\workflows\Exception\RequiredStateMissingException
   * @expectedExceptionMessage Required State Type Test' requires states with the ID 'fresh', 'rotten' in workflow 'test'
   */
  public function testNoStatesRequiredStateAPI() {
    $workflow = new Workflow([
      'id' => 'test',
      'type' => 'workflow_type_required_state_test',
    ], 'workflow');
    $workflow->save();
  }

  /**
   * Ensures that initialized configuration can be changed.
   */
  public function testChangeRequiredStateAPI() {
    $workflow = new Workflow([
      'id' => 'test',
      'type' => 'workflow_type_required_state_test',
    ], 'workflow');
    $workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
    $workflow->save();

    // Ensure states added by default configuration can be changed.
    $this->assertEquals('Fresh', $workflow->getState('fresh')->label());
    $workflow
      ->setStateLabel('fresh', 'Fresher')
      ->save();
    $this->assertEquals('Fresher', $workflow->getState('fresh')->label());

    // Ensure transitions can be altered.
    $workflow
      ->addState('cooked', 'Cooked')
      ->setTransitionFromStates('rot', ['fresh', 'cooked'])
      ->save();
    $this->assertTrue($workflow->hasTransitionFromStateToState('fresh', 'rotten'));
    $this->assertTrue($workflow->hasTransitionFromStateToState('cooked', 'rotten'));

    $workflow
      ->setTransitionFromStates('rot', ['cooked'])
      ->save();
    $this->assertFalse($workflow->hasTransitionFromStateToState('fresh', 'rotten'));
    $this->assertTrue($workflow->hasTransitionFromStateToState('cooked', 'rotten'));

    // Ensure the default configuration does not cause ordering issues.
    $workflow->addTransition('cook', 'Cook', ['fresh'], 'cooked')->save();
    $this->assertSame([
      'cooked',
      'fresh',
      'rotten',
    ], array_keys($workflow->get('states')));
    $this->assertSame([
      'cook',
      'rot',
    ], array_keys($workflow->get('transitions')));

    // Ensure that transitions can be deleted.
    $workflow->deleteTransition('rot')->save();
    $this->assertFalse($workflow->hasTransition('rot'));
  }

}