Update test for runner
[civicrm-core.git] / tests / phpunit / CRMTraits / Import / ParserTrait.php
CommitLineData
112e13fa
EM
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * Trait ParserTrait
14 *
15 * Trait for testing imports.
16 */
17trait CRMTraits_Import_ParserTrait {
18
19 /**
20 * Import the csv file values.
21 *
22 * This function uses a flow that mimics the UI flow.
23 *
24 * @param string $csv Name of csv file.
25 * @param array $fieldMappings
26 * @param array $submittedValues
27 */
28 protected function importCSV(string $csv, array $fieldMappings, array $submittedValues = []): void {
29 $reflector = new ReflectionClass(get_class($this));
30 $directory = dirname($reflector->getFileName());
31 $submittedValues = array_merge([
32 'uploadFile' => ['name' => $directory . '/data/' . $csv],
33 'skipColumnHeader' => TRUE,
34 'fieldSeparator' => ',',
35 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
36 'mapper' => $this->getMapperFromFieldMappings($fieldMappings),
37 'dataSource' => 'CRM_Import_DataSource_CSV',
38 'file' => ['name' => $csv],
39 'dateFormats' => CRM_Core_Form_Date::DATE_yyyy_mm_dd,
40 'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP,
41 'groups' => [],
42 ], $submittedValues);
43 $form = $this->getDataSourceForm($submittedValues);
44 $form->buildForm();
45 $form->postProcess();
46 $this->userJobID = $form->getUserJobID();
47 $form = $this->getMapFieldForm($submittedValues);
48 $form->setUserJobID($this->userJobID);
49 $form->buildForm();
50 $this->assertTrue($form->validate());
51 $form->postProcess();
52 $form = $this->getPreviewForm($submittedValues);
53 $form->setUserJobID($this->userJobID);
54 $form->buildForm();
55 $this->assertTrue($form->validate());
79091aeb
EM
56 try {
57 $form->postProcess();
58 $this->fail('Expected a redirect');
59 }
60 catch (CRM_Core_Exception_PrematureExitException $e) {
61 $queue = Civi::queue('user_job_' . $this->userJobID);
62 $runner = new CRM_Queue_Runner([
63 'queue' => $queue,
64 'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
65 ]);
66 $runner->runAll();
67 }
112e13fa
EM
68 }
69
70 /**
71 * Get the import's datasource form.
72 *
73 * Defaults to contribution - other classes should override.
74 *
75 * @param array $submittedValues
76 *
77 * @return \CRM_Contribute_Import_Form_DataSource
78 * @noinspection PhpUnnecessaryLocalVariableInspection
79 */
80 protected function getDataSourceForm(array $submittedValues): CRM_Contribute_Import_Form_DataSource {
81 /* @var \CRM_Contribute_Import_Form_DataSource $form */
82 $form = $this->getFormObject('CRM_Contribute_Import_Form_DataSource', $submittedValues);
83 return $form;
84 }
85
86 /**
87 * Get the import's mapField form.
88 *
89 * Defaults to contribution - other classes should override.
90 *
91 * @param array $submittedValues
92 *
93 * @return \CRM_Contribute_Import_Form_MapField
94 * @noinspection PhpUnnecessaryLocalVariableInspection
95 */
96 protected function getMapFieldForm(array $submittedValues): CRM_Contribute_Import_Form_MapField {
97 /* @var \CRM_Contribute_Import_Form_MapField $form */
98 $form = $this->getFormObject('CRM_Contribute_Import_Form_MapField', $submittedValues);
99 return $form;
100 }
101
102 /**
103 * Get the import's preview form.
104 *
105 * Defaults to contribution - other classes should override.
106 *
107 * @param array $submittedValues
108 *
109 * @return \CRM_Contribute_Import_Form_Preview
110 * @noinspection PhpUnnecessaryLocalVariableInspection
111 */
112 protected function getPreviewForm(array $submittedValues): CRM_Contribute_Import_Form_Preview {
113 /* @var CRM_Contribute_Import_Form_Preview $form */
114 $form = $this->getFormObject('CRM_Contribute_Import_Form_Preview', $submittedValues);
115 return $form;
116 }
117
118}