Commit 0dc860f4 authored by RoSk0's avatar RoSk0

Added CrmCoreUserSyncRelationRules service.

parent ffc2f728
<?php
namespace Drupal\crm_core_user_sync;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\crm_core_contact\IndividualInterface;
use Drupal\user\UserInterface;
/**
* CrmCoreUserSyncRelation service.
*/
class CrmCoreUserSyncRelationRules {
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
protected $rules;
/**
* Constructs a CrmCoreUserSyncRelationRules object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @param string $configName
* Name of the configuration object that stores rules.
*/
public function __construct(ConfigFactoryInterface $config_factory, $configName) {
$this->configFactory = $config_factory;
$this->configName = $configName;
}
/**
* Retrieves the individual contact id for specified user.
*
* @return int|null
* Individual id, if relation exists.
*/
protected function getRules() {
if ($this->rules === NULL) {
$rules = $this
->configFactory
->get($this->configName)
->get('rules');
uasort($rules, [$this, 'weightCmp']);
$this->rules = $rules;
}
return $this->rules;
}
/**
* Rules weight comparison function.
*/
protected function weightCmp(array $a, array $b) {
if ($a['weight'] == $b['weight']) {
return 0;
}
return ($a['weight'] < $b['weight']) ? -1 : 1;
}
/**
* Checks if provided contact can be linked to this account.
*
* @param \Drupal\user\UserInterface $account
* User account to check.
* @param \Drupal\crm_core_contact\IndividualInterface $contact
* Contact record to check.
*
* @return bool
* TRUE if the contact is valid.
*/
public function valid(UserInterface $account, IndividualInterface $contact) {
return $contact->bundle() === $this->getContactType($account);
}
/**
* Get contact type resolved from configured synchronization rules.
*
* @param \Drupal\user\UserInterface $account
* User account to check.
*
* @return string|false
* Intividual contact type (bundle) to be user for this user account.
*/
public function getContactType(UserInterface $account) {
foreach ($this->getRules() as $rule) {
if ($rule['enabled'] && $account->hasRole($rule['role'])) {
return $rule['contact_type'];
}
}
return FALSE;
}
}
<?php
namespace Drupal\Tests\crm_core_user_sync\Unit;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\crm_core_contact\Entity\Individual;
use Drupal\crm_core_contact\IndividualInterface;
use Drupal\crm_core_user_sync\CrmCoreUserSyncRelationRules;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
/**
* Test CrmCoreUserSyncRelationRules service.
*
* @property \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationRules rulesService
* @group crm_core_user_sync
* @coversDefaultClass \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationRules
*/
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->getMock(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->getMock(UserInterface::class);
$account_authenticated
->expects($this->any())
->method('hasRole')
->willReturnMap([['authenticated', TRUE], ['customer', FALSE]]);
$account_customer = $this->getMock(UserInterface::class);
$account_customer
->expects($this->any())
->method('hasRole')
->willReturnMap([['authenticated', FALSE], ['customer', TRUE]]);
$contact_individual = $this->getMock(IndividualInterface::class);
$contact_individual
->expects($this->any())
->method('bundle')
->willReturn('individual');
$contact_customer = $this->getMock(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.');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment