class StateTransitionValidationTest in Workbench Moderation 8
Same name and namespace in other branches
- 8.2 tests/src/Unit/StateTransitionValidationTest.php \Drupal\Tests\workbench_moderation\Unit\StateTransitionValidationTest
@coversDefaultClass \Drupal\workbench_moderation\StateTransitionValidation @group workbench_moderation
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\workbench_moderation\Unit\StateTransitionValidationTest
Expanded class hierarchy of StateTransitionValidationTest
File
- tests/
src/ Unit/ StateTransitionValidationTest.php, line 18
Namespace
Drupal\Tests\workbench_moderation\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
StateTransitionValidationTest:: |
protected | function | Builds a mocked Entity Type Manager. | |
StateTransitionValidationTest:: |
protected | function | Builds a mock storage object for States. | |
StateTransitionValidationTest:: |
protected | function | Builds an array of mocked Transition objects. | |
StateTransitionValidationTest:: |
protected | function | Builds a mock storage object for Transitions. | |
StateTransitionValidationTest:: |
public | function | @covers ::isTransitionAllowed @covers ::calculatePossibleTransitions | |
StateTransitionValidationTest:: |
public | function | @covers ::isTransitionAllowed @covers ::calculatePossibleTransitions | |
StateTransitionValidationTest:: |
public | function | Verifies user-aware transition validation. | |
StateTransitionValidationTest:: |
public | function | Data provider for the user transition test. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |