Merge pull request #13269 from eileenmcnaughton/acltesttrait
[civicrm-core.git] / tests / phpunit / api / v3 / ReportTemplateTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test APIv3 civicrm_report_instance_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Report
33 * @group headless
34 */
35 class api_v3_ReportTemplateTest extends CiviUnitTestCase {
36
37 use CRMTraits_ACL_PermissionTrait;
38
39 protected $_apiversion = 3;
40
41 protected $contactIDs = [];
42
43 /**
44 * Our group reports use an alter so transaction cleanup won't work.
45 *
46 * @throws \Exception
47 */
48 public function tearDown() {
49 $this->quickCleanUpFinancialEntities();
50 $this->quickCleanup(array('civicrm_group', 'civicrm_saved_search', 'civicrm_group_contact', 'civicrm_group_contact_cache', 'civicrm_group'));
51 parent::tearDown();
52 }
53
54 public function testReportTemplate() {
55 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
56 'label' => 'Example Form',
57 'description' => 'Longish description of the example form',
58 'class_name' => 'CRM_Report_Form_Examplez',
59 'report_url' => 'example/path',
60 'component' => 'CiviCase',
61 ));
62 $this->assertAPISuccess($result);
63 $this->assertEquals(1, $result['count']);
64 $entityId = $result['id'];
65 $this->assertTrue(is_numeric($entityId));
66 $this->assertEquals(7, $result['values'][$entityId]['component_id']);
67 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
68 WHERE name = "CRM_Report_Form_Examplez"
69 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
70 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
71 WHERE name = "CRM_Report_Form_Examplez"');
72
73 // change component to null
74 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
75 'id' => $entityId,
76 'component' => '',
77 ));
78 $this->assertAPISuccess($result);
79 $this->assertEquals(1, $result['count']);
80 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
81 WHERE name = "CRM_Report_Form_Examplez"
82 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
83 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
84 WHERE name = "CRM_Report_Form_Examplez"
85 AND component_id IS NULL');
86
87 // deactivate
88 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
89 'id' => $entityId,
90 'is_active' => 0,
91 ));
92 $this->assertAPISuccess($result);
93 $this->assertEquals(1, $result['count']);
94 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
95 WHERE name = "CRM_Report_Form_Examplez"
96 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
97 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value
98 WHERE name = "CRM_Report_Form_Examplez"');
99
100 // activate
101 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
102 'id' => $entityId,
103 'is_active' => 1,
104 ));
105 $this->assertAPISuccess($result);
106 $this->assertEquals(1, $result['count']);
107 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
108 WHERE name = "CRM_Report_Form_Examplez"
109 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
110 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
111 WHERE name = "CRM_Report_Form_Examplez"');
112
113 $result = $this->callAPISuccess('ReportTemplate', 'delete', array(
114 'id' => $entityId,
115 ));
116 $this->assertAPISuccess($result);
117 $this->assertEquals(1, $result['count']);
118 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value
119 WHERE name = "CRM_Report_Form_Examplez"
120 ');
121 }
122
123 /**
124 * Test api to get rows from reports.
125 *
126 * @dataProvider getReportTemplatesSupportingSelectWhere
127 *
128 * @param $reportID
129 *
130 * @throws \PHPUnit_Framework_IncompleteTestError
131 */
132 public function testReportTemplateSelectWhere($reportID) {
133 $this->hookClass->setHook('civicrm_selectWhereClause', array($this, 'hookSelectWhere'));
134 $result = $this->callAPISuccess('report_template', 'getrows', [
135 'report_id' => $reportID,
136 'options' => ['metadata' => ['sql']],
137 ]);
138 $found = FALSE;
139 foreach ($result['metadata']['sql'] as $sql) {
140 if (strstr($sql, " = 'Organization' ")) {
141 $found = TRUE;
142 }
143 }
144 $this->assertTrue($found, $reportID);
145 }
146
147 /**
148 * Get templates suitable for SelectWhere test.
149 *
150 * @return array
151 */
152 public function getReportTemplatesSupportingSelectWhere() {
153 $allTemplates = $this->getReportTemplates();
154 // Exclude all that do not work as of test being written. I have not dug into why not.
155 $currentlyExcluded = [
156 'contribute/repeat',
157 'member/summary',
158 'event/summary',
159 'case/summary',
160 'case/timespent',
161 'case/demographics',
162 'contact/log',
163 'contribute/bookkeeping',
164 'grant/detail',
165 'event/incomesummary',
166 'case/detail',
167 'Mailing/bounce',
168 'Mailing/summary',
169 'grant/statistics',
170 'logging/contact/detail',
171 'logging/contact/summary',
172 ];
173 foreach ($allTemplates as $index => $template) {
174 $reportID = $template[0];
175 if (in_array($reportID, $currentlyExcluded) || stristr($reportID, 'has existing issues')) {
176 unset($allTemplates[$index]);
177 }
178 }
179 return $allTemplates;
180 }
181
182 /**
183 * @param \CRM_Core_DAO $entity
184 * @param array $clauses
185 */
186 public function hookSelectWhere($entity, &$clauses) {
187 // Restrict access to cases by type
188 if ($entity == 'Contact') {
189 $clauses['contact_type'][] = " = 'Organization' ";
190 }
191 }
192
193 /**
194 * Test getrows on contact summary report.
195 */
196 public function testReportTemplateGetRowsContactSummary() {
197 $description = "Retrieve rows from a report template (optionally providing the instance_id).";
198 $result = $this->callAPIAndDocument('report_template', 'getrows', array(
199 'report_id' => 'contact/summary',
200 'options' => array('metadata' => array('labels', 'title')),
201 ), __FUNCTION__, __FILE__, $description, 'Getrows');
202 $this->assertEquals('Contact Name', $result['metadata']['labels']['civicrm_contact_sort_name']);
203
204 //the second part of this test has been commented out because it relied on the db being reset to
205 // it's base state
206 //wasn't able to get that to work consistently
207 // however, when the db is in the base state the tests do pass
208 // and because the test covers 'all' contacts we can't create our own & assume the others don't exist
209 /*
210 $this->assertEquals(2, $result['count']);
211 $this->assertEquals('Default Organization', $result[0]['civicrm_contact_sort_name']);
212 $this->assertEquals('Second Domain', $result[1]['civicrm_contact_sort_name']);
213 $this->assertEquals('15 Main St', $result[1]['civicrm_address_street_address']);
214 */
215 }
216
217 /**
218 * Test api to get rows from reports.
219 *
220 * @dataProvider getReportTemplates
221 *
222 * @param $reportID
223 *
224 * @throws \PHPUnit_Framework_IncompleteTestError
225 */
226 public function testReportTemplateGetRowsAllReports($reportID) {
227 //$reportID = 'logging/contact/summary';
228 if (stristr($reportID, 'has existing issues')) {
229 $this->markTestIncomplete($reportID);
230 }
231 if (substr($reportID, 0, '7') === 'logging') {
232 Civi::settings()->set('logging', 1);
233 }
234
235 $this->callAPISuccess('report_template', 'getrows', array(
236 'report_id' => $reportID,
237 ));
238 if (substr($reportID, 0, '7') === 'logging') {
239 Civi::settings()->set('logging', 0);
240 }
241 }
242
243 /**
244 * Test logging report when a custom data table has a table removed by hook.
245 *
246 * Here we are checking that no fatal is triggered.
247 */
248 public function testLoggingReportWithHookRemovalOfCustomDataTable() {
249 Civi::settings()->set('logging', 1);
250 $group1 = $this->customGroupCreate();
251 $group2 = $this->customGroupCreate(['name' => 'second_one', 'title' => 'second one', 'table_name' => 'civicrm_value_second_one']);
252 $this->customFieldCreate(array('custom_group_id' => $group1['id'], 'label' => 'field one'));
253 $this->customFieldCreate(array('custom_group_id' => $group2['id'], 'label' => 'field two'));
254 $this->hookClass->setHook('civicrm_alterLogTables', array($this, 'alterLogTablesRemoveCustom'));
255
256 $this->callAPISuccess('report_template', 'getrows', array(
257 'report_id' => 'logging/contact/summary',
258 ));
259 Civi::settings()->set('logging', 0);
260 $this->customGroupDelete($group1['id']);
261 $this->customGroupDelete($group2['id']);
262 }
263
264 /**
265 * Remove one log table from the logging spec.
266 *
267 * @param array $logTableSpec
268 */
269 public function alterLogTablesRemoveCustom(&$logTableSpec) {
270 unset($logTableSpec['civicrm_value_second_one']);
271 }
272
273 /**
274 * Test api to get rows from reports with ACLs enabled.
275 *
276 * Checking for lack of fatal error at the moment.
277 *
278 * @dataProvider getReportTemplates
279 *
280 * @param $reportID
281 *
282 * @throws \PHPUnit_Framework_IncompleteTestError
283 */
284 public function testReportTemplateGetRowsAllReportsACL($reportID) {
285 if (stristr($reportID, 'has existing issues')) {
286 $this->markTestIncomplete($reportID);
287 }
288 $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
289 $this->callAPISuccess('report_template', 'getrows', array(
290 'report_id' => $reportID,
291 ));
292 }
293
294 /**
295 * Test get statistics.
296 *
297 * @dataProvider getReportTemplates
298 *
299 * @param $reportID
300 *
301 * @throws \PHPUnit_Framework_IncompleteTestError
302 */
303 public function testReportTemplateGetStatisticsAllReports($reportID) {
304 if (stristr($reportID, 'has existing issues')) {
305 $this->markTestIncomplete($reportID);
306 }
307 if (in_array($reportID, array('contribute/softcredit', 'contribute/bookkeeping'))) {
308 $this->markTestIncomplete($reportID . " has non enotices when calling statistics fn");
309 }
310 $description = "Get Statistics from a report (note there isn't much data to get in the test DB).";
311 $result = $this->callAPIAndDocument('report_template', 'getstatistics', array(
312 'report_id' => $reportID,
313 ), __FUNCTION__, __FILE__, $description, 'Getstatistics', 'getstatistics');
314 }
315
316 /**
317 * Data provider function for getting all templates.
318 *
319 * Note that the function needs to
320 * be static so cannot use $this->callAPISuccess
321 */
322 public static function getReportTemplates() {
323 $reportsToSkip = array(
324 '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',
325 'contribute/history' => 'Declaration of CRM_Report_Form_Contribute_History::buildRows() should be compatible with CRM_Report_Form::buildRows($sql, &$rows)',
326 'activitySummary' => 'We use temp tables for the main query generation and name are dynamic. These names are not available in stats() when called directly.',
327 );
328
329 $reports = civicrm_api3('report_template', 'get', array('return' => 'value', 'options' => array('limit' => 500)));
330 foreach ($reports['values'] as $report) {
331 if (empty($reportsToSkip[$report['value']])) {
332 $reportTemplates[] = array($report['value']);
333 }
334 else {
335 $reportTemplates[] = array($report['value'] . " has existing issues : " . $reportsToSkip[$report['value']]);
336 }
337 }
338
339 return $reportTemplates;
340 }
341
342 /**
343 * Get contribution templates that work with basic filter tests.
344 *
345 * These templates require minimal data config.
346 */
347 public static function getContributionReportTemplates() {
348 return array(array('contribute/summary'), array('contribute/detail'), array('contribute/repeat'), array('topDonor' => 'contribute/topDonor'));
349 }
350
351 /**
352 * Get contribution templates that work with basic filter tests.
353 *
354 * These templates require minimal data config.
355 */
356 public static function getMembershipReportTemplates() {
357 return array(array('member/detail'));
358 }
359
360 public static function getMembershipAndContributionReportTemplatesForGroupTests() {
361 $templates = array_merge(self::getContributionReportTemplates(), self::getMembershipReportTemplates());
362 foreach ($templates as $key => $value) {
363 if (array_key_exists('topDonor', $value)) {
364 // Report is not standard enough to test here.
365 unset($templates[$key]);
366 }
367
368 }
369 return $templates;
370 }
371
372 /**
373 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
374 */
375 public function testLybuntReportWithData() {
376 $inInd = $this->individualCreate();
377 $outInd = $this->individualCreate();
378 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-03-01'));
379 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
380 $rows = $this->callAPISuccess('report_template', 'getrows', array(
381 'report_id' => 'contribute/lybunt',
382 'yid_value' => 2015,
383 'yid_op' => 'calendar',
384 'options' => array('metadata' => array('sql')),
385 ));
386 $this->assertEquals(1, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
387 }
388
389 /**
390 * Test Lybunt report applies ACLs.
391 */
392 public function testLybuntReportWithDataAndACLFilter() {
393 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM');
394 $inInd = $this->individualCreate();
395 $outInd = $this->individualCreate();
396 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-03-01'));
397 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
398 $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
399 $params = array(
400 'report_id' => 'contribute/lybunt',
401 'yid_value' => 2015,
402 'yid_op' => 'calendar',
403 'options' => array('metadata' => array('sql')),
404 'check_permissions' => 1,
405 );
406
407 $rows = $this->callAPISuccess('report_template', 'getrows', $params);
408 $this->assertEquals(0, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
409
410 CRM_Utils_Hook::singleton()->reset();
411 }
412
413 /**
414 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
415 */
416 public function testLybuntReportWithFYData() {
417 $inInd = $this->individualCreate();
418 $outInd = $this->individualCreate();
419 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-10-01'));
420 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
421 $this->callAPISuccess('Setting', 'create', array('fiscalYearStart' => array('M' => 7, 'd' => 1)));
422 $rows = $this->callAPISuccess('report_template', 'getrows', array(
423 'report_id' => 'contribute/lybunt',
424 'yid_value' => 2015,
425 'yid_op' => 'fiscal',
426 'options' => array('metadata' => array('sql')),
427 'order_bys' => array(
428 array(
429 'column' => 'first_name',
430 'order' => 'ASC',
431 ),
432 ),
433 ));
434
435 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
436
437 $expected = preg_replace('/\s+/', ' ', 'DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
438 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
439 AND contribution_civireport.is_test = 0
440 AND contribution_civireport.receive_date BETWEEN \'20140701000000\' AND \'20150630235959\'
441 WHERE contact_civireport.id NOT IN (
442 SELECT cont_exclude.contact_id
443 FROM civicrm_contribution cont_exclude
444 WHERE cont_exclude.receive_date BETWEEN \'2015-7-1\' AND \'20160630235959\')
445 AND ( contribution_civireport.contribution_status_id IN (1) )
446 GROUP BY contact_civireport.id');
447 // Exclude whitespace in comparison as we don't care if it changes & this allows us to make the above readable.
448 $whitespacelessSql = preg_replace('/\s+/', ' ', $rows['metadata']['sql'][0]);
449 $this->assertContains($expected, $whitespacelessSql);
450 }
451
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 testLybuntReportWithFYDataOrderByLastYearAmount() {
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 'fields' => array('first_name'),
467 'order_bys' => array(
468 array(
469 'column' => 'last_year_total_amount',
470 'order' => 'ASC',
471 ),
472 ),
473 ));
474
475 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
476 }
477
478 /**
479 * Test the group filter works on the contribution summary (with a smart group).
480 *
481 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
482 *
483 * @param string $template
484 * Name of the template to test.
485 */
486 public function testContributionSummaryWithSmartGroupFilter($template) {
487 $groupID = $this->setUpPopulatedSmartGroup();
488 $rows = $this->callAPISuccess('report_template', 'getrows', array(
489 'report_id' => $template,
490 'gid_value' => $groupID,
491 'gid_op' => 'in',
492 'options' => array('metadata' => array('sql')),
493 ));
494 $this->assertNumberOfContactsInResult(3, $rows, $template);
495 if ($template === 'contribute/summary') {
496 $this->assertEquals(3, $rows['values'][0]['civicrm_contribution_total_amount_count']);
497 }
498 }
499
500 /**
501 * Test the group filter works on the contribution summary.
502 *
503 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
504 */
505 public function testContributionSummaryWithNotINSmartGroupFilter($template) {
506 $groupID = $this->setUpPopulatedSmartGroup();
507 $rows = $this->callAPISuccess('report_template', 'getrows', array(
508 'report_id' => 'contribute/summary',
509 'gid_value' => $groupID,
510 'gid_op' => 'notin',
511 'options' => array('metadata' => array('sql')),
512 ));
513 $this->assertEquals(2, $rows['values'][0]['civicrm_contribution_total_amount_count']);
514 }
515
516 /**
517 * Test the group filter works on the contribution summary.
518 */
519 public function testContributionDetailSoftCredits() {
520 $contactID = $this->individualCreate();
521 $contactID2 = $this->individualCreate();
522 $this->contributionCreate(['contact_id' => $contactID, 'api.ContributionSoft.create' => ['amount' => 5, 'contact_id' => $contactID2]]);
523 $template = 'contribute/detail';
524 $rows = $this->callAPISuccess('report_template', 'getrows', array(
525 'report_id' => $template,
526 'contribution_or_soft_value' => 'contributions_only',
527 'fields' => ['soft_credits' => 1, 'contribution_or_soft' => 1, 'sort_name' => 1],
528 'options' => array('metadata' => array('sql')),
529 ));
530 $this->assertEquals(
531 "<a href='/index.php?q=civicrm/contact/view&amp;reset=1&amp;cid=" . $contactID2 . "'>Anderson, Anthony</a> $ 5.00",
532 $rows['values'][0]['civicrm_contribution_soft_credits']
533 );
534 }
535
536 /**
537 * Test the amount column is populated on soft credit details.
538 */
539 public function testContributionDetailSoftCreditsOnly() {
540 $contactID = $this->individualCreate();
541 $contactID2 = $this->individualCreate();
542 $this->contributionCreate(['contact_id' => $contactID, 'api.ContributionSoft.create' => ['amount' => 5, 'contact_id' => $contactID2]]);
543 $template = 'contribute/detail';
544 $rows = $this->callAPISuccess('report_template', 'getrows', array(
545 'report_id' => $template,
546 'contribution_or_soft_value' => 'soft_credits_only',
547 'fields' => [
548 'sort_name' => '1',
549 'email' => '1',
550 'financial_type_id' => '1',
551 'receive_date' => '1',
552 'total_amount' => '1',
553 ],
554 'options' => array('metadata' => ['sql', 'labels']),
555 ));
556 foreach (array_keys($rows['metadata']['labels']) as $header) {
557 $this->assertTrue(!empty($rows['values'][0][$header]));
558 }
559 }
560
561 /**
562 * Test the group filter works on the various reports.
563 *
564 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
565 *
566 * @param string $template
567 * Report template unique identifier.
568 */
569 public function testReportsWithNonSmartGroupFilter($template) {
570 $groupID = $this->setUpPopulatedGroup();
571 $rows = $this->callAPISuccess('report_template', 'getrows', array(
572 'report_id' => $template,
573 'gid_value' => array($groupID),
574 'gid_op' => 'in',
575 'options' => array('metadata' => array('sql')),
576 ));
577 $this->assertNumberOfContactsInResult(1, $rows, $template);
578 }
579
580 /**
581 * Assert the included results match the expected.
582 *
583 * There may or may not be a group by in play so the assertion varies a little.
584 *
585 * @param int $numberExpected
586 * @param array $rows
587 * Rows returned from the report.
588 * @param string $template
589 */
590 protected function assertNumberOfContactsInResult($numberExpected, $rows, $template) {
591 if (isset($rows['values'][0]['civicrm_contribution_total_amount_count'])) {
592 $this->assertEquals($numberExpected, $rows['values'][0]['civicrm_contribution_total_amount_count'], 'wrong row count in ' . $template);
593 }
594 else {
595 $this->assertEquals($numberExpected, count($rows['values']), 'wrong row count in ' . $template);
596 }
597 }
598
599 /**
600 * Test the group filter works on the contribution summary when 2 groups are involved.
601 */
602 public function testContributionSummaryWithTwoGroups() {
603 $groupID = $this->setUpPopulatedGroup();
604 $groupID2 = $this->setUpPopulatedSmartGroup();
605 $rows = $this->callAPISuccess('report_template', 'getrows', array(
606 'report_id' => 'contribute/summary',
607 'gid_value' => array($groupID, $groupID2),
608 'gid_op' => 'in',
609 'options' => array('metadata' => array('sql')),
610 ));
611 $this->assertEquals(4, $rows['values'][0]['civicrm_contribution_total_amount_count']);
612 }
613
614 /**
615 * CRM-20640: Test the group filter works on the contribution summary when a single contact in 2 groups.
616 */
617 public function testContributionSummaryWithSingleContactsInTwoGroups() {
618 list($groupID1, $individualID) = $this->setUpPopulatedGroup(TRUE);
619 // create second group and add the individual to it.
620 $groupID2 = $this->groupCreate(array('name' => uniqid(), 'title' => uniqid()));
621 $this->callAPISuccess('GroupContact', 'create', array(
622 'group_id' => $groupID2,
623 'contact_id' => $individualID,
624 'status' => 'Added',
625 ));
626
627 $rows = $this->callAPISuccess('report_template', 'getrows', array(
628 'report_id' => 'contribute/summary',
629 'gid_value' => array($groupID1, $groupID2),
630 'gid_op' => 'in',
631 'options' => array('metadata' => array('sql')),
632 ));
633 $this->assertEquals(1, $rows['count']);
634 }
635
636 /**
637 * Test the group filter works on the contribution summary when 2 groups are involved.
638 */
639 public function testContributionSummaryWithTwoGroupsWithIntersection() {
640 $groups = $this->setUpIntersectingGroups();
641
642 $rows = $this->callAPISuccess('report_template', 'getrows', array(
643 'report_id' => 'contribute/summary',
644 'gid_value' => $groups,
645 'gid_op' => 'in',
646 'options' => array('metadata' => array('sql')),
647 ));
648 $this->assertEquals(7, $rows['values'][0]['civicrm_contribution_total_amount_count']);
649 }
650
651 /**
652 * Set up a smart group for testing.
653 *
654 * The smart group includes all Households by filter. In addition an individual
655 * is created and hard-added and an individual is created that is not added.
656 *
657 * One household is hard-added as well as being in the filter.
658 *
659 * This gives us a range of scenarios for testing contacts are included only once
660 * whenever they are hard-added or in the criteria.
661 *
662 * @return int
663 */
664 public function setUpPopulatedSmartGroup() {
665 $household1ID = $this->householdCreate();
666 $individual1ID = $this->individualCreate();
667 $householdID = $this->householdCreate();
668 $individualID = $this->individualCreate();
669 $individualIDRemoved = $this->individualCreate();
670 $groupID = $this->smartGroupCreate(array(), array('name' => uniqid(), 'title' => uniqid()));
671 $this->callAPISuccess('GroupContact', 'create', array(
672 'group_id' => $groupID,
673 'contact_id' => $individualIDRemoved,
674 'status' => 'Removed',
675 ));
676 $this->callAPISuccess('GroupContact', 'create', array(
677 'group_id' => $groupID,
678 'contact_id' => $individualID,
679 'status' => 'Added',
680 ));
681 $this->callAPISuccess('GroupContact', 'create', array(
682 'group_id' => $groupID,
683 'contact_id' => $householdID,
684 'status' => 'Added',
685 ));
686 foreach (array($household1ID, $individual1ID, $householdID, $individualID, $individualIDRemoved) as $contactID) {
687 $this->contributionCreate(array('contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => ''));
688 $this->contactMembershipCreate(array('contact_id' => $contactID));
689 }
690
691 // Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
692 CRM_Contact_BAO_GroupContactCache::clearGroupContactCache($groupID);
693 return $groupID;
694 }
695
696 /**
697 * Set up a static group for testing.
698 *
699 * An individual is created and hard-added and an individual is created that is not added.
700 *
701 * This gives us a range of scenarios for testing contacts are included only once
702 * whenever they are hard-added or in the criteria.
703 *
704 * @param bool $returnAddedContact
705 *
706 * @return int
707 */
708 public function setUpPopulatedGroup($returnAddedContact = FALSE) {
709 $individual1ID = $this->individualCreate();
710 $individualID = $this->individualCreate();
711 $individualIDRemoved = $this->individualCreate();
712 $groupID = $this->groupCreate(array('name' => uniqid(), 'title' => uniqid()));
713 $this->callAPISuccess('GroupContact', 'create', array(
714 'group_id' => $groupID,
715 'contact_id' => $individualIDRemoved,
716 'status' => 'Removed',
717 ));
718 $this->callAPISuccess('GroupContact', 'create', array(
719 'group_id' => $groupID,
720 'contact_id' => $individualID,
721 'status' => 'Added',
722 ));
723
724 foreach (array($individual1ID, $individualID, $individualIDRemoved) as $contactID) {
725 $this->contributionCreate(array('contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => ''));
726 $this->contactMembershipCreate(array('contact_id' => $contactID));
727 }
728
729 // Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
730 CRM_Contact_BAO_GroupContactCache::clearGroupContactCache($groupID);
731
732 if ($returnAddedContact) {
733 return array($groupID, $individualID);
734 }
735
736 return $groupID;
737 }
738
739 /**
740 * @return array
741 */
742 public function setUpIntersectingGroups() {
743 $groupID = $this->setUpPopulatedGroup();
744 $groupID2 = $this->setUpPopulatedSmartGroup();
745 $addedToBothIndividualID = $this->individualCreate();
746 $removedFromBothIndividualID = $this->individualCreate();
747 $addedToSmartGroupRemovedFromOtherIndividualID = $this->individualCreate();
748 $removedFromSmartGroupAddedToOtherIndividualID = $this->individualCreate();
749 $this->callAPISuccess('GroupContact', 'create', array(
750 'group_id' => $groupID,
751 'contact_id' => $addedToBothIndividualID,
752 'status' => 'Added',
753 ));
754 $this->callAPISuccess('GroupContact', 'create', array(
755 'group_id' => $groupID2,
756 'contact_id' => $addedToBothIndividualID,
757 'status' => 'Added',
758 ));
759 $this->callAPISuccess('GroupContact', 'create', array(
760 'group_id' => $groupID,
761 'contact_id' => $removedFromBothIndividualID,
762 'status' => 'Removed',
763 ));
764 $this->callAPISuccess('GroupContact', 'create', array(
765 'group_id' => $groupID2,
766 'contact_id' => $removedFromBothIndividualID,
767 'status' => 'Removed',
768 ));
769 $this->callAPISuccess('GroupContact', 'create', array(
770 'group_id' => $groupID2,
771 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
772 'status' => 'Added',
773 ));
774 $this->callAPISuccess('GroupContact', 'create', array(
775 'group_id' => $groupID,
776 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
777 'status' => 'Removed',
778 ));
779 $this->callAPISuccess('GroupContact', 'create', array(
780 'group_id' => $groupID,
781 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
782 'status' => 'Added',
783 ));
784 $this->callAPISuccess('GroupContact', 'create', array(
785 'group_id' => $groupID2,
786 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
787 'status' => 'Removed',
788 ));
789
790 foreach (array(
791 $addedToBothIndividualID,
792 $removedFromBothIndividualID,
793 $addedToSmartGroupRemovedFromOtherIndividualID,
794 $removedFromSmartGroupAddedToOtherIndividualID,
795 ) as $contactID) {
796 $this->contributionCreate(array(
797 'contact_id' => $contactID,
798 'invoice_id' => '',
799 'trxn_id' => '',
800 ));
801 }
802 return array($groupID, $groupID2);
803 }
804
805 /**
806 * Test Deferred Revenue Report.
807 */
808 public function testDeferredRevenueReport() {
809 $indv1 = $this->individualCreate();
810 $indv2 = $this->individualCreate();
811 $params = array(
812 'contribution_invoice_settings' => array(
813 'deferred_revenue_enabled' => '1',
814 ),
815 );
816 $this->callAPISuccess('Setting', 'create', $params);
817 $this->contributionCreate(
818 array(
819 'contact_id' => $indv1,
820 'receive_date' => '2016-10-01',
821 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+3 month')),
822 'financial_type_id' => 2,
823 )
824 );
825 $this->contributionCreate(
826 array(
827 'contact_id' => $indv1,
828 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+22 month')),
829 'financial_type_id' => 4,
830 'trxn_id' => NULL,
831 'invoice_id' => NULL,
832 )
833 );
834 $this->contributionCreate(
835 array(
836 'contact_id' => $indv2,
837 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+1 month')),
838 'financial_type_id' => 4,
839 'trxn_id' => NULL,
840 'invoice_id' => NULL,
841 )
842 );
843 $this->contributionCreate(
844 array(
845 'contact_id' => $indv2,
846 'receive_date' => '2016-03-01',
847 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+4 month')),
848 'financial_type_id' => 2,
849 'trxn_id' => NULL,
850 'invoice_id' => NULL,
851 )
852 );
853 $rows = $this->callAPISuccess('report_template', 'getrows', array(
854 'report_id' => 'contribute/deferredrevenue',
855 ));
856 $this->assertEquals(2, $rows['count'], "Report failed to get row count");
857 $count = array(2, 1);
858 foreach ($rows['values'] as $row) {
859 $this->assertEquals(array_pop($count), count($row['rows']), "Report failed to get row count");
860 }
861 }
862
863 /**
864 * Test the group filter works on the various reports.
865 *
866 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
867 *
868 * @param string $template
869 * Report template unique identifier.
870 */
871 public function testReportsWithNoTInSmartGroupFilter($template) {
872 $groupID = $this->setUpPopulatedGroup();
873 $rows = $this->callAPISuccess('report_template', 'getrows', array(
874 'report_id' => $template,
875 'gid_value' => array($groupID),
876 'gid_op' => 'notin',
877 'options' => array('metadata' => array('sql')),
878 ));
879 $this->assertNumberOfContactsInResult(2, $rows, $template);
880 }
881
882 /**
883 * Test activity summary report - requiring all current fields to be output.
884 */
885 public function testActivitySummary() {
886 $this->createContactsWithActivities();
887 $fields = [
888 'contact_source' => '1',
889 'contact_assignee' => '1',
890 'contact_target' => '1',
891 'contact_source_email' => '1',
892 'contact_assignee_email' => '1',
893 'contact_target_email' => '1',
894 'contact_source_phone' => '1',
895 'contact_assignee_phone' => '1',
896 'contact_target_phone' => '1',
897 'activity_type_id' => '1',
898 'activity_subject' => '1',
899 'activity_date_time' => '1',
900 'status_id' => '1',
901 'duration' => '1',
902 'location' => '1',
903 'details' => '1',
904 'priority_id' => '1',
905 'result' => '1',
906 'engagement_level' => '1',
907 'address_name' => '1',
908 'street_address' => '1',
909 'supplemental_address_1' => '1',
910 'supplemental_address_2' => '1',
911 'supplemental_address_3' => '1',
912 'street_number' => '1',
913 'street_name' => '1',
914 'street_unit' => '1',
915 'city' => '1',
916 'postal_code' => '1',
917 'postal_code_suffix' => '1',
918 'country_id' => '1',
919 'state_province_id' => '1',
920 'county_id' => '1',
921 ];
922 $params = [
923 'fields' => $fields,
924 'current_user_op' => 'eq',
925 'current_user_value' => '0',
926 'include_case_activities_op' => 'eq',
927 'include_case_activities_value' => 0,
928 'order_bys' => [
929 1 => ['column' => 'activity_date_time', 'order' => 'ASC'],
930 2 => ['column' => 'activity_type_id', 'order' => 'ASC'],
931 ],
932 ];
933
934 $params['report_id'] = 'Activity';
935
936 $rows = $this->callAPISuccess('report_template', 'getrows', $params)['values'];
937 $expected = [
938 'civicrm_contact_contact_source' => 'Łąchowski-Roberts, Anthony',
939 '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>',
940 '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>',
941 'civicrm_contact_contact_source_id' => $this->contactIDs[2],
942 'civicrm_contact_contact_assignee_id' => $this->contactIDs[1],
943 'civicrm_contact_contact_target_id' => $this->contactIDs[0] . ';' . $this->contactIDs[1],
944 'civicrm_email_contact_source_email' => 'anthony_anderson@civicrm.org',
945 'civicrm_email_contact_assignee_email' => 'anthony_anderson@civicrm.org',
946 'civicrm_email_contact_target_email' => 'techo@spying.com;anthony_anderson@civicrm.org',
947 'civicrm_phone_contact_source_phone' => NULL,
948 'civicrm_phone_contact_assignee_phone' => NULL,
949 'civicrm_phone_contact_target_phone' => NULL,
950 'civicrm_activity_id' => '1',
951 'civicrm_activity_source_record_id' => NULL,
952 'civicrm_activity_activity_type_id' => 'Meeting',
953 'civicrm_activity_activity_subject' => 'Very secret meeting',
954 'civicrm_activity_activity_date_time' => date('Y-m-d 23:59:58', strtotime('now')),
955 'civicrm_activity_status_id' => 'Scheduled',
956 'civicrm_activity_duration' => '120',
957 'civicrm_activity_location' => 'Pennsylvania',
958 'civicrm_activity_details' => 'a test activity',
959 'civicrm_activity_priority_id' => 'Normal',
960 'civicrm_address_address_name' => NULL,
961 'civicrm_address_street_address' => NULL,
962 'civicrm_address_supplemental_address_1' => NULL,
963 'civicrm_address_supplemental_address_2' => NULL,
964 'civicrm_address_supplemental_address_3' => NULL,
965 'civicrm_address_street_number' => NULL,
966 'civicrm_address_street_name' => NULL,
967 'civicrm_address_street_unit' => NULL,
968 'civicrm_address_city' => NULL,
969 'civicrm_address_postal_code' => NULL,
970 'civicrm_address_postal_code_suffix' => NULL,
971 'civicrm_address_country_id' => NULL,
972 'civicrm_address_state_province_id' => NULL,
973 'civicrm_address_county_id' => NULL,
974 'civicrm_contact_contact_source_link' => '/index.php?q=civicrm/contact/view&amp;reset=1&amp;cid=' . $this->contactIDs[2],
975 'civicrm_contact_contact_source_hover' => 'View Contact Summary for this Contact',
976 'civicrm_activity_activity_type_id_hover' => 'View Activity Record',
977 ];
978 $row = $rows[0];
979 // This link is not relative - skip for now
980 unset($row['civicrm_activity_activity_type_id_link']);
981 if ($row['civicrm_email_contact_target_email'] === 'anthony_anderson@civicrm.org;techo@spying.com') {
982 // order is unpredictable
983 $expected['civicrm_email_contact_target_email'] = 'anthony_anderson@civicrm.org;techo@spying.com';
984 }
985
986 $this->assertEquals($expected, $row);
987 }
988
989 /**
990 * Set up some activity data..... use some chars that challenge our utf handling.
991 */
992 public function createContactsWithActivities() {
993 $this->contactIDs[] = $this->individualCreate(['last_name' => 'Brzęczysław', 'email' => 'techo@spying.com']);
994 $this->contactIDs[] = $this->individualCreate(['last_name' => 'Łąchowski-Roberts']);
995 $this->contactIDs[] = $this->individualCreate(['last_name' => 'Łąchowski-Roberts']);
996
997 $this->callAPISuccess('Activity', 'create', [
998 'subject' => 'Very secret meeting',
999 'activity_date_time' => date('Y-m-d 23:59:58', strtotime('now')),
1000 'duration' => 120,
1001 'location' => 'Pennsylvania',
1002 'details' => 'a test activity',
1003 'status_id' => 1,
1004 'activity_type_id' => 'Meeting',
1005 'source_contact_id' => $this->contactIDs[2],
1006 'target_contact_id' => array($this->contactIDs[0], $this->contactIDs[1]),
1007 'assignee_contact_id' => $this->contactIDs[1],
1008 ]);
1009 }
1010
1011 /**
1012 * Test the group filter works on the contribution summary.
1013 */
1014 public function testContributionDetailTotalHeader() {
1015 $contactID = $this->individualCreate();
1016 $contactID2 = $this->individualCreate();
1017 $this->contributionCreate(['contact_id' => $contactID, 'api.ContributionSoft.create' => ['amount' => 5, 'contact_id' => $contactID2]]);
1018 $template = 'contribute/detail';
1019 $rows = $this->callAPISuccess('report_template', 'getrows', array(
1020 'report_id' => $template,
1021 'contribution_or_soft_value' => 'contributions_only',
1022 'fields' => [
1023 'sort_name' => '1',
1024 'age' => '1',
1025 'email' => '1',
1026 'phone' => '1',
1027 'financial_type_id' => '1',
1028 'receive_date' => '1',
1029 'total_amount' => '1',
1030 ],
1031 'order_bys' => [['column' => 'sort_name', 'order' => 'ASC', 'section' => '1']],
1032 'options' => array('metadata' => array('sql')),
1033 ));
1034 }
1035
1036 }