Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / ReportTemplateTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
6a488035
TO
12/**
13 * Test APIv3 civicrm_report_instance_* functions
14 *
6c6e6187
TO
15 * @package CiviCRM_APIv3
16 * @subpackage API_Report
acb109b7 17 * @group headless
6a488035 18 */
6a488035 19class api_v3_ReportTemplateTest extends CiviUnitTestCase {
2af6f0c0 20
21 use CRMTraits_ACL_PermissionTrait;
e40ce31e 22 use CRMTraits_PCP_PCPTestTrait;
d7a896b4 23 use CRMTraits_Custom_CustomDataTrait;
2af6f0c0 24
5a14305b 25 protected $contactIDs = [];
26
2c6b4783 27 /**
28 * Our group reports use an alter so transaction cleanup won't work.
29 *
30 * @throws \Exception
31 */
32 public function tearDown() {
33 $this->quickCleanUpFinancialEntities();
d7a896b4 34 $this->quickCleanup(['civicrm_group', 'civicrm_saved_search', 'civicrm_group_contact', 'civicrm_group_contact_cache', 'civicrm_group'], TRUE);
2c6b4783 35 parent::tearDown();
6a488035
TO
36 }
37
6a488035 38 public function testReportTemplate() {
9099cab3 39 $result = $this->callAPISuccess('ReportTemplate', 'create', [
6a488035
TO
40 'label' => 'Example Form',
41 'description' => 'Longish description of the example form',
42 'class_name' => 'CRM_Report_Form_Examplez',
43 'report_url' => 'example/path',
44 'component' => 'CiviCase',
9099cab3 45 ]);
1cbea43e 46 $this->assertAPISuccess($result);
ba4a1892 47 $this->assertEquals(1, $result['count']);
6a488035 48 $entityId = $result['id'];
ba4a1892
TM
49 $this->assertTrue(is_numeric($entityId));
50 $this->assertEquals(7, $result['values'][$entityId]['component_id']);
6a488035
TO
51 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
52 WHERE name = "CRM_Report_Form_Examplez"
0201b57f 53 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
6a488035
TO
54 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
55 WHERE name = "CRM_Report_Form_Examplez"');
56
57 // change component to null
9099cab3 58 $result = $this->callAPISuccess('ReportTemplate', 'create', [
92915c55 59 'id' => $entityId,
6a488035 60 'component' => '',
9099cab3 61 ]);
1cbea43e 62 $this->assertAPISuccess($result);
ba4a1892 63 $this->assertEquals(1, $result['count']);
6a488035
TO
64 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
65 WHERE name = "CRM_Report_Form_Examplez"
0201b57f 66 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
6a488035
TO
67 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
68 WHERE name = "CRM_Report_Form_Examplez"
69 AND component_id IS NULL');
70
71 // deactivate
9099cab3 72 $result = $this->callAPISuccess('ReportTemplate', 'create', [
92915c55 73 'id' => $entityId,
6a488035 74 'is_active' => 0,
9099cab3 75 ]);
1cbea43e 76 $this->assertAPISuccess($result);
ba4a1892 77 $this->assertEquals(1, $result['count']);
6a488035
TO
78 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
79 WHERE name = "CRM_Report_Form_Examplez"
0201b57f 80 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
6a488035
TO
81 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value
82 WHERE name = "CRM_Report_Form_Examplez"');
83
84 // activate
9099cab3 85 $result = $this->callAPISuccess('ReportTemplate', 'create', [
92915c55 86 'id' => $entityId,
6a488035 87 'is_active' => 1,
9099cab3 88 ]);
1cbea43e 89 $this->assertAPISuccess($result);
ba4a1892 90 $this->assertEquals(1, $result['count']);
6a488035
TO
91 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
92 WHERE name = "CRM_Report_Form_Examplez"
0201b57f 93 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
6a488035
TO
94 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
95 WHERE name = "CRM_Report_Form_Examplez"');
96
9099cab3 97 $result = $this->callAPISuccess('ReportTemplate', 'delete', [
92915c55 98 'id' => $entityId,
9099cab3 99 ]);
1cbea43e 100 $this->assertAPISuccess($result);
ba4a1892 101 $this->assertEquals(1, $result['count']);
6a488035
TO
102 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value
103 WHERE name = "CRM_Report_Form_Examplez"
104 ');
105 }
e2779f6e 106
880f81a1 107 /**
108 * Test api to get rows from reports.
109 *
110 * @dataProvider getReportTemplatesSupportingSelectWhere
111 *
112 * @param $reportID
113 *
a6439b6a 114 * @throws \PHPUnit\Framework\IncompleteTestError
880f81a1 115 */
116 public function testReportTemplateSelectWhere($reportID) {
9099cab3 117 $this->hookClass->setHook('civicrm_selectWhereClause', [$this, 'hookSelectWhere']);
880f81a1 118 $result = $this->callAPISuccess('report_template', 'getrows', [
119 'report_id' => $reportID,
120 'options' => ['metadata' => ['sql']],
121 ]);
122 $found = FALSE;
123 foreach ($result['metadata']['sql'] as $sql) {
124 if (strstr($sql, " = 'Organization' ")) {
125 $found = TRUE;
126 }
127 }
128 $this->assertTrue($found, $reportID);
129 }
130
131 /**
132 * Get templates suitable for SelectWhere test.
133 *
134 * @return array
135 */
136 public function getReportTemplatesSupportingSelectWhere() {
137 $allTemplates = $this->getReportTemplates();
138 // Exclude all that do not work as of test being written. I have not dug into why not.
139 $currentlyExcluded = [
140 'contribute/repeat',
880f81a1 141 'member/summary',
142 'event/summary',
143 'case/summary',
144 'case/timespent',
145 'case/demographics',
146 'contact/log',
147 'contribute/bookkeeping',
148 'grant/detail',
149 'event/incomesummary',
150 'case/detail',
151 'Mailing/bounce',
152 'Mailing/summary',
153 'grant/statistics',
154 'logging/contact/detail',
155 'logging/contact/summary',
156 ];
157 foreach ($allTemplates as $index => $template) {
158 $reportID = $template[0];
159 if (in_array($reportID, $currentlyExcluded) || stristr($reportID, 'has existing issues')) {
160 unset($allTemplates[$index]);
161 }
162 }
163 return $allTemplates;
164 }
165
166 /**
167 * @param \CRM_Core_DAO $entity
168 * @param array $clauses
169 */
170 public function hookSelectWhere($entity, &$clauses) {
171 // Restrict access to cases by type
172 if ($entity == 'Contact') {
173 $clauses['contact_type'][] = " = 'Organization' ";
174 }
175 }
176
e2779f6e 177 /**
fe482240 178 * Test getrows on contact summary report.
e2779f6e 179 */
00be9182 180 public function testReportTemplateGetRowsContactSummary() {
5c49fee0 181 $description = "Retrieve rows from a report template (optionally providing the instance_id).";
9099cab3 182 $result = $this->callAPISuccess('report_template', 'getrows', [
e2779f6e 183 'report_id' => 'contact/summary',
9099cab3
CW
184 'options' => ['metadata' => ['labels', 'title']],
185 ], __FUNCTION__, __FILE__, $description, 'Getrows');
781d91c8 186 $this->assertEquals('Contact Name', $result['metadata']['labels']['civicrm_contact_sort_name']);
e2779f6e
E
187
188 //the second part of this test has been commented out because it relied on the db being reset to
189 // it's base state
190 //wasn't able to get that to work consistently
191 // however, when the db is in the base state the tests do pass
192 // and because the test covers 'all' contacts we can't create our own & assume the others don't exist
193 /*
194 $this->assertEquals(2, $result['count']);
195 $this->assertEquals('Default Organization', $result[0]['civicrm_contact_sort_name']);
196 $this->assertEquals('Second Domain', $result[1]['civicrm_contact_sort_name']);
197 $this->assertEquals('15 Main St', $result[1]['civicrm_address_street_address']);
e70a7fc0 198 */
e2779f6e
E
199 }
200
b736d2b6
SP
201 /**
202 * Test getrows on Mailing Opened report.
203 */
204 public function testReportTemplateGetRowsMailingUniqueOpened() {
205 $description = "Retrieve rows from a mailing opened report template.";
1d291c60 206 $this->loadXMLDataSet(dirname(__FILE__) . '/../../CRM/Mailing/BAO/queryDataset.xml');
ead1a15e
MD
207
208 // Check total rows without distinct
209 global $_REQUEST;
210 $_REQUEST['distinct'] = 0;
9099cab3 211 $result = $this->callAPIAndDocument('report_template', 'getrows', [
b736d2b6 212 'report_id' => 'Mailing/opened',
9099cab3
CW
213 'options' => ['metadata' => ['labels', 'title']],
214 ], __FUNCTION__, __FILE__, $description, 'Getrows');
ead1a15e 215 $this->assertEquals(14, $result['count']);
b736d2b6 216
ead1a15e
MD
217 // Check total rows with distinct
218 $_REQUEST['distinct'] = 1;
9099cab3 219 $result = $this->callAPIAndDocument('report_template', 'getrows', [
ead1a15e 220 'report_id' => 'Mailing/opened',
9099cab3
CW
221 'options' => ['metadata' => ['labels', 'title']],
222 ], __FUNCTION__, __FILE__, $description, 'Getrows');
ead1a15e
MD
223 $this->assertEquals(5, $result['count']);
224
225 // Check total rows with distinct by passing NULL value to distinct parameter
226 $_REQUEST['distinct'] = NULL;
9099cab3 227 $result = $this->callAPIAndDocument('report_template', 'getrows', [
ead1a15e 228 'report_id' => 'Mailing/opened',
9099cab3
CW
229 'options' => ['metadata' => ['labels', 'title']],
230 ], __FUNCTION__, __FILE__, $description, 'Getrows');
ead1a15e 231 $this->assertEquals(5, $result['count']);
b736d2b6
SP
232 }
233
e2779f6e 234 /**
63dc1f23 235 * Test api to get rows from reports.
fe482240 236 *
e2779f6e 237 * @dataProvider getReportTemplates
fe482240 238 *
1e1fdcf6 239 * @param $reportID
fe482240 240 *
a6439b6a 241 * @throws \PHPUnit\Framework\IncompleteTestError
e2779f6e 242 */
00be9182 243 public function testReportTemplateGetRowsAllReports($reportID) {
63dc1f23 244 //$reportID = 'logging/contact/summary';
22e263ad 245 if (stristr($reportID, 'has existing issues')) {
e2779f6e
E
246 $this->markTestIncomplete($reportID);
247 }
63dc1f23 248 if (substr($reportID, 0, '7') === 'logging') {
249 Civi::settings()->set('logging', 1);
250 }
251
9099cab3 252 $this->callAPISuccess('report_template', 'getrows', [
92915c55 253 'report_id' => $reportID,
9099cab3 254 ]);
63dc1f23 255 if (substr($reportID, 0, '7') === 'logging') {
256 Civi::settings()->set('logging', 0);
257 }
e2779f6e
E
258 }
259
f2f65d33 260 /**
261 * Test logging report when a custom data table has a table removed by hook.
262 *
263 * Here we are checking that no fatal is triggered.
264 */
265 public function testLoggingReportWithHookRemovalOfCustomDataTable() {
266 Civi::settings()->set('logging', 1);
267 $group1 = $this->customGroupCreate();
268 $group2 = $this->customGroupCreate(['name' => 'second_one', 'title' => 'second one', 'table_name' => 'civicrm_value_second_one']);
9099cab3
CW
269 $this->customFieldCreate(['custom_group_id' => $group1['id'], 'label' => 'field one']);
270 $this->customFieldCreate(['custom_group_id' => $group2['id'], 'label' => 'field two']);
271 $this->hookClass->setHook('civicrm_alterLogTables', [$this, 'alterLogTablesRemoveCustom']);
f2f65d33 272
9099cab3 273 $this->callAPISuccess('report_template', 'getrows', [
f2f65d33 274 'report_id' => 'logging/contact/summary',
9099cab3 275 ]);
f2f65d33 276 Civi::settings()->set('logging', 0);
277 $this->customGroupDelete($group1['id']);
278 $this->customGroupDelete($group2['id']);
279 }
280
281 /**
282 * Remove one log table from the logging spec.
283 *
284 * @param array $logTableSpec
285 */
286 public function alterLogTablesRemoveCustom(&$logTableSpec) {
287 unset($logTableSpec['civicrm_value_second_one']);
e2779f6e
E
288 }
289
6c8223f6 290 /**
291 * Test api to get rows from reports with ACLs enabled.
292 *
293 * Checking for lack of fatal error at the moment.
294 *
295 * @dataProvider getReportTemplates
296 *
297 * @param $reportID
298 *
a6439b6a 299 * @throws \PHPUnit\Framework\IncompleteTestError
6c8223f6 300 */
301 public function testReportTemplateGetRowsAllReportsACL($reportID) {
302 if (stristr($reportID, 'has existing issues')) {
303 $this->markTestIncomplete($reportID);
304 }
9099cab3
CW
305 $this->hookClass->setHook('civicrm_aclWhereClause', [$this, 'aclWhereHookNoResults']);
306 $this->callAPISuccess('report_template', 'getrows', [
6c8223f6 307 'report_id' => $reportID,
9099cab3 308 ]);
6c8223f6 309 }
310
e2779f6e 311 /**
fe482240
EM
312 * Test get statistics.
313 *
e2779f6e 314 * @dataProvider getReportTemplates
fe482240 315 *
1e1fdcf6 316 * @param $reportID
fe482240 317 *
a6439b6a 318 * @throws \PHPUnit\Framework\IncompleteTestError
e2779f6e 319 */
00be9182 320 public function testReportTemplateGetStatisticsAllReports($reportID) {
22e263ad 321 if (stristr($reportID, 'has existing issues')) {
e2779f6e
E
322 $this->markTestIncomplete($reportID);
323 }
9099cab3 324 if (in_array($reportID, ['contribute/softcredit', 'contribute/bookkeeping'])) {
e2779f6e
E
325 $this->markTestIncomplete($reportID . " has non enotices when calling statistics fn");
326 }
5c49fee0 327 $description = "Get Statistics from a report (note there isn't much data to get in the test DB).";
f5cae495
SL
328 if ($reportID === 'contribute/summary') {
329 $this->hookClass->setHook('civicrm_alterReportVar', [$this, 'alterReportVarHook']);
330 }
9099cab3 331 $result = $this->callAPIAndDocument('report_template', 'getstatistics', [
e2779f6e 332 'report_id' => $reportID,
9099cab3 333 ], __FUNCTION__, __FILE__, $description, 'Getstatistics', 'getstatistics');
e2779f6e
E
334 }
335
f5cae495
SL
336 /**
337 * Implements hook_civicrm_alterReportVar().
338 */
339 public function alterReportVarHook($varType, &$var, &$object) {
340 if ($varType === 'sql' && $object instanceof CRM_Report_Form_Contribute_Summary) {
341 $from = $var->getVar('_from');
342 $from .= " LEFT JOIN civicrm_financial_type as temp ON temp.id = contribution_civireport.financial_type_id";
343 $var->setVar('_from', $from);
344 $where = $var->getVar('_where');
345 $where .= " AND ( temp.id IS NOT NULL )";
346 $var->setVar('_where', $where);
347 }
348 }
349
e2779f6e 350 /**
fe482240
EM
351 * Data provider function for getting all templates.
352 *
353 * Note that the function needs to
e2779f6e
E
354 * be static so cannot use $this->callAPISuccess
355 */
356 public static function getReportTemplates() {
9099cab3 357 $reportsToSkip = [
92915c55 358 'event/income' => 'I do no understand why but error is Call to undefined method CRM_Report_Form_Event_Income::from() in CRM/Report/Form.php on line 2120',
92915c55 359 'contribute/history' => 'Declaration of CRM_Report_Form_Contribute_History::buildRows() should be compatible with CRM_Report_Form::buildRows($sql, &$rows)',
9099cab3 360 ];
e2779f6e 361
9099cab3 362 $reports = civicrm_api3('report_template', 'get', ['return' => 'value', 'options' => ['limit' => 500]]);
e2779f6e 363 foreach ($reports['values'] as $report) {
22e263ad 364 if (empty($reportsToSkip[$report['value']])) {
9099cab3 365 $reportTemplates[] = [$report['value']];
e2779f6e
E
366 }
367 else {
9099cab3 368 $reportTemplates[] = [$report['value'] . " has existing issues : " . $reportsToSkip[$report['value']]];
e2779f6e
E
369 }
370 }
371
e2779f6e
E
372 return $reportTemplates;
373 }
96025800 374
2c6b4783 375 /**
376 * Get contribution templates that work with basic filter tests.
377 *
378 * These templates require minimal data config.
379 */
380 public static function getContributionReportTemplates() {
9099cab3 381 return [['contribute/summary'], ['contribute/detail'], ['contribute/repeat'], ['topDonor' => 'contribute/topDonor']];
eae0f0d9 382 }
383
384 /**
385 * Get contribution templates that work with basic filter tests.
386 *
387 * These templates require minimal data config.
388 */
389 public static function getMembershipReportTemplates() {
9099cab3 390 return [['member/detail']];
eae0f0d9 391 }
392
c69ecb12 393 /**
394 * Get the membership and contribution reports to test.
395 *
396 * @return array
397 */
eae0f0d9 398 public static function getMembershipAndContributionReportTemplatesForGroupTests() {
399 $templates = array_merge(self::getContributionReportTemplates(), self::getMembershipReportTemplates());
400 foreach ($templates as $key => $value) {
401 if (array_key_exists('topDonor', $value)) {
402 // Report is not standard enough to test here.
403 unset($templates[$key]);
404 }
405
406 }
407 return $templates;
2c6b4783 408 }
409
c160fde8 410 /**
411 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
412 */
413 public function testLybuntReportWithData() {
414 $inInd = $this->individualCreate();
415 $outInd = $this->individualCreate();
9099cab3
CW
416 $this->contributionCreate(['contact_id' => $inInd, 'receive_date' => '2014-03-01']);
417 $this->contributionCreate(['contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL]);
418 $rows = $this->callAPISuccess('report_template', 'getrows', [
c160fde8 419 'report_id' => 'contribute/lybunt',
420 'yid_value' => 2015,
421 'yid_op' => 'calendar',
9099cab3
CW
422 'options' => ['metadata' => ['sql']],
423 ]);
c160fde8 424 $this->assertEquals(1, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
425 }
426
33072bc7 427 /**
428 * Test Lybunt report applies ACLs.
429 */
430 public function testLybuntReportWithDataAndACLFilter() {
9099cab3 431 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM'];
33072bc7 432 $inInd = $this->individualCreate();
433 $outInd = $this->individualCreate();
9099cab3
CW
434 $this->contributionCreate(['contact_id' => $inInd, 'receive_date' => '2014-03-01']);
435 $this->contributionCreate(['contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL]);
436 $this->hookClass->setHook('civicrm_aclWhereClause', [$this, 'aclWhereHookNoResults']);
437 $params = [
33072bc7 438 'report_id' => 'contribute/lybunt',
439 'yid_value' => 2015,
440 'yid_op' => 'calendar',
9099cab3 441 'options' => ['metadata' => ['sql']],
33072bc7 442 'check_permissions' => 1,
9099cab3 443 ];
33072bc7 444
445 $rows = $this->callAPISuccess('report_template', 'getrows', $params);
446 $this->assertEquals(0, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
447
448 CRM_Utils_Hook::singleton()->reset();
449 }
450
c160fde8 451 /**
452 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
453 */
454 public function testLybuntReportWithFYData() {
455 $inInd = $this->individualCreate();
456 $outInd = $this->individualCreate();
9099cab3
CW
457 $this->contributionCreate(['contact_id' => $inInd, 'receive_date' => '2014-10-01']);
458 $this->contributionCreate(['contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL]);
459 $this->callAPISuccess('Setting', 'create', ['fiscalYearStart' => ['M' => 7, 'd' => 1]]);
460 $rows = $this->callAPISuccess('report_template', 'getrows', [
c160fde8 461 'report_id' => 'contribute/lybunt',
462 'yid_value' => 2015,
463 'yid_op' => 'fiscal',
9099cab3
CW
464 'options' => ['metadata' => ['sql']],
465 'order_bys' => [
466 [
c160fde8 467 'column' => 'first_name',
468 'order' => 'ASC',
9099cab3
CW
469 ],
470 ],
471 ]);
c160fde8 472
473 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
15d9e604 474
03843223 475 $expected = preg_replace('/\s+/', ' ', 'DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci AS
15d9e604 476 SELECT SQL_CALC_FOUND_ROWS contact_civireport.id as cid FROM civicrm_contact contact_civireport INNER JOIN civicrm_contribution contribution_civireport USE index (received_date) ON contribution_civireport.contact_id = contact_civireport.id
477 AND contribution_civireport.is_test = 0
478 AND contribution_civireport.receive_date BETWEEN \'20140701000000\' AND \'20150630235959\'
1679e19c 479 WHERE contact_civireport.id NOT IN (
480 SELECT cont_exclude.contact_id
481 FROM civicrm_contribution cont_exclude
482 WHERE cont_exclude.receive_date BETWEEN \'2015-7-1\' AND \'20160630235959\')
483 AND ( contribution_civireport.contribution_status_id IN (1) )
484 GROUP BY contact_civireport.id');
485 // Exclude whitespace in comparison as we don't care if it changes & this allows us to make the above readable.
486 $whitespacelessSql = preg_replace('/\s+/', ' ', $rows['metadata']['sql'][0]);
487 $this->assertContains($expected, $whitespacelessSql);
c160fde8 488 }
489
3fd9a92a 490 /**
491 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
492 */
493 public function testLybuntReportWithFYDataOrderByLastYearAmount() {
494 $inInd = $this->individualCreate();
495 $outInd = $this->individualCreate();
9099cab3
CW
496 $this->contributionCreate(['contact_id' => $inInd, 'receive_date' => '2014-10-01']);
497 $this->contributionCreate(['contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL]);
498 $this->callAPISuccess('Setting', 'create', ['fiscalYearStart' => ['M' => 7, 'd' => 1]]);
499 $rows = $this->callAPISuccess('report_template', 'getrows', [
3fd9a92a 500 'report_id' => 'contribute/lybunt',
501 'yid_value' => 2015,
502 'yid_op' => 'fiscal',
9099cab3
CW
503 'options' => ['metadata' => ['sql']],
504 'fields' => ['first_name'],
505 'order_bys' => [
506 [
3fd9a92a 507 'column' => 'last_year_total_amount',
508 'order' => 'ASC',
9099cab3
CW
509 ],
510 ],
511 ]);
3fd9a92a 512
513 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
514 }
515
2c6b4783 516 /**
517 * Test the group filter works on the contribution summary (with a smart group).
eae0f0d9 518 *
519 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
520 *
521 * @param string $template
522 * Name of the template to test.
2c6b4783 523 */
eae0f0d9 524 public function testContributionSummaryWithSmartGroupFilter($template) {
2c6b4783 525 $groupID = $this->setUpPopulatedSmartGroup();
9099cab3 526 $rows = $this->callAPISuccess('report_template', 'getrows', [
eae0f0d9 527 'report_id' => $template,
2c6b4783 528 'gid_value' => $groupID,
529 'gid_op' => 'in',
9099cab3
CW
530 'options' => ['metadata' => ['sql']],
531 ]);
eae0f0d9 532 $this->assertNumberOfContactsInResult(3, $rows, $template);
533 if ($template === 'contribute/summary') {
534 $this->assertEquals(3, $rows['values'][0]['civicrm_contribution_total_amount_count']);
535 }
2c6b4783 536 }
537
538 /**
eae0f0d9 539 * Test the group filter works on the contribution summary.
540 *
541 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
2c6b4783 542 */
eae0f0d9 543 public function testContributionSummaryWithNotINSmartGroupFilter($template) {
2c6b4783 544 $groupID = $this->setUpPopulatedSmartGroup();
9099cab3 545 $rows = $this->callAPISuccess('report_template', 'getrows', [
2c6b4783 546 'report_id' => 'contribute/summary',
547 'gid_value' => $groupID,
43c1fa19 548 'gid_op' => 'notin',
9099cab3
CW
549 'options' => ['metadata' => ['sql']],
550 ]);
2c6b4783 551 $this->assertEquals(2, $rows['values'][0]['civicrm_contribution_total_amount_count']);
e6bab5ea 552 }
2c6b4783 553
0f880d50 554 /**
555 * Test no fatal on order by per https://lab.civicrm.org/dev/core/issues/739
556 */
557 public function testCaseDetailsCaseTypeHeader() {
558 $this->callAPISuccess('report_template', 'getrows', [
559 'report_id' => 'case/detail',
560 'fields' => ['subject' => 1, 'client_sort_name' => 1],
39b959db
SL
561 'order_bys' => [
562 1 => [
0f880d50 563 'column' => 'case_type_title',
564 'order' => 'ASC',
565 'section' => '1',
566 ],
567 ],
568 ]);
569 }
570
e6bab5ea 571 /**
572 * Test the group filter works on the contribution summary.
573 */
574 public function testContributionDetailSoftCredits() {
575 $contactID = $this->individualCreate();
576 $contactID2 = $this->individualCreate();
577 $this->contributionCreate(['contact_id' => $contactID, 'api.ContributionSoft.create' => ['amount' => 5, 'contact_id' => $contactID2]]);
578 $template = 'contribute/detail';
9099cab3 579 $rows = $this->callAPISuccess('report_template', 'getrows', [
e6bab5ea 580 'report_id' => $template,
581 'contribution_or_soft_value' => 'contributions_only',
582 'fields' => ['soft_credits' => 1, 'contribution_or_soft' => 1, 'sort_name' => 1],
9099cab3
CW
583 'options' => ['metadata' => ['sql']],
584 ]);
e6bab5ea 585 $this->assertEquals(
586 "<a href='/index.php?q=civicrm/contact/view&amp;reset=1&amp;cid=" . $contactID2 . "'>Anderson, Anthony</a> $ 5.00",
587 $rows['values'][0]['civicrm_contribution_soft_credits']
588 );
2c6b4783 589 }
590
d70ada18 591 /**
592 * Test the amount column is populated on soft credit details.
593 */
594 public function testContributionDetailSoftCreditsOnly() {
595 $contactID = $this->individualCreate();
596 $contactID2 = $this->individualCreate();
597 $this->contributionCreate(['contact_id' => $contactID, 'api.ContributionSoft.create' => ['amount' => 5, 'contact_id' => $contactID2]]);
598 $template = 'contribute/detail';
9099cab3 599 $rows = $this->callAPISuccess('report_template', 'getrows', [
d70ada18 600 'report_id' => $template,
601 'contribution_or_soft_value' => 'soft_credits_only',
602 'fields' => [
603 'sort_name' => '1',
604 'email' => '1',
605 'financial_type_id' => '1',
606 'receive_date' => '1',
607 'total_amount' => '1',
608 ],
9099cab3
CW
609 'options' => ['metadata' => ['sql', 'labels']],
610 ]);
d70ada18 611 foreach (array_keys($rows['metadata']['labels']) as $header) {
612 $this->assertTrue(!empty($rows['values'][0][$header]));
613 }
614 }
615
2c6b4783 616 /**
eae0f0d9 617 * Test the group filter works on the various reports.
2c6b4783 618 *
eae0f0d9 619 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
2c6b4783 620 *
621 * @param string $template
622 * Report template unique identifier.
623 */
eae0f0d9 624 public function testReportsWithNonSmartGroupFilter($template) {
2c6b4783 625 $groupID = $this->setUpPopulatedGroup();
9099cab3 626 $rows = $this->callAPISuccess('report_template', 'getrows', [
2c6b4783 627 'report_id' => $template,
9099cab3 628 'gid_value' => [$groupID],
2c6b4783 629 'gid_op' => 'in',
9099cab3
CW
630 'options' => ['metadata' => ['sql']],
631 ]);
2c6b4783 632 $this->assertNumberOfContactsInResult(1, $rows, $template);
633 }
634
635 /**
636 * Assert the included results match the expected.
637 *
638 * There may or may not be a group by in play so the assertion varies a little.
639 *
640 * @param int $numberExpected
641 * @param array $rows
642 * Rows returned from the report.
643 * @param string $template
644 */
645 protected function assertNumberOfContactsInResult($numberExpected, $rows, $template) {
646 if (isset($rows['values'][0]['civicrm_contribution_total_amount_count'])) {
647 $this->assertEquals($numberExpected, $rows['values'][0]['civicrm_contribution_total_amount_count'], 'wrong row count in ' . $template);
648 }
649 else {
650 $this->assertEquals($numberExpected, count($rows['values']), 'wrong row count in ' . $template);
651 }
652 }
653
654 /**
655 * Test the group filter works on the contribution summary when 2 groups are involved.
656 */
657 public function testContributionSummaryWithTwoGroups() {
658 $groupID = $this->setUpPopulatedGroup();
659 $groupID2 = $this->setUpPopulatedSmartGroup();
9099cab3 660 $rows = $this->callAPISuccess('report_template', 'getrows', [
2c6b4783 661 'report_id' => 'contribute/summary',
9099cab3 662 'gid_value' => [$groupID, $groupID2],
2c6b4783 663 'gid_op' => 'in',
9099cab3
CW
664 'options' => ['metadata' => ['sql']],
665 ]);
2c6b4783 666 $this->assertEquals(4, $rows['values'][0]['civicrm_contribution_total_amount_count']);
667 }
668
27367f58 669 /**
670 * CRM-20640: Test the group filter works on the contribution summary when a single contact in 2 groups.
671 */
672 public function testContributionSummaryWithSingleContactsInTwoGroups() {
673 list($groupID1, $individualID) = $this->setUpPopulatedGroup(TRUE);
674 // create second group and add the individual to it.
9099cab3
CW
675 $groupID2 = $this->groupCreate(['name' => uniqid(), 'title' => uniqid()]);
676 $this->callAPISuccess('GroupContact', 'create', [
27367f58 677 'group_id' => $groupID2,
678 'contact_id' => $individualID,
679 'status' => 'Added',
9099cab3 680 ]);
27367f58 681
9099cab3 682 $rows = $this->callAPISuccess('report_template', 'getrows', [
27367f58 683 'report_id' => 'contribute/summary',
9099cab3 684 'gid_value' => [$groupID1, $groupID2],
27367f58 685 'gid_op' => 'in',
9099cab3
CW
686 'options' => ['metadata' => ['sql']],
687 ]);
27367f58 688 $this->assertEquals(1, $rows['count']);
689 }
690
2c6b4783 691 /**
692 * Test the group filter works on the contribution summary when 2 groups are involved.
693 */
694 public function testContributionSummaryWithTwoGroupsWithIntersection() {
695 $groups = $this->setUpIntersectingGroups();
696
9099cab3 697 $rows = $this->callAPISuccess('report_template', 'getrows', [
2c6b4783 698 'report_id' => 'contribute/summary',
699 'gid_value' => $groups,
700 'gid_op' => 'in',
9099cab3
CW
701 'options' => ['metadata' => ['sql']],
702 ]);
2c6b4783 703 $this->assertEquals(7, $rows['values'][0]['civicrm_contribution_total_amount_count']);
704 }
705
706 /**
707 * Set up a smart group for testing.
708 *
709 * The smart group includes all Households by filter. In addition an individual
710 * is created and hard-added and an individual is created that is not added.
711 *
712 * One household is hard-added as well as being in the filter.
713 *
714 * This gives us a range of scenarios for testing contacts are included only once
715 * whenever they are hard-added or in the criteria.
716 *
717 * @return int
718 */
719 public function setUpPopulatedSmartGroup() {
720 $household1ID = $this->householdCreate();
721 $individual1ID = $this->individualCreate();
722 $householdID = $this->householdCreate();
723 $individualID = $this->individualCreate();
724 $individualIDRemoved = $this->individualCreate();
9099cab3
CW
725 $groupID = $this->smartGroupCreate([], ['name' => uniqid(), 'title' => uniqid()]);
726 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 727 'group_id' => $groupID,
728 'contact_id' => $individualIDRemoved,
729 'status' => 'Removed',
9099cab3
CW
730 ]);
731 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 732 'group_id' => $groupID,
733 'contact_id' => $individualID,
734 'status' => 'Added',
9099cab3
CW
735 ]);
736 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 737 'group_id' => $groupID,
738 'contact_id' => $householdID,
739 'status' => 'Added',
9099cab3
CW
740 ]);
741 foreach ([$household1ID, $individual1ID, $householdID, $individualID, $individualIDRemoved] as $contactID) {
742 $this->contributionCreate(['contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => '']);
743 $this->contactMembershipCreate(['contact_id' => $contactID]);
2c6b4783 744 }
745
746 // Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
0626851e 747 CRM_Contact_BAO_GroupContactCache::clearGroupContactCache($groupID);
2c6b4783 748 return $groupID;
749 }
750
751 /**
eae0f0d9 752 * Set up a static group for testing.
2c6b4783 753 *
eae0f0d9 754 * An individual is created and hard-added and an individual is created that is not added.
2c6b4783 755 *
756 * This gives us a range of scenarios for testing contacts are included only once
757 * whenever they are hard-added or in the criteria.
758 *
27367f58 759 * @param bool $returnAddedContact
760 *
2c6b4783 761 * @return int
762 */
27367f58 763 public function setUpPopulatedGroup($returnAddedContact = FALSE) {
2c6b4783 764 $individual1ID = $this->individualCreate();
765 $individualID = $this->individualCreate();
766 $individualIDRemoved = $this->individualCreate();
9099cab3
CW
767 $groupID = $this->groupCreate(['name' => uniqid(), 'title' => uniqid()]);
768 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 769 'group_id' => $groupID,
770 'contact_id' => $individualIDRemoved,
771 'status' => 'Removed',
9099cab3
CW
772 ]);
773 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 774 'group_id' => $groupID,
775 'contact_id' => $individualID,
776 'status' => 'Added',
9099cab3 777 ]);
2c6b4783 778
9099cab3
CW
779 foreach ([$individual1ID, $individualID, $individualIDRemoved] as $contactID) {
780 $this->contributionCreate(['contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => '']);
781 $this->contactMembershipCreate(['contact_id' => $contactID]);
2c6b4783 782 }
783
784 // Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
0626851e 785 CRM_Contact_BAO_GroupContactCache::clearGroupContactCache($groupID);
27367f58 786
787 if ($returnAddedContact) {
9099cab3 788 return [$groupID, $individualID];
27367f58 789 }
790
2c6b4783 791 return $groupID;
792 }
793
794 /**
795 * @return array
796 */
797 public function setUpIntersectingGroups() {
798 $groupID = $this->setUpPopulatedGroup();
799 $groupID2 = $this->setUpPopulatedSmartGroup();
800 $addedToBothIndividualID = $this->individualCreate();
801 $removedFromBothIndividualID = $this->individualCreate();
802 $addedToSmartGroupRemovedFromOtherIndividualID = $this->individualCreate();
803 $removedFromSmartGroupAddedToOtherIndividualID = $this->individualCreate();
9099cab3 804 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 805 'group_id' => $groupID,
806 'contact_id' => $addedToBothIndividualID,
807 'status' => 'Added',
9099cab3
CW
808 ]);
809 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 810 'group_id' => $groupID2,
811 'contact_id' => $addedToBothIndividualID,
812 'status' => 'Added',
9099cab3
CW
813 ]);
814 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 815 'group_id' => $groupID,
816 'contact_id' => $removedFromBothIndividualID,
817 'status' => 'Removed',
9099cab3
CW
818 ]);
819 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 820 'group_id' => $groupID2,
821 'contact_id' => $removedFromBothIndividualID,
822 'status' => 'Removed',
9099cab3
CW
823 ]);
824 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 825 'group_id' => $groupID2,
826 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
827 'status' => 'Added',
9099cab3
CW
828 ]);
829 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 830 'group_id' => $groupID,
831 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
832 'status' => 'Removed',
9099cab3
CW
833 ]);
834 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 835 'group_id' => $groupID,
836 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
837 'status' => 'Added',
9099cab3
CW
838 ]);
839 $this->callAPISuccess('GroupContact', 'create', [
2c6b4783 840 'group_id' => $groupID2,
841 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
842 'status' => 'Removed',
9099cab3 843 ]);
2c6b4783 844
9099cab3 845 foreach ([
39b959db
SL
846 $addedToBothIndividualID,
847 $removedFromBothIndividualID,
848 $addedToSmartGroupRemovedFromOtherIndividualID,
849 $removedFromSmartGroupAddedToOtherIndividualID,
9099cab3
CW
850 ] as $contactID) {
851 $this->contributionCreate([
2c6b4783 852 'contact_id' => $contactID,
853 'invoice_id' => '',
854 'trxn_id' => '',
9099cab3 855 ]);
2c6b4783 856 }
9099cab3 857 return [$groupID, $groupID2];
2c6b4783 858 }
859
45f8590c
PN
860 /**
861 * Test Deferred Revenue Report.
fc7106d2 862 *
863 * @throws \CRM_Core_Exception
45f8590c
PN
864 */
865 public function testDeferredRevenueReport() {
866 $indv1 = $this->individualCreate();
867 $indv2 = $this->individualCreate();
fc7106d2 868 Civi::settings()->set('deferred_revenue_enabled', TRUE);
45f8590c 869 $this->contributionCreate(
9099cab3 870 [
45f8590c
PN
871 'contact_id' => $indv1,
872 'receive_date' => '2016-10-01',
873 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+3 month')),
874 'financial_type_id' => 2,
9099cab3 875 ]
45f8590c
PN
876 );
877 $this->contributionCreate(
9099cab3 878 [
45f8590c
PN
879 'contact_id' => $indv1,
880 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+22 month')),
881 'financial_type_id' => 4,
882 'trxn_id' => NULL,
883 'invoice_id' => NULL,
9099cab3 884 ]
45f8590c
PN
885 );
886 $this->contributionCreate(
9099cab3 887 [
45f8590c
PN
888 'contact_id' => $indv2,
889 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+1 month')),
890 'financial_type_id' => 4,
891 'trxn_id' => NULL,
892 'invoice_id' => NULL,
9099cab3 893 ]
45f8590c
PN
894 );
895 $this->contributionCreate(
9099cab3 896 [
45f8590c
PN
897 'contact_id' => $indv2,
898 'receive_date' => '2016-03-01',
899 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+4 month')),
900 'financial_type_id' => 2,
901 'trxn_id' => NULL,
902 'invoice_id' => NULL,
9099cab3 903 ]
45f8590c 904 );
9099cab3 905 $rows = $this->callAPISuccess('report_template', 'getrows', [
45f8590c 906 'report_id' => 'contribute/deferredrevenue',
9099cab3 907 ]);
45f8590c 908 $this->assertEquals(2, $rows['count'], "Report failed to get row count");
9099cab3 909 $count = [2, 1];
45f8590c
PN
910 foreach ($rows['values'] as $row) {
911 $this->assertEquals(array_pop($count), count($row['rows']), "Report failed to get row count");
912 }
913 }
d7a896b4 914
915 /**
916 * Test the custom data order by works when not in select.
917 *
918 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
919 *
920 * @param string $template
921 * Report template unique identifier.
922 *
923 * @throws \CRM_Core_Exception
924 */
925 public function testReportsCustomDataOrderBy($template) {
926 $this->entity = 'Contact';
927 $this->createCustomGroupWithFieldOfType();
928 $this->callAPISuccess('report_template', 'getrows', [
929 'report_id' => $template,
930 'contribution_or_soft_value' => 'contributions_only',
931 'order_bys' => [['column' => 'custom_' . $this->ids['CustomField']['text'], 'order' => 'ASC']],
932 ]);
933 }
45f8590c 934
5e3dec81
JP
935 /**
936 * Test the group filter works on the various reports.
937 *
938 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
939 *
940 * @param string $template
941 * Report template unique identifier.
942 */
943 public function testReportsWithNoTInSmartGroupFilter($template) {
944 $groupID = $this->setUpPopulatedGroup();
9099cab3 945 $rows = $this->callAPISuccess('report_template', 'getrows', [
5e3dec81 946 'report_id' => $template,
9099cab3 947 'gid_value' => [$groupID],
5e3dec81 948 'gid_op' => 'notin',
9099cab3
CW
949 'options' => ['metadata' => ['sql']],
950 ]);
5e3dec81
JP
951 $this->assertNumberOfContactsInResult(2, $rows, $template);
952 }
953
5a14305b 954 /**
d5625bc9 955 * Test activity details report - requiring all current fields to be output.
5a14305b 956 */
d5625bc9 957 public function testActivityDetails() {
5a14305b 958 $this->createContactsWithActivities();
959 $fields = [
960 'contact_source' => '1',
961 'contact_assignee' => '1',
962 'contact_target' => '1',
963 'contact_source_email' => '1',
964 'contact_assignee_email' => '1',
965 'contact_target_email' => '1',
966 'contact_source_phone' => '1',
967 'contact_assignee_phone' => '1',
968 'contact_target_phone' => '1',
969 'activity_type_id' => '1',
970 'activity_subject' => '1',
971 'activity_date_time' => '1',
972 'status_id' => '1',
973 'duration' => '1',
974 'location' => '1',
975 'details' => '1',
976 'priority_id' => '1',
977 'result' => '1',
978 'engagement_level' => '1',
979 'address_name' => '1',
980 'street_address' => '1',
981 'supplemental_address_1' => '1',
982 'supplemental_address_2' => '1',
983 'supplemental_address_3' => '1',
984 'street_number' => '1',
985 'street_name' => '1',
986 'street_unit' => '1',
987 'city' => '1',
988 'postal_code' => '1',
989 'postal_code_suffix' => '1',
990 'country_id' => '1',
991 'state_province_id' => '1',
992 'county_id' => '1',
993 ];
994 $params = [
995 'fields' => $fields,
996 'current_user_op' => 'eq',
997 'current_user_value' => '0',
998 'include_case_activities_op' => 'eq',
999 'include_case_activities_value' => 0,
87e5be4b 1000 'order_bys' => [
1001 1 => ['column' => 'activity_date_time', 'order' => 'ASC'],
1002 2 => ['column' => 'activity_type_id', 'order' => 'ASC'],
1003 ],
5a14305b 1004 ];
1005
1006 $params['report_id'] = 'Activity';
1007
1008 $rows = $this->callAPISuccess('report_template', 'getrows', $params)['values'];
1009 $expected = [
1010 'civicrm_contact_contact_source' => 'Łąchowski-Roberts, Anthony',
1011 'civicrm_contact_contact_assignee' => '<a title=\'View Contact Summary for this Contact\' href=\'/index.php?q=civicrm/contact/view&amp;reset=1&amp;cid=4\'>Łąchowski-Roberts, Anthony</a>',
1012 'civicrm_contact_contact_target' => '<a title=\'View Contact Summary for this Contact\' href=\'/index.php?q=civicrm/contact/view&amp;reset=1&amp;cid=3\'>Brzęczysław, Anthony</a>; <a title=\'View Contact Summary for this Contact\' href=\'/index.php?q=civicrm/contact/view&amp;reset=1&amp;cid=4\'>Łąchowski-Roberts, Anthony</a>',
1013 'civicrm_contact_contact_source_id' => $this->contactIDs[2],
1014 'civicrm_contact_contact_assignee_id' => $this->contactIDs[1],
1015 'civicrm_contact_contact_target_id' => $this->contactIDs[0] . ';' . $this->contactIDs[1],
1016 'civicrm_email_contact_source_email' => 'anthony_anderson@civicrm.org',
1017 'civicrm_email_contact_assignee_email' => 'anthony_anderson@civicrm.org',
87e5be4b 1018 'civicrm_email_contact_target_email' => 'techo@spying.com;anthony_anderson@civicrm.org',
5a14305b 1019 'civicrm_phone_contact_source_phone' => NULL,
1020 'civicrm_phone_contact_assignee_phone' => NULL,
1021 'civicrm_phone_contact_target_phone' => NULL,
1022 'civicrm_activity_id' => '1',
1023 'civicrm_activity_source_record_id' => NULL,
1024 'civicrm_activity_activity_type_id' => 'Meeting',
1025 'civicrm_activity_activity_subject' => 'Very secret meeting',
486d6c5c 1026 'civicrm_activity_activity_date_time' => date('Y-m-d 23:59:58', strtotime('now')),
5a14305b 1027 'civicrm_activity_status_id' => 'Scheduled',
1028 'civicrm_activity_duration' => '120',
1029 'civicrm_activity_location' => 'Pennsylvania',
1030 'civicrm_activity_details' => 'a test activity',
1031 'civicrm_activity_priority_id' => 'Normal',
1032 'civicrm_address_address_name' => NULL,
1033 'civicrm_address_street_address' => NULL,
1034 'civicrm_address_supplemental_address_1' => NULL,
1035 'civicrm_address_supplemental_address_2' => NULL,
1036 'civicrm_address_supplemental_address_3' => NULL,
1037 'civicrm_address_street_number' => NULL,
1038 'civicrm_address_street_name' => NULL,
1039 'civicrm_address_street_unit' => NULL,
1040 'civicrm_address_city' => NULL,
1041 'civicrm_address_postal_code' => NULL,
1042 'civicrm_address_postal_code_suffix' => NULL,
1043 'civicrm_address_country_id' => NULL,
1044 'civicrm_address_state_province_id' => NULL,
1045 'civicrm_address_county_id' => NULL,
1046 'civicrm_contact_contact_source_link' => '/index.php?q=civicrm/contact/view&amp;reset=1&amp;cid=' . $this->contactIDs[2],
1047 'civicrm_contact_contact_source_hover' => 'View Contact Summary for this Contact',
1048 'civicrm_activity_activity_type_id_hover' => 'View Activity Record',
5a14305b 1049 ];
1050 $row = $rows[0];
1051 // This link is not relative - skip for now
1052 unset($row['civicrm_activity_activity_type_id_link']);
ab1ce467 1053 if ($row['civicrm_email_contact_target_email'] === 'anthony_anderson@civicrm.org;techo@spying.com') {
87e5be4b 1054 // order is unpredictable
1055 $expected['civicrm_email_contact_target_email'] = 'anthony_anderson@civicrm.org;techo@spying.com';
1056 }
5a14305b 1057
1058 $this->assertEquals($expected, $row);
1059 }
1060
1061 /**
1062 * Set up some activity data..... use some chars that challenge our utf handling.
1063 */
1064 public function createContactsWithActivities() {
87e5be4b 1065 $this->contactIDs[] = $this->individualCreate(['last_name' => 'Brzęczysław', 'email' => 'techo@spying.com']);
5a14305b 1066 $this->contactIDs[] = $this->individualCreate(['last_name' => 'Łąchowski-Roberts']);
1067 $this->contactIDs[] = $this->individualCreate(['last_name' => 'Łąchowski-Roberts']);
1068
1069 $this->callAPISuccess('Activity', 'create', [
1070 'subject' => 'Very secret meeting',
486d6c5c 1071 'activity_date_time' => date('Y-m-d 23:59:58', strtotime('now')),
5a14305b 1072 'duration' => 120,
1073 'location' => 'Pennsylvania',
1074 'details' => 'a test activity',
1075 'status_id' => 1,
1076 'activity_type_id' => 'Meeting',
1077 'source_contact_id' => $this->contactIDs[2],
9099cab3 1078 'target_contact_id' => [$this->contactIDs[0], $this->contactIDs[1]],
5a14305b 1079 'assignee_contact_id' => $this->contactIDs[1],
1080 ]);
1081 }
1082
17c0ca6f
AS
1083 /**
1084 * Test the group filter works on the contribution summary.
1085 */
1086 public function testContributionDetailTotalHeader() {
1087 $contactID = $this->individualCreate();
1088 $contactID2 = $this->individualCreate();
1089 $this->contributionCreate(['contact_id' => $contactID, 'api.ContributionSoft.create' => ['amount' => 5, 'contact_id' => $contactID2]]);
1090 $template = 'contribute/detail';
9099cab3 1091 $rows = $this->callAPISuccess('report_template', 'getrows', [
17c0ca6f
AS
1092 'report_id' => $template,
1093 'contribution_or_soft_value' => 'contributions_only',
1094 'fields' => [
1095 'sort_name' => '1',
1096 'age' => '1',
1097 'email' => '1',
1098 'phone' => '1',
1099 'financial_type_id' => '1',
1100 'receive_date' => '1',
1101 'total_amount' => '1',
39b959db 1102 ],
17c0ca6f 1103 'order_bys' => [['column' => 'sort_name', 'order' => 'ASC', 'section' => '1']],
9099cab3
CW
1104 'options' => ['metadata' => ['sql']],
1105 ]);
17c0ca6f
AS
1106 }
1107
34496b9c 1108 /**
1109 * Test contact subtype filter on grant report.
1110 */
1111 public function testGrantReportSeparatedFilter() {
1112 $contactID = $this->individualCreate(['contact_sub_type' => ['Student', 'Parent']]);
1113 $contactID2 = $this->individualCreate();
1114 $this->callAPISuccess('Grant', 'create', ['contact_id' => $contactID, 'status_id' => 1, 'grant_type_id' => 1, 'amount_total' => 1]);
1115 $this->callAPISuccess('Grant', 'create', ['contact_id' => $contactID2, 'status_id' => 1, 'grant_type_id' => 1, 'amount_total' => 1]);
1116 $rows = $this->callAPISuccess('report_template', 'getrows', [
1117 'report_id' => 'grant/detail',
1118 'contact_sub_type_op' => 'in',
1119 'contact_sub_type_value' => ['Student'],
1120 ]);
1121 $this->assertEquals(1, $rows['count']);
1122 }
1123
2fdf00e9
EL
1124 /**
1125 * Test contact subtype filter on summary report.
1126 *
1127 * @throws \CRM_Core_Exception
1128 */
1129 public function testContactSubtypeNotNull() {
1130 $this->individualCreate(['contact_sub_type' => ['Student', 'Parent']]);
1131 $this->individualCreate();
1132
1133 $rows = $this->callAPISuccess('report_template', 'getrows', [
1134 'report_id' => 'contact/summary',
1135 'contact_sub_type_op' => 'nnll',
1136 'contact_sub_type_value' => [],
1137 'contact_type_op' => 'eq',
1138 'contact_type_value' => 'Individual',
1139 ]);
1140 $this->assertEquals(1, $rows['count']);
1141 }
1142
1143 /**
1144 * Test contact subtype filter on summary report.
1145 *
1146 * @throws \CRM_Core_Exception
1147 */
1148 public function testContactSubtypeNull() {
1149 $this->individualCreate(['contact_sub_type' => ['Student', 'Parent']]);
1150 $this->individualCreate();
1151
1152 $rows = $this->callAPISuccess('report_template', 'getrows', [
1153 'report_id' => 'contact/summary',
1154 'contact_sub_type_op' => 'nll',
1155 'contact_sub_type_value' => [],
1156 'contact_type_op' => 'eq',
1157 'contact_type_value' => 'Individual',
1158 ]);
1159 $this->assertEquals(1, $rows['count']);
1160 }
1161
f8dcf338
AS
1162 /**
1163 * Test contact subtype filter on summary report.
1164 *
1165 * @throws \CRM_Core_Exception
1166 */
1167 public function testContactSubtypeIn() {
1168 $this->individualCreate(['contact_sub_type' => ['Student', 'Parent']]);
1169 $this->individualCreate();
1170
1171 $rows = $this->callAPISuccess('report_template', 'getrows', [
1172 'report_id' => 'contact/summary',
1173 'contact_sub_type_op' => 'in',
1174 'contact_sub_type_value' => ['Student'],
1175 'contact_type_op' => 'in',
1176 'contact_type_value' => 'Individual',
1177 ]);
1178 $this->assertEquals(1, $rows['count']);
1179 }
1180
1181 /**
1182 * Test contact subtype filter on summary report.
1183 *
1184 * @throws \CRM_Core_Exception
1185 */
1186 public function testContactSubtypeNotIn() {
1187 $this->individualCreate(['contact_sub_type' => ['Student', 'Parent']]);
1188 $this->individualCreate();
1189
1190 $rows = $this->callAPISuccess('report_template', 'getrows', [
1191 'report_id' => 'contact/summary',
1192 'contact_sub_type_op' => 'notin',
1193 'contact_sub_type_value' => ['Student'],
1194 'contact_type_op' => 'in',
1195 'contact_type_value' => 'Individual',
1196 ]);
1197 $this->assertEquals(1, $rows['count']);
1198 }
1199
e40ce31e
JM
1200 /**
1201 * Test PCP report to ensure total donors and total committed is accurate.
1202 */
1203 public function testPcpReportTotals() {
1204 $donor1ContactId = $this->individualCreate();
1205 $donor2ContactId = $this->individualCreate();
1206 $donor3ContactId = $this->individualCreate();
1207
1208 // We are going to create two PCP pages. We will create two contributions
1209 // on the first PCP page and one contribution on the second PCP page.
1210 //
1211 // Then, we will ensure that the first PCP page reports a total of both
1212 // contributions (but not the contribution made on the second PCP page).
1213
1214 // A PCP page requires three components:
1215 // 1. A contribution page
1216 // 2. A PCP Block
1217 // 3. A PCP page
1218
1219 // pcpBLockParams creates a contribution page and returns the parameters
1220 // necessary to create a PBP Block.
1221 $blockParams = $this->pcpBlockParams();
1222 $pcpBlock = CRM_PCP_BAO_PCPBlock::create($blockParams);
1223
1224 // Keep track of the contribution page id created. We will use this
1225 // contribution page id for all the PCP pages.
1226 $contribution_page_id = $pcpBlock->entity_id;
1227
1228 // pcpParams returns the parameters needed to create a PCP page.
1229 $pcpParams = $this->pcpParams();
1230 // Keep track of the owner of the page so we can properly apply the
1231 // soft credit.
1232 $pcpOwnerContact1Id = $pcpParams['contact_id'];
1233 $pcpParams['pcp_block_id'] = $pcpBlock->id;
1234 $pcpParams['page_id'] = $contribution_page_id;
1235 $pcpParams['page_type'] = 'contribute';
1236 $pcp1 = CRM_PCP_BAO_PCP::create($pcpParams);
1237
1238 // Nice work. Now, let's create a second PCP page.
1239 $pcpParams = $this->pcpParams();
1240 // Keep track of the owner of the page.
1241 $pcpOwnerContact2Id = $pcpParams['contact_id'];
1242 // We're using the same pcpBlock id and contribution page that we created above.
1243 $pcpParams['pcp_block_id'] = $pcpBlock->id;
1244 $pcpParams['page_id'] = $contribution_page_id;
1245 $pcpParams['page_type'] = 'contribute';
1246 $pcp2 = CRM_PCP_BAO_PCP::create($pcpParams);
1247
1248 // Get soft credit types, with the name column as the key.
9099cab3 1249 $soft_credit_types = CRM_Contribute_BAO_ContributionSoft::buildOptions("soft_credit_type_id", NULL, ["flip" => TRUE, 'labelColumn' => 'name']);
e40ce31e
JM
1250 $pcp_soft_credit_type_id = $soft_credit_types['pcp'];
1251
1252 // Create two contributions assigned to this contribution page and
1253 // assign soft credits appropriately.
1254 // FIRST...
9099cab3 1255 $contribution1params = [
e40ce31e
JM
1256 'contact_id' => $donor1ContactId,
1257 'contribution_page_id' => $contribution_page_id,
1258 'total_amount' => '75.00',
9099cab3 1259 ];
e40ce31e
JM
1260 $c1 = $this->contributionCreate($contribution1params);
1261 // Now the soft contribution.
9099cab3 1262 $p = [
e40ce31e
JM
1263 'contribution_id' => $c1,
1264 'pcp_id' => $pcp1->id,
1265 'contact_id' => $pcpOwnerContact1Id,
1266 'amount' => 75.00,
1267 'currency' => 'USD',
1268 'soft_credit_type_id' => $pcp_soft_credit_type_id,
9099cab3 1269 ];
e40ce31e
JM
1270 $this->callAPISuccess('contribution_soft', 'create', $p);
1271 // SECOND...
9099cab3 1272 $contribution2params = [
e40ce31e
JM
1273 'contact_id' => $donor2ContactId,
1274 'contribution_page_id' => $contribution_page_id,
1275 'total_amount' => '25.00',
9099cab3 1276 ];
e40ce31e
JM
1277 $c2 = $this->contributionCreate($contribution2params);
1278 // Now the soft contribution.
9099cab3 1279 $p = [
e40ce31e
JM
1280 'contribution_id' => $c2,
1281 'pcp_id' => $pcp1->id,
1282 'contact_id' => $pcpOwnerContact1Id,
1283 'amount' => 25.00,
1284 'currency' => 'USD',
1285 'soft_credit_type_id' => $pcp_soft_credit_type_id,
9099cab3 1286 ];
e40ce31e
JM
1287 $this->callAPISuccess('contribution_soft', 'create', $p);
1288
1289 // Create one contributions assigned to the second PCP page
9099cab3 1290 $contribution3params = [
e40ce31e
JM
1291 'contact_id' => $donor3ContactId,
1292 'contribution_page_id' => $contribution_page_id,
1293 'total_amount' => '200.00',
9099cab3 1294 ];
e40ce31e
JM
1295 $c3 = $this->contributionCreate($contribution3params);
1296 // Now the soft contribution.
c69ecb12 1297 $p = [
e40ce31e
JM
1298 'contribution_id' => $c3,
1299 'pcp_id' => $pcp2->id,
1300 'contact_id' => $pcpOwnerContact2Id,
1301 'amount' => 200.00,
1302 'currency' => 'USD',
1303 'soft_credit_type_id' => $pcp_soft_credit_type_id,
c69ecb12 1304 ];
e40ce31e
JM
1305 $this->callAPISuccess('contribution_soft', 'create', $p);
1306
1307 $template = 'contribute/pcp';
9099cab3 1308 $rows = $this->callAPISuccess('report_template', 'getrows', [
e40ce31e
JM
1309 'report_id' => $template,
1310 'title' => 'My PCP',
1311 'fields' => [
1312 'amount_1' => '1',
1313 'soft_id' => '1',
39b959db 1314 ],
9099cab3 1315 ]);
e40ce31e
JM
1316 $values = $rows['values'][0];
1317 $this->assertEquals(100.00, $values['civicrm_contribution_soft_amount_1_sum'], "Total commited should be $100");
1318 $this->assertEquals(2, $values['civicrm_contribution_soft_soft_id_count'], "Total donors should be 2");
1319 }
1320
38a85cdd
J
1321 /**
1322 * Test a report that uses getAddressColumns();
1323 */
1324 public function testGetAddressColumns() {
1325 $template = 'event/participantlisting';
c69ecb12 1326 $this->callAPISuccess('report_template', 'getrows', [
38a85cdd
J
1327 'report_id' => $template,
1328 'fields' => [
1329 'sort_name' => '1',
1330 'street_address' => '1',
1331 ],
1332 ]);
1333 }
1334
6a488035 1335}