You are here

class ScheduledTransitionsEntityHooksUnitTest in Scheduled Transitions 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/ScheduledTransitionsEntityHooksUnitTest.php \Drupal\Tests\scheduled_transitions\Unit\ScheduledTransitionsEntityHooksUnitTest

Tests entity hooks.

@coversDefaultClass \Drupal\scheduled_transitions\ScheduledTransitionsEntityHooks @group scheduled_transitions

Hierarchy

Expanded class hierarchy of ScheduledTransitionsEntityHooksUnitTest

File

tests/src/Unit/ScheduledTransitionsEntityHooksUnitTest.php, line 29

Namespace

Drupal\Tests\scheduled_transitions\Unit
View source
class ScheduledTransitionsEntityHooksUnitTest extends UnitTestCase {

  /**
   * A test config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $testConfigFactory;

  /**
   * A test entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $testEntityTypeManager;

  /**
   * A test moderation information service.
   *
   * @var \Drupal\content_moderation\ModerationInformationInterface
   */
  protected $testModerationInformation;

  /**
   * ScheduledTransitionsEntityHooksUnitTest constructor.
   */
  public function __construct() {
    parent::__construct();
    $this->testConfigFactory = $this
      ->createMock(ConfigFactoryInterface::class);
    $this->testEntityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $this->testModerationInformation = $this
      ->createMock(ModerationInformationInterface::class);
    $cacheContextsManager = $this
      ->getMockBuilder(CacheContextsManager::class)
      ->disableOriginalConstructor()
      ->getMock();
    $cacheContextsManager
      ->method('assertValidTokens')
      ->willReturn(TRUE);
    $container = new ContainerBuilder();
    $container
      ->set('cache_contexts_manager', $cacheContextsManager);
    $this->testContainer = $container;
  }

  /**
   * Tests operation not handled by hook.
   *
   * @covers ::entityAccess
   */
  public function testEntityAccessUnhandled() : void {
    $entityHooks = new ScheduledTransitionsEntityHooks($this->testConfigFactory, $this->testEntityTypeManager, $this->testModerationInformation);
    $entity = $this
      ->createMock(EntityInterface::class);
    $operation = $this
      ->randomMachineName();
    $account = $this
      ->createMock(AccountInterface::class);
    $access = $entityHooks
      ->entityAccess($entity, $operation, $account);
    $this
      ->assertInstanceOf(AccessResultNeutral::class, $access);
  }

  /**
   * Tests view operation, and when user doesn't have permission.
   *
   * No permission returns neutral with correct cacheabiltiy.
   *
   * @covers ::entityAccess
   */
  public function testEntityAccessViewOperationNoPermission() : void {
    \Drupal::setContainer($this->testContainer);
    $entityHooks = new ScheduledTransitionsEntityHooks($this->testConfigFactory, $this->testEntityTypeManager, $this->testModerationInformation);
    $entity = $this
      ->createMock(EntityInterface::class);
    $entity
      ->expects($this
      ->once())
      ->method('getEntityTypeId')
      ->willReturn('foo');
    $entity
      ->expects($this
      ->once())
      ->method('bundle')
      ->willReturn('bar');
    $operation = ScheduledTransitionsPermissions::ENTITY_OPERATION_VIEW_TRANSITIONS;
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->once())
      ->method('hasPermission')
      ->with('view scheduled transitions foo bar')
      ->willReturn(FALSE);

    /** @var \Drupal\Core\Access\AccessResultForbidden $access */
    $access = $entityHooks
      ->entityAccess($entity, $operation, $account);
    $this
      ->assertInstanceOf(AccessResultForbidden::class, $access);
    $this
      ->assertEquals([
      'user.permissions',
    ], $access
      ->getCacheContexts());
    $this
      ->assertEquals("The 'view scheduled transitions foo bar' permission is required.", $access
      ->getReason());
  }

  /**
   * Tests add operation, and when user doesn't have permission.
   *
   * No permission returns neutral with correct cacheabiltiy.
   *
   * @covers ::entityAccess
   */
  public function testEntityAccessAddOperationNoPermission() : void {
    \Drupal::setContainer($this->testContainer);
    $entityHooks = new ScheduledTransitionsEntityHooks($this->testConfigFactory, $this->testEntityTypeManager, $this->testModerationInformation);
    $entity = $this
      ->createMock(EntityInterface::class);
    $entity
      ->expects($this
      ->once())
      ->method('getEntityTypeId')
      ->willReturn('foo');
    $entity
      ->expects($this
      ->once())
      ->method('bundle')
      ->willReturn('bar');
    $operation = ScheduledTransitionsPermissions::ENTITY_OPERATION_ADD_TRANSITION;
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->once())
      ->method('hasPermission')
      ->with('add scheduled transitions foo bar')
      ->willReturn(FALSE);

    /** @var \Drupal\Core\Access\AccessResultForbidden $access */
    $access = $entityHooks
      ->entityAccess($entity, $operation, $account);
    $this
      ->assertInstanceOf(AccessResultForbidden::class, $access);
    $this
      ->assertEquals([
      'user.permissions',
    ], $access
      ->getCacheContexts());
    $this
      ->assertEquals("The 'add scheduled transitions foo bar' permission is required.", $access
      ->getReason());
  }

  /**
   * Tests view operation, and when user has permission, but no mirroring.
   *
   * @covers ::entityAccess
   */
  public function testEntityAccessViewOperationHasPermissionNoMirroring() : void {
    \Drupal::setContainer($this->testContainer);
    $testConfig = $this
      ->createMock(ImmutableConfig::class);
    $testConfig
      ->expects($this
      ->once())
      ->method('get')
      ->with('mirror_operations.' . ScheduledTransitionsPermissions::ENTITY_OPERATION_VIEW_TRANSITIONS)
      ->willReturn(NULL);
    $this->testConfigFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with('scheduled_transitions.settings')
      ->willReturn($testConfig);
    $entityHooks = new ScheduledTransitionsEntityHooks($this->testConfigFactory, $this->testEntityTypeManager, $this->testModerationInformation);
    $entity = $this
      ->createMock(EntityInterface::class);
    $entity
      ->expects($this
      ->once())
      ->method('getEntityTypeId')
      ->willReturn('foo');
    $entity
      ->expects($this
      ->once())
      ->method('bundle')
      ->willReturn('bar');
    $operation = ScheduledTransitionsPermissions::ENTITY_OPERATION_VIEW_TRANSITIONS;
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->once())
      ->method('hasPermission')
      ->with('view scheduled transitions foo bar')
      ->willReturn(TRUE);
    $entity
      ->expects($this
      ->never())
      ->method('access');

    /** @var \Drupal\Core\Access\AccessResultForbidden $access */
    $access = $entityHooks
      ->entityAccess($entity, $operation, $account);
    $this
      ->assertInstanceOf(AccessResultNeutral::class, $access);
    $this
      ->assertEquals([
      'user.permissions',
    ], $access
      ->getCacheContexts());
    $this
      ->assertNull($access
      ->getReason());
    $this
      ->assertEquals([
      ScheduledTransitionsSettingsForm::SETTINGS_TAG,
    ], $access
      ->getCacheTags());
  }

  /**
   * Tests add operation, and when user has permission, but no mirroring.
   *
   * @covers ::entityAccess
   */
  public function testEntityAccessAddOperationHasPermissionNoMirroring() : void {
    \Drupal::setContainer($this->testContainer);
    $testConfig = $this
      ->createMock(ImmutableConfig::class);
    $testConfig
      ->expects($this
      ->once())
      ->method('get')
      ->with('mirror_operations.' . ScheduledTransitionsPermissions::ENTITY_OPERATION_ADD_TRANSITION)
      ->willReturn(NULL);
    $this->testConfigFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with('scheduled_transitions.settings')
      ->willReturn($testConfig);
    $entityHooks = new ScheduledTransitionsEntityHooks($this->testConfigFactory, $this->testEntityTypeManager, $this->testModerationInformation);
    $entity = $this
      ->createMock(EntityInterface::class);
    $entity
      ->expects($this
      ->once())
      ->method('getEntityTypeId')
      ->willReturn('foo');
    $entity
      ->expects($this
      ->once())
      ->method('bundle')
      ->willReturn('bar');
    $operation = ScheduledTransitionsPermissions::ENTITY_OPERATION_ADD_TRANSITION;
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->once())
      ->method('hasPermission')
      ->with('add scheduled transitions foo bar')
      ->willReturn(TRUE);
    $entity
      ->expects($this
      ->never())
      ->method('access');

    /** @var \Drupal\Core\Access\AccessResultForbidden $access */
    $access = $entityHooks
      ->entityAccess($entity, $operation, $account);
    $this
      ->assertInstanceOf(AccessResultNeutral::class, $access);
    $this
      ->assertEquals([
      'user.permissions',
    ], $access
      ->getCacheContexts());
    $this
      ->assertNull($access
      ->getReason());
    $this
      ->assertEquals([
      ScheduledTransitionsSettingsForm::SETTINGS_TAG,
    ], $access
      ->getCacheTags());
  }

  /**
   * Tests view operation, and when user has permission, but no mirroring.
   *
   * @covers ::entityAccess
   */
  public function testEntityAccessViewOperationHasPermissionWithMirroring() : void {
    \Drupal::setContainer($this->testContainer);
    $testConfig = $this
      ->createMock(ImmutableConfig::class);
    $testConfig
      ->expects($this
      ->once())
      ->method('get')
      ->with('mirror_operations.' . ScheduledTransitionsPermissions::ENTITY_OPERATION_VIEW_TRANSITIONS)
      ->willReturn('testop');
    $this->testConfigFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with('scheduled_transitions.settings')
      ->willReturn($testConfig);
    $entityHooks = new ScheduledTransitionsEntityHooks($this->testConfigFactory, $this->testEntityTypeManager, $this->testModerationInformation);
    $entity = $this
      ->createMock(EntityInterface::class);
    $entity
      ->expects($this
      ->once())
      ->method('getEntityTypeId')
      ->willReturn('foo');
    $entity
      ->expects($this
      ->once())
      ->method('bundle')
      ->willReturn('bar');
    $operation = ScheduledTransitionsPermissions::ENTITY_OPERATION_VIEW_TRANSITIONS;
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->once())
      ->method('hasPermission')
      ->with('view scheduled transitions foo bar')
      ->willReturn(TRUE);
    $entity
      ->expects($this
      ->once())
      ->method('access')
      ->with('testop', $account, TRUE)
      ->willReturn(AccessResult::forbidden('no can do'));

    /** @var \Drupal\Core\Access\AccessResultForbidden $access */
    $access = $entityHooks
      ->entityAccess($entity, $operation, $account);
    $this
      ->assertInstanceOf(AccessResultForbidden::class, $access);
    $this
      ->assertEquals([
      'user.permissions',
    ], $access
      ->getCacheContexts());
    $this
      ->assertEquals('no can do', $access
      ->getReason());
    $this
      ->assertEquals([
      ScheduledTransitionsSettingsForm::SETTINGS_TAG,
    ], $access
      ->getCacheTags());
  }

  /**
   * Tests add operation, and when user has permission, but no mirroring.
   *
   * @covers ::entityAccess
   */
  public function testEntityAccessAddOperationHasPermissionWithMirroring() : void {
    \Drupal::setContainer($this->testContainer);
    $testConfig = $this
      ->createMock(ImmutableConfig::class);
    $testConfig
      ->expects($this
      ->once())
      ->method('get')
      ->with('mirror_operations.' . ScheduledTransitionsPermissions::ENTITY_OPERATION_ADD_TRANSITION)
      ->willReturn('testop');
    $this->testConfigFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with('scheduled_transitions.settings')
      ->willReturn($testConfig);
    $entityHooks = new ScheduledTransitionsEntityHooks($this->testConfigFactory, $this->testEntityTypeManager, $this->testModerationInformation);
    $entity = $this
      ->createMock(EntityInterface::class);
    $entity
      ->expects($this
      ->once())
      ->method('getEntityTypeId')
      ->willReturn('foo');
    $entity
      ->expects($this
      ->once())
      ->method('bundle')
      ->willReturn('bar');
    $operation = ScheduledTransitionsPermissions::ENTITY_OPERATION_ADD_TRANSITION;
    $account = $this
      ->createMock(AccountInterface::class);
    $account
      ->expects($this
      ->once())
      ->method('hasPermission')
      ->with('add scheduled transitions foo bar')
      ->willReturn(TRUE);
    $entity
      ->expects($this
      ->once())
      ->method('access')
      ->with('testop', $account, TRUE)
      ->willReturn(AccessResult::forbidden('no can do'));

    /** @var \Drupal\Core\Access\AccessResultForbidden $access */
    $access = $entityHooks
      ->entityAccess($entity, $operation, $account);
    $this
      ->assertInstanceOf(AccessResultForbidden::class, $access);
    $this
      ->assertEquals([
      'user.permissions',
    ], $access
      ->getCacheContexts());
    $this
      ->assertEquals('no can do', $access
      ->getReason());
    $this
      ->assertEquals([
      ScheduledTransitionsSettingsForm::SETTINGS_TAG,
    ], $access
      ->getCacheTags());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
ScheduledTransitionsEntityHooksUnitTest::$testConfigFactory protected property A test config factory.
ScheduledTransitionsEntityHooksUnitTest::$testEntityTypeManager protected property A test entity type manager service.
ScheduledTransitionsEntityHooksUnitTest::$testModerationInformation protected property A test moderation information service.
ScheduledTransitionsEntityHooksUnitTest::testEntityAccessAddOperationHasPermissionNoMirroring public function Tests add operation, and when user has permission, but no mirroring.
ScheduledTransitionsEntityHooksUnitTest::testEntityAccessAddOperationHasPermissionWithMirroring public function Tests add operation, and when user has permission, but no mirroring.
ScheduledTransitionsEntityHooksUnitTest::testEntityAccessAddOperationNoPermission public function Tests add operation, and when user doesn't have permission.
ScheduledTransitionsEntityHooksUnitTest::testEntityAccessUnhandled public function Tests operation not handled by hook.
ScheduledTransitionsEntityHooksUnitTest::testEntityAccessViewOperationHasPermissionNoMirroring public function Tests view operation, and when user has permission, but no mirroring.
ScheduledTransitionsEntityHooksUnitTest::testEntityAccessViewOperationHasPermissionWithMirroring public function Tests view operation, and when user has permission, but no mirroring.
ScheduledTransitionsEntityHooksUnitTest::testEntityAccessViewOperationNoPermission public function Tests view operation, and when user doesn't have permission.
ScheduledTransitionsEntityHooksUnitTest::__construct public function ScheduledTransitionsEntityHooksUnitTest constructor.
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