Merge pull request #10600 from civicrm/4.7.21-rc
[civicrm-core.git] / tests / phpunit / api / v3 / ReportTemplateTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
6a488035
TO
28/**
29 * Test APIv3 civicrm_report_instance_* functions
30 *
6c6e6187
TO
31 * @package CiviCRM_APIv3
32 * @subpackage API_Report
acb109b7 33 * @group headless
6a488035 34 */
6a488035 35class api_v3_ReportTemplateTest extends CiviUnitTestCase {
e2779f6e 36 protected $_apiversion = 3;
b7c9bc4c 37
2c6b4783 38 /**
39 * Our group reports use an alter so transaction cleanup won't work.
40 *
41 * @throws \Exception
42 */
43 public function tearDown() {
44 $this->quickCleanUpFinancialEntities();
45 $this->quickCleanup(array('civicrm_group', 'civicrm_saved_search', 'civicrm_group_contact'));
46 parent::tearDown();
6a488035
TO
47 }
48
6a488035 49 public function testReportTemplate() {
7fbb4198 50 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
6a488035
TO
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',
56 ));
1cbea43e 57 $this->assertAPISuccess($result);
ba4a1892 58 $this->assertEquals(1, $result['count']);
6a488035 59 $entityId = $result['id'];
ba4a1892
TM
60 $this->assertTrue(is_numeric($entityId));
61 $this->assertEquals(7, $result['values'][$entityId]['component_id']);
6a488035
TO
62 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
63 WHERE name = "CRM_Report_Form_Examplez"
0201b57f 64 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
6a488035
TO
65 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
66 WHERE name = "CRM_Report_Form_Examplez"');
67
68 // change component to null
6c6e6187 69 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
92915c55 70 'id' => $entityId,
6a488035
TO
71 'component' => '',
72 ));
1cbea43e 73 $this->assertAPISuccess($result);
ba4a1892 74 $this->assertEquals(1, $result['count']);
6a488035
TO
75 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
76 WHERE name = "CRM_Report_Form_Examplez"
0201b57f 77 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
6a488035
TO
78 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
79 WHERE name = "CRM_Report_Form_Examplez"
80 AND component_id IS NULL');
81
82 // deactivate
6c6e6187 83 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
92915c55 84 'id' => $entityId,
6a488035
TO
85 'is_active' => 0,
86 ));
1cbea43e 87 $this->assertAPISuccess($result);
ba4a1892 88 $this->assertEquals(1, $result['count']);
6a488035
TO
89 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
90 WHERE name = "CRM_Report_Form_Examplez"
0201b57f 91 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
6a488035
TO
92 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value
93 WHERE name = "CRM_Report_Form_Examplez"');
94
95 // activate
6c6e6187 96 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
92915c55 97 'id' => $entityId,
6a488035
TO
98 'is_active' => 1,
99 ));
1cbea43e 100 $this->assertAPISuccess($result);
ba4a1892 101 $this->assertEquals(1, $result['count']);
6a488035
TO
102 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
103 WHERE name = "CRM_Report_Form_Examplez"
0201b57f 104 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
6a488035
TO
105 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
106 WHERE name = "CRM_Report_Form_Examplez"');
107
92915c55
TO
108 $result = $this->callAPISuccess('ReportTemplate', 'delete', array(
109 'id' => $entityId,
6a488035 110 ));
1cbea43e 111 $this->assertAPISuccess($result);
ba4a1892 112 $this->assertEquals(1, $result['count']);
6a488035
TO
113 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value
114 WHERE name = "CRM_Report_Form_Examplez"
115 ');
116 }
e2779f6e
E
117
118 /**
fe482240 119 * Test getrows on contact summary report.
e2779f6e 120 */
00be9182 121 public function testReportTemplateGetRowsContactSummary() {
5c49fee0 122 $description = "Retrieve rows from a report template (optionally providing the instance_id).";
fee15d2a 123 $result = $this->callAPIAndDocument('report_template', 'getrows', array(
e2779f6e 124 'report_id' => 'contact/summary',
21dfd5f5 125 'options' => array('metadata' => array('labels', 'title')),
fee15d2a 126 ), __FUNCTION__, __FILE__, $description, 'Getrows', 'getrows');
781d91c8 127 $this->assertEquals('Contact Name', $result['metadata']['labels']['civicrm_contact_sort_name']);
e2779f6e
E
128
129 //the second part of this test has been commented out because it relied on the db being reset to
130 // it's base state
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
134 /*
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']);
e70a7fc0 139 */
e2779f6e
E
140 }
141
142 /**
fe482240
EM
143 * Tet api to get rows from reports.
144 *
e2779f6e 145 * @dataProvider getReportTemplates
fe482240 146 *
1e1fdcf6 147 * @param $reportID
fe482240 148 *
1e1fdcf6 149 * @throws \PHPUnit_Framework_IncompleteTestError
e2779f6e 150 */
00be9182 151 public function testReportTemplateGetRowsAllReports($reportID) {
22e263ad 152 if (stristr($reportID, 'has existing issues')) {
e2779f6e
E
153 $this->markTestIncomplete($reportID);
154 }
fe482240 155 $this->callAPISuccess('report_template', 'getrows', array(
92915c55 156 'report_id' => $reportID,
e2779f6e
E
157 ));
158 }
159
160 /**
fe482240
EM
161 * Test get statistics.
162 *
e2779f6e 163 * @dataProvider getReportTemplates
fe482240 164 *
1e1fdcf6 165 * @param $reportID
fe482240 166 *
1e1fdcf6 167 * @throws \PHPUnit_Framework_IncompleteTestError
e2779f6e 168 */
00be9182 169 public function testReportTemplateGetStatisticsAllReports($reportID) {
22e263ad 170 if (stristr($reportID, 'has existing issues')) {
e2779f6e
E
171 $this->markTestIncomplete($reportID);
172 }
22e263ad 173 if (in_array($reportID, array('contribute/softcredit', 'contribute/bookkeeping'))) {
e2779f6e
E
174 $this->markTestIncomplete($reportID . " has non enotices when calling statistics fn");
175 }
5c49fee0 176 $description = "Get Statistics from a report (note there isn't much data to get in the test DB).";
fee15d2a 177 $result = $this->callAPIAndDocument('report_template', 'getstatistics', array(
e2779f6e 178 'report_id' => $reportID,
fee15d2a 179 ), __FUNCTION__, __FILE__, $description, 'Getstatistics', 'getstatistics');
e2779f6e
E
180 }
181
182 /**
fe482240
EM
183 * Data provider function for getting all templates.
184 *
185 * Note that the function needs to
e2779f6e
E
186 * be static so cannot use $this->callAPISuccess
187 */
188 public static function getReportTemplates() {
189 $reportsToSkip = array(
92915c55 190 'activity' => 'does not respect function signature on from clause',
92915c55 191 'contribute/topDonor' => 'construction of query in postProcess makes inaccessible ',
92915c55 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',
92915c55 193 'logging/contact/summary' => '(likely to be test related) probably logging off Undefined index: Form/Contact/LoggingSummary.php(231): PHP',
92915c55 194 'logging/contribute/summary' => '(likely to be test related) probably logging off DB Error: no such table',
92915c55 195 'contribute/history' => 'Declaration of CRM_Report_Form_Contribute_History::buildRows() should be compatible with CRM_Report_Form::buildRows($sql, &$rows)',
cb5aba81 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.',
e2779f6e
E
197 );
198
199 $reports = civicrm_api3('report_template', 'get', array('return' => 'value', 'options' => array('limit' => 500)));
200 foreach ($reports['values'] as $report) {
22e263ad 201 if (empty($reportsToSkip[$report['value']])) {
e2779f6e
E
202 $reportTemplates[] = array($report['value']);
203 }
204 else {
86bfa4f6 205 $reportTemplates[] = array($report['value'] . " has existing issues : " . $reportsToSkip[$report['value']]);
e2779f6e
E
206 }
207 }
208
e2779f6e
E
209 return $reportTemplates;
210 }
96025800 211
2c6b4783 212 /**
213 * Get contribution templates that work with basic filter tests.
214 *
215 * These templates require minimal data config.
216 */
217 public static function getContributionReportTemplates() {
218 return array(array('contribute/summary'), array('contribute/detail'), array('contribute/repeat'), array('contribute/topDonor'));
219 }
220
c160fde8 221 /**
222 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
223 */
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',
231 'yid_value' => 2015,
232 'yid_op' => 'calendar',
233 'options' => array('metadata' => array('sql')),
234 ));
235 $this->assertEquals(1, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
236 }
237
33072bc7 238 /**
239 * Test Lybunt report applies ACLs.
240 */
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'));
248 $params = array(
249 'report_id' => 'contribute/lybunt',
250 'yid_value' => 2015,
251 'yid_op' => 'calendar',
252 'options' => array('metadata' => array('sql')),
253 'check_permissions' => 1,
254 );
255
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));
258
259 CRM_Utils_Hook::singleton()->reset();
260 }
261
c160fde8 262 /**
263 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
264 */
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',
273 'yid_value' => 2015,
274 'yid_op' => 'fiscal',
275 'options' => array('metadata' => array('sql')),
276 'order_bys' => array(
277 array(
278 'column' => 'first_name',
279 'order' => 'ASC',
280 ),
281 ),
282 ));
283
284 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
285 }
286
3fd9a92a 287 /**
288 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
289 */
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',
298 'yid_value' => 2015,
299 'yid_op' => 'fiscal',
300 'options' => array('metadata' => array('sql')),
301 'fields' => array('first_name'),
302 'order_bys' => array(
303 array(
304 'column' => 'last_year_total_amount',
305 'order' => 'ASC',
306 ),
307 ),
308 ));
309
310 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
311 }
312
2c6b4783 313 /**
314 * Test the group filter works on the contribution summary (with a smart group).
315 */
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,
321 'gid_op' => 'in',
322 'options' => array('metadata' => array('sql')),
323 ));
324 $this->assertEquals(3, $rows['values'][0]['civicrm_contribution_total_amount_count']);
325
326 }
327
328 /**
329 * Test the group filter works on the contribution summary (with a smart group).
330 */
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,
43c1fa19 336 'gid_op' => 'notin',
2c6b4783 337 'options' => array('metadata' => array('sql')),
338 ));
339 $this->assertEquals(2, $rows['values'][0]['civicrm_contribution_total_amount_count']);
340
341 }
342
343 /**
344 * Test the group filter works on the contribution summary (with a smart group).
345 *
346 * @dataProvider getContributionReportTemplates
347 *
348 * @param string $template
349 * Report template unique identifier.
350 */
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),
356 'gid_op' => 'in',
357 'options' => array('metadata' => array('sql')),
358 ));
359 $this->assertNumberOfContactsInResult(1, $rows, $template);
360 }
361
362 /**
363 * Assert the included results match the expected.
364 *
365 * There may or may not be a group by in play so the assertion varies a little.
366 *
367 * @param int $numberExpected
368 * @param array $rows
369 * Rows returned from the report.
370 * @param string $template
371 */
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);
375 }
376 else {
377 $this->assertEquals($numberExpected, count($rows['values']), 'wrong row count in ' . $template);
378 }
379 }
380
381 /**
382 * Test the group filter works on the contribution summary when 2 groups are involved.
383 */
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),
390 'gid_op' => 'in',
391 'options' => array('metadata' => array('sql')),
392 ));
393 $this->assertEquals(4, $rows['values'][0]['civicrm_contribution_total_amount_count']);
394 }
395
396 /**
397 * Test the group filter works on the contribution summary when 2 groups are involved.
398 */
399 public function testContributionSummaryWithTwoGroupsWithIntersection() {
400 $groups = $this->setUpIntersectingGroups();
401
402 $rows = $this->callAPISuccess('report_template', 'getrows', array(
403 'report_id' => 'contribute/summary',
404 'gid_value' => $groups,
405 'gid_op' => 'in',
406 'options' => array('metadata' => array('sql')),
407 ));
408 $this->assertEquals(7, $rows['values'][0]['civicrm_contribution_total_amount_count']);
409 }
410
411 /**
412 * Set up a smart group for testing.
413 *
414 * The smart group includes all Households by filter. In addition an individual
415 * is created and hard-added and an individual is created that is not added.
416 *
417 * One household is hard-added as well as being in the filter.
418 *
419 * This gives us a range of scenarios for testing contacts are included only once
420 * whenever they are hard-added or in the criteria.
421 *
422 * @return int
423 */
424 public function setUpPopulatedSmartGroup() {
425 $household1ID = $this->householdCreate();
426 $individual1ID = $this->individualCreate();
427 $householdID = $this->householdCreate();
428 $individualID = $this->individualCreate();
429 $individualIDRemoved = $this->individualCreate();
430 $groupID = $this->smartGroupCreate(array(), array('name' => uniqid(), 'title' => uniqid()));
431 $this->callAPISuccess('GroupContact', 'create', array(
432 'group_id' => $groupID,
433 'contact_id' => $individualIDRemoved,
434 'status' => 'Removed',
435 ));
436 $this->callAPISuccess('GroupContact', 'create', array(
437 'group_id' => $groupID,
438 'contact_id' => $individualID,
439 'status' => 'Added',
440 ));
441 $this->callAPISuccess('GroupContact', 'create', array(
442 'group_id' => $groupID,
443 'contact_id' => $householdID,
444 'status' => 'Added',
445 ));
446 foreach (array($household1ID, $individual1ID, $householdID, $individualID, $individualIDRemoved) as $contactID) {
447 $this->contributionCreate(array('contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => ''));
448 }
449
450 // Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
451 CRM_Contact_BAO_GroupContactCache::remove($groupID, FALSE);
452 return $groupID;
453 }
454
455 /**
456 * Set up a smart group for testing.
457 *
458 * The smart group includes all Households by filter. In addition an individual
459 * is created and hard-added and an individual is created that is not added.
460 *
461 * One household is hard-added as well as being in the filter.
462 *
463 * This gives us a range of scenarios for testing contacts are included only once
464 * whenever they are hard-added or in the criteria.
465 *
466 * @return int
467 */
468 public function setUpPopulatedGroup() {
469 $individual1ID = $this->individualCreate();
470 $individualID = $this->individualCreate();
471 $individualIDRemoved = $this->individualCreate();
472 $groupID = $this->groupCreate(array('name' => uniqid(), 'title' => uniqid()));
473 $this->callAPISuccess('GroupContact', 'create', array(
474 'group_id' => $groupID,
475 'contact_id' => $individualIDRemoved,
476 'status' => 'Removed',
477 ));
478 $this->callAPISuccess('GroupContact', 'create', array(
479 'group_id' => $groupID,
480 'contact_id' => $individualID,
481 'status' => 'Added',
482 ));
483
484 foreach (array($individual1ID, $individualID, $individualIDRemoved) as $contactID) {
485 $this->contributionCreate(array('contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => ''));
486 }
487
488 // Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
489 CRM_Contact_BAO_GroupContactCache::remove($groupID, FALSE);
490 return $groupID;
491 }
492
493 /**
494 * @return array
495 */
496 public function setUpIntersectingGroups() {
497 $groupID = $this->setUpPopulatedGroup();
498 $groupID2 = $this->setUpPopulatedSmartGroup();
499 $addedToBothIndividualID = $this->individualCreate();
500 $removedFromBothIndividualID = $this->individualCreate();
501 $addedToSmartGroupRemovedFromOtherIndividualID = $this->individualCreate();
502 $removedFromSmartGroupAddedToOtherIndividualID = $this->individualCreate();
503 $this->callAPISuccess('GroupContact', 'create', array(
504 'group_id' => $groupID,
505 'contact_id' => $addedToBothIndividualID,
506 'status' => 'Added',
507 ));
508 $this->callAPISuccess('GroupContact', 'create', array(
509 'group_id' => $groupID2,
510 'contact_id' => $addedToBothIndividualID,
511 'status' => 'Added',
512 ));
513 $this->callAPISuccess('GroupContact', 'create', array(
514 'group_id' => $groupID,
515 'contact_id' => $removedFromBothIndividualID,
516 'status' => 'Removed',
517 ));
518 $this->callAPISuccess('GroupContact', 'create', array(
519 'group_id' => $groupID2,
520 'contact_id' => $removedFromBothIndividualID,
521 'status' => 'Removed',
522 ));
523 $this->callAPISuccess('GroupContact', 'create', array(
524 'group_id' => $groupID2,
525 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
526 'status' => 'Added',
527 ));
528 $this->callAPISuccess('GroupContact', 'create', array(
529 'group_id' => $groupID,
530 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
531 'status' => 'Removed',
532 ));
533 $this->callAPISuccess('GroupContact', 'create', array(
534 'group_id' => $groupID,
535 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
536 'status' => 'Added',
537 ));
538 $this->callAPISuccess('GroupContact', 'create', array(
539 'group_id' => $groupID2,
540 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
541 'status' => 'Removed',
542 ));
543
544 foreach (array(
545 $addedToBothIndividualID,
546 $removedFromBothIndividualID,
547 $addedToSmartGroupRemovedFromOtherIndividualID,
548 $removedFromSmartGroupAddedToOtherIndividualID,
549 ) as $contactID) {
550 $this->contributionCreate(array(
551 'contact_id' => $contactID,
552 'invoice_id' => '',
553 'trxn_id' => '',
554 ));
555 }
556 return array($groupID, $groupID2);
557 }
558
45f8590c
PN
559 /**
560 * Test Deferred Revenue Report.
561 */
562 public function testDeferredRevenueReport() {
563 $indv1 = $this->individualCreate();
564 $indv2 = $this->individualCreate();
565 $params = array(
566 'contribution_invoice_settings' => array(
567 'deferred_revenue_enabled' => '1',
568 ),
569 );
570 $this->callAPISuccess('Setting', 'create', $params);
571 $this->contributionCreate(
572 array(
573 'contact_id' => $indv1,
574 'receive_date' => '2016-10-01',
575 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+3 month')),
576 'financial_type_id' => 2,
577 )
578 );
579 $this->contributionCreate(
580 array(
581 'contact_id' => $indv1,
582 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+22 month')),
583 'financial_type_id' => 4,
584 'trxn_id' => NULL,
585 'invoice_id' => NULL,
586 )
587 );
588 $this->contributionCreate(
589 array(
590 'contact_id' => $indv2,
591 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+1 month')),
592 'financial_type_id' => 4,
593 'trxn_id' => NULL,
594 'invoice_id' => NULL,
595 )
596 );
597 $this->contributionCreate(
598 array(
599 'contact_id' => $indv2,
600 'receive_date' => '2016-03-01',
601 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+4 month')),
602 'financial_type_id' => 2,
603 'trxn_id' => NULL,
604 'invoice_id' => NULL,
605 )
606 );
607 $rows = $this->callAPISuccess('report_template', 'getrows', array(
608 'report_id' => 'contribute/deferredrevenue',
609 ));
610 $this->assertEquals(2, $rows['count'], "Report failed to get row count");
611 $count = array(2, 1);
612 foreach ($rows['values'] as $row) {
613 $this->assertEquals(array_pop($count), count($row['rows']), "Report failed to get row count");
614 }
615 }
616
6a488035 617}