1. 8.4.x core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php
  2. 8.2.x core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php
  3. 8.3.x core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php
  4. 8.5.x core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php

Namespace

Drupal\Tests\content_moderation\Unit

File

core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php
View source
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Unit;
  3. use Drupal\content_moderation\Entity\Handler\ModerationHandler;
  4. use Drupal\Core\Config\Entity\ConfigEntityInterface;
  5. use Drupal\Core\Entity\ContentEntityInterface;
  6. use Drupal\Core\Entity\ContentEntityType;
  7. use Drupal\Core\Entity\EntityStorageInterface;
  8. use Drupal\Core\Entity\EntityTypeManagerInterface;
  9. use Drupal\Core\Session\AccountInterface;
  10. use Drupal\content_moderation\ModerationInformation;
  11. /**
  12. * @coversDefaultClass \Drupal\content_moderation\ModerationInformation
  13. * @group content_moderation
  14. */
  15. class ModerationInformationTest extends \PHPUnit_Framework_TestCase {
  16. /**
  17. * Builds a mock user.
  18. *
  19. * @return AccountInterface
  20. * The mocked user.
  21. */
  22. protected function getUser() {
  23. return $this->prophesize(AccountInterface::class)->reveal();
  24. }
  25. /**
  26. * Returns a mock Entity Type Manager.
  27. *
  28. * @param \Drupal\Core\Entity\EntityStorageInterface $entity_bundle_storage
  29. * Entity bundle storage.
  30. *
  31. * @return EntityTypeManagerInterface
  32. * The mocked entity type manager.
  33. */
  34. protected function getEntityTypeManager(EntityStorageInterface $entity_bundle_storage) {
  35. $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
  36. $entity_type_manager->getStorage('entity_test_bundle')->willReturn($entity_bundle_storage);
  37. return $entity_type_manager->reveal();
  38. }
  39. /**
  40. * Sets up content moderation and entity manager mocking.
  41. *
  42. * @param bool $status
  43. * TRUE if content_moderation should be enabled, FALSE if not.
  44. *
  45. * @return \Drupal\Core\Entity\EntityTypeManagerInterface
  46. * The mocked entity type manager.
  47. */
  48. public function setupModerationEntityManager($status) {
  49. $bundle = $this->prophesize(ConfigEntityInterface::class);
  50. $bundle->getThirdPartySetting('content_moderation', 'enabled', FALSE)->willReturn($status);
  51. $entity_storage = $this->prophesize(EntityStorageInterface::class);
  52. $entity_storage->load('test_bundle')->willReturn($bundle->reveal());
  53. return $this->getEntityTypeManager($entity_storage->reveal());
  54. }
  55. /**
  56. * @dataProvider providerBoolean
  57. * @covers ::isModeratedEntity
  58. */
  59. public function testIsModeratedEntity($status) {
  60. $moderation_information = new ModerationInformation($this->setupModerationEntityManager($status), $this->getUser());
  61. $entity_type = new ContentEntityType([
  62. 'id' => 'test_entity_type',
  63. 'bundle_entity_type' => 'entity_test_bundle',
  64. 'handlers' => ['moderation' => ModerationHandler::class],
  65. ]);
  66. $entity = $this->prophesize(ContentEntityInterface::class);
  67. $entity->getEntityType()->willReturn($entity_type);
  68. $entity->bundle()->willReturn('test_bundle');
  69. $this->assertEquals($status, $moderation_information->isModeratedEntity($entity->reveal()));
  70. }
  71. /**
  72. * @covers ::isModeratedEntity
  73. */
  74. public function testIsModeratedEntityForNonBundleEntityType() {
  75. $entity_type = new ContentEntityType([
  76. 'id' => 'test_entity_type',
  77. ]);
  78. $entity = $this->prophesize(ContentEntityInterface::class);
  79. $entity->getEntityType()->willReturn($entity_type);
  80. $entity->bundle()->willReturn('test_entity_type');
  81. $entity_storage = $this->prophesize(EntityStorageInterface::class);
  82. $entity_type_manager = $this->getEntityTypeManager($entity_storage->reveal());
  83. $moderation_information = new ModerationInformation($entity_type_manager, $this->getUser());
  84. $this->assertEquals(FALSE, $moderation_information->isModeratedEntity($entity->reveal()));
  85. }
  86. /**
  87. * @dataProvider providerBoolean
  88. * @covers ::shouldModerateEntitiesOfBundle
  89. */
  90. public function testShouldModerateEntities($status) {
  91. $entity_type = new ContentEntityType([
  92. 'id' => 'test_entity_type',
  93. 'bundle_entity_type' => 'entity_test_bundle',
  94. 'handlers' => ['moderation' => ModerationHandler::class],
  95. ]);
  96. $moderation_information = new ModerationInformation($this->setupModerationEntityManager($status), $this->getUser());
  97. $this->assertEquals($status, $moderation_information->shouldModerateEntitiesOfBundle($entity_type, 'test_bundle'));
  98. }
  99. /**
  100. * Data provider for several tests.
  101. */
  102. public function providerBoolean() {
  103. return [
  104. [FALSE],
  105. [TRUE],
  106. ];
  107. }
  108. }

Classes

Namesort descending Description
ModerationInformationTest @coversDefaultClass \Drupal\content_moderation\ModerationInformation @group content_moderation