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