class ModerationInformationTest in Drupal 8
Same name in this branch
- 8 core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php \Drupal\Tests\content_moderation\Unit\ModerationInformationTest
- 8 core/modules/content_moderation/tests/src/Kernel/ModerationInformationTest.php \Drupal\Tests\content_moderation\Kernel\ModerationInformationTest
Same name and namespace in other branches
- 9 core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php \Drupal\Tests\content_moderation\Unit\ModerationInformationTest
@coversDefaultClass \Drupal\content_moderation\ModerationInformation @group content_moderation
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\content_moderation\Unit\ModerationInformationTest
Expanded class hierarchy of ModerationInformationTest
File
- core/
modules/ content_moderation/ tests/ src/ Unit/ ModerationInformationTest.php, line 21
Namespace
Drupal\Tests\content_moderation\UnitView source
class ModerationInformationTest extends UnitTestCase {
/**
* Builds a mock user.
*
* @return \Drupal\Core\Session\AccountInterface
* The mocked user.
*/
protected function getUser() {
return $this
->prophesize(AccountInterface::class)
->reveal();
}
/**
* Returns a mock Entity Type Manager.
*
* @return \Drupal\Core\Entity\EntityTypeManagerInterface
* The mocked entity type manager.
*/
protected function getEntityTypeManager() {
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
return $entity_type_manager
->reveal();
}
/**
* Sets up content moderation and entity manager mocking.
*
* @param string $bundle
* The bundle ID.
* @param string|null $workflow
* The workflow ID. If nul no workflow information is added to the bundle.
*
* @return \Drupal\Core\Entity\EntityTypeManagerInterface
* The mocked entity type manager.
*/
public function setupModerationBundleInfo($bundle, $workflow = NULL) {
$bundle_info_array = [];
if ($workflow) {
$bundle_info_array['workflow'] = $workflow;
}
$bundle_info = $this
->prophesize(EntityTypeBundleInfoInterface::class);
$bundle_info
->getBundleInfo("test_entity_type")
->willReturn([
$bundle => $bundle_info_array,
]);
$bundle_info
->getBundleInfo("unmoderated_test_type")
->willReturn([
$bundle => [],
]);
return $bundle_info
->reveal();
}
/**
* @covers ::isModeratedEntityType
*/
public function testIsModeratedEntityType() {
$moderation_information = new ModerationInformation($this
->getEntityTypeManager(), $this
->setupModerationBundleInfo('test_bundle', 'workflow'));
$moderated_entity_type = $this
->prophesize(EntityTypeInterface::class);
$moderated_entity_type
->id()
->willReturn('test_entity_type');
$unmoderated_entity_type = $this
->prophesize(EntityTypeInterface::class);
$unmoderated_entity_type
->id()
->willReturn('unmoderated_test_type');
$this
->assertTrue($moderation_information
->isModeratedEntityType($moderated_entity_type
->reveal()));
$this
->assertFalse($moderation_information
->isModeratedEntityType($unmoderated_entity_type
->reveal()));
}
/**
* @dataProvider providerWorkflow
* @covers ::isModeratedEntity
*/
public function testIsModeratedEntity($workflow, $expected) {
$moderation_information = new ModerationInformation($this
->getEntityTypeManager(), $this
->setupModerationBundleInfo('test_bundle', $workflow));
$entity_type = new ContentEntityType([
'id' => 'test_entity_type',
'bundle_entity_type' => 'entity_test_bundle',
'handlers' => [
'moderation' => ModerationHandler::class,
],
]);
$entity = $this
->prophesize(ContentEntityInterface::class);
$entity
->getEntityType()
->willReturn($entity_type);
$entity
->bundle()
->willReturn('test_bundle');
$this
->assertEquals($expected, $moderation_information
->isModeratedEntity($entity
->reveal()));
}
/**
* @dataProvider providerWorkflow
* @covers ::getWorkflowForEntity
*/
public function testGetWorkflowForEntity($workflow) {
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
if ($workflow) {
$workflow_entity = $this
->prophesize(WorkflowInterface::class)
->reveal();
$workflow_storage = $this
->prophesize(EntityStorageInterface::class);
$workflow_storage
->load('workflow')
->willReturn($workflow_entity)
->shouldBeCalled();
$entity_type_manager
->getStorage('workflow')
->willReturn($workflow_storage
->reveal());
}
else {
$workflow_entity = NULL;
}
$moderation_information = new ModerationInformation($entity_type_manager
->reveal(), $this
->setupModerationBundleInfo('test_bundle', $workflow));
$entity = $this
->prophesize(ContentEntityInterface::class);
$entity
->getEntityTypeId()
->willReturn('test_entity_type');
$entity
->bundle()
->willReturn('test_bundle');
$this
->assertEquals($workflow_entity, $moderation_information
->getWorkflowForEntity($entity
->reveal()));
}
/**
* @dataProvider providerWorkflow
* @covers ::shouldModerateEntitiesOfBundle
*/
public function testShouldModerateEntities($workflow, $expected) {
$entity_type = new ContentEntityType([
'id' => 'test_entity_type',
'bundle_entity_type' => 'entity_test_bundle',
'handlers' => [
'moderation' => ModerationHandler::class,
],
]);
$moderation_information = new ModerationInformation($this
->getEntityTypeManager(), $this
->setupModerationBundleInfo('test_bundle', $workflow));
$this
->assertEquals($expected, $moderation_information
->shouldModerateEntitiesOfBundle($entity_type, 'test_bundle'));
}
/**
* Data provider for several tests.
*/
public function providerWorkflow() {
return [
[
NULL,
FALSE,
],
[
'workflow',
TRUE,
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ModerationInformationTest:: |
protected | function | Returns a mock Entity Type Manager. | |
ModerationInformationTest:: |
protected | function | Builds a mock user. | |
ModerationInformationTest:: |
public | function | Data provider for several tests. | |
ModerationInformationTest:: |
public | function | Sets up content moderation and entity manager mocking. | |
ModerationInformationTest:: |
public | function | @dataProvider providerWorkflow @covers ::getWorkflowForEntity | |
ModerationInformationTest:: |
public | function | @dataProvider providerWorkflow @covers ::isModeratedEntity | |
ModerationInformationTest:: |
public | function | @covers ::isModeratedEntityType | |
ModerationInformationTest:: |
public | function | @dataProvider providerWorkflow @covers ::shouldModerateEntitiesOfBundle | |
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. | |
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 |