ModerationInformationTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Unit;
  3. use Drupal\content_moderation\Entity\Handler\ModerationHandler;
  4. use Drupal\Core\Entity\ContentEntityInterface;
  5. use Drupal\Core\Entity\ContentEntityType;
  6. use Drupal\Core\Entity\EntityStorageInterface;
  7. use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
  8. use Drupal\Core\Entity\EntityTypeManagerInterface;
  9. use Drupal\Core\Session\AccountInterface;
  10. use Drupal\content_moderation\ModerationInformation;
  11. use Drupal\Tests\UnitTestCase;
  12. use Drupal\workflows\WorkflowInterface;
  13. /**
  14. * @coversDefaultClass \Drupal\content_moderation\ModerationInformation
  15. * @group content_moderation
  16. */
  17. class ModerationInformationTest extends UnitTestCase {
  18. /**
  19. * Builds a mock user.
  20. *
  21. * @return \Drupal\Core\Session\AccountInterface
  22. * The mocked user.
  23. */
  24. protected function getUser() {
  25. return $this->prophesize(AccountInterface::class)->reveal();
  26. }
  27. /**
  28. * Returns a mock Entity Type Manager.
  29. *
  30. * @return \Drupal\Core\Entity\EntityTypeManagerInterface
  31. * The mocked entity type manager.
  32. */
  33. protected function getEntityTypeManager() {
  34. $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
  35. return $entity_type_manager->reveal();
  36. }
  37. /**
  38. * Sets up content moderation and entity manager mocking.
  39. *
  40. * @param string $bundle
  41. * The bundle ID.
  42. * @param string|null $workflow
  43. * The workflow ID. If nul no workflow information is added to the bundle.
  44. *
  45. * @return \Drupal\Core\Entity\EntityTypeManagerInterface
  46. * The mocked entity type manager.
  47. */
  48. public function setupModerationBundleInfo($bundle, $workflow = NULL) {
  49. $bundle_info_array = [];
  50. if ($workflow) {
  51. $bundle_info_array['workflow'] = $workflow;
  52. }
  53. $bundle_info = $this->prophesize(EntityTypeBundleInfoInterface::class);
  54. $bundle_info->getBundleInfo("test_entity_type")->willReturn([$bundle => $bundle_info_array]);
  55. return $bundle_info->reveal();
  56. }
  57. /**
  58. * @dataProvider providerWorkflow
  59. * @covers ::isModeratedEntity
  60. */
  61. public function testIsModeratedEntity($workflow, $expected) {
  62. $moderation_information = new ModerationInformation($this->getEntityTypeManager(), $this->setupModerationBundleInfo('test_bundle', $workflow));
  63. $entity_type = new ContentEntityType([
  64. 'id' => 'test_entity_type',
  65. 'bundle_entity_type' => 'entity_test_bundle',
  66. 'handlers' => ['moderation' => ModerationHandler::class],
  67. ]);
  68. $entity = $this->prophesize(ContentEntityInterface::class);
  69. $entity->getEntityType()->willReturn($entity_type);
  70. $entity->bundle()->willReturn('test_bundle');
  71. $this->assertEquals($expected, $moderation_information->isModeratedEntity($entity->reveal()));
  72. }
  73. /**
  74. * @dataProvider providerWorkflow
  75. * @covers ::getWorkflowForEntity
  76. */
  77. public function testGetWorkflowForEntity($workflow) {
  78. $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
  79. if ($workflow) {
  80. $workflow_entity = $this->prophesize(WorkflowInterface::class)->reveal();
  81. $workflow_storage = $this->prophesize(EntityStorageInterface::class);
  82. $workflow_storage->load('workflow')->willReturn($workflow_entity)->shouldBeCalled();
  83. $entity_type_manager->getStorage('workflow')->willReturn($workflow_storage->reveal());
  84. }
  85. else {
  86. $workflow_entity = NULL;
  87. }
  88. $moderation_information = new ModerationInformation($entity_type_manager->reveal(), $this->setupModerationBundleInfo('test_bundle', $workflow));
  89. $entity = $this->prophesize(ContentEntityInterface::class);
  90. $entity->getEntityTypeId()->willReturn('test_entity_type');
  91. $entity->bundle()->willReturn('test_bundle');
  92. $this->assertEquals($workflow_entity, $moderation_information->getWorkflowForEntity($entity->reveal()));
  93. }
  94. /**
  95. * @dataProvider providerWorkflow
  96. * @covers ::shouldModerateEntitiesOfBundle
  97. */
  98. public function testShouldModerateEntities($workflow, $expected) {
  99. $entity_type = new ContentEntityType([
  100. 'id' => 'test_entity_type',
  101. 'bundle_entity_type' => 'entity_test_bundle',
  102. 'handlers' => ['moderation' => ModerationHandler::class],
  103. ]);
  104. $moderation_information = new ModerationInformation($this->getEntityTypeManager(), $this->setupModerationBundleInfo('test_bundle', $workflow));
  105. $this->assertEquals($expected, $moderation_information->shouldModerateEntitiesOfBundle($entity_type, 'test_bundle'));
  106. }
  107. /**
  108. * Data provider for several tests.
  109. */
  110. public function providerWorkflow() {
  111. return [
  112. [NULL, FALSE],
  113. ['workflow', TRUE],
  114. ];
  115. }
  116. }