class ModerationInformationTest

Same name in this branch
  1. main core/modules/content_moderation/tests/src/Kernel/ModerationInformationTest.php \Drupal\Tests\content_moderation\Kernel\ModerationInformationTest
Same name and namespace in other branches
  1. 11.x core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php \Drupal\Tests\content_moderation\Unit\ModerationInformationTest
  2. 11.x core/modules/content_moderation/tests/src/Kernel/ModerationInformationTest.php \Drupal\Tests\content_moderation\Kernel\ModerationInformationTest
  3. 10 core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php \Drupal\Tests\content_moderation\Unit\ModerationInformationTest
  4. 10 core/modules/content_moderation/tests/src/Kernel/ModerationInformationTest.php \Drupal\Tests\content_moderation\Kernel\ModerationInformationTest
  5. 9 core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php \Drupal\Tests\content_moderation\Unit\ModerationInformationTest
  6. 9 core/modules/content_moderation/tests/src/Kernel/ModerationInformationTest.php \Drupal\Tests\content_moderation\Kernel\ModerationInformationTest
  7. 8.9.x core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php \Drupal\Tests\content_moderation\Unit\ModerationInformationTest
  8. 8.9.x core/modules/content_moderation/tests/src/Kernel/ModerationInformationTest.php \Drupal\Tests\content_moderation\Kernel\ModerationInformationTest

Tests Drupal\content_moderation\ModerationInformation.

Attributes

#[CoversClass(ModerationInformation::class)] #[Group('content_moderation')]

Hierarchy

Expanded class hierarchy of ModerationInformationTest

File

core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php, line 26

Namespace

Drupal\Tests\content_moderation\Unit
View 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);
    $entity_type_manager->getHandler(Argument::any(), 'moderation')
      ->willReturn(new ModerationHandler());
    return $entity_type_manager->reveal();
  }
  
  /**
   * Sets up content moderation and entity type bundle info 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();
  }
  
  /**
   * Tests is moderated entity type.
   */
  public function testIsModeratedEntityType() : void {
    $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()));
  }
  
  /**
   * Tests is moderated entity.
   */
  public function testIsModeratedEntity(?string $workflow, bool $expected) : void {
    $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->getEntityTypeId()
      ->willReturn($entity_type->id());
    $entity->bundle()
      ->willReturn('test_bundle');
    $this->assertEquals($expected, $moderation_information->isModeratedEntity($entity->reveal()));
  }
  
  /**
   * Tests get workflow for entity.
   */
  public function testGetWorkflowForEntity(?string $workflow, bool $expected) : void {
    $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()));
  }
  
  /**
   * Tests should moderate entities.
   *
   * @legacy-covers ::shouldModerateEntitiesOfBundle
   */
  public function testShouldModerateEntities(?string $workflow, bool $expected) : void {
    $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 static function providerWorkflow() : array {
    return [
      [
        NULL,
        FALSE,
      ],
      [
        'workflow',
        TRUE,
      ],
    ];
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution.
ExpectDeprecationTrait::expectDeprecation Deprecated public function Adds an expected deprecation.
ExpectDeprecationTrait::regularExpressionForFormatDescription private function
ModerationInformationTest::getEntityTypeManager protected function Returns a mock Entity Type Manager.
ModerationInformationTest::getUser protected function Builds a mock user.
ModerationInformationTest::providerWorkflow public static function Data provider for several tests.
ModerationInformationTest::setupModerationBundleInfo public function Sets up content moderation and entity type bundle info mocking.
ModerationInformationTest::testGetWorkflowForEntity public function Tests get workflow for entity.
ModerationInformationTest::testIsModeratedEntity public function Tests is moderated entity.
ModerationInformationTest::testIsModeratedEntityType public function Tests is moderated entity type.
ModerationInformationTest::testShouldModerateEntities public function Tests should moderate entities.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setUp protected function 366
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.