X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2FCRM%2FContact%2FImport%2FParser%2FContactTest.php;h=c6229cd2ab2bdd7c917e34f683e400619f0e5630;hb=4b3ef37139a43ea150b8762710bfbd44f3b8d993;hp=7a0ac492cb611c169816bee725629fcdf02de6af;hpb=415a9951148804ba9a43d952483171d1e6a6cce8;p=civicrm-core.git diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 7a0ac492cb..c6229cd2ab 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -14,6 +14,8 @@ * File for the CRM_Contact_Imports_Parser_ContactTest class. */ +use Civi\Api4\UserJob; + /** * Test contact import parser. * @@ -36,7 +38,7 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { * @throws \CRM_Core_Exception */ public function tearDown(): void { - $this->quickCleanup(['civicrm_address', 'civicrm_phone', 'civicrm_email'], TRUE); + $this->quickCleanup(['civicrm_address', 'civicrm_phone', 'civicrm_email', 'civicrm_user_job'], TRUE); parent::tearDown(); } @@ -57,7 +59,7 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { $fields = array_keys($contactImportValues); $values = array_values($contactImportValues); $parser = new CRM_Contact_Import_Parser_Contact($fields, []); - $parser->_contactType = 'Individual'; + $parser->setUserJobID($this->getUserJobID()); $parser->init(); $this->mapRelationshipFields($fields, $parser->getAllFields()); @@ -68,22 +70,21 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { ], [ NULL, NULL, - "Organization", + 'Organization', ], [ NULL, NULL, - "organization_name", + 'organization_name', ], [], [], [], [], []); - - $parser->_contactType = 'Individual'; + $parser->setUserJobID($this->getUserJobID()); $parser->_onDuplicate = CRM_Import_Parser::DUPLICATE_UPDATE; $parser->init(); $this->assertEquals(CRM_Import_Parser::VALID, $parser->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values), 'Return code from parser import was not as expected'); $this->callAPISuccess("Contact", "get", [ - "first_name" => "Alok", - "last_name" => "Patel", - "organization_name" => "Agileware", + 'first_name' => 'Alok', + 'last_name' => 'Patel', + 'organization_name' => 'Agileware', ]); } @@ -451,6 +452,7 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { * * There is an expectation that you can import by label here. * + * @throws \API_Exception * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ @@ -463,9 +465,11 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { ['name' => 'prefix_id', 'column_number' => 3], ['name' => 'suffix_id', 'column_number' => 4], ]; + $processor = new CRM_Import_ImportProcessor(); $processor->setMappingFields($mapping); $processor->setContactType('Individual'); + $processor->setUserJobID($this->getUserJobID()); $importer = $processor->getImporterObject(); $contactValues = [ @@ -597,12 +601,13 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { /** * Test importing 2 phones of different types. * + * @throws \API_Exception * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ - public function testImportTwoPhonesDifferentTypes() { + public function testImportTwoPhonesDifferentTypes(): void { $processor = new CRM_Import_ImportProcessor(); - $processor->setContactType('Individual'); + $processor->setUserJobID($this->getUserJobID()); $processor->setMappingFields( [ ['name' => 'first_name'], @@ -856,11 +861,13 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { * Ensure we can import multiple preferred_communication_methods, single * gender, and single preferred language using both labels and values. * - * @throws \CRM_Core_Exception @throws \CiviCRM_API3_Exception + * @throws \API_Exception + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ - public function testImportFieldsWithVariousOptions() { + public function testImportFieldsWithVariousOptions(): void { $processor = new CRM_Import_ImportProcessor(); - $processor->setContactType('Individual'); + $processor->setUserJobID($this->getUserJobID()); $processor->setMappingFields( [ ['name' => 'first_name'], @@ -871,7 +878,7 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { ] ); $importer = $processor->getImporterObject(); - $fields = ['Ima', 'Texter', "SMS,Phone", "Female", "Danish"]; + $fields = ['Ima', 'Texter', 'SMS,Phone', 'Female', 'Danish']; $importer->import(CRM_Import_Parser::DUPLICATE_NOCHECK, $fields); $contact = $this->callAPISuccessGetSingle('Contact', ['last_name' => 'Texter']); @@ -913,7 +920,7 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { } $values = array_values($originalValues); $parser = new CRM_Contact_Import_Parser_Contact($fields, $mapperLocType); - $parser->_contactType = 'Individual'; + $parser->setUserJobID($this->getUserJobID()); $parser->_dedupeRuleGroupID = $ruleGroupId; $parser->_onDuplicate = $onDuplicateAction; $parser->init(); @@ -933,6 +940,93 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { } } + /** + * Test mapping fields within the Parser class. + * + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + public function testMapFields(): void { + $parser = new CRM_Contact_Import_Parser_Contact( + // Array of field names + ['first_name', 'phone', NULL, 'im', NULL], + // Array of location types, ie columns 2 & 4 have types. + [NULL, 1, NULL, 1, NULL], + // Array of phone types + [NULL, 1, NULL, NULL, NULL], + // Array of im provider types + [NULL, NULL, NULL, 1, NULL], + // Array of filled in relationship values. + [NULL, NULL, '5_a_b', NULL, '5_a_b'], + // Array of the contact type to map to - note this can be determined from ^^ + [NULL, NULL, 'Organization', NULL, 'Organization'], + // Related contact field names + [NULL, NULL, 'url', NULL, 'phone'], + // Related contact location types + [NULL, NULL, NULL, NULL, 1], + // Related contact phone types + [NULL, NULL, NULL, NULL, 1], + // Related contact im provider types + [NULL, NULL, NULL, NULL, NULL], + // Website types + [NULL, NULL, NULL, NULL, NULL], + // Related contact website types + [NULL, NULL, 1, NULL, NULL] + ); + $parser->setUserJobID($this->getUserJobID([ + 'mapper' => [ + ['first_name'], + ['phone', 1, 1], + ['5_a_b', 'url', 1], + ['im', 1, 1], + ['5_a_b', 'phone', 1, 1], + ], + ])); + $parser->init(); + $params = $parser->getMappedRow( + ['Bob', '123', 'https://example.org', 'my-handle', '456'] + ); + $this->assertEquals([ + 'first_name' => 'Bob', + 'phone' => [ + [ + 'phone' => '123', + 'location_type_id' => 1, + 'phone_type_id' => 1, + ], + ], + '5_a_b' => [ + 'contact_type' => 'Organization', + 'url' => + [ + + [ + 'url' => 'https://example.org', + 'website_type_id' => 1, + ], + ], + 'phone' => + [ + [ + 'phone' => '456', + 'location_type_id' => 1, + 'phone_type_id' => 1, + ], + ], + ], + 'im' => + [ + + [ + 'im' => 'my-handle', + 'location_type_id' => 1, + 'provider_id' => 1, + ], + ], + 'contact_type' => 'Individual', + ], $params); + } + /** * Set up the underlying contact. * @@ -954,4 +1048,22 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { return [$originalValues, $result]; } + /** + * @return mixed + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + protected function getUserJobID($submittedValues = []) { + return UserJob::create()->setValues([ + 'metadata' => [ + 'submitted_values' => array_merge([ + 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL, + 'contactSubType' => '', + ], $submittedValues), + ], + 'status_id:name' => 'draft', + 'type_id:name' => 'contact_import', + ])->execute()->first()['id']; + } + }