Merge pull request #11660 from JMAConsulting/CRM-21754
[civicrm-core.git] / tests / phpunit / CRM / Report / Form / ActivityTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test Activity report outcome
30 *
31 * @package CiviCRM
32 */
33 class CRM_Report_Form_ActivityTest extends CiviReportTestCase {
34 protected $_tablesToTruncate = array(
35 'civicrm_contact',
36 'civicrm_email',
37 'civicrm_phone',
38 'civicrm_address',
39 'civicrm_contribution',
40 );
41
42 public function setUp() {
43 parent::setUp();
44 $this->quickCleanup($this->_tablesToTruncate);
45 }
46
47 public function tearDown() {
48 parent::tearDown();
49 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp1');
50 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp2');
51 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp3');
52 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_activity_temp_target');
53 }
54
55 /**
56 * Ensure long custom field names don't result in errors.
57 */
58 public function testLongCustomFieldNames() {
59 // Create custom group with long name and custom field with long name.
60 $long_name = 'this is a very very very very long name with 65 characters in it';
61 $group_params = array(
62 'title' => $long_name,
63 'extends' => 'Activity',
64 );
65 $result = $this->customGroupCreate($group_params);
66 $custom_group_id = $result['id'];
67 $field_params = array(
68 'custom_group_id' => $custom_group_id,
69 'label' => $long_name,
70 );
71 $result = $this->customFieldCreate($field_params);
72 $custom_field_id = $result['id'];
73 $input = array(
74 'fields' => array(
75 'custom_' . $custom_field_id,
76 ),
77 );
78 $obj = $this->getReportObject('CRM_Report_Form_Activity', $input);
79 //$params = $obj->_params;
80 //$params['fields'] = array('custom_' . $custom_field_id);
81 //$obj->setParams($params);
82 $obj->getResultSet();
83 $this->assertTrue(TRUE, "Testo");
84 }
85
86 /**
87 * Ensure that activity detail report only shows addres fields of target contact
88 */
89 public function testTargetAddressFields() {
90 $countryNames = array_flip(CRM_Core_PseudoConstant::country());
91 // Create contact 1 and 2 with address fields, later considered as target contacts for activity
92 $contactID1 = $this->individualCreate(array(
93 'api.Address.create' => array(
94 'contact_id' => '$value.id',
95 'location_type_id' => 'Home',
96 'city' => 'ABC',
97 'country_id' => $countryNames['India'],
98 ),
99 ));
100 $contactID2 = $this->individualCreate(array(
101 'api.Address.create' => array(
102 'contact_id' => '$value.id',
103 'location_type_id' => 'Home',
104 'city' => 'DEF',
105 'country_id' => $countryNames['United States'],
106 ),
107 ));
108 // Create Contact 3 later considered as assignee contact of activity
109 $contactID3 = $this->individualCreate(array(
110 'api.Address.create' => array(
111 'contact_id' => '$value.id',
112 'location_type_id' => 'Home',
113 'city' => 'GHI',
114 'country_id' => $countryNames['China'],
115 ),
116 ));
117
118 // create dummy activity type
119 $activityTypeID = CRM_Utils_Array::value('id', $this->callAPISuccess('option_value', 'create', array(
120 'option_group_id' => 'activity_type',
121 'name' => 'Test activity type',
122 'label' => 'Test activity type',
123 )));
124 // create activity
125 $result = $this->callAPISuccess('activity', 'create', array(
126 'subject' => 'Make-it-Happen Meeting',
127 'activity_date_time' => date('Ymd'),
128 'duration' => 120,
129 'location' => 'Pennsylvania',
130 'details' => 'a test activity',
131 'status_id' => 1,
132 'activity_type_id' => 'Test activity type',
133 'source_contact_id' => $this->individualCreate(),
134 'target_contact_id' => array($contactID1, $contactID2),
135 'assignee_contact_id' => $contactID3,
136 ));
137 // display city and country field so that we can check its value
138 $input = array(
139 'fields' => array(
140 'city',
141 'country_id',
142 ),
143 'order_bys' => array(
144 'city' => array(),
145 'country_id' => array('default' => TRUE),
146 ),
147 );
148 // generate result
149 $obj = $this->getReportObject('CRM_Report_Form_Activity', $input);
150 $rows = $obj->getResultSet();
151
152 // ensure that only 1 activity is created
153 $this->assertEquals(1, count($rows));
154 // ensure that country values of respective target contacts are only shown
155 $this->assertEquals('India;United States', $rows[0]['civicrm_address_country_id']);
156 // ensure that city values of respective target contacts are only shown
157 $this->assertEquals('ABC;DEF', $rows[0]['civicrm_address_city']);
158 }
159
160 }