public function testResolvePersonOrAccountByEmailAddress() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; $user = UserTestHelper::createBasicUser('joseph'); $anotherUser = UserTestHelper::createBasicUser('josephine'); $emailAddress = '*****@*****.**'; // There are no users in system with the email. Yii::app()->user->userModel = $user; $personOrAccount = EmailArchivingUtil::resolvePersonOrAccountByEmailAddress($emailAddress, false, false, false); $this->assertNull($personOrAccount); // There is user is system with the email. Yii::app()->user->userModel = $super; $anotherUser->primaryEmail->emailAddress = $emailAddress; $this->assertTrue($anotherUser->save()); $personOrAccount = EmailArchivingUtil::resolvePersonOrAccountByEmailAddress($emailAddress, false, false, false); $this->assertEquals($anotherUser->id, $personOrAccount->id); $this->assertTrue($personOrAccount instanceof User); // Now test email with accounts. // User can access accounts, but there are no accounts in system with the email. $anotherUser->primaryEmail->emailAddress = '*****@*****.**'; $this->assertTrue($anotherUser->save()); Yii::app()->user->userModel = $user; $personOrAccount = EmailArchivingUtil::resolvePersonOrAccountByEmailAddress($emailAddress, false, false, true); $this->assertNull($personOrAccount); // User can access accounts, but there are no accounts in system with the email. // But there is user is system with the email Yii::app()->user->userModel = $super; $anotherUser->primaryEmail->emailAddress = $emailAddress; $this->assertTrue($anotherUser->save()); Yii::app()->user->userModel = $user; $personOrAccount = EmailArchivingUtil::resolvePersonOrAccountByEmailAddress($emailAddress, false, false, true); $this->assertEquals($anotherUser->id, $personOrAccount->id); $this->assertTrue($personOrAccount instanceof User); // User can access accounts, and there is account in system with the email. // But owner of email is super users, so it shouldn't return account Yii::app()->user->userModel = $super; $anotherUser->primaryEmail->emailAddress = '*****@*****.**'; $this->assertTrue($anotherUser->save()); Yii::app()->user->userModel = $super; $email = new Email(); $email->emailAddress = $emailAddress; $email2 = new Email(); $email2->emailAddress = '*****@*****.**'; $account = new Account(); $account->owner = $super; $account->name = 'Test Account'; $account->primaryEmail = $email; $account->secondaryEmail = $email2; $this->assertTrue($account->save()); Yii::app()->user->userModel = $user; $personOrAccount = EmailArchivingUtil::resolvePersonOrAccountByEmailAddress($emailAddress, false, false, true); $this->assertNull($personOrAccount); Yii::app()->user->userModel = $super; $account->owner = $user; $this->assertTrue($account->save()); Yii::app()->user->userModel = $user; $personOrAccount = EmailArchivingUtil::resolvePersonOrAccountByEmailAddress($emailAddress, false, false, true); $this->assertEquals($account->id, $personOrAccount->id); $this->assertTrue($personOrAccount instanceof Account); // Now test with contacts/leads. Please note that we are not removing email address // from users and accounts, so if contact or lead exist with this email, they should be returned Yii::app()->user->userModel = $user; $personOrAccount = EmailArchivingUtil::resolvePersonOrAccountByEmailAddress($emailAddress, true, true, false); $this->assertNull($personOrAccount); // User can access contacts, but there are no contact in system with the email. // But there is user and account is system with the email Yii::app()->user->userModel = $super; $anotherUser->primaryEmail->emailAddress = $emailAddress; $this->assertTrue($anotherUser->save()); Yii::app()->user->userModel = $user; $personOrAccount = EmailArchivingUtil::resolvePersonOrAccountByEmailAddress($emailAddress, false, false, true); $this->assertEquals($account->id, $personOrAccount->id); $this->assertTrue($personOrAccount instanceof Account); // User can access contacts, and there is contact in system with the email. // But owner of email is super users, so it shouldn't return contact Yii::app()->user->userModel = $super; $this->assertTrue(ContactsModule::loadStartingData()); $this->assertEquals(6, count(ContactState::GetAll())); $contactStates = ContactState::getByName('Qualified'); $email = new Email(); $email->emailAddress = $emailAddress; $email2 = new Email(); $email2->emailAddress = '*****@*****.**'; $contact = new Contact(); $contact->state = $contactStates[0]; $contact->owner = $super; $contact->firstName = 'Super'; $contact->lastName = 'Man'; $contact->primaryEmail = $email; $contact->secondaryEmail = $email; $this->assertTrue($account->save()); $anotherUser->primaryEmail->emailAddress = '*****@*****.**'; $this->assertTrue($anotherUser->save()); Yii::app()->user->userModel = $user; $personOrAccount = EmailArchivingUtil::resolvePersonOrAccountByEmailAddress($emailAddress, true, true, false); $this->assertNull($personOrAccount); Yii::app()->user->userModel = $super; $contact->owner = $user; $this->assertTrue($contact->save()); $anotherUser->primaryEmail->emailAddress = $emailAddress; $this->assertTrue($anotherUser->save()); Yii::app()->user->userModel = $user; $personOrAccount = EmailArchivingUtil::resolvePersonOrAccountByEmailAddress($emailAddress, true, true, true); $this->assertEquals($contact->id, $personOrAccount->id); $this->assertTrue($personOrAccount instanceof Contact); }
/** * Create EmailMessageRecipient * @param array $recipientInfo * @param boolean $userCanAccessContacts * @param boolean $userCanAccessLeads * @param boolean $userCanAccessAccounts * @return EmailMessageRecipient */ protected function createEmailMessageRecipient($recipientInfo, $userCanAccessContacts, $userCanAccessLeads, $userCanAccessAccounts) { $recipient = new EmailMessageRecipient(); $recipient->toAddress = $recipientInfo['email']; $recipient->toName = $recipientInfo['name']; $recipient->type = $recipientInfo['type']; $personOrAccount = EmailArchivingUtil::resolvePersonOrAccountByEmailAddress($recipientInfo['email'], $userCanAccessContacts, $userCanAccessLeads, $userCanAccessAccounts); $recipient->personOrAccount = $personOrAccount; return $recipient; }