Skip to content

Commit

Permalink
Merge pull request #23756 from eileenmcnaughton/import_to_group
Browse files Browse the repository at this point in the history
dev/core#3160 fix inability to import 'just contactid' and add to group
  • Loading branch information
totten authored Jun 10, 2022
2 parents 6a376dd + 4521630 commit d76cb82
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 8 additions & 1 deletion CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Contact ID
3
25 changes: 25 additions & 0 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -2065,11 +2083,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();
Expand Down

0 comments on commit d76cb82

Please sign in to comment.