You are here

class StateTransitionValidationTest in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/StateTransitionValidationTest.php \Drupal\Tests\workbench_moderation\Unit\StateTransitionValidationTest

@coversDefaultClass \Drupal\workbench_moderation\StateTransitionValidation @group workbench_moderation

Hierarchy

Expanded class hierarchy of StateTransitionValidationTest

File

tests/src/Unit/StateTransitionValidationTest.php, line 18

Namespace

Drupal\Tests\workbench_moderation\Unit
View source
class StateTransitionValidationTest extends UnitTestCase {

  /**
   * Builds a mock storage object for Transitions.
   *
   * @return \Drupal\Core\Entity\EntityStorageInterface
   *   Returns an entity storage config.
   */
  protected function setupTransitionStorage() {
    $entity_storage = $this
      ->prophesize(EntityStorageInterface::class);
    $list = $this
      ->setupTransitionEntityList();
    $entity_storage
      ->loadMultiple()
      ->willReturn($list);
    $entity_storage
      ->loadMultiple(Argument::type('array'))
      ->will(function ($args) use ($list) {
      $keys = $args[0];
      if (empty($keys)) {
        return $list;
      }
      $return = array_map(function ($key) use ($list) {
        return $list[$key];
      }, $keys);
      return $return;
    });
    return $entity_storage
      ->reveal();
  }

  /**
   * Builds an array of mocked Transition objects.
   *
   * @return \Drupal\workbench_moderation\ModerationStateTransitionInterface[]
   *   Returns a list.
   */
  protected function setupTransitionEntityList() {
    $transition = $this
      ->prophesize(ModerationStateTransitionInterface::class);
    $transition
      ->id()
      ->willReturn('draft__needs_review');
    $transition
      ->getFromState()
      ->willReturn('draft');
    $transition
      ->getToState()
      ->willReturn('needs_review');
    $list[$transition
      ->reveal()
      ->id()] = $transition
      ->reveal();
    $transition = $this
      ->prophesize(ModerationStateTransitionInterface::class);
    $transition
      ->id()
      ->willReturn('needs_review__staging');
    $transition
      ->getFromState()
      ->willReturn('needs_review');
    $transition
      ->getToState()
      ->willReturn('staging');
    $list[$transition
      ->reveal()
      ->id()] = $transition
      ->reveal();
    $transition = $this
      ->prophesize(ModerationStateTransitionInterface::class);
    $transition
      ->id()
      ->willReturn('staging__published');
    $transition
      ->getFromState()
      ->willReturn('staging');
    $transition
      ->getToState()
      ->willReturn('published');
    $list[$transition
      ->reveal()
      ->id()] = $transition
      ->reveal();
    $transition = $this
      ->prophesize(ModerationStateTransitionInterface::class);
    $transition
      ->id()
      ->willReturn('needs_review__draft');
    $transition
      ->getFromState()
      ->willReturn('needs_review');
    $transition
      ->getToState()
      ->willReturn('draft');
    $list[$transition
      ->reveal()
      ->id()] = $transition
      ->reveal();
    $transition = $this
      ->prophesize(ModerationStateTransitionInterface::class);
    $transition
      ->id()
      ->willReturn('draft__draft');
    $transition
      ->getFromState()
      ->willReturn('draft');
    $transition
      ->getToState()
      ->willReturn('draft');
    $list[$transition
      ->reveal()
      ->id()] = $transition
      ->reveal();
    $transition = $this
      ->prophesize(ModerationStateTransitionInterface::class);
    $transition
      ->id()
      ->willReturn('needs_review__needs_review');
    $transition
      ->getFromState()
      ->willReturn('needs_review');
    $transition
      ->getToState()
      ->willReturn('needs_review');
    $list[$transition
      ->reveal()
      ->id()] = $transition
      ->reveal();
    $transition = $this
      ->prophesize(ModerationStateTransitionInterface::class);
    $transition
      ->id()
      ->willReturn('published__published');
    $transition
      ->getFromState()
      ->willReturn('published');
    $transition
      ->getToState()
      ->willReturn('published');
    $list[$transition
      ->reveal()
      ->id()] = $transition
      ->reveal();
    return $list;
  }

  /**
   * Builds a mock storage object for States.
   *
   * @return \Drupal\Core\Entity\EntityStorageInterface
   *   Returns an entity storage config.
   */
  protected function setupStateStorage() {
    $entity_storage = $this
      ->prophesize(EntityStorageInterface::class);
    $state = $this
      ->prophesize(ModerationStateInterface::class);
    $state
      ->id()
      ->willReturn('draft');
    $state
      ->label()
      ->willReturn('Draft');
    $state
      ->isPublishedState()
      ->willReturn(FALSE);
    $state
      ->isDefaultRevisionState()
      ->willReturn(FALSE);
    $states['draft'] = $state
      ->reveal();
    $state = $this
      ->prophesize(ModerationStateInterface::class);
    $state
      ->id()
      ->willReturn('needs_review');
    $state
      ->label()
      ->willReturn('Needs Review');
    $state
      ->isPublishedState()
      ->willReturn(FALSE);
    $state
      ->isDefaultRevisionState()
      ->willReturn(FALSE);
    $states['needs_review'] = $state
      ->reveal();
    $state = $this
      ->prophesize(ModerationStateInterface::class);
    $state
      ->id()
      ->willReturn('published');
    $state
      ->label()
      ->willReturn('Published');
    $state
      ->isPublishedState()
      ->willReturn(TRUE);
    $state
      ->isDefaultRevisionState()
      ->willReturn(TRUE);
    $states['published'] = $state
      ->reveal();
    $entity_storage
      ->loadMultiple()
      ->willReturn($states);
    return $entity_storage
      ->reveal();
  }

  /**
   * Builds a mocked Entity Type Manager.
   *
   * @return \Drupal\Core\Entity\EntityTypeManagerInterface
   *   Returns an entity type manager.
   */
  protected function setupEntityTypeManager() {
    $entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $entityTypeManager
      ->getStorage('moderation_state')
      ->willReturn($this
      ->setupStateStorage());
    $entityTypeManager
      ->getStorage('moderation_state_transition')
      ->willReturn($this
      ->setupTransitionStorage());
    return $entityTypeManager
      ->reveal();
  }

  /**
   * @covers ::isTransitionAllowed
   * @covers ::calculatePossibleTransitions
   */
  public function testIsTransitionAllowedWithValidTransition() {
    $state_transition_validation = new StateTransitionValidation($this
      ->setupEntityTypeManager());
    $this
      ->assertTrue($state_transition_validation
      ->isTransitionAllowed('draft', 'draft'));
    $this
      ->assertTrue($state_transition_validation
      ->isTransitionAllowed('draft', 'needs_review'));
    $this
      ->assertTrue($state_transition_validation
      ->isTransitionAllowed('needs_review', 'needs_review'));
    $this
      ->assertTrue($state_transition_validation
      ->isTransitionAllowed('needs_review', 'staging'));
    $this
      ->assertTrue($state_transition_validation
      ->isTransitionAllowed('staging', 'published'));
    $this
      ->assertTrue($state_transition_validation
      ->isTransitionAllowed('needs_review', 'draft'));
  }

  /**
   * @covers ::isTransitionAllowed
   * @covers ::calculatePossibleTransitions
   */
  public function testIsTransitionAllowedWithInValidTransition() {
    $state_transition_validation = new StateTransitionValidation($this
      ->setupEntityTypeManager());
    $this
      ->assertFalse($state_transition_validation
      ->isTransitionAllowed('published', 'needs_review'));
    $this
      ->assertFalse($state_transition_validation
      ->isTransitionAllowed('published', 'staging'));
    $this
      ->assertFalse($state_transition_validation
      ->isTransitionAllowed('staging', 'needs_review'));
    $this
      ->assertFalse($state_transition_validation
      ->isTransitionAllowed('staging', 'staging'));
    $this
      ->assertFalse($state_transition_validation
      ->isTransitionAllowed('needs_review', 'published'));
    $this
      ->assertFalse($state_transition_validation
      ->isTransitionAllowed('published', 'archived'));
    $this
      ->assertFalse($state_transition_validation
      ->isTransitionAllowed('archived', 'published'));
  }

  /**
   * Verifies user-aware transition validation.
   *
   * @param string $from
   *   The state to transition from.
   * @param string $to
   *   The state to transition to.
   * @param string $permission
   *   The permission to give the user, or not.
   * @param bool $allowed
   *   Whether or not to grant a user this permission.
   * @param bool $result
   *   Whether userMayTransition() is expected to return TRUE or FALSE.
   *
   * @dataProvider userTransitionsProvider
   */
  public function testUserSensitiveValidTransitions($from, $to, $permission, $allowed, $result) {
    $user = $this
      ->prophesize(AccountInterface::class);

    // The one listed permission will be returned as instructed; Any others are
    // always denied.
    $user
      ->hasPermission($permission)
      ->willReturn($allowed);
    $user
      ->hasPermission(Argument::type('string'))
      ->willReturn(FALSE);
    $validator = new Validator($this
      ->setupEntityTypeManager());
    $this
      ->assertEquals($result, $validator
      ->userMayTransition($from, $to, $user
      ->reveal()));
  }

  /**
   * Data provider for the user transition test.
   *
   * @return array
   *   Returns an array.
   */
  public function userTransitionsProvider() {

    // The user has the right permission, so let it through.
    $ret[] = [
      'draft',
      'draft',
      'use draft__draft transition',
      TRUE,
      TRUE,
    ];

    // The user doesn't have the right permission, block it.
    $ret[] = [
      'draft',
      'draft',
      'use draft__draft transition',
      FALSE,
      FALSE,
    ];

    // The user has some other permission that doesn't matter.
    $ret[] = [
      'draft',
      'draft',
      'use draft__needs_review transition',
      TRUE,
      FALSE,
    ];

    // The user has permission, but the transition isn't allowed anyway.
    $ret[] = [
      'published',
      'needs_review',
      'use published__needs_review transition',
      TRUE,
      FALSE,
    ];
    return $ret;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
StateTransitionValidationTest::setupEntityTypeManager protected function Builds a mocked Entity Type Manager.
StateTransitionValidationTest::setupStateStorage protected function Builds a mock storage object for States.
StateTransitionValidationTest::setupTransitionEntityList protected function Builds an array of mocked Transition objects.
StateTransitionValidationTest::setupTransitionStorage protected function Builds a mock storage object for Transitions.
StateTransitionValidationTest::testIsTransitionAllowedWithInValidTransition public function @covers ::isTransitionAllowed @covers ::calculatePossibleTransitions
StateTransitionValidationTest::testIsTransitionAllowedWithValidTransition public function @covers ::isTransitionAllowed @covers ::calculatePossibleTransitions
StateTransitionValidationTest::testUserSensitiveValidTransitions public function Verifies user-aware transition validation.
StateTransitionValidationTest::userTransitionsProvider public function Data provider for the user transition test.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340