You are here

class RequestSubscriberTest in CRM Core 8

Same name and namespace in other branches
  1. 8.3 modules/crm_core_user_sync/tests/src/Unit/RequestSubscriberTest.php \Drupal\Tests\crm_core_user_sync\Unit\RequestSubscriberTest

Test description.

@group crm_core_user_sync

Hierarchy

Expanded class hierarchy of RequestSubscriberTest

File

modules/crm_core_user_sync/tests/src/Unit/RequestSubscriberTest.php, line 24

Namespace

Drupal\Tests\crm_core_user_sync\Unit
View source
class RequestSubscriberTest extends UnitTestCase {

  /**
   * Tests nothing happens for Anonymous.
   */
  public function testRequestSubscriberAnonymous() {
    $current_user = $this
      ->createMock(AccountProxyInterface::class);
    $current_user
      ->expects($this
      ->once())
      ->method('isAuthenticated')
      ->willReturn(FALSE);
    $configFactory = $this
      ->createMock(ConfigFactoryInterface::class);
    $configFactory
      ->expects($this
      ->never())
      ->method('get');
    $relationService = $this
      ->createMock(CrmCoreUserSyncRelationInterface::class);
    $entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $entityTypeManager
      ->expects($this
      ->never())
      ->method('getStorage');
    $kernel = $this
      ->prophesize(HttpKernelInterface::class);
    $request = Request::create('/', 'GET');
    $subscriber = new RequestSubscriber($current_user, $configFactory, $relationService, $entityTypeManager);
    $event = new GetResponseEvent($kernel
      ->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
    $subscriber
      ->onKernelRequest($event);

    // Nothing to assert as we should exit immediately. Current user expectation
    // will fail the test if something...
  }

  /**
   * Tests nothing loaded for user without related contact.
   */
  public function testRequestSubscriberAuthenticatedWithoutContact() {
    $current_user = $this
      ->createMock(AccountProxyInterface::class);
    $current_user
      ->expects($this
      ->once())
      ->method('isAuthenticated')
      ->willReturn(TRUE);
    $current_user
      ->expects($this
      ->once())
      ->method('id')
      ->willReturn('101');
    $config = $this
      ->getMockBuilder(ImmutableConfig::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->once())
      ->method('get')
      ->with('contact_load')
      ->willReturn(TRUE);
    $config_name = 'crm_core_user_sync.settings';
    $configFactory = $this
      ->createMock(ConfigFactoryInterface::class);
    $configFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with($config_name)
      ->willReturn($config);
    $relationService = $this
      ->createMock(CrmCoreUserSyncRelationInterface::class);
    $relationService
      ->expects($this
      ->once())
      ->method('getUserIndividualId')
      ->willReturn(FALSE);
    $entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $entityTypeManager
      ->expects($this
      ->never())
      ->method('getStorage');
    $kernel = $this
      ->prophesize(HttpKernelInterface::class);
    $request = Request::create('/', 'GET');
    $subscriber = new RequestSubscriber($current_user, $configFactory, $relationService, $entityTypeManager);
    $event = new GetResponseEvent($kernel
      ->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
    $subscriber
      ->onKernelRequest($event);

    // Nothing to assert. Entity type manager expectations will the test.
  }

  /**
   * Tests contact loaded for the user with related contact.
   */
  public function testRequestSubscriberAuthenticatedWithContact() {
    $current_user = $this
      ->createMock(AccountProxyInterface::class);
    $current_user
      ->expects($this
      ->once())
      ->method('isAuthenticated')
      ->willReturn(TRUE);
    $current_user
      ->expects($this
      ->once())
      ->method('id')
      ->willReturn('101');
    $account = $this
      ->createMock(AccountInterface::class);
    $current_user
      ->expects($this
      ->once())
      ->method('getAccount')
      ->willReturn($account);
    $current_user
      ->expects($this
      ->at(0))
      ->method('setAccount')
      ->willReturnReference($account);
    $config = $this
      ->getMockBuilder(ImmutableConfig::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->once())
      ->method('get')
      ->with('contact_load')
      ->willReturn(TRUE);
    $config_name = 'crm_core_user_sync.settings';
    $configFactory = $this
      ->createMock(ConfigFactoryInterface::class);
    $configFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with($config_name)
      ->willReturn($config);
    $individualId = 101;
    $individual = $this
      ->createMock(IndividualInterface::class);
    $relationService = $this
      ->createMock(CrmCoreUserSyncRelationInterface::class);
    $relationService
      ->expects($this
      ->once())
      ->method('getUserIndividualId')
      ->willReturn($individualId);
    $entityStorage = $this
      ->createMock(EntityStorageInterface::class);
    $entityStorage
      ->expects($this
      ->once())
      ->method('load')
      ->with($individualId)
      ->willReturn($individual);
    $entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $entityTypeManager
      ->expects($this
      ->once())
      ->method('getStorage')
      ->with('crm_core_individual')
      ->willReturn($entityStorage);
    $kernel = $this
      ->prophesize(HttpKernelInterface::class);
    $request = Request::create('/', 'GET');
    $subscriber = new RequestSubscriber($current_user, $configFactory, $relationService, $entityTypeManager);
    $event = new GetResponseEvent($kernel
      ->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
    $subscriber
      ->onKernelRequest($event);
    $this
      ->assertEquals($individual, $account->crm_core['contact'], 'Related contact was loaded');
  }

}

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.
RequestSubscriberTest::testRequestSubscriberAnonymous public function Tests nothing happens for Anonymous.
RequestSubscriberTest::testRequestSubscriberAuthenticatedWithContact public function Tests contact loaded for the user with related contact.
RequestSubscriberTest::testRequestSubscriberAuthenticatedWithoutContact public function Tests nothing loaded for user without related contact.
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