c21b88e8886b46d5e120f0526d6ec1f0c0601c46
[civicrm-core.git] / tests / phpunit / CRM / Activity / Import / Parser / ActivityTest.php
1 <?php
2
3 /**
4 * This file is part of CiviCRM
5 *
6 * CiviCRM is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public License
8 * as published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * CiviCRM is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public
17 * License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
21 /**
22 * Test Activity Import Parser functions
23 *
24 * @package CiviCRM
25 * @group headless
26 */
27 class CRM_Activity_Import_Parser_ActivityTest extends CiviUnitTestCase {
28 use CRMTraits_Custom_CustomDataTrait;
29
30 /**
31 * Prepare for tests.
32 */
33 public function setUp():void {
34 parent::setUp();
35 $this->createLoggedInUser();
36 }
37
38 /**
39 * Clean up after test.
40 *
41 * @throws \CRM_Core_Exception
42 */
43 public function tearDown():void {
44 $this->quickCleanup(['civicrm_contact', 'civicrm_activity', 'civicrm_activity_contact'], TRUE);
45 parent::tearDown();
46 }
47
48 /**
49 * Test Import.
50 *
51 * So far this is just testing the class constructor & preparing for more
52 * tests.
53 *
54 * @throws \API_Exception
55 * @throws \CRM_Core_Exception
56 * @throws \CiviCRM_API3_Exception
57 */
58 public function testImport(): void {
59 $this->createCustomGroupWithFieldOfType(['extends' => 'Activity'], 'checkbox');
60 $values = [
61 'activity_details' => 'fascinating',
62 'activity_type_id' => 1,
63 'activity_date_time' => '2010-01-06',
64 'target_contact_id' => $this->individualCreate(),
65 'activity_subject' => 'riveting stuff',
66 $this->getCustomFieldName('checkbox') => 'L',
67 ];
68 $this->importValues($values);
69 $this->callAPISuccessGetSingle('Activity', [$this->getCustomFieldName('checkbox') => 'L']);
70 }
71
72 /**
73 * Create an import object.
74 *
75 * @param array $fields
76 *
77 * @return \CRM_Activity_Import_Parser_Activity
78 */
79 protected function createImportObject(array $fields): \CRM_Activity_Import_Parser_Activity {
80 // @todo Eyes are weary so sanity-check this later:
81 // This loop seems the same as array_values($fields)? And this appears
82 // to only be called from one place that already has them sequentially
83 // indexed so is it even needed?
84 $fieldMapper = [];
85 foreach ($fields as $index => $field) {
86 $fieldMapper[] = $field;
87 }
88 $importer = new CRM_Activity_Import_Parser_Activity($fieldMapper);
89 $importer->init();
90 return $importer;
91 }
92
93 /**
94 * Run the importer.
95 *
96 * @param array $values
97 * @param int $expectedOutcome
98 */
99 protected function importValues(array $values, $expectedOutcome = 1): void {
100 $importer = $this->createImportObject(array_keys($values));
101 $params = array_values($values);
102 CRM_Core_Session::singleton()->set('dateTypes', 1);
103 $this->assertEquals($expectedOutcome, $importer->import(NULL, $params));
104 }
105
106 }