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->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']);
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 api to get rows from reports.
220 * @dataProvider getReportTemplates
224 * @throws \PHPUnit_Framework_IncompleteTestError
226 public function testReportTemplateGetRowsAllReports($reportID) {
227 //$reportID = 'logging/contact/summary';
228 if (stristr($reportID, 'has existing issues')) {
229 $this->markTestIncomplete($reportID);
231 if (substr($reportID, 0, '7') === 'logging') {
232 Civi
::settings()->set('logging', 1);
235 $this->callAPISuccess('report_template', 'getrows', array(
236 'report_id' => $reportID,
238 if (substr($reportID, 0, '7') === 'logging') {
239 Civi
::settings()->set('logging', 0);
244 * Test logging report when a custom data table has a table removed by hook.
246 * Here we are checking that no fatal is triggered.
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'));
256 $this->callAPISuccess('report_template', 'getrows', array(
257 'report_id' => 'logging/contact/summary',
259 Civi
::settings()->set('logging', 0);
260 $this->customGroupDelete($group1['id']);
261 $this->customGroupDelete($group2['id']);
265 * Remove one log table from the logging spec.
267 * @param array $logTableSpec
269 public function alterLogTablesRemoveCustom(&$logTableSpec) {
270 unset($logTableSpec['civicrm_value_second_one']);
274 * Test api to get rows from reports with ACLs enabled.
276 * Checking for lack of fatal error at the moment.
278 * @dataProvider getReportTemplates
282 * @throws \PHPUnit_Framework_IncompleteTestError
284 public function testReportTemplateGetRowsAllReportsACL($reportID) {
285 if (stristr($reportID, 'has existing issues')) {
286 $this->markTestIncomplete($reportID);
288 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
289 $this->callAPISuccess('report_template', 'getrows', array(
290 'report_id' => $reportID,
295 * Test get statistics.
297 * @dataProvider getReportTemplates
301 * @throws \PHPUnit_Framework_IncompleteTestError
303 public function testReportTemplateGetStatisticsAllReports($reportID) {
304 if (stristr($reportID, 'has existing issues')) {
305 $this->markTestIncomplete($reportID);
307 if (in_array($reportID, array('contribute/softcredit', 'contribute/bookkeeping'))) {
308 $this->markTestIncomplete($reportID . " has non enotices when calling statistics fn");
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');
317 * Data provider function for getting all templates.
319 * Note that the function needs to
320 * be static so cannot use $this->callAPISuccess
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.',
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']);
335 $reportTemplates[] = array($report['value'] . " has existing issues : " . $reportsToSkip[$report['value']]);
339 return $reportTemplates;
343 * Get contribution templates that work with basic filter tests.
345 * These templates require minimal data config.
347 public static function getContributionReportTemplates() {
348 return array(array('contribute/summary'), array('contribute/detail'), array('contribute/repeat'), array('topDonor' => 'contribute/topDonor'));
352 * Get contribution templates that work with basic filter tests.
354 * These templates require minimal data config.
356 public static function getMembershipReportTemplates() {
357 return array(array('member/detail'));
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]);
373 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
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',
383 'yid_op' => 'calendar',
384 'options' => array('metadata' => array('sql')),
386 $this->assertEquals(1, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
390 * Test Lybunt report applies ACLs.
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'));
400 'report_id' => 'contribute/lybunt',
402 'yid_op' => 'calendar',
403 'options' => array('metadata' => array('sql')),
404 'check_permissions' => 1,
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));
410 CRM_Utils_Hook
::singleton()->reset();
414 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
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',
425 'yid_op' => 'fiscal',
426 'options' => array('metadata' => array('sql')),
427 'order_bys' => array(
429 'column' => 'first_name',
435 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
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);
453 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
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',
464 'yid_op' => 'fiscal',
465 'options' => array('metadata' => array('sql')),
466 'fields' => array('first_name'),
467 'order_bys' => array(
469 'column' => 'last_year_total_amount',
475 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
479 * Test the group filter works on the contribution summary (with a smart group).
481 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
483 * @param string $template
484 * Name of the template to test.
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,
492 'options' => array('metadata' => array('sql')),
494 $this->assertNumberOfContactsInResult(3, $rows, $template);
495 if ($template === 'contribute/summary') {
496 $this->assertEquals(3, $rows['values'][0]['civicrm_contribution_total_amount_count']);
501 * Test the group filter works on the contribution summary.
503 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
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,
511 'options' => array('metadata' => array('sql')),
513 $this->assertEquals(2, $rows['values'][0]['civicrm_contribution_total_amount_count']);
517 * Test the group filter works on the contribution summary.
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')),
531 "<a href='/index.php?q=civicrm/contact/view&reset=1&cid=" . $contactID2 . "'>Anderson, Anthony</a> $ 5.00",
532 $rows['values'][0]['civicrm_contribution_soft_credits']
537 * Test the amount column is populated on soft credit details.
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',
550 'financial_type_id' => '1',
551 'receive_date' => '1',
552 'total_amount' => '1',
554 'options' => array('metadata' => ['sql', 'labels']),
556 foreach (array_keys($rows['metadata']['labels']) as $header) {
557 $this->assertTrue(!empty($rows['values'][0][$header]));
562 * Test the group filter works on the various reports.
564 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
566 * @param string $template
567 * Report template unique identifier.
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),
575 'options' => array('metadata' => array('sql')),
577 $this->assertNumberOfContactsInResult(1, $rows, $template);
581 * Assert the included results match the expected.
583 * There may or may not be a group by in play so the assertion varies a little.
585 * @param int $numberExpected
587 * Rows returned from the report.
588 * @param string $template
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);
595 $this->assertEquals($numberExpected, count($rows['values']), 'wrong row count in ' . $template);
600 * Test the group filter works on the contribution summary when 2 groups are involved.
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),
609 'options' => array('metadata' => array('sql')),
611 $this->assertEquals(4, $rows['values'][0]['civicrm_contribution_total_amount_count']);
615 * CRM-20640: Test the group filter works on the contribution summary when a single contact in 2 groups.
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,
627 $rows = $this->callAPISuccess('report_template', 'getrows', array(
628 'report_id' => 'contribute/summary',
629 'gid_value' => array($groupID1, $groupID2),
631 'options' => array('metadata' => array('sql')),
633 $this->assertEquals(1, $rows['count']);
637 * Test the group filter works on the contribution summary when 2 groups are involved.
639 public function testContributionSummaryWithTwoGroupsWithIntersection() {
640 $groups = $this->setUpIntersectingGroups();
642 $rows = $this->callAPISuccess('report_template', 'getrows', array(
643 'report_id' => 'contribute/summary',
644 'gid_value' => $groups,
646 'options' => array('metadata' => array('sql')),
648 $this->assertEquals(7, $rows['values'][0]['civicrm_contribution_total_amount_count']);
652 * Set up a smart group for testing.
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.
657 * One household is hard-added as well as being in the filter.
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.
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',
676 $this->callAPISuccess('GroupContact', 'create', array(
677 'group_id' => $groupID,
678 'contact_id' => $individualID,
681 $this->callAPISuccess('GroupContact', 'create', array(
682 'group_id' => $groupID,
683 'contact_id' => $householdID,
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));
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);
697 * Set up a static group for testing.
699 * An individual is created and hard-added and an individual is created that is not added.
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.
704 * @param bool $returnAddedContact
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',
718 $this->callAPISuccess('GroupContact', 'create', array(
719 'group_id' => $groupID,
720 'contact_id' => $individualID,
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));
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);
732 if ($returnAddedContact) {
733 return array($groupID, $individualID);
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,
754 $this->callAPISuccess('GroupContact', 'create', array(
755 'group_id' => $groupID2,
756 'contact_id' => $addedToBothIndividualID,
759 $this->callAPISuccess('GroupContact', 'create', array(
760 'group_id' => $groupID,
761 'contact_id' => $removedFromBothIndividualID,
762 'status' => 'Removed',
764 $this->callAPISuccess('GroupContact', 'create', array(
765 'group_id' => $groupID2,
766 'contact_id' => $removedFromBothIndividualID,
767 'status' => 'Removed',
769 $this->callAPISuccess('GroupContact', 'create', array(
770 'group_id' => $groupID2,
771 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
774 $this->callAPISuccess('GroupContact', 'create', array(
775 'group_id' => $groupID,
776 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
777 'status' => 'Removed',
779 $this->callAPISuccess('GroupContact', 'create', array(
780 'group_id' => $groupID,
781 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
784 $this->callAPISuccess('GroupContact', 'create', array(
785 'group_id' => $groupID2,
786 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
787 'status' => 'Removed',
791 $addedToBothIndividualID,
792 $removedFromBothIndividualID,
793 $addedToSmartGroupRemovedFromOtherIndividualID,
794 $removedFromSmartGroupAddedToOtherIndividualID,
796 $this->contributionCreate(array(
797 'contact_id' => $contactID,
802 return array($groupID, $groupID2);
806 * Test Deferred Revenue Report.
808 public function testDeferredRevenueReport() {
809 $indv1 = $this->individualCreate();
810 $indv2 = $this->individualCreate();
812 'contribution_invoice_settings' => array(
813 'deferred_revenue_enabled' => '1',
816 $this->callAPISuccess('Setting', 'create', $params);
817 $this->contributionCreate(
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,
825 $this->contributionCreate(
827 'contact_id' => $indv1,
828 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+22 month')),
829 'financial_type_id' => 4,
831 'invoice_id' => NULL,
834 $this->contributionCreate(
836 'contact_id' => $indv2,
837 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+1 month')),
838 'financial_type_id' => 4,
840 'invoice_id' => NULL,
843 $this->contributionCreate(
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,
850 'invoice_id' => NULL,
853 $rows = $this->callAPISuccess('report_template', 'getrows', array(
854 'report_id' => 'contribute/deferredrevenue',
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");
864 * Test the group filter works on the various reports.
866 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
868 * @param string $template
869 * Report template unique identifier.
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),
877 'options' => array('metadata' => array('sql')),
879 $this->assertNumberOfContactsInResult(2, $rows, $template);
883 * Test activity summary report - requiring all current fields to be output.
885 public function testActivitySummary() {
886 $this->createContactsWithActivities();
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',
904 'priority_id' => '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',
916 'postal_code' => '1',
917 'postal_code_suffix' => '1',
919 'state_province_id' => '1',
924 'current_user_op' => 'eq',
925 'current_user_value' => '0',
926 'include_case_activities_op' => 'eq',
927 'include_case_activities_value' => 0,
929 1 => ['column' => 'activity_date_time', 'order' => 'ASC'],
930 2 => ['column' => 'activity_type_id', 'order' => 'ASC'],
934 $params['report_id'] = 'Activity';
936 $rows = $this->callAPISuccess('report_template', 'getrows', $params)['values'];
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&reset=1&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&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>',
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&reset=1&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',
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';
986 $this->assertEquals($expected, $row);
990 * Set up some activity data..... use some chars that challenge our utf handling.
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']);
997 $this->callAPISuccess('Activity', 'create', [
998 'subject' => 'Very secret meeting',
999 'activity_date_time' => date('Y-m-d 23:59:58', strtotime('now')),
1001 'location' => 'Pennsylvania',
1002 'details' => 'a test activity',
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],
1012 * Test the group filter works on the contribution summary.
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',
1027 'financial_type_id' => '1',
1028 'receive_date' => '1',
1029 'total_amount' => '1',
1031 'order_bys' => [['column' => 'sort_name', 'order' => 'ASC', 'section' => '1']],
1032 'options' => array('metadata' => array('sql')),