You are here

class LatestRevisionCheckTest in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/LatestRevisionCheckTest.php \Drupal\Tests\workbench_moderation\Unit\LatestRevisionCheckTest

@coversDefaultClass \Drupal\workbench_moderation\Access\LatestRevisionCheck @group workbench_moderation

Hierarchy

Expanded class hierarchy of LatestRevisionCheckTest

File

tests/src/Unit/LatestRevisionCheckTest.php, line 19

Namespace

Drupal\Tests\workbench_moderation\Unit
View source
class LatestRevisionCheckTest extends UnitTestCase {

  /**
   * Test the access check of the LatestRevisionCheck service.
   *
   * @param string $entity_class
   *   The class of the entity to mock.
   * @param string $entity_type
   *   The machine name of the entity to mock.
   * @param bool $has_forward
   *   Whether this entity should have a forward revision in the system.
   * @param string $result_class
   *   The AccessResult class that should result. One of AccessResultAllowed,
   *   AccessResultForbidden, AccessResultNeutral.
   *
   * @dataProvider accessSituationProvider
   */
  public function testLatestAccessPermissions($entity_class, $entity_type, $has_forward, $result_class) {

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this
      ->prophesize($entity_class);
    $entity
      ->getCacheContexts()
      ->willReturn([]);
    $entity
      ->getCacheTags()
      ->willReturn([]);
    $entity
      ->getCacheMaxAge()
      ->willReturn(0);

    /** @var \Drupal\workbench_moderation\ModerationInformationInterface $mod_info */
    $mod_info = $this
      ->prophesize(ModerationInformation::class);
    $mod_info
      ->hasForwardRevision($entity
      ->reveal())
      ->willReturn($has_forward);
    $route = $this
      ->prophesize(Route::class);
    $route
      ->getOption('_workbench_moderation_entity_type')
      ->willReturn($entity_type);
    $route_match = $this
      ->prophesize(RouteMatch::class);
    $route_match
      ->getParameter($entity_type)
      ->willReturn($entity
      ->reveal());
    $lrc = new LatestRevisionCheck($mod_info
      ->reveal());

    /** @var \Drupal\Core\Access\AccessResult $result */
    $result = $lrc
      ->access($route
      ->reveal(), $route_match
      ->reveal());
    $this
      ->assertInstanceOf($result_class, $result);
  }

  /**
   * Data provider for testLastAccessPermissions().
   *
   * @return array
   *   Array with node access and block content access.
   */
  public function accessSituationProvider() {
    return [
      [
        Node::class,
        'node',
        TRUE,
        AccessResultAllowed::class,
      ],
      [
        Node::class,
        'node',
        FALSE,
        AccessResultForbidden::class,
      ],
      [
        BlockContent::class,
        'block_content',
        TRUE,
        AccessResultAllowed::class,
      ],
      [
        BlockContent::class,
        'block_content',
        FALSE,
        AccessResultForbidden::class,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LatestRevisionCheckTest::accessSituationProvider public function Data provider for testLastAccessPermissions().
LatestRevisionCheckTest::testLatestAccessPermissions public function Test the access check of the LatestRevisionCheck service.
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.
UnitTestCase::setUp protected function 340