From 66107ff83ff9ac5c78c76860c3bc57ba755ef8a8 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 27 Aug 2022 18:06:15 +1200 Subject: [PATCH] Add tests for contact lookup --- .../Form/data/individual_valid_basic.csv | 2 + .../CRM/Contact/Import/Parser/ContactTest.php | 68 +++++++++++++++++++ .../Import/Parser/ContributionTest.php | 19 ++++++ 3 files changed, 89 insertions(+) create mode 100644 tests/phpunit/CRM/Contact/Import/Form/data/individual_valid_basic.csv diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/individual_valid_basic.csv b/tests/phpunit/CRM/Contact/Import/Form/data/individual_valid_basic.csv new file mode 100644 index 0000000000..fe871ff20b --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/individual_valid_basic.csv @@ -0,0 +1,2 @@ +Player First Name,Email,Source,Another email +Susie,mum@example.org,Import,bob@example.org diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index bbc4340d55..c2d5406601 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -561,6 +561,73 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { $this->callAPISuccessGetCount('IM', ['contact_id' => $id], 1); } + + /** + * Test whether importing a contact using email match will match a non-primary. + * + * @throws \CRM_Core_Exception + */ + public function testImportMatchNonPrimary(): void { + $anthony = $this->individualCreate(); + Email::create()->setValues([ + 'contact_id' => $anthony, + 'location_type_id:name' => 'Billing', + 'is_primary' => FALSE, + 'email' => 'mum@example.org', + ])->execute(); + $this->importCSV('individual_valid_basic.csv', [ + ['first_name'], + ['email'], + ['source'], + ['do_not_import'], + ], ['onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE]); + $contact = Contact::get() + ->addWhere('id', '=', $anthony) + ->execute() + ->first(); + $this->assertEquals('Import', $contact['source']); + } + + /** + * Test whether importing a contact using email match will match a non-primary. + * + * @throws \CRM_Core_Exception + */ + public function testImportMatchSpecifiedLocationToPrimary(): void { + $anthony = $this->individualCreate(['email' => 'mum@example.org']); + + $this->importCSV('individual_valid_basic.csv', [ + ['first_name'], + ['email', CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Email', 'location_type_id', 'Other')], + ['source'], + ['do_not_import'], + ], ['onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE]); + $contact = Contact::get() + ->addWhere('id', '=', $anthony) + ->execute() + ->first(); + $this->assertEquals('Import', $contact['source']); + + // Change the existing primary email to Bob & check that it will match the first + // of two emails. + Email::update() + ->addWhere('email', '=', 'mum@example.org') + ->addWhere('is_primary', '=', TRUE) + ->setValues(['email' => 'bob@example.org'])->execute(); + + $this->importCSV('individual_valid_basic.csv', [ + ['first_name'], + ['email', CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Email', 'location_type_id', 'Other')], + ['middle_name'], + ['email', CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Email', 'location_type_id', 'Work')], + ], ['onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE]); + $contact = Contact::get() + ->addWhere('id', '=', $anthony) + ->execute() + ->first(); + $this->assertEquals('Import', $contact['middle_name']); + } + /** * Test that the import parser adds the address to the primary location. * @@ -2064,6 +2131,7 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { 'onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE, 'groups' => [], ], $submittedValues); + /* @var CRM_Contact_Import_Form_DataSource $form */ $form = $this->getFormObject('CRM_Contact_Import_Form_DataSource', $submittedValues); $values = $_SESSION['_' . $form->controller->_name . '_container']['values']; diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php index 831adf6ce5..93c7ab29e8 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -6,6 +6,7 @@ use Civi\Api4\Contribution; use Civi\Api4\ContributionSoft; +use Civi\Api4\Email; use Civi\Api4\Note; use Civi\Api4\OptionValue; use Civi\Api4\UserJob; @@ -301,6 +302,24 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { $this->assertEquals('Call him back', $note['note']); } + /** + * Test whether importing a contribution using email match will match a non-primary. + * + * @throws \CRM_Core_Exception + */ + public function testImportMatchNonPrimary(): void { + $anthony = $this->individualCreate(); + Email::create()->setValues([ + 'contact_id' => $anthony, + 'location_type_id:name' => 'Billing', + 'is_primary' => FALSE, + 'email' => 'mum@example.com', + ])->execute(); + $this->importContributionsDotCSV(); + $contribution = Contribution::get()->execute()->first(); + $this->assertEquals($anthony, $contribution['contact_id']); + } + /** * @throws \CRM_Core_Exception */ -- 2.25.1