Commit 5861c3a9 authored by RoSk0's avatar RoSk0

Added test that relation service relate method is idempotent.

parent 5b4e350f
......@@ -126,6 +126,11 @@ class CrmCoreUserSyncRelation implements CrmCoreUserSyncRelationInterface {
}
if (empty($contact)) {
// Account already have related contact.
if ($this->getUserIndividualId($account->id())) {
return NULL;
}
// Get corresponding contact type.
$contact_type = $this->rules->getContactType($account);
......
......@@ -182,4 +182,25 @@ class CrmCoreUserSyncRelationTest extends EntityKernelTestBase {
$authenticated_relation_id = $this->relationService->getUserRelationId($account_authenticated->id());
$this->assertNotEmpty($authenticated_relation_id, 'Relation was created');
}
/**
* Tests that "relate" method is idempotent.
*
* @see \Drupal\crm_core_user_sync\CrmCoreUserSyncRelation::relate()
*/
public function testNoDuplicatedContactsCreated() {
$account_name = $this->randomString();
$account_authenticated = User::create([
'name' => $account_name,
]);
$account_authenticated->save();
$authenticated_individual_id = $this->relationService->getUserIndividualId($account_authenticated->id());
$this->assertNotEmpty($authenticated_individual_id, 'Related contact was created');
$this->relationService->relate($account_authenticated);
$contacts_with_name = $this->entityManager->getStorage('crm_core_individual')->getQuery()->condition('name.given', $account_name)->count()->execute();
$this->assertEquals(1, $contacts_with_name, 'There is only one contact was created');
}
}
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