Version 1
[yaffs-website] / web / core / modules / content_moderation / tests / src / Unit / ModerationInformationTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Unit;
4
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;
14
15 /**
16  * @coversDefaultClass \Drupal\content_moderation\ModerationInformation
17  * @group content_moderation
18  */
19 class ModerationInformationTest extends \PHPUnit_Framework_TestCase {
20
21   /**
22    * Builds a mock user.
23    *
24    * @return AccountInterface
25    *   The mocked user.
26    */
27   protected function getUser() {
28     return $this->prophesize(AccountInterface::class)->reveal();
29   }
30
31   /**
32    * Returns a mock Entity Type Manager.
33    *
34    * @return EntityTypeManagerInterface
35    *   The mocked entity type manager.
36    */
37   protected function getEntityTypeManager() {
38     $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
39     return $entity_type_manager->reveal();
40   }
41
42   /**
43    * Sets up content moderation and entity manager mocking.
44    *
45    * @param string $bundle
46    *   The bundle ID.
47    * @param string|null $workflow
48    *   The workflow ID. If nul no workflow information is added to the bundle.
49    *
50    * @return \Drupal\Core\Entity\EntityTypeManagerInterface
51    *   The mocked entity type manager.
52    */
53   public function setupModerationBundleInfo($bundle, $workflow = NULL) {
54     $bundle_info_array = [];
55     if ($workflow) {
56       $bundle_info_array['workflow'] = $workflow;
57     }
58     $bundle_info = $this->prophesize(EntityTypeBundleInfoInterface::class);
59     $bundle_info->getBundleInfo("test_entity_type")->willReturn([$bundle => $bundle_info_array]);
60
61     return $bundle_info->reveal();
62   }
63
64   /**
65    * @dataProvider providerWorkflow
66    * @covers ::isModeratedEntity
67    */
68   public function testIsModeratedEntity($workflow, $expected) {
69     $moderation_information = new ModerationInformation($this->getEntityTypeManager(), $this->setupModerationBundleInfo('test_bundle', $workflow));
70
71     $entity_type = new ContentEntityType([
72       'id' => 'test_entity_type',
73       'bundle_entity_type' => 'entity_test_bundle',
74       'handlers' => ['moderation' => ModerationHandler::class],
75     ]);
76     $entity = $this->prophesize(ContentEntityInterface::class);
77     $entity->getEntityType()->willReturn($entity_type);
78     $entity->bundle()->willReturn('test_bundle');
79
80     $this->assertEquals($expected, $moderation_information->isModeratedEntity($entity->reveal()));
81   }
82
83   /**
84    * @dataProvider providerWorkflow
85    * @covers ::getWorkflowForEntity
86    */
87   public function testGetWorkflowForEntity($workflow) {
88     $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
89     if ($workflow) {
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());
94     }
95     else {
96       $workflow_entity = NULL;
97     }
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');
102
103     $this->assertEquals($workflow_entity, $moderation_information->getWorkflowForEntity($entity->reveal()));
104   }
105
106   /**
107    * @dataProvider providerWorkflow
108    * @covers ::shouldModerateEntitiesOfBundle
109    */
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],
115     ]);
116
117     $moderation_information = new ModerationInformation($this->getEntityTypeManager(), $this->setupModerationBundleInfo('test_bundle', $workflow));
118
119     $this->assertEquals($expected, $moderation_information->shouldModerateEntitiesOfBundle($entity_type, 'test_bundle'));
120   }
121
122   /**
123    * Data provider for several tests.
124    */
125   public function providerWorkflow() {
126     return [
127       [NULL, FALSE],
128       ['workflow', TRUE],
129     ];
130   }
131
132 }