diff --git a/tests/phpunit/CRM/Activity/Import/Parser/ActivityTest.php b/tests/phpunit/CRM/Activity/Import/Parser/ActivityTest.php index e3ce61625017..1b8a6c5e8800 100644 --- a/tests/phpunit/CRM/Activity/Import/Parser/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/Import/Parser/ActivityTest.php @@ -28,6 +28,7 @@ */ class CRM_Activity_Import_Parser_ActivityTest extends CiviUnitTestCase { use CRMTraits_Custom_CustomDataTrait; + use CRMTraits_Import_ParserTrait; /** * @var int|null @@ -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 * @@ -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; + } + } diff --git a/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php b/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php index 212ed0e6fe43..e80b3abfed3b 100644 --- a/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php @@ -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(); + } } /** diff --git a/tests/phpunit/CRMTraits/Import/ParserTrait.php b/tests/phpunit/CRMTraits/Import/ParserTrait.php index 4531d74ccb71..8647a890b969 100644 --- a/tests/phpunit/CRMTraits/Import/ParserTrait.php +++ b/tests/phpunit/CRMTraits/Import/ParserTrait.php @@ -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(); + } } /**