3 namespace Drupal\Tests\content_moderation\Unit;
5 use Drupal\content_moderation\Entity\Handler\ModerationHandler;
6 use Drupal\Core\Entity\ContentEntityInterface;
7 use Drupal\Core\Entity\ContentEntityType;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Drupal\content_moderation\ModerationInformation;
13 use Drupal\workflows\WorkflowInterface;
16 * @coversDefaultClass \Drupal\content_moderation\ModerationInformation
17 * @group content_moderation
19 class ModerationInformationTest extends \PHPUnit_Framework_TestCase {
24 * @return AccountInterface
27 protected function getUser() {
28 return $this->prophesize(AccountInterface::class)->reveal();
32 * Returns a mock Entity Type Manager.
34 * @return EntityTypeManagerInterface
35 * The mocked entity type manager.
37 protected function getEntityTypeManager() {
38 $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
39 return $entity_type_manager->reveal();
43 * Sets up content moderation and entity manager mocking.
45 * @param string $bundle
47 * @param string|null $workflow
48 * The workflow ID. If nul no workflow information is added to the bundle.
50 * @return \Drupal\Core\Entity\EntityTypeManagerInterface
51 * The mocked entity type manager.
53 public function setupModerationBundleInfo($bundle, $workflow = NULL) {
54 $bundle_info_array = [];
56 $bundle_info_array['workflow'] = $workflow;
58 $bundle_info = $this->prophesize(EntityTypeBundleInfoInterface::class);
59 $bundle_info->getBundleInfo("test_entity_type")->willReturn([$bundle => $bundle_info_array]);
61 return $bundle_info->reveal();
65 * @dataProvider providerWorkflow
66 * @covers ::isModeratedEntity
68 public function testIsModeratedEntity($workflow, $expected) {
69 $moderation_information = new ModerationInformation($this->getEntityTypeManager(), $this->setupModerationBundleInfo('test_bundle', $workflow));
71 $entity_type = new ContentEntityType([
72 'id' => 'test_entity_type',
73 'bundle_entity_type' => 'entity_test_bundle',
74 'handlers' => ['moderation' => ModerationHandler::class],
76 $entity = $this->prophesize(ContentEntityInterface::class);
77 $entity->getEntityType()->willReturn($entity_type);
78 $entity->bundle()->willReturn('test_bundle');
80 $this->assertEquals($expected, $moderation_information->isModeratedEntity($entity->reveal()));
84 * @dataProvider providerWorkflow
85 * @covers ::getWorkflowForEntity
87 public function testGetWorkflowForEntity($workflow) {
88 $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
90 $workflow_entity = $this->prophesize(WorkflowInterface::class)->reveal();
91 $workflow_storage = $this->prophesize(EntityStorageInterface::class);
92 $workflow_storage->load('workflow')->willReturn($workflow_entity)->shouldBeCalled();
93 $entity_type_manager->getStorage('workflow')->willReturn($workflow_storage->reveal());
96 $workflow_entity = NULL;
98 $moderation_information = new ModerationInformation($entity_type_manager->reveal(), $this->setupModerationBundleInfo('test_bundle', $workflow));
99 $entity = $this->prophesize(ContentEntityInterface::class);
100 $entity->getEntityTypeId()->willReturn('test_entity_type');
101 $entity->bundle()->willReturn('test_bundle');
103 $this->assertEquals($workflow_entity, $moderation_information->getWorkflowForEntity($entity->reveal()));
107 * @dataProvider providerWorkflow
108 * @covers ::shouldModerateEntitiesOfBundle
110 public function testShouldModerateEntities($workflow, $expected) {
111 $entity_type = new ContentEntityType([
112 'id' => 'test_entity_type',
113 'bundle_entity_type' => 'entity_test_bundle',
114 'handlers' => ['moderation' => ModerationHandler::class],
117 $moderation_information = new ModerationInformation($this->getEntityTypeManager(), $this->setupModerationBundleInfo('test_bundle', $workflow));
119 $this->assertEquals($expected, $moderation_information->shouldModerateEntitiesOfBundle($entity_type, 'test_bundle'));
123 * Data provider for several tests.
125 public function providerWorkflow() {