Merge pull request #11642 from JKingsnorth/CRM-21739
[civicrm-core.git] / tests / phpunit / api / v3 / ReportTemplateTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test APIv3 civicrm_report_instance_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Report
33 * @group headless
34 */
35 class api_v3_ReportTemplateTest extends CiviUnitTestCase {
36 protected $_apiversion = 3;
37
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', 'civicrm_group_contact_cache', 'civicrm_group'));
46 parent::tearDown();
47 }
48
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',
56 ));
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"');
67
68 // change component to null
69 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
70 'id' => $entityId,
71 'component' => '',
72 ));
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');
81
82 // deactivate
83 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
84 'id' => $entityId,
85 'is_active' => 0,
86 ));
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"');
94
95 // activate
96 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
97 'id' => $entityId,
98 'is_active' => 1,
99 ));
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"');
107
108 $result = $this->callAPISuccess('ReportTemplate', 'delete', array(
109 'id' => $entityId,
110 ));
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"
115 ');
116 }
117
118 /**
119 * Test getrows on contact summary report.
120 */
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']);
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']);
139 */
140 }
141
142 /**
143 * Tet api to get rows from reports.
144 *
145 * @dataProvider getReportTemplates
146 *
147 * @param $reportID
148 *
149 * @throws \PHPUnit_Framework_IncompleteTestError
150 */
151 public function testReportTemplateGetRowsAllReports($reportID) {
152 if (stristr($reportID, 'has existing issues')) {
153 $this->markTestIncomplete($reportID);
154 }
155 $this->callAPISuccess('report_template', 'getrows', array(
156 'report_id' => $reportID,
157 ));
158 }
159
160 /**
161 * Test get statistics.
162 *
163 * @dataProvider getReportTemplates
164 *
165 * @param $reportID
166 *
167 * @throws \PHPUnit_Framework_IncompleteTestError
168 */
169 public function testReportTemplateGetStatisticsAllReports($reportID) {
170 if (stristr($reportID, 'has existing issues')) {
171 $this->markTestIncomplete($reportID);
172 }
173 if (in_array($reportID, array('contribute/softcredit', 'contribute/bookkeeping'))) {
174 $this->markTestIncomplete($reportID . " has non enotices when calling statistics fn");
175 }
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');
180 }
181
182 /**
183 * Data provider function for getting all templates.
184 *
185 * Note that the function needs to
186 * be static so cannot use $this->callAPISuccess
187 */
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.',
197 );
198
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']);
203 }
204 else {
205 $reportTemplates[] = array($report['value'] . " has existing issues : " . $reportsToSkip[$report['value']]);
206 }
207 }
208
209 return $reportTemplates;
210 }
211
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('topDonor' => 'contribute/topDonor'));
219 }
220
221 /**
222 * Get contribution templates that work with basic filter tests.
223 *
224 * These templates require minimal data config.
225 */
226 public static function getMembershipReportTemplates() {
227 return array(array('member/detail'));
228 }
229
230 public static function getMembershipAndContributionReportTemplatesForGroupTests() {
231 $templates = array_merge(self::getContributionReportTemplates(), self::getMembershipReportTemplates());
232 foreach ($templates as $key => $value) {
233 if (array_key_exists('topDonor', $value)) {
234 // Report is not standard enough to test here.
235 unset($templates[$key]);
236 }
237
238 }
239 return $templates;
240 }
241
242 /**
243 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
244 */
245 public function testLybuntReportWithData() {
246 $inInd = $this->individualCreate();
247 $outInd = $this->individualCreate();
248 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-03-01'));
249 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
250 $rows = $this->callAPISuccess('report_template', 'getrows', array(
251 'report_id' => 'contribute/lybunt',
252 'yid_value' => 2015,
253 'yid_op' => 'calendar',
254 'options' => array('metadata' => array('sql')),
255 ));
256 $this->assertEquals(1, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
257 }
258
259 /**
260 * Test Lybunt report applies ACLs.
261 */
262 public function testLybuntReportWithDataAndACLFilter() {
263 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('administer CiviCRM');
264 $inInd = $this->individualCreate();
265 $outInd = $this->individualCreate();
266 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-03-01'));
267 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
268 $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
269 $params = array(
270 'report_id' => 'contribute/lybunt',
271 'yid_value' => 2015,
272 'yid_op' => 'calendar',
273 'options' => array('metadata' => array('sql')),
274 'check_permissions' => 1,
275 );
276
277 $rows = $this->callAPISuccess('report_template', 'getrows', $params);
278 $this->assertEquals(0, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
279
280 CRM_Utils_Hook::singleton()->reset();
281 }
282
283 /**
284 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
285 */
286 public function testLybuntReportWithFYData() {
287 $inInd = $this->individualCreate();
288 $outInd = $this->individualCreate();
289 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-10-01'));
290 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
291 $this->callAPISuccess('Setting', 'create', array('fiscalYearStart' => array('M' => 7, 'd' => 1)));
292 $rows = $this->callAPISuccess('report_template', 'getrows', array(
293 'report_id' => 'contribute/lybunt',
294 'yid_value' => 2015,
295 'yid_op' => 'fiscal',
296 'options' => array('metadata' => array('sql')),
297 'order_bys' => array(
298 array(
299 'column' => 'first_name',
300 'order' => 'ASC',
301 ),
302 ),
303 ));
304
305 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
306
307 $this->assertContains('DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
308 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
309 AND contribution_civireport.is_test = 0
310 AND contribution_civireport.receive_date BETWEEN \'20140701000000\' AND \'20150630235959\'
311
312 LEFT JOIN civicrm_contribution cont_exclude ON cont_exclude.contact_id = contact_civireport.id
313 AND cont_exclude.receive_date BETWEEN \'2015-7-1\' AND \'20160630235959\' WHERE cont_exclude.id IS NULL AND 1 AND ( contribution_civireport.contribution_status_id IN (1) )
314 GROUP BY contact_civireport.id', $rows['metadata']['sql'][0]);
315 }
316
317 /**
318 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
319 */
320 public function testLybuntReportWithFYDataOrderByLastYearAmount() {
321 $inInd = $this->individualCreate();
322 $outInd = $this->individualCreate();
323 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-10-01'));
324 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
325 $this->callAPISuccess('Setting', 'create', array('fiscalYearStart' => array('M' => 7, 'd' => 1)));
326 $rows = $this->callAPISuccess('report_template', 'getrows', array(
327 'report_id' => 'contribute/lybunt',
328 'yid_value' => 2015,
329 'yid_op' => 'fiscal',
330 'options' => array('metadata' => array('sql')),
331 'fields' => array('first_name'),
332 'order_bys' => array(
333 array(
334 'column' => 'last_year_total_amount',
335 'order' => 'ASC',
336 ),
337 ),
338 ));
339
340 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
341 }
342
343 /**
344 * Test the group filter works on the contribution summary (with a smart group).
345 *
346 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
347 *
348 * @param string $template
349 * Name of the template to test.
350 */
351 public function testContributionSummaryWithSmartGroupFilter($template) {
352 $groupID = $this->setUpPopulatedSmartGroup();
353 $rows = $this->callAPISuccess('report_template', 'getrows', array(
354 'report_id' => $template,
355 'gid_value' => $groupID,
356 'gid_op' => 'in',
357 'options' => array('metadata' => array('sql')),
358 ));
359 $this->assertNumberOfContactsInResult(3, $rows, $template);
360 if ($template === 'contribute/summary') {
361 $this->assertEquals(3, $rows['values'][0]['civicrm_contribution_total_amount_count']);
362 }
363 }
364
365 /**
366 * Test the group filter works on the contribution summary.
367 *
368 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
369 */
370 public function testContributionSummaryWithNotINSmartGroupFilter($template) {
371 $groupID = $this->setUpPopulatedSmartGroup();
372 $rows = $this->callAPISuccess('report_template', 'getrows', array(
373 'report_id' => 'contribute/summary',
374 'gid_value' => $groupID,
375 'gid_op' => 'notin',
376 'options' => array('metadata' => array('sql')),
377 ));
378 $this->assertEquals(2, $rows['values'][0]['civicrm_contribution_total_amount_count']);
379
380 }
381
382 /**
383 * Test the group filter works on the various reports.
384 *
385 * @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
386 *
387 * @param string $template
388 * Report template unique identifier.
389 */
390 public function testReportsWithNonSmartGroupFilter($template) {
391 $groupID = $this->setUpPopulatedGroup();
392 $rows = $this->callAPISuccess('report_template', 'getrows', array(
393 'report_id' => $template,
394 'gid_value' => array($groupID),
395 'gid_op' => 'in',
396 'options' => array('metadata' => array('sql')),
397 ));
398 $this->assertNumberOfContactsInResult(1, $rows, $template);
399 }
400
401 /**
402 * Assert the included results match the expected.
403 *
404 * There may or may not be a group by in play so the assertion varies a little.
405 *
406 * @param int $numberExpected
407 * @param array $rows
408 * Rows returned from the report.
409 * @param string $template
410 */
411 protected function assertNumberOfContactsInResult($numberExpected, $rows, $template) {
412 if (isset($rows['values'][0]['civicrm_contribution_total_amount_count'])) {
413 $this->assertEquals($numberExpected, $rows['values'][0]['civicrm_contribution_total_amount_count'], 'wrong row count in ' . $template);
414 }
415 else {
416 $this->assertEquals($numberExpected, count($rows['values']), 'wrong row count in ' . $template);
417 }
418 }
419
420 /**
421 * Test the group filter works on the contribution summary when 2 groups are involved.
422 */
423 public function testContributionSummaryWithTwoGroups() {
424 $groupID = $this->setUpPopulatedGroup();
425 $groupID2 = $this->setUpPopulatedSmartGroup();
426 $rows = $this->callAPISuccess('report_template', 'getrows', array(
427 'report_id' => 'contribute/summary',
428 'gid_value' => array($groupID, $groupID2),
429 'gid_op' => 'in',
430 'options' => array('metadata' => array('sql')),
431 ));
432 $this->assertEquals(4, $rows['values'][0]['civicrm_contribution_total_amount_count']);
433 }
434
435 /**
436 * CRM-20640: Test the group filter works on the contribution summary when a single contact in 2 groups.
437 */
438 public function testContributionSummaryWithSingleContactsInTwoGroups() {
439 list($groupID1, $individualID) = $this->setUpPopulatedGroup(TRUE);
440 // create second group and add the individual to it.
441 $groupID2 = $this->groupCreate(array('name' => uniqid(), 'title' => uniqid()));
442 $this->callAPISuccess('GroupContact', 'create', array(
443 'group_id' => $groupID2,
444 'contact_id' => $individualID,
445 'status' => 'Added',
446 ));
447
448 $rows = $this->callAPISuccess('report_template', 'getrows', array(
449 'report_id' => 'contribute/summary',
450 'gid_value' => array($groupID1, $groupID2),
451 'gid_op' => 'in',
452 'options' => array('metadata' => array('sql')),
453 ));
454 $this->assertEquals(1, $rows['count']);
455 }
456
457 /**
458 * Test the group filter works on the contribution summary when 2 groups are involved.
459 */
460 public function testContributionSummaryWithTwoGroupsWithIntersection() {
461 $groups = $this->setUpIntersectingGroups();
462
463 $rows = $this->callAPISuccess('report_template', 'getrows', array(
464 'report_id' => 'contribute/summary',
465 'gid_value' => $groups,
466 'gid_op' => 'in',
467 'options' => array('metadata' => array('sql')),
468 ));
469 $this->assertEquals(7, $rows['values'][0]['civicrm_contribution_total_amount_count']);
470 }
471
472 /**
473 * Set up a smart group for testing.
474 *
475 * The smart group includes all Households by filter. In addition an individual
476 * is created and hard-added and an individual is created that is not added.
477 *
478 * One household is hard-added as well as being in the filter.
479 *
480 * This gives us a range of scenarios for testing contacts are included only once
481 * whenever they are hard-added or in the criteria.
482 *
483 * @return int
484 */
485 public function setUpPopulatedSmartGroup() {
486 $household1ID = $this->householdCreate();
487 $individual1ID = $this->individualCreate();
488 $householdID = $this->householdCreate();
489 $individualID = $this->individualCreate();
490 $individualIDRemoved = $this->individualCreate();
491 $groupID = $this->smartGroupCreate(array(), array('name' => uniqid(), 'title' => uniqid()));
492 $this->callAPISuccess('GroupContact', 'create', array(
493 'group_id' => $groupID,
494 'contact_id' => $individualIDRemoved,
495 'status' => 'Removed',
496 ));
497 $this->callAPISuccess('GroupContact', 'create', array(
498 'group_id' => $groupID,
499 'contact_id' => $individualID,
500 'status' => 'Added',
501 ));
502 $this->callAPISuccess('GroupContact', 'create', array(
503 'group_id' => $groupID,
504 'contact_id' => $householdID,
505 'status' => 'Added',
506 ));
507 foreach (array($household1ID, $individual1ID, $householdID, $individualID, $individualIDRemoved) as $contactID) {
508 $this->contributionCreate(array('contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => ''));
509 $this->contactMembershipCreate(array('contact_id' => $contactID));
510 }
511
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);
514 return $groupID;
515 }
516
517 /**
518 * Set up a static group for testing.
519 *
520 * An individual is created and hard-added and an individual is created that is not added.
521 *
522 * This gives us a range of scenarios for testing contacts are included only once
523 * whenever they are hard-added or in the criteria.
524 *
525 * @param bool $returnAddedContact
526 *
527 * @return int
528 */
529 public function setUpPopulatedGroup($returnAddedContact = FALSE) {
530 $individual1ID = $this->individualCreate();
531 $individualID = $this->individualCreate();
532 $individualIDRemoved = $this->individualCreate();
533 $groupID = $this->groupCreate(array('name' => uniqid(), 'title' => uniqid()));
534 $this->callAPISuccess('GroupContact', 'create', array(
535 'group_id' => $groupID,
536 'contact_id' => $individualIDRemoved,
537 'status' => 'Removed',
538 ));
539 $this->callAPISuccess('GroupContact', 'create', array(
540 'group_id' => $groupID,
541 'contact_id' => $individualID,
542 'status' => 'Added',
543 ));
544
545 foreach (array($individual1ID, $individualID, $individualIDRemoved) as $contactID) {
546 $this->contributionCreate(array('contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => ''));
547 $this->contactMembershipCreate(array('contact_id' => $contactID));
548 }
549
550 // Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
551 CRM_Contact_BAO_GroupContactCache::clearGroupContactCache($groupID);
552
553 if ($returnAddedContact) {
554 return array($groupID, $individualID);
555 }
556
557 return $groupID;
558 }
559
560 /**
561 * @return array
562 */
563 public function setUpIntersectingGroups() {
564 $groupID = $this->setUpPopulatedGroup();
565 $groupID2 = $this->setUpPopulatedSmartGroup();
566 $addedToBothIndividualID = $this->individualCreate();
567 $removedFromBothIndividualID = $this->individualCreate();
568 $addedToSmartGroupRemovedFromOtherIndividualID = $this->individualCreate();
569 $removedFromSmartGroupAddedToOtherIndividualID = $this->individualCreate();
570 $this->callAPISuccess('GroupContact', 'create', array(
571 'group_id' => $groupID,
572 'contact_id' => $addedToBothIndividualID,
573 'status' => 'Added',
574 ));
575 $this->callAPISuccess('GroupContact', 'create', array(
576 'group_id' => $groupID2,
577 'contact_id' => $addedToBothIndividualID,
578 'status' => 'Added',
579 ));
580 $this->callAPISuccess('GroupContact', 'create', array(
581 'group_id' => $groupID,
582 'contact_id' => $removedFromBothIndividualID,
583 'status' => 'Removed',
584 ));
585 $this->callAPISuccess('GroupContact', 'create', array(
586 'group_id' => $groupID2,
587 'contact_id' => $removedFromBothIndividualID,
588 'status' => 'Removed',
589 ));
590 $this->callAPISuccess('GroupContact', 'create', array(
591 'group_id' => $groupID2,
592 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
593 'status' => 'Added',
594 ));
595 $this->callAPISuccess('GroupContact', 'create', array(
596 'group_id' => $groupID,
597 'contact_id' => $addedToSmartGroupRemovedFromOtherIndividualID,
598 'status' => 'Removed',
599 ));
600 $this->callAPISuccess('GroupContact', 'create', array(
601 'group_id' => $groupID,
602 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
603 'status' => 'Added',
604 ));
605 $this->callAPISuccess('GroupContact', 'create', array(
606 'group_id' => $groupID2,
607 'contact_id' => $removedFromSmartGroupAddedToOtherIndividualID,
608 'status' => 'Removed',
609 ));
610
611 foreach (array(
612 $addedToBothIndividualID,
613 $removedFromBothIndividualID,
614 $addedToSmartGroupRemovedFromOtherIndividualID,
615 $removedFromSmartGroupAddedToOtherIndividualID,
616 ) as $contactID) {
617 $this->contributionCreate(array(
618 'contact_id' => $contactID,
619 'invoice_id' => '',
620 'trxn_id' => '',
621 ));
622 }
623 return array($groupID, $groupID2);
624 }
625
626 /**
627 * Test Deferred Revenue Report.
628 */
629 public function testDeferredRevenueReport() {
630 $indv1 = $this->individualCreate();
631 $indv2 = $this->individualCreate();
632 $params = array(
633 'contribution_invoice_settings' => array(
634 'deferred_revenue_enabled' => '1',
635 ),
636 );
637 $this->callAPISuccess('Setting', 'create', $params);
638 $this->contributionCreate(
639 array(
640 'contact_id' => $indv1,
641 'receive_date' => '2016-10-01',
642 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+3 month')),
643 'financial_type_id' => 2,
644 )
645 );
646 $this->contributionCreate(
647 array(
648 'contact_id' => $indv1,
649 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+22 month')),
650 'financial_type_id' => 4,
651 'trxn_id' => NULL,
652 'invoice_id' => NULL,
653 )
654 );
655 $this->contributionCreate(
656 array(
657 'contact_id' => $indv2,
658 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+1 month')),
659 'financial_type_id' => 4,
660 'trxn_id' => NULL,
661 'invoice_id' => NULL,
662 )
663 );
664 $this->contributionCreate(
665 array(
666 'contact_id' => $indv2,
667 'receive_date' => '2016-03-01',
668 'revenue_recognition_date' => date('Y-m-t', strtotime(date('ymd') . '+4 month')),
669 'financial_type_id' => 2,
670 'trxn_id' => NULL,
671 'invoice_id' => NULL,
672 )
673 );
674 $rows = $this->callAPISuccess('report_template', 'getrows', array(
675 'report_id' => 'contribute/deferredrevenue',
676 ));
677 $this->assertEquals(2, $rows['count'], "Report failed to get row count");
678 $count = array(2, 1);
679 foreach ($rows['values'] as $row) {
680 $this->assertEquals(array_pop($count), count($row['rows']), "Report failed to get row count");
681 }
682 }
683
684 }