You are here

class ModerationDeriverTest in Workbench Moderation to Content Moderation 8.2

@coversDefaultClass \Drupal\wbm2cm\Plugin\Deriver\ModerationDeriver @group wbm2cm

Hierarchy

Expanded class hierarchy of ModerationDeriverTest

File

tests/src/Unit/Plugin/Deriver/ModerationDeriverTest.php, line 14

Namespace

Drupal\Tests\wbm2cm\Unit\Plugin\Deriver
View source
class ModerationDeriverTest extends UnitTestCase {

  /**
   * The mocked entity type manager service.
   *
   * @var EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
  }

  /**
   * @covers ::getDerivativeDefinitions
   *
   * @dataProvider getDerivativeDefinitionsProvider
   */
  public function testGetDerivativeDefinitions(array $definitions, $expected_count, $expected_derivatives) {
    foreach ($definitions as $entity_type_id => $flags) {
      $definitions[$entity_type_id] = $this
        ->mockEntityType($flags, $entity_type_id);
    }
    $this->entityTypeManager
      ->getDefinitions()
      ->willReturn($definitions);
    $plugin_definition = [
      'id' => $this
        ->randomMachineName(),
      'class' => 'It worked!',
      'source_module' => 'wbm2cm',
    ];
    $deriver = new ModerationDeriver($this->entityTypeManager
      ->reveal(), array_keys($definitions));
    $derivatives = $deriver
      ->getDerivativeDefinitions($plugin_definition);
    $this
      ->assertCount($expected_count, $derivatives);
    foreach ($expected_derivatives as $id) {
      $this
        ->assertEquals($plugin_definition, $derivatives[$id]);
    }
  }

  /**
   * Mocks an entity type definition based on flags from the data provider.
   *
   * @param bool[] $flags
   *   Boolean flags that determine the behavior of the mocked entity type. They
   *   represent revisionability and translatability, in that order.
   *
   * @return \Drupal\Core\Entity\ContentEntityTypeInterface
   *   The mocked entity type definition.
   */
  protected function mockEntityType(array $flags, $entity_type_id = NULL) {
    list($revisionable, $translatable) = $flags;
    $entity_type = $this
      ->prophesize(ContentEntityTypeInterface::class);
    $entity_type
      ->id()
      ->willReturn($entity_type_id ?: $this
      ->randomMachineName());
    $entity_type
      ->getProvider()
      ->willReturn('wbm2cm');
    $entity_type
      ->isRevisionable()
      ->willReturn((bool) $revisionable);
    $entity_type
      ->isTranslatable()
      ->willReturn((bool) $translatable);
    return $entity_type
      ->reveal();
  }

  /**
   * Data provider for testGetDerivativeDefinitions().
   *
   * @return array
   *   A set of test scenarios. Each is an array with the following items:
   *   - A set of arrays, each representing an entity type, and keyed by the
   *     entity type ID. Each array is a tuple of three boolean flags
   *     representing (in order) the revisionablity and translatability
   *     of the entity type. See mockEntityType() for more info.
   *   - How many plugin definitions the deriver is expected to return.
   *   - The derivative IDs that the deriver is expected to return.
   */
  public function getDerivativeDefinitionsProvider() {
    return [
      'revisionable and translatable' => [
        [
          'foo' => [
            TRUE,
            TRUE,
          ],
        ],
        1,
        [
          'foo',
        ],
      ],
      'translatable but not revisionable' => [
        [
          'foo' => [
            TRUE,
            TRUE,
          ],
          'bar' => [
            FALSE,
            TRUE,
          ],
        ],
        1,
        [
          'foo',
        ],
      ],
      'revisionable but not translatable' => [
        [
          'foo' => [
            TRUE,
            TRUE,
          ],
          'bar' => [
            TRUE,
            FALSE,
          ],
        ],
        1,
        [
          'foo',
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ModerationDeriverTest::$entityTypeManager protected property The mocked entity type manager service.
ModerationDeriverTest::getDerivativeDefinitionsProvider public function Data provider for testGetDerivativeDefinitions().
ModerationDeriverTest::mockEntityType protected function Mocks an entity type definition based on flags from the data provider.
ModerationDeriverTest::setUp protected function Overrides UnitTestCase::setUp
ModerationDeriverTest::testGetDerivativeDefinitions public function @covers ::getDerivativeDefinitions
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.