Merge pull request #19806 from eileenmcnaughton/msg_compat
[civicrm-core.git] / tests / phpunit / CRM / Report / Form / ActivityTest.php
CommitLineData
2caf0feb
JM
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
2caf0feb 5 | |
7d61e75f
TO
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 |
2caf0feb
JM
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * Test Activity report outcome
14 *
15 * @package CiviCRM
16 */
17class CRM_Report_Form_ActivityTest extends CiviReportTestCase {
9099cab3 18 protected $_tablesToTruncate = [
2caf0feb
JM
19 'civicrm_contact',
20 'civicrm_email',
21 'civicrm_phone',
22 'civicrm_address',
23 'civicrm_contribution',
9099cab3 24 ];
2caf0feb
JM
25
26 public function setUp() {
27 parent::setUp();
28 $this->quickCleanup($this->_tablesToTruncate);
29 }
30
dd09ee0c 31 public function tearDown(): void {
2caf0feb 32 parent::tearDown();
c00963a1 33 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_activity_temp_target');
2caf0feb
JM
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';
9099cab3 42 $group_params = [
2caf0feb
JM
43 'title' => $long_name,
44 'extends' => 'Activity',
9099cab3 45 ];
2caf0feb
JM
46 $result = $this->customGroupCreate($group_params);
47 $custom_group_id = $result['id'];
9099cab3 48 $field_params = [
2caf0feb
JM
49 'custom_group_id' => $custom_group_id,
50 'label' => $long_name,
9099cab3 51 ];
2caf0feb
JM
52 $result = $this->customFieldCreate($field_params);
53 $custom_field_id = $result['id'];
9099cab3
CW
54 $input = [
55 'fields' => [
2caf0feb 56 'custom_' . $custom_field_id,
9099cab3
CW
57 ],
58 ];
2caf0feb
JM
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
c00963a1 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
9099cab3
CW
73 $contactID1 = $this->individualCreate([
74 'api.Address.create' => [
c00963a1 75 'contact_id' => '$value.id',
76 'location_type_id' => 'Home',
77 'city' => 'ABC',
78 'country_id' => $countryNames['India'],
9099cab3
CW
79 ],
80 ]);
81 $contactID2 = $this->individualCreate([
82 'api.Address.create' => [
c00963a1 83 'contact_id' => '$value.id',
84 'location_type_id' => 'Home',
85 'city' => 'DEF',
86 'country_id' => $countryNames['United States'],
9099cab3
CW
87 ],
88 ]);
c00963a1 89 // Create Contact 3 later considered as assignee contact of activity
9099cab3
CW
90 $contactID3 = $this->individualCreate([
91 'api.Address.create' => [
c00963a1 92 'contact_id' => '$value.id',
93 'location_type_id' => 'Home',
94 'city' => 'GHI',
95 'country_id' => $countryNames['China'],
9099cab3
CW
96 ],
97 ]);
c00963a1 98
99 // create dummy activity type
9099cab3 100 $activityTypeID = CRM_Utils_Array::value('id', $this->callAPISuccess('option_value', 'create', [
c00963a1 101 'option_group_id' => 'activity_type',
102 'name' => 'Test activity type',
103 'label' => 'Test activity type',
9099cab3 104 ]));
c00963a1 105 // create activity
9099cab3 106 $result = $this->callAPISuccess('activity', 'create', [
c00963a1 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(),
9099cab3 115 'target_contact_id' => [$contactID1, $contactID2],
c00963a1 116 'assignee_contact_id' => $contactID3,
9099cab3 117 ]);
c00963a1 118 // display city and country field so that we can check its value
9099cab3
CW
119 $input = [
120 'fields' => [
c00963a1 121 'city',
122 'country_id',
9099cab3
CW
123 ],
124 'order_bys' => [
125 'city' => [],
126 'country_id' => ['default' => TRUE],
127 ],
128 ];
c00963a1 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
b72100fc 136 $this->assertTrue(in_array($rows[0]['civicrm_address_country_id'], ['India;United States', 'United States;India']));
c00963a1 137 // ensure that city values of respective target contacts are only shown
77347e0a 138 $this->assertTrue(in_array($rows[0]['civicrm_address_city'], ['ABC;DEF', 'DEF;ABC']));
c00963a1 139 }
140
2caf0feb 141}