You are here

class CrmCoreUserSyncRelationRulesTest in CRM Core 8.3

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

Test CrmCoreUserSyncRelationRules service.

@property \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationRules rulesService @group crm_core_user_sync @coversDefaultClass \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationRules

Hierarchy

Expanded class hierarchy of CrmCoreUserSyncRelationRulesTest

File

modules/crm_core_user_sync/tests/src/Unit/CrmCoreUserSyncRelationRulesTest.php, line 19

Namespace

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $rules = [];
    $rules[] = [
      'role' => 'customer',
      'contact_type' => 'customer',
      'enabled' => TRUE,
      'weight' => 1,
    ];
    $rules[] = [
      'role' => 'authenticated',
      'contact_type' => 'individual',
      'enabled' => TRUE,
      'weight' => 10,
    ];
    $config = $this
      ->getMockBuilder(ImmutableConfig::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->once())
      ->method('get')
      ->with('rules')
      ->willReturn($rules);
    $config_name = 'crm_core_user_sync.settings';
    $configFactory = $this
      ->createMock(ConfigFactoryInterface::class);
    $configFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with($config_name)
      ->willReturn($config);
    $this->rulesService = new CrmCoreUserSyncRelationRules($configFactory, $config_name);
  }

  /**
   * Tests CrmCoreUserSyncRelationRules service.
   */
  public function testCrmCoreUserSyncRelationRulesService() {
    $account_authenticated = $this
      ->createMock(UserInterface::class);
    $account_authenticated
      ->expects($this
      ->any())
      ->method('hasRole')
      ->willReturnMap([
      [
        'authenticated',
        TRUE,
      ],
      [
        'customer',
        FALSE,
      ],
    ]);
    $account_customer = $this
      ->createMock(UserInterface::class);
    $account_customer
      ->expects($this
      ->any())
      ->method('hasRole')
      ->willReturnMap([
      [
        'authenticated',
        FALSE,
      ],
      [
        'customer',
        TRUE,
      ],
    ]);
    $contact_individual = $this
      ->createMock(IndividualInterface::class);
    $contact_individual
      ->expects($this
      ->any())
      ->method('bundle')
      ->willReturn('individual');
    $contact_customer = $this
      ->createMock(IndividualInterface::class);
    $contact_customer
      ->expects($this
      ->any())
      ->method('bundle')
      ->willReturn('customer');
    $this
      ->assertFalse($this->rulesService
      ->valid($account_customer, $contact_individual), 'Individual contact cannot be related to customer user.');
    $this
      ->assertFalse($this->rulesService
      ->valid($account_authenticated, $contact_customer), 'Customer contact can be related to authenticated user.');
    $this
      ->assertTrue($this->rulesService
      ->valid($account_authenticated, $contact_individual), 'Individual contact can be related to authenticated user.');
    $this
      ->assertTrue($this->rulesService
      ->valid($account_customer, $contact_customer), 'Customer contact can be related to customer user.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CrmCoreUserSyncRelationRulesTest::setUp protected function Overrides UnitTestCase::setUp
CrmCoreUserSyncRelationRulesTest::testCrmCoreUserSyncRelationRulesService public function Tests CrmCoreUserSyncRelationRules 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.