Commit | Line | Data |
---|---|---|
99308da4 EM |
1 | <?php |
2 | ||
3 | /** | |
7eebbdaa | 4 | * File for the Participant import class |
99308da4 EM |
5 | * |
6 | * (PHP 5) | |
7 | * | |
8 | * @package CiviCRM | |
9 | * | |
10 | * This file is part of CiviCRM | |
11 | * | |
12 | * CiviCRM is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Affero General Public License | |
14 | * as published by the Free Software Foundation; either version 3 of | |
15 | * the License, or (at your option) any later version. | |
16 | * | |
17 | * CiviCRM is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU Affero General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Affero General Public | |
23 | * License along with this program. If not, see | |
24 | * <http://www.gnu.org/licenses/>. | |
25 | */ | |
26 | ||
b4167b7c | 27 | use Civi\Api4\Mapping; |
99308da4 EM |
28 | use Civi\Api4\UserJob; |
29 | ||
30 | /** | |
31 | * @package CiviCRM | |
32 | * @group headless | |
33 | */ | |
34 | class CRM_Participant_Import_Parser_ParticipantTest extends CiviUnitTestCase { | |
35 | ||
36 | use CRMTraits_Custom_CustomDataTrait; | |
37 | ||
38 | protected $entity = 'Participant'; | |
39 | ||
40 | /** | |
41 | * Tears down the fixture, for example, closes a network connection. | |
42 | * This method is called after a test is executed. | |
43 | */ | |
44 | public function tearDown(): void { | |
45 | $this->quickCleanup([ | |
46 | 'civicrm_event', | |
47 | 'civicrm_participant', | |
48 | 'civicrm_contact', | |
49 | 'civicrm_email', | |
50 | 'civicrm_user_job', | |
51 | 'civicrm_queue', | |
52 | 'civicrm_queue_item', | |
b4167b7c EM |
53 | 'civicrm_mapping', |
54 | 'civicrm_mapping_field', | |
99308da4 EM |
55 | ], TRUE); |
56 | parent::tearDown(); | |
57 | } | |
58 | ||
59 | /** | |
60 | * Import the csv file values. | |
61 | * | |
62 | * This function uses a flow that mimics the UI flow. | |
63 | * | |
64 | * @param string $csv Name of csv file. | |
65 | * @param array $fieldMappings | |
66 | * @param array $submittedValues | |
67 | */ | |
68 | protected function importCSV(string $csv, array $fieldMappings, array $submittedValues = []): void { | |
69 | $submittedValues = array_merge([ | |
70 | 'uploadFile' => ['name' => __DIR__ . '/data/' . $csv], | |
71 | 'skipColumnHeader' => TRUE, | |
72 | 'fieldSeparator' => ',', | |
73 | 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL, | |
74 | 'mapper' => $this->getMapperFromFieldMappings($fieldMappings), | |
75 | 'dataSource' => 'CRM_Import_DataSource_CSV', | |
76 | 'file' => ['name' => $csv], | |
77 | 'dateFormats' => CRM_Core_Form_Date::DATE_yyyy_mm_dd, | |
78 | 'onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE, | |
79 | 'groups' => [], | |
b4167b7c EM |
80 | 'saveMapping' => TRUE, |
81 | 'saveMappingName' => 'my mapping', | |
82 | 'saveMappingDesc' => 'new mapping', | |
99308da4 EM |
83 | ], $submittedValues); |
84 | /* @var \CRM_Event_Import_Form_DataSource $form */ | |
85 | $form = $this->getFormObject('CRM_Event_Import_Form_DataSource', $submittedValues); | |
86 | $values = $_SESSION['_' . $form->controller->_name . '_container']['values']; | |
87 | $form->buildForm(); | |
88 | $form->postProcess(); | |
89 | // This gets reset in DataSource so re-do.... | |
90 | $_SESSION['_' . $form->controller->_name . '_container']['values'] = $values; | |
91 | ||
92 | $this->userJobID = $form->getUserJobID(); | |
93 | /* @var CRM_Event_Import_Form_MapField $form */ | |
94 | $form = $this->getFormObject('CRM_Event_Import_Form_MapField', $submittedValues); | |
95 | $form->setUserJobID($this->userJobID); | |
96 | $form->buildForm(); | |
b4167b7c | 97 | $this->assertTrue($form->validate()); |
99308da4 EM |
98 | $form->postProcess(); |
99 | /* @var CRM_Event_Import_Form_Preview $form */ | |
100 | $form = $this->getFormObject('CRM_Event_Import_Form_Preview', $submittedValues); | |
101 | $form->setUserJobID($this->userJobID); | |
102 | $form->buildForm(); | |
b4167b7c | 103 | $this->assertTrue($form->validate()); |
99308da4 EM |
104 | $form->postProcess(); |
105 | } | |
106 | ||
107 | /** | |
108 | * @param array $mappings | |
109 | * | |
110 | * @return array | |
111 | */ | |
112 | protected function getMapperFromFieldMappings(array $mappings): array { | |
113 | $mapper = []; | |
114 | foreach ($mappings as $mapping) { | |
115 | $fieldInput = [$mapping['name']]; | |
116 | $mapper[] = $fieldInput; | |
117 | } | |
118 | return $mapper; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Test the full form-flow import. | |
b4167b7c EM |
123 | * |
124 | * @dataProvider importData | |
99308da4 | 125 | */ |
b4167b7c | 126 | public function testImportCSV($csv, $mapper) :void { |
99308da4 EM |
127 | $this->campaignCreate(['name' => 'Soccer cup']); |
128 | $this->eventCreate(['title' => 'Rain-forest Cup Youth Soccer Tournament']); | |
129 | $this->individualCreate(['email' => 'mum@example.com']); | |
b4167b7c | 130 | $this->importCSV($csv, $mapper); |
99308da4 EM |
131 | $dataSource = new CRM_Import_DataSource_CSV($this->userJobID); |
132 | $row = $dataSource->getRow(); | |
133 | $this->assertEquals('IMPORTED', $row['_status']); | |
134 | $this->callAPISuccessGetSingle('Participant', ['campaign_id' => 'Soccer Cup']); | |
b4167b7c EM |
135 | $mapping = Mapping::get()->addWhere('mapping_type_id:name', '=', 'Import Participant')->execute()->first(); |
136 | $this->assertEquals('my mapping', $mapping['name']); | |
137 | $this->assertEquals('new mapping', $mapping['description']); | |
138 | } | |
139 | ||
140 | /** | |
141 | * Data provider for importCSV. | |
142 | */ | |
143 | public function importData(): array { | |
144 | $defaultMapper = [ | |
145 | ['name' => 'event_id'], | |
146 | ['name' => 'campaign_id'], | |
147 | ['name' => 'email'], | |
148 | ['name' => 'fee_amount'], | |
149 | ['name' => 'fee_currency'], | |
150 | ['name' => 'fee_level'], | |
151 | ['name' => 'is_pay_later'], | |
152 | ['name' => 'role_id'], | |
153 | ['name' => 'source'], | |
154 | ['name' => 'status_id'], | |
155 | ['name' => 'register_date'], | |
156 | ['name' => 'do_not_import'], | |
157 | ]; | |
158 | return [ | |
159 | ['csv' => 'participant.csv', 'mapper' => $defaultMapper], | |
160 | ['csv' => 'participant_with_event_id.csv', 'mapper' => $defaultMapper], | |
161 | ]; | |
99308da4 EM |
162 | } |
163 | ||
164 | /** | |
165 | * @param array $submittedValues | |
166 | * | |
167 | * @return int | |
168 | * @noinspection PhpDocMissingThrowsInspection | |
169 | */ | |
170 | protected function getUserJobID(array $submittedValues = []): int { | |
171 | $userJobID = UserJob::create()->setValues([ | |
172 | 'metadata' => [ | |
173 | 'submitted_values' => array_merge([ | |
174 | 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL, | |
175 | 'contactSubType' => '', | |
176 | 'dataSource' => 'CRM_Import_DataSource_SQL', | |
177 | 'sqlQuery' => 'SELECT first_name FROM civicrm_contact', | |
178 | 'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP, | |
179 | 'dedupe_rule_id' => NULL, | |
180 | 'dateFormats' => CRM_Core_Form_Date::DATE_yyyy_mm_dd, | |
181 | ], $submittedValues), | |
182 | ], | |
183 | 'status_id:name' => 'draft', | |
184 | 'type_id:name' => 'participant_import', | |
185 | ])->execute()->first()['id']; | |
186 | if ($submittedValues['dataSource'] ?? NULL === 'CRM_Import_DataSource') { | |
187 | $dataSource = new CRM_Import_DataSource_CSV($userJobID); | |
188 | } | |
189 | else { | |
190 | $dataSource = new CRM_Import_DataSource_SQL($userJobID); | |
191 | } | |
192 | $dataSource->initialize(); | |
193 | return $userJobID; | |
194 | } | |
195 | ||
196 | } |