3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
{
36 protected $_apiversion = 3;
39 * Our group reports use an alter so transaction cleanup won't work.
43 public function tearDown() {
44 $this->quickCleanUpFinancialEntities();
45 $this->quickCleanup(array('civicrm_group', 'civicrm_saved_search', 'civicrm_group_contact', 'civicrm_group_contact_cache', 'civicrm_group'));
49 public function testReportTemplate() {
50 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
51 'label' => 'Example Form',
52 'description' => 'Longish description of the example form',
53 'class_name' => 'CRM_Report_Form_Examplez',
54 'report_url' => 'example/path',
55 'component' => 'CiviCase',
57 $this->assertAPISuccess($result);
58 $this->assertEquals(1, $result['count']);
59 $entityId = $result['id'];
60 $this->assertTrue(is_numeric($entityId));
61 $this->assertEquals(7, $result['values'][$entityId]['component_id']);
62 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
63 WHERE name = "CRM_Report_Form_Examplez"
64 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
65 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
66 WHERE name = "CRM_Report_Form_Examplez"');
68 // change component to null
69 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
73 $this->assertAPISuccess($result);
74 $this->assertEquals(1, $result['count']);
75 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
76 WHERE name = "CRM_Report_Form_Examplez"
77 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
78 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
79 WHERE name = "CRM_Report_Form_Examplez"
80 AND component_id IS NULL');
83 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
87 $this->assertAPISuccess($result);
88 $this->assertEquals(1, $result['count']);
89 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
90 WHERE name = "CRM_Report_Form_Examplez"
91 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
92 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value
93 WHERE name = "CRM_Report_Form_Examplez"');
96 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
100 $this->assertAPISuccess($result);
101 $this->assertEquals(1, $result['count']);
102 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
103 WHERE name = "CRM_Report_Form_Examplez"
104 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
105 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
106 WHERE name = "CRM_Report_Form_Examplez"');
108 $result = $this->callAPISuccess('ReportTemplate', 'delete', array(
111 $this->assertAPISuccess($result);
112 $this->assertEquals(1, $result['count']);
113 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value
114 WHERE name = "CRM_Report_Form_Examplez"
119 * Test getrows on contact summary report.
121 public function testReportTemplateGetRowsContactSummary() {
122 $description = "Retrieve rows from a report template (optionally providing the instance_id).";
123 $result = $this->callAPIAndDocument('report_template', 'getrows', array(
124 'report_id' => 'contact/summary',
125 'options' => array('metadata' => array('labels', 'title')),
126 ), __FUNCTION__
, __FILE__
, $description, 'Getrows');
127 $this->assertEquals('Contact Name', $result['metadata']['labels']['civicrm_contact_sort_name']);
129 //the second part of this test has been commented out because it relied on the db being reset to
131 //wasn't able to get that to work consistently
132 // however, when the db is in the base state the tests do pass
133 // and because the test covers 'all' contacts we can't create our own & assume the others don't exist
135 $this->assertEquals(2, $result['count']);
136 $this->assertEquals('Default Organization', $result[0]['civicrm_contact_sort_name']);
137 $this->assertEquals('Second Domain', $result[1]['civicrm_contact_sort_name']);
138 $this->assertEquals('15 Main St', $result[1]['civicrm_address_street_address']);
143 * Tet api to get rows from reports.
145 * @dataProvider getReportTemplates
149 * @throws \PHPUnit_Framework_IncompleteTestError
151 public function testReportTemplateGetRowsAllReports($reportID) {
152 if (stristr($reportID, 'has existing issues')) {
153 $this->markTestIncomplete($reportID);
155 $this->callAPISuccess('report_template', 'getrows', array(
156 'report_id' => $reportID,
161 * Test get statistics.
163 * @dataProvider getReportTemplates
167 * @throws \PHPUnit_Framework_IncompleteTestError
169 public function testReportTemplateGetStatisticsAllReports($reportID) {
170 if (stristr($reportID, 'has existing issues')) {
171 $this->markTestIncomplete($reportID);
173 if (in_array($reportID, array('contribute/softcredit', 'contribute/bookkeeping'))) {
174 $this->markTestIncomplete($reportID . " has non enotices when calling statistics fn");
176 $description = "Get Statistics from a report (note there isn't much data to get in the test DB).";
177 $result = $this->callAPIAndDocument('report_template', 'getstatistics', array(
178 'report_id' => $reportID,
179 ), __FUNCTION__
, __FILE__
, $description, 'Getstatistics', 'getstatistics');
183 * Data provider function for getting all templates.
185 * Note that the function needs to
186 * be static so cannot use $this->callAPISuccess
188 public static function getReportTemplates() {
189 $reportsToSkip = array(
190 'activity' => 'does not respect function signature on from clause',
191 'contribute/topDonor' => 'construction of query in postProcess makes inaccessible ',
192 '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',
193 'logging/contact/summary' => '(likely to be test related) probably logging off Undefined index: Form/Contact/LoggingSummary.php(231): PHP',
194 'logging/contribute/summary' => '(likely to be test related) probably logging off DB Error: no such table',
195 'contribute/history' => 'Declaration of CRM_Report_Form_Contribute_History::buildRows() should be compatible with CRM_Report_Form::buildRows($sql, &$rows)',
196 'activitySummary' => 'We use temp tables for the main query generation and name are dynamic. These names are not available in stats() when called directly.',
199 $reports = civicrm_api3('report_template', 'get', array('return' => 'value', 'options' => array('limit' => 500)));
200 foreach ($reports['values'] as $report) {
201 if (empty($reportsToSkip[$report['value']])) {
202 $reportTemplates[] = array($report['value']);
205 $reportTemplates[] = array($report['value'] . " has existing issues : " . $reportsToSkip[$report['value']]);
209 return $reportTemplates;
213 * Get contribution templates that work with basic filter tests.
215 * These templates require minimal data config.
217 public static function getContributionReportTemplates() {
218 return array(array('contribute/summary'), array('contribute/detail'), array('contribute/repeat'), array('contribute/topDonor'));
222 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
224 public function testLybuntReportWithData() {
225 $inInd = $this->individualCreate();
226 $outInd = $this->individualCreate();
227 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-03-01'));
228 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
229 $rows = $this->callAPISuccess('report_template', 'getrows', array(
230 'report_id' => 'contribute/lybunt',
232 'yid_op' => 'calendar',
233 'options' => array('metadata' => array('sql')),
235 $this->assertEquals(1, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
239 * Test Lybunt report applies ACLs.
241 public function testLybuntReportWithDataAndACLFilter() {
242 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('administer CiviCRM');
243 $inInd = $this->individualCreate();
244 $outInd = $this->individualCreate();
245 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-03-01'));
246 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
247 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
249 'report_id' => 'contribute/lybunt',
251 'yid_op' => 'calendar',
252 'options' => array('metadata' => array('sql')),
253 'check_permissions' => 1,
256 $rows = $this->callAPISuccess('report_template', 'getrows', $params);
257 $this->assertEquals(0, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
259 CRM_Utils_Hook
::singleton()->reset();
263 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
265 public function testLybuntReportWithFYData() {
266 $inInd = $this->individualCreate();
267 $outInd = $this->individualCreate();
268 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-10-01'));
269 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
270 $this->callAPISuccess('Setting', 'create', array('fiscalYearStart' => array('M' => 7, 'd' => 1)));
271 $rows = $this->callAPISuccess('report_template', 'getrows', array(
272 'report_id' => 'contribute/lybunt',
274 'yid_op' => 'fiscal',
275 'options' => array('metadata' => array('sql')),
276 'order_bys' => array(
278 'column' => 'first_name',
284 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
288 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
290 public function testLybuntReportWithFYDataOrderByLastYearAmount() {
291 $inInd = $this->individualCreate();
292 $outInd = $this->individualCreate();
293 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-10-01'));
294 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
295 $this->callAPISuccess('Setting', 'create', array('fiscalYearStart' => array('M' => 7, 'd' => 1)));
296 $rows = $this->callAPISuccess('report_template', 'getrows', array(
297 'report_id' => 'contribute/lybunt',
299 'yid_op' => 'fiscal',
300 'options' => array('metadata' => array('sql')),
301 'fields' => array('first_name'),
302 'order_bys' => array(
304 'column' => 'last_year_total_amount',
310 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
314 * Test the group filter works on the contribution summary (with a smart group).
316 public function testContributionSummaryWithSmartGroupFilter() {
317 $groupID = $this->setUpPopulatedSmartGroup();
318 $rows = $this->callAPISuccess('report_template', 'getrows', array(
319 'report_id' => 'contribute/summary',
320 'gid_value' => $groupID,
322 'options' => array('metadata' => array('sql')),
324 $this->assertEquals(3, $rows['values'][0]['civicrm_contribution_total_amount_count']);
329 * Test the group filter works on the contribution summary (with a smart group).
331 public function testContributionSummaryWithNotINSmartGroupFilter() {
332 $groupID = $this->setUpPopulatedSmartGroup();
333 $rows = $this->callAPISuccess('report_template', 'getrows', array(
334 'report_id' => 'contribute/summary',
335 'gid_value' => $groupID,
337 'options' => array('metadata' => array('sql')),
339 $this->assertEquals(2, $rows['values'][0]['civicrm_contribution_total_amount_count']);
344 * Test the group filter works on the contribution summary (with a smart group).
346 * @dataProvider getContributionReportTemplates
348 * @param string $template
349 * Report template unique identifier.
351 public function testContributionSummaryWithNonSmartGroupFilter($template) {
352 $groupID = $this->setUpPopulatedGroup();
353 $rows = $this->callAPISuccess('report_template', 'getrows', array(
354 'report_id' => $template,
355 'gid_value' => array($groupID),
357 'options' => array('metadata' => array('sql')),
359 $this->assertNumberOfContactsInResult(1, $rows, $template);
363 * Assert the included results match the expected.
365 * There may or may not be a group by in play so the assertion varies a little.
367 * @param int $numberExpected
369 * Rows returned from the report.
370 * @param string $template
372 protected function assertNumberOfContactsInResult($numberExpected, $rows, $template) {
373 if (isset($rows['values'][0]['civicrm_contribution_total_amount_count'])) {
374 $this->assertEquals($numberExpected, $rows['values'][0]['civicrm_contribution_total_amount_count'], 'wrong row count in ' . $template);
377 $this->assertEquals($numberExpected, count($rows['values']), 'wrong row count in ' . $template);
382 * Test the group filter works on the contribution summary when 2 groups are involved.
384 public function testContributionSummaryWithTwoGroups() {
385 $groupID = $this->setUpPopulatedGroup();
386 $groupID2 = $this->setUpPopulatedSmartGroup();
387 $rows = $this->callAPISuccess('report_template', 'getrows', array(
388 'report_id' => 'contribute/summary',
389 'gid_value' => array($groupID, $groupID2),
391 'options' => array('metadata' => array('sql')),
393 $this->assertEquals(4, $rows['values'][0]['civicrm_contribution_total_amount_count']);
397 * CRM-20640: Test the group filter works on the contribution summary when a single contact in 2 groups.
399 public function testContributionSummaryWithSingleContactsInTwoGroups() {
400 list($groupID1, $individualID) = $this->setUpPopulatedGroup(TRUE);
401 // create second group and add the individual to it.
402 $groupID2 = $this->groupCreate(array('name' => uniqid(), 'title' => uniqid()));
403 $this->callAPISuccess('GroupContact', 'create', array(
404 'group_id' => $groupID2,
405 'contact_id' => $individualID,
409 $rows = $this->callAPISuccess('report_template', 'getrows', array(
410 'report_id' => 'contribute/summary',
411 'gid_value' => array($groupID1, $groupID2),
413 'options' => array('metadata' => array('sql')),
415 $this->assertEquals(1, $rows['count']);
419 * Test the group filter works on the contribution summary when 2 groups are involved.
421 public function testContributionSummaryWithTwoGroupsWithIntersection() {
422 $groups = $this->setUpIntersectingGroups();
424 $rows = $this->callAPISuccess('report_template', 'getrows', array(
425 'report_id' => 'contribute/summary',
426 'gid_value' => $groups,
428 'options' => array('metadata' => array('sql')),
430 $this->assertEquals(7, $rows['values'][0]['civicrm_contribution_total_amount_count']);
434 * Set up a smart group for testing.
436 * The smart group includes all Households by filter. In addition an individual
437 * is created and hard-added and an individual is created that is not added.
439 * One household is hard-added as well as being in the filter.
441 * This gives us a range of scenarios for testing contacts are included only once
442 * whenever they are hard-added or in the criteria.
446 public function setUpPopulatedSmartGroup() {
447 $household1ID = $this->householdCreate();
448 $individual1ID = $this->individualCreate();
449 $householdID = $this->householdCreate();
450 $individualID = $this->individualCreate();
451 $individualIDRemoved = $this->individualCreate();
452 $groupID = $this->smartGroupCreate(array(), array('name' => uniqid(), 'title' => uniqid()));
453 $this->callAPISuccess('GroupContact', 'create', array(
454 'group_id' => $groupID,
455 'contact_id' => $individualIDRemoved,
456 'status' => 'Removed',
458 $this->callAPISuccess('GroupContact', 'create', array(
459 'group_id' => $groupID,
460 'contact_id' => $individualID,
463 $this->callAPISuccess('GroupContact', 'create', array(
464 'group_id' => $groupID,
465 'contact_id' => $householdID,
468 foreach (array($household1ID, $individual1ID, $householdID, $individualID, $individualIDRemoved) as $contactID) {
469 $this->contributionCreate(array('contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => ''));
472 // Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
473 CRM_Contact_BAO_GroupContactCache
::clearGroupContactCache($groupID);
478 * Set up a smart group for testing.
480 * The smart group includes all Households by filter. In addition an individual
481 * is created and hard-added and an individual is created that is not added.
483 * One household is hard-added as well as being in the filter.
485 * This gives us a range of scenarios for testing contacts are included only once
486 * whenever they are hard-added or in the criteria.
488 * @param bool $returnAddedContact
492 public function setUpPopulatedGroup($returnAddedContact = FALSE) {
493 $individual1ID = $this->individualCreate();
494 $individualID = $this->individualCreate();
495 $individualIDRemoved = $this->individualCreate();
496 $groupID = $this->groupCreate(array('name' => uniqid(), 'title' => uniqid()));
497 $this->callAPISuccess('GroupContact', 'create', array(
498 'group_id' => $groupID,
499 'contact_id' => $individualIDRemoved,
500 'status' => 'Removed',
502 $this->callAPISuccess('GroupContact', 'create', array(
503 'group_id' => $groupID,
504 'contact_id' => $individualID,
508 foreach (array($individual1ID, $individualID, $individualIDRemoved) as $contactID) {
509 $this->contributionCreate(array('contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => ''));
512 // Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
513 CRM_Contact_BAO_GroupContactCache
::clearGroupContactCache($groupID);
515 if ($returnAddedContact) {
516 return array($groupID, $individualID);
525 public function setUpIntersectingGroups() {
526 $groupID = $this->setUpPopulatedGroup();
527 $groupID2 = $this->setUpPopulatedSmartGroup();
528 $addedToBothIndividualID = $this->individualCreate();
529 $removedFromBothIndividualID = $this->individualCreate();
530 $addedToSmartGroupRemovedFromOtherIndividualID = $this->individualCreate();
531 $removedFromSmartGroupAddedToOtherIndividualID = $this->individualCreate();
532 $this->callAPISuccess('GroupContact', 'create', array(
533 'group_id' => $groupID,
534 'contact_id' => $addedToBothIndividualID,
537 $this->callAPISuccess('GroupContact', 'create', array(
538 'group_id' => $groupID2,
539 'contact_id' => $addedToBothIndividualID,
542 $this->callAPISuccess('GroupContact', 'create', array(
543 'group_id' => $groupID,
544 'contact_id' => $removedFromBothIndividualID,
545 'status' => 'Removed',
547 $this->callAPISuccess('GroupContact', 'create', array(
548 'group_id' => $groupID2,
549 'contact_id' => $removedFromBothIndividualID,
550 'status' => 'Removed',
552 $this->callAPISuccess('GroupContact', 'create', array(
553 'group_id' => $groupID2,
554 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
557 $this->callAPISuccess('GroupContact', 'create', array(
558 'group_id' => $groupID,
559 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
560 'status' => 'Removed',
562 $this->callAPISuccess('GroupContact', 'create', array(
563 'group_id' => $groupID,
564 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
567 $this->callAPISuccess('GroupContact', 'create', array(
568 'group_id' => $groupID2,
569 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
570 'status' => 'Removed',
574 $addedToBothIndividualID,
575 $removedFromBothIndividualID,
576 $addedToSmartGroupRemovedFromOtherIndividualID,
577 $removedFromSmartGroupAddedToOtherIndividualID,
579 $this->contributionCreate(array(
580 'contact_id' => $contactID,
585 return array($groupID, $groupID2);
589 * Test Deferred Revenue Report.
591 public function testDeferredRevenueReport() {
592 $indv1 = $this->individualCreate();
593 $indv2 = $this->individualCreate();
595 'contribution_invoice_settings' => array(
596 'deferred_revenue_enabled' => '1',
599 $this->callAPISuccess('Setting', 'create', $params);
600 $this->contributionCreate(
602 'contact_id' => $indv1,
603 'receive_date' => '2016-10-01',
604 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+3 month')),
605 'financial_type_id' => 2,
608 $this->contributionCreate(
610 'contact_id' => $indv1,
611 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+22 month')),
612 'financial_type_id' => 4,
614 'invoice_id' => NULL,
617 $this->contributionCreate(
619 'contact_id' => $indv2,
620 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+1 month')),
621 'financial_type_id' => 4,
623 'invoice_id' => NULL,
626 $this->contributionCreate(
628 'contact_id' => $indv2,
629 'receive_date' => '2016-03-01',
630 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+4 month')),
631 'financial_type_id' => 2,
633 'invoice_id' => NULL,
636 $rows = $this->callAPISuccess('report_template', 'getrows', array(
637 'report_id' => 'contribute/deferredrevenue',
639 $this->assertEquals(2, $rows['count'], "Report failed to get row count");
640 $count = array(2, 1);
641 foreach ($rows['values'] as $row) {
642 $this->assertEquals(array_pop($count), count($row['rows']), "Report failed to get row count");