You are here

public function RequestSubscriberTest::testRequestSubscriberAuthenticatedWithContact 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::testRequestSubscriberAuthenticatedWithContact()

Tests contact loaded for the user with related contact.

File

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

Class

RequestSubscriberTest
Test description.

Namespace

Drupal\Tests\crm_core_user_sync\Unit

Code

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');
}