From 4521630a5f9dc502ec8b47aa90f1db7a46d44214 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 10 Jun 2022 21:59:25 +1200 Subject: [PATCH] dev/core#3160 fix inability to import 'just contactid' and add to group --- CRM/Contact/Import/Parser/Contact.php | 2 +- CRM/Import/Parser.php | 9 ++++++- .../Import/Form/data/contact_id_only.csv | 2 ++ .../CRM/Contact/Import/Parser/ContactTest.php | 25 +++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/Import/Form/data/contact_id_only.csv diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 137d61d88a0e..172280834b66 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -194,7 +194,7 @@ public function import($values) { $extraFields['related_contact_matched']++; } } - $this->setImportStatus($rowNumber, $this->getStatus(CRM_Import_Parser::VALID), $this->getSuccessMessage(), $contactID, $extraFields); + $this->setImportStatus($rowNumber, $this->getStatus(CRM_Import_Parser::VALID), $this->getSuccessMessage(), $contactID, $extraFields, [$contactID]); return CRM_Import_Parser::VALID; } diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index 7ec35b7c9aa7..aace3236f148 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -1994,11 +1994,18 @@ protected function getSubtypes($contactType) { * Optional created entity ID * @param array $additionalFields * Additional fields to be tracked + * @param array $createdContactIDs * * @noinspection PhpDocMissingThrowsInspection * @noinspection PhpUnhandledExceptionInspection */ - protected function setImportStatus(int $id, string $status, string $message = '', ?int $entityID = NULL, $additionalFields = []): void { + protected function setImportStatus(int $id, string $status, string $message = '', ?int $entityID = NULL, $additionalFields = [], $createdContactIDs = []): void { + foreach ($createdContactIDs as $createdContactID) { + // Store any created contacts for post_actions like tag or add to group. + // These are done on a 'per-batch' status in processPorstActions + // so holding in a property is OK. + $this->createdContacts[$createdContactID] = $createdContactID; + } $this->getDataSourceObject()->updateStatus($id, $status, $message, $entityID, $additionalFields); } diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/contact_id_only.csv b/tests/phpunit/CRM/Contact/Import/Form/data/contact_id_only.csv new file mode 100644 index 000000000000..63e41e1f5be9 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/contact_id_only.csv @@ -0,0 +1,2 @@ +Contact ID +3 diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 00c907789639..d411bc82290b 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -18,6 +18,8 @@ use Civi\Api4\Contact; use Civi\Api4\ContactType; use Civi\Api4\Email; +use Civi\Api4\Group; +use Civi\Api4\GroupContact; use Civi\Api4\IM; use Civi\Api4\LocationType; use Civi\Api4\OpenID; @@ -1068,6 +1070,22 @@ public function testImport($csv, $mapper, $expectedError, $expectedOutcomes = [] } } + /** + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + public function testImportContactToGroup(): void { + $this->individualCreate(); + $this->importCSV('contact_id_only.csv', [['id']], [ + 'newGroupName' => 'My New Group', + ]); + $dataSource = new CRM_Import_DataSource_CSV(UserJob::get(FALSE)->setSelect(['id'])->execute()->first()['id']); + $row = $dataSource->getRow(); + $this->assertEquals('IMPORTED', $row['_status']); + $group = Group::get()->addWhere('title', '=', 'My New Group')->execute()->first(); + $this->assertCount(1, GroupContact::get()->addWhere('group_id', '=', $group['id'])->execute()); + } + /** * Get combinations to test for validation. * @@ -2059,11 +2077,18 @@ protected function importCSV(string $csv, array $mapper, array $submittedValues 'groups' => [], ], $submittedValues); $form = $this->getFormObject('CRM_Contact_Import_Form_DataSource', $submittedValues); + $values = $_SESSION['_' . $form->controller->_name . '_container']['values']; + $form->buildForm(); $form->postProcess(); $this->userJobID = $form->getUserJobID(); + + // This gets reset in DataSource so re-do.... + $_SESSION['_' . $form->controller->_name . '_container']['values'] = $values; + /* @var CRM_Contact_Import_Form_MapField $form */ $form = $this->getFormObject('CRM_Contact_Import_Form_MapField', $submittedValues); + $form->setUserJobID($this->userJobID); $form->buildForm(); $form->postProcess();