StateTransitionValidationTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Unit;
  3. use Drupal\content_moderation\ModerationInformationInterface;
  4. use Drupal\Core\DependencyInjection\ContainerBuilder;
  5. use Drupal\Core\Entity\ContentEntityInterface;
  6. use Drupal\Core\Session\AccountInterface;
  7. use Drupal\content_moderation\StateTransitionValidation;
  8. use Drupal\Tests\UnitTestCase;
  9. use Drupal\workflow_type_test\Plugin\WorkflowType\TestType;
  10. use Drupal\workflows\Entity\Workflow;
  11. use Drupal\workflows\WorkflowTypeManager;
  12. use Prophecy\Argument;
  13. /**
  14. * @coversDefaultClass \Drupal\content_moderation\StateTransitionValidation
  15. * @group content_moderation
  16. */
  17. class StateTransitionValidationTest extends UnitTestCase {
  18. /**
  19. * Verifies user-aware transition validation.
  20. *
  21. * @param string $from_id
  22. * The state to transition from.
  23. * @param string $to_id
  24. * The state to transition to.
  25. * @param string $permission
  26. * The permission to give the user, or not.
  27. * @param bool $allowed
  28. * Whether or not to grant a user this permission.
  29. * @param bool $result
  30. * Whether getValidTransitions() is expected to have the.
  31. *
  32. * @dataProvider userTransitionsProvider
  33. */
  34. public function testUserSensitiveValidTransitions($from_id, $to_id, $permission, $allowed, $result) {
  35. $user = $this->prophesize(AccountInterface::class);
  36. // The one listed permission will be returned as instructed; Any others are
  37. // always denied.
  38. $user->hasPermission($permission)->willReturn($allowed);
  39. $user->hasPermission(Argument::type('string'))->willReturn(FALSE);
  40. $entity = $this->prophesize(ContentEntityInterface::class);
  41. $entity = $entity->reveal();
  42. $entity->moderation_state = new \stdClass();
  43. $entity->moderation_state->value = $from_id;
  44. $validator = new StateTransitionValidation($this->setUpModerationInformation($entity));
  45. $has_transition = FALSE;
  46. foreach ($validator->getValidTransitions($entity, $user->reveal()) as $transition) {
  47. if ($transition->to()->id() === $to_id) {
  48. $has_transition = TRUE;
  49. break;
  50. }
  51. }
  52. $this->assertSame($result, $has_transition);
  53. }
  54. protected function setUpModerationInformation(ContentEntityInterface $entity) {
  55. // Create a container so that the plugin manager and workflow type can be
  56. // mocked.
  57. $container = new ContainerBuilder();
  58. $workflow_manager = $this->prophesize(WorkflowTypeManager::class);
  59. $workflow_manager->createInstance('content_moderation', Argument::any())->willReturn(new TestType([], '', []));
  60. $container->set('plugin.manager.workflows.type', $workflow_manager->reveal());
  61. \Drupal::setContainer($container);
  62. $workflow = new Workflow(['id' => 'process', 'type' => 'content_moderation'], 'workflow');
  63. $workflow
  64. ->getTypePlugin()
  65. ->addState('draft', 'draft')
  66. ->addState('needs_review', 'needs_review')
  67. ->addState('published', 'published')
  68. ->addTransition('draft', 'draft', ['draft'], 'draft')
  69. ->addTransition('review', 'review', ['draft'], 'needs_review')
  70. ->addTransition('publish', 'publish', ['needs_review', 'published'], 'published');
  71. $moderation_info = $this->prophesize(ModerationInformationInterface::class);
  72. $moderation_info->getWorkflowForEntity($entity)->willReturn($workflow);
  73. return $moderation_info->reveal();
  74. }
  75. /**
  76. * Data provider for the user transition test.
  77. */
  78. public function userTransitionsProvider() {
  79. // The user has the right permission, so let it through.
  80. $ret[] = ['draft', 'draft', 'use process transition draft', TRUE, TRUE];
  81. // The user doesn't have the right permission, block it.
  82. $ret[] = ['draft', 'draft', 'use process transition draft', FALSE, FALSE];
  83. // The user has some other permission that doesn't matter.
  84. $ret[] = ['draft', 'draft', 'use process transition review', TRUE, FALSE];
  85. return $ret;
  86. }
  87. }