Merge pull request #19806 from eileenmcnaughton/msg_compat
[civicrm-core.git] / tests / phpunit / CRM / Report / Form / ActivityTest.php
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 * Test Activity report outcome
14 *
15 * @package CiviCRM
16 */
17 class CRM_Report_Form_ActivityTest extends CiviReportTestCase {
18 protected $_tablesToTruncate = [
19 'civicrm_contact',
20 'civicrm_email',
21 'civicrm_phone',
22 'civicrm_address',
23 'civicrm_contribution',
24 ];
25
26 public function setUp() {
27 parent::setUp();
28 $this->quickCleanup($this->_tablesToTruncate);
29 }
30
31 public function tearDown(): void {
32 parent::tearDown();
33 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_activity_temp_target');
34 }
35
36 /**
37 * Ensure long custom field names don't result in errors.
38 */
39 public function testLongCustomFieldNames() {
40 // Create custom group with long name and custom field with long name.
41 $long_name = 'this is a very very very very long name with 65 characters in it';
42 $group_params = [
43 'title' => $long_name,
44 'extends' => 'Activity',
45 ];
46 $result = $this->customGroupCreate($group_params);
47 $custom_group_id = $result['id'];
48 $field_params = [
49 'custom_group_id' => $custom_group_id,
50 'label' => $long_name,
51 ];
52 $result = $this->customFieldCreate($field_params);
53 $custom_field_id = $result['id'];
54 $input = [
55 'fields' => [
56 'custom_' . $custom_field_id,
57 ],
58 ];
59 $obj = $this->getReportObject('CRM_Report_Form_Activity', $input);
60 //$params = $obj->_params;
61 //$params['fields'] = array('custom_' . $custom_field_id);
62 //$obj->setParams($params);
63 $obj->getResultSet();
64 $this->assertTrue(TRUE, "Testo");
65 }
66
67 /**
68 * Ensure that activity detail report only shows addres fields of target contact
69 */
70 public function testTargetAddressFields() {
71 $countryNames = array_flip(CRM_Core_PseudoConstant::country());
72 // Create contact 1 and 2 with address fields, later considered as target contacts for activity
73 $contactID1 = $this->individualCreate([
74 'api.Address.create' => [
75 'contact_id' => '$value.id',
76 'location_type_id' => 'Home',
77 'city' => 'ABC',
78 'country_id' => $countryNames['India'],
79 ],
80 ]);
81 $contactID2 = $this->individualCreate([
82 'api.Address.create' => [
83 'contact_id' => '$value.id',
84 'location_type_id' => 'Home',
85 'city' => 'DEF',
86 'country_id' => $countryNames['United States'],
87 ],
88 ]);
89 // Create Contact 3 later considered as assignee contact of activity
90 $contactID3 = $this->individualCreate([
91 'api.Address.create' => [
92 'contact_id' => '$value.id',
93 'location_type_id' => 'Home',
94 'city' => 'GHI',
95 'country_id' => $countryNames['China'],
96 ],
97 ]);
98
99 // create dummy activity type
100 $activityTypeID = CRM_Utils_Array::value('id', $this->callAPISuccess('option_value', 'create', [
101 'option_group_id' => 'activity_type',
102 'name' => 'Test activity type',
103 'label' => 'Test activity type',
104 ]));
105 // create activity
106 $result = $this->callAPISuccess('activity', 'create', [
107 'subject' => 'Make-it-Happen Meeting',
108 'activity_date_time' => date('Ymd'),
109 'duration' => 120,
110 'location' => 'Pennsylvania',
111 'details' => 'a test activity',
112 'status_id' => 1,
113 'activity_type_id' => 'Test activity type',
114 'source_contact_id' => $this->individualCreate(),
115 'target_contact_id' => [$contactID1, $contactID2],
116 'assignee_contact_id' => $contactID3,
117 ]);
118 // display city and country field so that we can check its value
119 $input = [
120 'fields' => [
121 'city',
122 'country_id',
123 ],
124 'order_bys' => [
125 'city' => [],
126 'country_id' => ['default' => TRUE],
127 ],
128 ];
129 // generate result
130 $obj = $this->getReportObject('CRM_Report_Form_Activity', $input);
131 $rows = $obj->getResultSet();
132
133 // ensure that only 1 activity is created
134 $this->assertEquals(1, count($rows));
135 // ensure that country values of respective target contacts are only shown
136 $this->assertTrue(in_array($rows[0]['civicrm_address_country_id'], ['India;United States', 'United States;India']));
137 // ensure that city values of respective target contacts are only shown
138 $this->assertTrue(in_array($rows[0]['civicrm_address_city'], ['ABC;DEF', 'DEF;ABC']));
139 }
140
141 }