Skip to content

Commit

Permalink
Update test for runner
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jun 9, 2022
1 parent 4ff8674 commit 79091ae
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 45 deletions.
92 changes: 49 additions & 43 deletions tests/phpunit/CRM/Activity/Import/Parser/ActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
class CRM_Activity_Import_Parser_ActivityTest extends CiviUnitTestCase {
use CRMTraits_Custom_CustomDataTrait;
use CRMTraits_Import_ParserTrait;

/**
* @var int|null
Expand Down Expand Up @@ -315,49 +316,6 @@ public function activityImportValidationProvider(): array {
];
}

/**
* Import the csv file values.
*
* This function uses a flow that mimics the UI flow.
*
* @param string $csv Name of csv file.
* @param array $fieldMappings
* @param array $submittedValues
*/
protected function importCSV(string $csv, array $fieldMappings, array $submittedValues = []): void {
$submittedValues = array_merge([
'uploadFile' => ['name' => __DIR__ . '/data/' . $csv],
'skipColumnHeader' => TRUE,
'fieldSeparator' => ',',
'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
'mapper' => $this->getMapperFromFieldMappings($fieldMappings),
'dataSource' => 'CRM_Import_DataSource_CSV',
'file' => ['name' => $csv],
'dateFormats' => CRM_Core_Form_Date::DATE_yyyy_mm_dd,
'onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE,
'groups' => [],
], $submittedValues);
/* @var \CRM_Activity_Import_Form_DataSource $form */
$form = $this->getFormObject('CRM_Activity_Import_Form_DataSource', $submittedValues);
$values = $_SESSION['_' . $form->controller->_name . '_container']['values'];
$form->buildForm();
$form->postProcess();
// This gets reset in DataSource so re-do....
$_SESSION['_' . $form->controller->_name . '_container']['values'] = $values;

$this->userJobID = $form->getUserJobID();
/* @var CRM_Activity_Import_Form_MapField $form */
$form = $this->getFormObject('CRM_Activity_Import_Form_MapField', $submittedValues);
$form->setUserJobID($this->userJobID);
$form->buildForm();
$form->postProcess();
/* @var CRM_Activity_Import_Form_Preview $form */
$form = $this->getFormObject('CRM_Activity_Import_Form_Preview', $submittedValues);
$form->setUserJobID($this->userJobID);
$form->buildForm();
$form->postProcess();
}

/**
* @param array $mappings
*
Expand Down Expand Up @@ -427,4 +385,52 @@ protected function getUserJobID(array $submittedValues = []): int {
return $userJobID;
}

/**
* Get the import's datasource form.
*
* Defaults to contribution - other classes should override.
*
* @param array $submittedValues
*
* @return \CRM_Activity_Import_Form_DataSource
* @noinspection PhpUnnecessaryLocalVariableInspection
*/
protected function getDataSourceForm(array $submittedValues): CRM_Activity_Import_Form_DataSource {
/* @var \CRM_Activity_Import_Form_DataSource $form */
$form = $this->getFormObject('CRM_Activity_Import_Form_DataSource', $submittedValues);
return $form;
}

/**
* Get the import's mapField form.
*
* Defaults to contribution - other classes should override.
*
* @param array $submittedValues
*
* @return \CRM_Activity_Import_Form_MapField
* @noinspection PhpUnnecessaryLocalVariableInspection
*/
protected function getMapFieldForm(array $submittedValues): CRM_Activity_Import_Form_MapField {
/* @var \CRM_Activity_Import_Form_MapField $form */
$form = $this->getFormObject('CRM_Activity_Import_Form_MapField', $submittedValues);
return $form;
}

/**
* Get the import's preview form.
*
* Defaults to contribution - other classes should override.
*
* @param array $submittedValues
*
* @return \CRM_Activity_Import_Form_Preview
* @noinspection PhpUnnecessaryLocalVariableInspection
*/
protected function getPreviewForm(array $submittedValues): CRM_Activity_Import_Form_Preview {
/* @var CRM_Activity_Import_Form_Preview $form */
$form = $this->getFormObject('CRM_Activity_Import_Form_Preview', $submittedValues);
return $form;
}

}
12 changes: 11 additions & 1 deletion tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,17 @@ protected function importCSV(string $csv, array $fieldMappings, array $submitted
$form = $this->getFormObject('CRM_Member_Import_Form_Preview', $submittedValues);
$form->setUserJobID($this->userJobID);
$form->buildForm();
$form->postProcess();
try {
$form->postProcess();
}
catch (CRM_Core_Exception_PrematureExitException $e) {
$queue = Civi::queue('user_job_' . $this->userJobID);
$runner = new CRM_Queue_Runner([
'queue' => $queue,
'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
]);
$runner->runAll();
}
}

/**
Expand Down
13 changes: 12 additions & 1 deletion tests/phpunit/CRMTraits/Import/ParserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,18 @@ protected function importCSV(string $csv, array $fieldMappings, array $submitted
$form->setUserJobID($this->userJobID);
$form->buildForm();
$this->assertTrue($form->validate());
$form->postProcess();
try {
$form->postProcess();
$this->fail('Expected a redirect');
}
catch (CRM_Core_Exception_PrematureExitException $e) {
$queue = Civi::queue('user_job_' . $this->userJobID);
$runner = new CRM_Queue_Runner([
'queue' => $queue,
'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
]);
$runner->runAll();
}
}

/**
Expand Down

0 comments on commit 79091ae

Please sign in to comment.