3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_report_instance_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Report
35 class api_v3_ReportTemplateTest
extends CiviUnitTestCase
{
37 use CRMTraits_ACL_PermissionTrait
;
39 protected $_apiversion = 3;
41 protected $contactIDs = [];
44 * Our group reports use an alter so transaction cleanup won't work.
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'));
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',
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"');
73 // change component to null
74 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
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');
88 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
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"');
101 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
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"');
113 $result = $this->callAPISuccess('ReportTemplate', 'delete', array(
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"
124 * Test api to get rows from reports.
126 * @dataProvider getReportTemplatesSupportingSelectWhere
130 * @throws \PHPUnit_Framework_IncompleteTestError
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']],
139 foreach ($result['metadata']['sql'] as $sql) {
140 if (strstr($sql, " = 'Organization' ")) {
144 $this->assertTrue($found, $reportID);
148 * Get templates suitable for SelectWhere test.
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 = [
163 'contribute/bookkeeping',
165 'event/incomesummary',
170 'logging/contact/detail',
171 'logging/contact/summary',
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]);
179 return $allTemplates;
183 * @param \CRM_Core_DAO $entity
184 * @param array $clauses
186 public function hookSelectWhere($entity, &$clauses) {
187 // Restrict access to cases by type
188 if ($entity == 'Contact') {
189 $clauses['contact_type'][] = " = 'Organization' ";
194 * Test getrows on contact summary report.
196 public function testReportTemplateGetRowsContactSummary() {
197 $description = "Retrieve rows from a report template (optionally providing the instance_id).";
198 $result = $this->callApiSuccess('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']);
204 //the second part of this test has been commented out because it relied on the db being reset to
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
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']);
218 * Test getrows on Mailing Opened report.
220 public function testReportTemplateGetRowsMailingUniqueOpened() {
221 $description = "Retrieve rows from a mailing opened report template.";
222 $op = new PHPUnit_Extensions_Database_Operation_Insert();
223 $op->execute($this->_dbconn
,
224 $this->createFlatXMLDataSet(
225 dirname(__FILE__
) . '/../../CRM/Mailing/BAO/queryDataset.xml'
229 // Check total rows without distinct
231 $_REQUEST['distinct'] = 0;
232 $result = $this->callAPIAndDocument('report_template', 'getrows', array(
233 'report_id' => 'Mailing/opened',
234 'options' => array('metadata' => array('labels', 'title')),
235 ), __FUNCTION__
, __FILE__
, $description, 'Getrows');
236 $this->assertEquals(14, $result['count']);
238 // Check total rows with distinct
239 $_REQUEST['distinct'] = 1;
240 $result = $this->callAPIAndDocument('report_template', 'getrows', array(
241 'report_id' => 'Mailing/opened',
242 'options' => array('metadata' => array('labels', 'title')),
243 ), __FUNCTION__
, __FILE__
, $description, 'Getrows');
244 $this->assertEquals(5, $result['count']);
246 // Check total rows with distinct by passing NULL value to distinct parameter
247 $_REQUEST['distinct'] = NULL;
248 $result = $this->callAPIAndDocument('report_template', 'getrows', array(
249 'report_id' => 'Mailing/opened',
250 'options' => array('metadata' => array('labels', 'title')),
251 ), __FUNCTION__
, __FILE__
, $description, 'Getrows');
252 $this->assertEquals(5, $result['count']);
256 * Test api to get rows from reports.
258 * @dataProvider getReportTemplates
262 * @throws \PHPUnit_Framework_IncompleteTestError
264 public function testReportTemplateGetRowsAllReports($reportID) {
265 //$reportID = 'logging/contact/summary';
266 if (stristr($reportID, 'has existing issues')) {
267 $this->markTestIncomplete($reportID);
269 if (substr($reportID, 0, '7') === 'logging') {
270 Civi
::settings()->set('logging', 1);
273 $this->callAPISuccess('report_template', 'getrows', array(
274 'report_id' => $reportID,
276 if (substr($reportID, 0, '7') === 'logging') {
277 Civi
::settings()->set('logging', 0);
282 * Test logging report when a custom data table has a table removed by hook.
284 * Here we are checking that no fatal is triggered.
286 public function testLoggingReportWithHookRemovalOfCustomDataTable() {
287 Civi
::settings()->set('logging', 1);
288 $group1 = $this->customGroupCreate();
289 $group2 = $this->customGroupCreate(['name' => 'second_one', 'title' => 'second one', 'table_name' => 'civicrm_value_second_one']);
290 $this->customFieldCreate(array('custom_group_id' => $group1['id'], 'label' => 'field one'));
291 $this->customFieldCreate(array('custom_group_id' => $group2['id'], 'label' => 'field two'));
292 $this->hookClass
->setHook('civicrm_alterLogTables', array($this, 'alterLogTablesRemoveCustom'));
294 $this->callAPISuccess('report_template', 'getrows', array(
295 'report_id' => 'logging/contact/summary',
297 Civi
::settings()->set('logging', 0);
298 $this->customGroupDelete($group1['id']);
299 $this->customGroupDelete($group2['id']);
303 * Remove one log table from the logging spec.
305 * @param array $logTableSpec
307 public function alterLogTablesRemoveCustom(&$logTableSpec) {
308 unset($logTableSpec['civicrm_value_second_one']);
312 * Test api to get rows from reports with ACLs enabled.
314 * Checking for lack of fatal error at the moment.
316 * @dataProvider getReportTemplates
320 * @throws \PHPUnit_Framework_IncompleteTestError
322 public function testReportTemplateGetRowsAllReportsACL($reportID) {
323 if (stristr($reportID, 'has existing issues')) {
324 $this->markTestIncomplete($reportID);
326 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
327 $this->callAPISuccess('report_template', 'getrows', array(
328 'report_id' => $reportID,
333 * Test get statistics.
335 * @dataProvider getReportTemplates
339 * @throws \PHPUnit_Framework_IncompleteTestError
341 public function testReportTemplateGetStatisticsAllReports($reportID) {
342 if (stristr($reportID, 'has existing issues')) {
343 $this->markTestIncomplete($reportID);
345 if (in_array($reportID, array('contribute/softcredit', 'contribute/bookkeeping'))) {
346 $this->markTestIncomplete($reportID . " has non enotices when calling statistics fn");
348 $description = "Get Statistics from a report (note there isn't much data to get in the test DB).";
349 $result = $this->callAPIAndDocument('report_template', 'getstatistics', array(
350 'report_id' => $reportID,
351 ), __FUNCTION__
, __FILE__
, $description, 'Getstatistics', 'getstatistics');
355 * Data provider function for getting all templates.
357 * Note that the function needs to
358 * be static so cannot use $this->callAPISuccess
360 public static function getReportTemplates() {
361 $reportsToSkip = array(
362 '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',
363 'contribute/history' => 'Declaration of CRM_Report_Form_Contribute_History::buildRows() should be compatible with CRM_Report_Form::buildRows($sql, &$rows)',
364 'activitySummary' => 'We use temp tables for the main query generation and name are dynamic. These names are not available in stats() when called directly.',
367 $reports = civicrm_api3('report_template', 'get', array('return' => 'value', 'options' => array('limit' => 500)));
368 foreach ($reports['values'] as $report) {
369 if (empty($reportsToSkip[$report['value']])) {
370 $reportTemplates[] = array($report['value']);
373 $reportTemplates[] = array($report['value'] . " has existing issues : " . $reportsToSkip[$report['value']]);
377 return $reportTemplates;
381 * Get contribution templates that work with basic filter tests.
383 * These templates require minimal data config.
385 public static function getContributionReportTemplates() {
386 return array(array('contribute/summary'), array('contribute/detail'), array('contribute/repeat'), array('topDonor' => 'contribute/topDonor'));
390 * Get contribution templates that work with basic filter tests.
392 * These templates require minimal data config.
394 public static function getMembershipReportTemplates() {
395 return array(array('member/detail'));
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]);
411 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
413 public function testLybuntReportWithData() {
414 $inInd = $this->individualCreate();
415 $outInd = $this->individualCreate();
416 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-03-01'));
417 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
418 $rows = $this->callAPISuccess('report_template', 'getrows', array(
419 'report_id' => 'contribute/lybunt',
421 'yid_op' => 'calendar',
422 'options' => array('metadata' => array('sql')),
424 $this->assertEquals(1, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
428 * Test Lybunt report applies ACLs.
430 public function testLybuntReportWithDataAndACLFilter() {
431 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('administer CiviCRM');
432 $inInd = $this->individualCreate();
433 $outInd = $this->individualCreate();
434 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-03-01'));
435 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
436 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
438 'report_id' => 'contribute/lybunt',
440 'yid_op' => 'calendar',
441 'options' => array('metadata' => array('sql')),
442 'check_permissions' => 1,
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));
448 CRM_Utils_Hook
::singleton()->reset();
452 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
454 public function testLybuntReportWithFYData() {
455 $inInd = $this->individualCreate();
456 $outInd = $this->individualCreate();
457 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-10-01'));
458 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
459 $this->callAPISuccess('Setting', 'create', array('fiscalYearStart' => array('M' => 7, 'd' => 1)));
460 $rows = $this->callAPISuccess('report_template', 'getrows', array(
461 'report_id' => 'contribute/lybunt',
463 'yid_op' => 'fiscal',
464 'options' => array('metadata' => array('sql')),
465 'order_bys' => array(
467 'column' => 'first_name',
473 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
475 $expected = preg_replace('/\s+/', ' ', 'DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
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\'
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);
491 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
493 public function testLybuntReportWithFYDataOrderByLastYearAmount() {
494 $inInd = $this->individualCreate();
495 $outInd = $this->individualCreate();
496 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-10-01'));
497 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
498 $this->callAPISuccess('Setting', 'create', array('fiscalYearStart' => array('M' => 7, 'd' => 1)));
499 $rows = $this->callAPISuccess('report_template', 'getrows', array(
500 'report_id' => 'contribute/lybunt',
502 'yid_op' => 'fiscal',
503 'options' => array('metadata' => array('sql')),
504 'fields' => array('first_name'),
505 'order_bys' => array(
507 'column' => 'last_year_total_amount',
513 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
517 * Test the group filter works on the contribution summary (with a smart group).
519 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
521 * @param string $template
522 * Name of the template to test.
524 public function testContributionSummaryWithSmartGroupFilter($template) {
525 $groupID = $this->setUpPopulatedSmartGroup();
526 $rows = $this->callAPISuccess('report_template', 'getrows', array(
527 'report_id' => $template,
528 'gid_value' => $groupID,
530 'options' => array('metadata' => array('sql')),
532 $this->assertNumberOfContactsInResult(3, $rows, $template);
533 if ($template === 'contribute/summary') {
534 $this->assertEquals(3, $rows['values'][0]['civicrm_contribution_total_amount_count']);
539 * Test the group filter works on the contribution summary.
541 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
543 public function testContributionSummaryWithNotINSmartGroupFilter($template) {
544 $groupID = $this->setUpPopulatedSmartGroup();
545 $rows = $this->callAPISuccess('report_template', 'getrows', array(
546 'report_id' => 'contribute/summary',
547 'gid_value' => $groupID,
549 'options' => array('metadata' => array('sql')),
551 $this->assertEquals(2, $rows['values'][0]['civicrm_contribution_total_amount_count']);
555 * Test the group filter works on the contribution summary.
557 public function testContributionDetailSoftCredits() {
558 $contactID = $this->individualCreate();
559 $contactID2 = $this->individualCreate();
560 $this->contributionCreate(['contact_id' => $contactID, 'api.ContributionSoft.create' => ['amount' => 5, 'contact_id' => $contactID2]]);
561 $template = 'contribute/detail';
562 $rows = $this->callAPISuccess('report_template', 'getrows', array(
563 'report_id' => $template,
564 'contribution_or_soft_value' => 'contributions_only',
565 'fields' => ['soft_credits' => 1, 'contribution_or_soft' => 1, 'sort_name' => 1],
566 'options' => array('metadata' => array('sql')),
569 "<a href='/index.php?q=civicrm/contact/view&reset=1&cid=" . $contactID2 . "'>Anderson, Anthony</a> $ 5.00",
570 $rows['values'][0]['civicrm_contribution_soft_credits']
575 * Test the amount column is populated on soft credit details.
577 public function testContributionDetailSoftCreditsOnly() {
578 $contactID = $this->individualCreate();
579 $contactID2 = $this->individualCreate();
580 $this->contributionCreate(['contact_id' => $contactID, 'api.ContributionSoft.create' => ['amount' => 5, 'contact_id' => $contactID2]]);
581 $template = 'contribute/detail';
582 $rows = $this->callAPISuccess('report_template', 'getrows', array(
583 'report_id' => $template,
584 'contribution_or_soft_value' => 'soft_credits_only',
588 'financial_type_id' => '1',
589 'receive_date' => '1',
590 'total_amount' => '1',
592 'options' => array('metadata' => ['sql', 'labels']),
594 foreach (array_keys($rows['metadata']['labels']) as $header) {
595 $this->assertTrue(!empty($rows['values'][0][$header]));
600 * Test the group filter works on the various reports.
602 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
604 * @param string $template
605 * Report template unique identifier.
607 public function testReportsWithNonSmartGroupFilter($template) {
608 $groupID = $this->setUpPopulatedGroup();
609 $rows = $this->callAPISuccess('report_template', 'getrows', array(
610 'report_id' => $template,
611 'gid_value' => array($groupID),
613 'options' => array('metadata' => array('sql')),
615 $this->assertNumberOfContactsInResult(1, $rows, $template);
619 * Assert the included results match the expected.
621 * There may or may not be a group by in play so the assertion varies a little.
623 * @param int $numberExpected
625 * Rows returned from the report.
626 * @param string $template
628 protected function assertNumberOfContactsInResult($numberExpected, $rows, $template) {
629 if (isset($rows['values'][0]['civicrm_contribution_total_amount_count'])) {
630 $this->assertEquals($numberExpected, $rows['values'][0]['civicrm_contribution_total_amount_count'], 'wrong row count in ' . $template);
633 $this->assertEquals($numberExpected, count($rows['values']), 'wrong row count in ' . $template);
638 * Test the group filter works on the contribution summary when 2 groups are involved.
640 public function testContributionSummaryWithTwoGroups() {
641 $groupID = $this->setUpPopulatedGroup();
642 $groupID2 = $this->setUpPopulatedSmartGroup();
643 $rows = $this->callAPISuccess('report_template', 'getrows', array(
644 'report_id' => 'contribute/summary',
645 'gid_value' => array($groupID, $groupID2),
647 'options' => array('metadata' => array('sql')),
649 $this->assertEquals(4, $rows['values'][0]['civicrm_contribution_total_amount_count']);
653 * CRM-20640: Test the group filter works on the contribution summary when a single contact in 2 groups.
655 public function testContributionSummaryWithSingleContactsInTwoGroups() {
656 list($groupID1, $individualID) = $this->setUpPopulatedGroup(TRUE);
657 // create second group and add the individual to it.
658 $groupID2 = $this->groupCreate(array('name' => uniqid(), 'title' => uniqid()));
659 $this->callAPISuccess('GroupContact', 'create', array(
660 'group_id' => $groupID2,
661 'contact_id' => $individualID,
665 $rows = $this->callAPISuccess('report_template', 'getrows', array(
666 'report_id' => 'contribute/summary',
667 'gid_value' => array($groupID1, $groupID2),
669 'options' => array('metadata' => array('sql')),
671 $this->assertEquals(1, $rows['count']);
675 * Test the group filter works on the contribution summary when 2 groups are involved.
677 public function testContributionSummaryWithTwoGroupsWithIntersection() {
678 $groups = $this->setUpIntersectingGroups();
680 $rows = $this->callAPISuccess('report_template', 'getrows', array(
681 'report_id' => 'contribute/summary',
682 'gid_value' => $groups,
684 'options' => array('metadata' => array('sql')),
686 $this->assertEquals(7, $rows['values'][0]['civicrm_contribution_total_amount_count']);
690 * Set up a smart group for testing.
692 * The smart group includes all Households by filter. In addition an individual
693 * is created and hard-added and an individual is created that is not added.
695 * One household is hard-added as well as being in the filter.
697 * This gives us a range of scenarios for testing contacts are included only once
698 * whenever they are hard-added or in the criteria.
702 public function setUpPopulatedSmartGroup() {
703 $household1ID = $this->householdCreate();
704 $individual1ID = $this->individualCreate();
705 $householdID = $this->householdCreate();
706 $individualID = $this->individualCreate();
707 $individualIDRemoved = $this->individualCreate();
708 $groupID = $this->smartGroupCreate(array(), array('name' => uniqid(), 'title' => uniqid()));
709 $this->callAPISuccess('GroupContact', 'create', array(
710 'group_id' => $groupID,
711 'contact_id' => $individualIDRemoved,
712 'status' => 'Removed',
714 $this->callAPISuccess('GroupContact', 'create', array(
715 'group_id' => $groupID,
716 'contact_id' => $individualID,
719 $this->callAPISuccess('GroupContact', 'create', array(
720 'group_id' => $groupID,
721 'contact_id' => $householdID,
724 foreach (array($household1ID, $individual1ID, $householdID, $individualID, $individualIDRemoved) as $contactID) {
725 $this->contributionCreate(array('contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => ''));
726 $this->contactMembershipCreate(array('contact_id' => $contactID));
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);
735 * Set up a static group for testing.
737 * An individual is created and hard-added and an individual is created that is not added.
739 * This gives us a range of scenarios for testing contacts are included only once
740 * whenever they are hard-added or in the criteria.
742 * @param bool $returnAddedContact
746 public function setUpPopulatedGroup($returnAddedContact = FALSE) {
747 $individual1ID = $this->individualCreate();
748 $individualID = $this->individualCreate();
749 $individualIDRemoved = $this->individualCreate();
750 $groupID = $this->groupCreate(array('name' => uniqid(), 'title' => uniqid()));
751 $this->callAPISuccess('GroupContact', 'create', array(
752 'group_id' => $groupID,
753 'contact_id' => $individualIDRemoved,
754 'status' => 'Removed',
756 $this->callAPISuccess('GroupContact', 'create', array(
757 'group_id' => $groupID,
758 'contact_id' => $individualID,
762 foreach (array($individual1ID, $individualID, $individualIDRemoved) as $contactID) {
763 $this->contributionCreate(array('contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => ''));
764 $this->contactMembershipCreate(array('contact_id' => $contactID));
767 // Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
768 CRM_Contact_BAO_GroupContactCache
::clearGroupContactCache($groupID);
770 if ($returnAddedContact) {
771 return array($groupID, $individualID);
780 public function setUpIntersectingGroups() {
781 $groupID = $this->setUpPopulatedGroup();
782 $groupID2 = $this->setUpPopulatedSmartGroup();
783 $addedToBothIndividualID = $this->individualCreate();
784 $removedFromBothIndividualID = $this->individualCreate();
785 $addedToSmartGroupRemovedFromOtherIndividualID = $this->individualCreate();
786 $removedFromSmartGroupAddedToOtherIndividualID = $this->individualCreate();
787 $this->callAPISuccess('GroupContact', 'create', array(
788 'group_id' => $groupID,
789 'contact_id' => $addedToBothIndividualID,
792 $this->callAPISuccess('GroupContact', 'create', array(
793 'group_id' => $groupID2,
794 'contact_id' => $addedToBothIndividualID,
797 $this->callAPISuccess('GroupContact', 'create', array(
798 'group_id' => $groupID,
799 'contact_id' => $removedFromBothIndividualID,
800 'status' => 'Removed',
802 $this->callAPISuccess('GroupContact', 'create', array(
803 'group_id' => $groupID2,
804 'contact_id' => $removedFromBothIndividualID,
805 'status' => 'Removed',
807 $this->callAPISuccess('GroupContact', 'create', array(
808 'group_id' => $groupID2,
809 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
812 $this->callAPISuccess('GroupContact', 'create', array(
813 'group_id' => $groupID,
814 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
815 'status' => 'Removed',
817 $this->callAPISuccess('GroupContact', 'create', array(
818 'group_id' => $groupID,
819 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
822 $this->callAPISuccess('GroupContact', 'create', array(
823 'group_id' => $groupID2,
824 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
825 'status' => 'Removed',
829 $addedToBothIndividualID,
830 $removedFromBothIndividualID,
831 $addedToSmartGroupRemovedFromOtherIndividualID,
832 $removedFromSmartGroupAddedToOtherIndividualID,
834 $this->contributionCreate(array(
835 'contact_id' => $contactID,
840 return array($groupID, $groupID2);
844 * Test Deferred Revenue Report.
846 public function testDeferredRevenueReport() {
847 $indv1 = $this->individualCreate();
848 $indv2 = $this->individualCreate();
850 'contribution_invoice_settings' => array(
851 'deferred_revenue_enabled' => '1',
854 $this->callAPISuccess('Setting', 'create', $params);
855 $this->contributionCreate(
857 'contact_id' => $indv1,
858 'receive_date' => '2016-10-01',
859 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+3 month')),
860 'financial_type_id' => 2,
863 $this->contributionCreate(
865 'contact_id' => $indv1,
866 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+22 month')),
867 'financial_type_id' => 4,
869 'invoice_id' => NULL,
872 $this->contributionCreate(
874 'contact_id' => $indv2,
875 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+1 month')),
876 'financial_type_id' => 4,
878 'invoice_id' => NULL,
881 $this->contributionCreate(
883 'contact_id' => $indv2,
884 'receive_date' => '2016-03-01',
885 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+4 month')),
886 'financial_type_id' => 2,
888 'invoice_id' => NULL,
891 $rows = $this->callAPISuccess('report_template', 'getrows', array(
892 'report_id' => 'contribute/deferredrevenue',
894 $this->assertEquals(2, $rows['count'], "Report failed to get row count");
895 $count = array(2, 1);
896 foreach ($rows['values'] as $row) {
897 $this->assertEquals(array_pop($count), count($row['rows']), "Report failed to get row count");
902 * Test the group filter works on the various reports.
904 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
906 * @param string $template
907 * Report template unique identifier.
909 public function testReportsWithNoTInSmartGroupFilter($template) {
910 $groupID = $this->setUpPopulatedGroup();
911 $rows = $this->callAPISuccess('report_template', 'getrows', array(
912 'report_id' => $template,
913 'gid_value' => array($groupID),
915 'options' => array('metadata' => array('sql')),
917 $this->assertNumberOfContactsInResult(2, $rows, $template);
921 * Test activity summary report - requiring all current fields to be output.
923 public function testActivitySummary() {
924 $this->createContactsWithActivities();
926 'contact_source' => '1',
927 'contact_assignee' => '1',
928 'contact_target' => '1',
929 'contact_source_email' => '1',
930 'contact_assignee_email' => '1',
931 'contact_target_email' => '1',
932 'contact_source_phone' => '1',
933 'contact_assignee_phone' => '1',
934 'contact_target_phone' => '1',
935 'activity_type_id' => '1',
936 'activity_subject' => '1',
937 'activity_date_time' => '1',
942 'priority_id' => '1',
944 'engagement_level' => '1',
945 'address_name' => '1',
946 'street_address' => '1',
947 'supplemental_address_1' => '1',
948 'supplemental_address_2' => '1',
949 'supplemental_address_3' => '1',
950 'street_number' => '1',
951 'street_name' => '1',
952 'street_unit' => '1',
954 'postal_code' => '1',
955 'postal_code_suffix' => '1',
957 'state_province_id' => '1',
962 'current_user_op' => 'eq',
963 'current_user_value' => '0',
964 'include_case_activities_op' => 'eq',
965 'include_case_activities_value' => 0,
967 1 => ['column' => 'activity_date_time', 'order' => 'ASC'],
968 2 => ['column' => 'activity_type_id', 'order' => 'ASC'],
972 $params['report_id'] = 'Activity';
974 $rows = $this->callAPISuccess('report_template', 'getrows', $params)['values'];
976 'civicrm_contact_contact_source' => 'Łąchowski-Roberts, Anthony',
977 'civicrm_contact_contact_assignee' => '<a title=\'View Contact Summary for this Contact\' href=\'/index.php?q=civicrm/contact/view&reset=1&cid=4\'>Łąchowski-Roberts, Anthony</a>',
978 'civicrm_contact_contact_target' => '<a title=\'View Contact Summary for this Contact\' href=\'/index.php?q=civicrm/contact/view&reset=1&cid=3\'>Brzęczysław, Anthony</a>; <a title=\'View Contact Summary for this Contact\' href=\'/index.php?q=civicrm/contact/view&reset=1&cid=4\'>Łąchowski-Roberts, Anthony</a>',
979 'civicrm_contact_contact_source_id' => $this->contactIDs
[2],
980 'civicrm_contact_contact_assignee_id' => $this->contactIDs
[1],
981 'civicrm_contact_contact_target_id' => $this->contactIDs
[0] . ';' . $this->contactIDs
[1],
982 'civicrm_email_contact_source_email' => 'anthony_anderson@civicrm.org',
983 'civicrm_email_contact_assignee_email' => 'anthony_anderson@civicrm.org',
984 'civicrm_email_contact_target_email' => 'techo@spying.com;anthony_anderson@civicrm.org',
985 'civicrm_phone_contact_source_phone' => NULL,
986 'civicrm_phone_contact_assignee_phone' => NULL,
987 'civicrm_phone_contact_target_phone' => NULL,
988 'civicrm_activity_id' => '1',
989 'civicrm_activity_source_record_id' => NULL,
990 'civicrm_activity_activity_type_id' => 'Meeting',
991 'civicrm_activity_activity_subject' => 'Very secret meeting',
992 'civicrm_activity_activity_date_time' => date('Y-m-d 23:59:58', strtotime('now')),
993 'civicrm_activity_status_id' => 'Scheduled',
994 'civicrm_activity_duration' => '120',
995 'civicrm_activity_location' => 'Pennsylvania',
996 'civicrm_activity_details' => 'a test activity',
997 'civicrm_activity_priority_id' => 'Normal',
998 'civicrm_address_address_name' => NULL,
999 'civicrm_address_street_address' => NULL,
1000 'civicrm_address_supplemental_address_1' => NULL,
1001 'civicrm_address_supplemental_address_2' => NULL,
1002 'civicrm_address_supplemental_address_3' => NULL,
1003 'civicrm_address_street_number' => NULL,
1004 'civicrm_address_street_name' => NULL,
1005 'civicrm_address_street_unit' => NULL,
1006 'civicrm_address_city' => NULL,
1007 'civicrm_address_postal_code' => NULL,
1008 'civicrm_address_postal_code_suffix' => NULL,
1009 'civicrm_address_country_id' => NULL,
1010 'civicrm_address_state_province_id' => NULL,
1011 'civicrm_address_county_id' => NULL,
1012 'civicrm_contact_contact_source_link' => '/index.php?q=civicrm/contact/view&reset=1&cid=' . $this->contactIDs
[2],
1013 'civicrm_contact_contact_source_hover' => 'View Contact Summary for this Contact',
1014 'civicrm_activity_activity_type_id_hover' => 'View Activity Record',
1017 // This link is not relative - skip for now
1018 unset($row['civicrm_activity_activity_type_id_link']);
1019 if ($row['civicrm_email_contact_target_email'] === 'anthony_anderson@civicrm.org;techo@spying.com') {
1020 // order is unpredictable
1021 $expected['civicrm_email_contact_target_email'] = 'anthony_anderson@civicrm.org;techo@spying.com';
1024 $this->assertEquals($expected, $row);
1028 * Set up some activity data..... use some chars that challenge our utf handling.
1030 public function createContactsWithActivities() {
1031 $this->contactIDs
[] = $this->individualCreate(['last_name' => 'Brzęczysław', 'email' => 'techo@spying.com']);
1032 $this->contactIDs
[] = $this->individualCreate(['last_name' => 'Łąchowski-Roberts']);
1033 $this->contactIDs
[] = $this->individualCreate(['last_name' => 'Łąchowski-Roberts']);
1035 $this->callAPISuccess('Activity', 'create', [
1036 'subject' => 'Very secret meeting',
1037 'activity_date_time' => date('Y-m-d 23:59:58', strtotime('now')),
1039 'location' => 'Pennsylvania',
1040 'details' => 'a test activity',
1042 'activity_type_id' => 'Meeting',
1043 'source_contact_id' => $this->contactIDs
[2],
1044 'target_contact_id' => array($this->contactIDs
[0], $this->contactIDs
[1]),
1045 'assignee_contact_id' => $this->contactIDs
[1],
1050 * Test the group filter works on the contribution summary.
1052 public function testContributionDetailTotalHeader() {
1053 $contactID = $this->individualCreate();
1054 $contactID2 = $this->individualCreate();
1055 $this->contributionCreate(['contact_id' => $contactID, 'api.ContributionSoft.create' => ['amount' => 5, 'contact_id' => $contactID2]]);
1056 $template = 'contribute/detail';
1057 $rows = $this->callAPISuccess('report_template', 'getrows', array(
1058 'report_id' => $template,
1059 'contribution_or_soft_value' => 'contributions_only',
1065 'financial_type_id' => '1',
1066 'receive_date' => '1',
1067 'total_amount' => '1',
1069 'order_bys' => [['column' => 'sort_name', 'order' => 'ASC', 'section' => '1']],
1070 'options' => array('metadata' => array('sql')),