Merge pull request #7797 from JKingsnorth/CRM-17977
[civicrm-core.git] / tests / phpunit / api / v3 / ReportTemplateTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
00be9182 38 public function setUp() {
6a488035 39 parent::setUp();
e9c59dca 40 $this->useTransaction(TRUE);
6a488035
TO
41 }
42
6a488035 43 public function testReportTemplate() {
7fbb4198 44 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
6a488035
TO
45 'label' => 'Example Form',
46 'description' => 'Longish description of the example form',
47 'class_name' => 'CRM_Report_Form_Examplez',
48 'report_url' => 'example/path',
49 'component' => 'CiviCase',
50 ));
1cbea43e 51 $this->assertAPISuccess($result);
ba4a1892 52 $this->assertEquals(1, $result['count']);
6a488035 53 $entityId = $result['id'];
ba4a1892
TM
54 $this->assertTrue(is_numeric($entityId));
55 $this->assertEquals(7, $result['values'][$entityId]['component_id']);
6a488035
TO
56 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
57 WHERE name = "CRM_Report_Form_Examplez"
0201b57f 58 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
6a488035
TO
59 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
60 WHERE name = "CRM_Report_Form_Examplez"');
61
62 // change component to null
6c6e6187 63 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
92915c55 64 'id' => $entityId,
6a488035
TO
65 'component' => '',
66 ));
1cbea43e 67 $this->assertAPISuccess($result);
ba4a1892 68 $this->assertEquals(1, $result['count']);
6a488035
TO
69 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
70 WHERE name = "CRM_Report_Form_Examplez"
0201b57f 71 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
6a488035
TO
72 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
73 WHERE name = "CRM_Report_Form_Examplez"
74 AND component_id IS NULL');
75
76 // deactivate
6c6e6187 77 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
92915c55 78 'id' => $entityId,
6a488035
TO
79 'is_active' => 0,
80 ));
1cbea43e 81 $this->assertAPISuccess($result);
ba4a1892 82 $this->assertEquals(1, $result['count']);
6a488035
TO
83 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
84 WHERE name = "CRM_Report_Form_Examplez"
0201b57f 85 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
6a488035
TO
86 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value
87 WHERE name = "CRM_Report_Form_Examplez"');
88
89 // activate
6c6e6187 90 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
92915c55 91 'id' => $entityId,
6a488035
TO
92 'is_active' => 1,
93 ));
1cbea43e 94 $this->assertAPISuccess($result);
ba4a1892 95 $this->assertEquals(1, $result['count']);
6a488035
TO
96 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
97 WHERE name = "CRM_Report_Form_Examplez"
0201b57f 98 AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") ');
6a488035
TO
99 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
100 WHERE name = "CRM_Report_Form_Examplez"');
101
92915c55
TO
102 $result = $this->callAPISuccess('ReportTemplate', 'delete', array(
103 'id' => $entityId,
6a488035 104 ));
1cbea43e 105 $this->assertAPISuccess($result);
ba4a1892 106 $this->assertEquals(1, $result['count']);
6a488035
TO
107 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value
108 WHERE name = "CRM_Report_Form_Examplez"
109 ');
110 }
e2779f6e
E
111
112 /**
fe482240 113 * Test getrows on contact summary report.
e2779f6e 114 */
00be9182 115 public function testReportTemplateGetRowsContactSummary() {
5c49fee0 116 $description = "Retrieve rows from a report template (optionally providing the instance_id).";
fee15d2a 117 $result = $this->callAPIAndDocument('report_template', 'getrows', array(
e2779f6e 118 'report_id' => 'contact/summary',
21dfd5f5 119 'options' => array('metadata' => array('labels', 'title')),
fee15d2a 120 ), __FUNCTION__, __FILE__, $description, 'Getrows', 'getrows');
781d91c8 121 $this->assertEquals('Contact Name', $result['metadata']['labels']['civicrm_contact_sort_name']);
e2779f6e
E
122
123 //the second part of this test has been commented out because it relied on the db being reset to
124 // it's base state
125 //wasn't able to get that to work consistently
126 // however, when the db is in the base state the tests do pass
127 // and because the test covers 'all' contacts we can't create our own & assume the others don't exist
128 /*
129 $this->assertEquals(2, $result['count']);
130 $this->assertEquals('Default Organization', $result[0]['civicrm_contact_sort_name']);
131 $this->assertEquals('Second Domain', $result[1]['civicrm_contact_sort_name']);
132 $this->assertEquals('15 Main St', $result[1]['civicrm_address_street_address']);
e70a7fc0 133 */
e2779f6e
E
134 }
135
136 /**
fe482240
EM
137 * Tet api to get rows from reports.
138 *
e2779f6e 139 * @dataProvider getReportTemplates
fe482240 140 *
1e1fdcf6 141 * @param $reportID
fe482240 142 *
1e1fdcf6 143 * @throws \PHPUnit_Framework_IncompleteTestError
e2779f6e 144 */
00be9182 145 public function testReportTemplateGetRowsAllReports($reportID) {
22e263ad 146 if (stristr($reportID, 'has existing issues')) {
e2779f6e
E
147 $this->markTestIncomplete($reportID);
148 }
fe482240 149 $this->callAPISuccess('report_template', 'getrows', array(
92915c55 150 'report_id' => $reportID,
e2779f6e
E
151 ));
152 }
153
154 /**
fe482240
EM
155 * Test get statistics.
156 *
e2779f6e 157 * @dataProvider getReportTemplates
fe482240 158 *
1e1fdcf6 159 * @param $reportID
fe482240 160 *
1e1fdcf6 161 * @throws \PHPUnit_Framework_IncompleteTestError
e2779f6e 162 */
00be9182 163 public function testReportTemplateGetStatisticsAllReports($reportID) {
22e263ad 164 if (stristr($reportID, 'has existing issues')) {
e2779f6e
E
165 $this->markTestIncomplete($reportID);
166 }
22e263ad 167 if (in_array($reportID, array('contribute/softcredit', 'contribute/bookkeeping'))) {
e2779f6e
E
168 $this->markTestIncomplete($reportID . " has non enotices when calling statistics fn");
169 }
5c49fee0 170 $description = "Get Statistics from a report (note there isn't much data to get in the test DB).";
fee15d2a 171 $result = $this->callAPIAndDocument('report_template', 'getstatistics', array(
e2779f6e 172 'report_id' => $reportID,
fee15d2a 173 ), __FUNCTION__, __FILE__, $description, 'Getstatistics', 'getstatistics');
e2779f6e
E
174 }
175
176 /**
fe482240
EM
177 * Data provider function for getting all templates.
178 *
179 * Note that the function needs to
e2779f6e
E
180 * be static so cannot use $this->callAPISuccess
181 */
182 public static function getReportTemplates() {
183 $reportsToSkip = array(
92915c55 184 'activity' => 'does not respect function signature on from clause',
92915c55 185 'contribute/topDonor' => 'construction of query in postProcess makes inaccessible ',
92915c55 186 '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 187 'logging/contact/summary' => '(likely to be test related) probably logging off Undefined index: Form/Contact/LoggingSummary.php(231): PHP',
92915c55 188 'logging/contribute/summary' => '(likely to be test related) probably logging off DB Error: no such table',
92915c55 189 'contribute/history' => 'Declaration of CRM_Report_Form_Contribute_History::buildRows() should be compatible with CRM_Report_Form::buildRows($sql, &$rows)',
cb5aba81 190 '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
191 );
192
193 $reports = civicrm_api3('report_template', 'get', array('return' => 'value', 'options' => array('limit' => 500)));
194 foreach ($reports['values'] as $report) {
22e263ad 195 if (empty($reportsToSkip[$report['value']])) {
e2779f6e
E
196 $reportTemplates[] = array($report['value']);
197 }
198 else {
86bfa4f6 199 $reportTemplates[] = array($report['value'] . " has existing issues : " . $reportsToSkip[$report['value']]);
e2779f6e
E
200 }
201 }
202
e2779f6e
E
203 return $reportTemplates;
204 }
96025800 205
c160fde8 206 /**
207 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
208 */
209 public function testLybuntReportWithData() {
210 $inInd = $this->individualCreate();
211 $outInd = $this->individualCreate();
212 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-03-01'));
213 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
214 $rows = $this->callAPISuccess('report_template', 'getrows', array(
215 'report_id' => 'contribute/lybunt',
216 'yid_value' => 2015,
217 'yid_op' => 'calendar',
218 'options' => array('metadata' => array('sql')),
219 ));
220 $this->assertEquals(1, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
221 }
222
223 /**
224 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
225 */
226 public function testLybuntReportWithFYData() {
227 $inInd = $this->individualCreate();
228 $outInd = $this->individualCreate();
229 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-10-01'));
230 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
231 $this->callAPISuccess('Setting', 'create', array('fiscalYearStart' => array('M' => 7, 'd' => 1)));
232 $rows = $this->callAPISuccess('report_template', 'getrows', array(
233 'report_id' => 'contribute/lybunt',
234 'yid_value' => 2015,
235 'yid_op' => 'fiscal',
236 'options' => array('metadata' => array('sql')),
237 'order_bys' => array(
238 array(
239 'column' => 'first_name',
240 'order' => 'ASC',
241 ),
242 ),
243 ));
244
245 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
246 }
247
3fd9a92a 248 /**
249 * Test Lybunt report to check basic inclusion of a contact who gave in the year before the chosen year.
250 */
251 public function testLybuntReportWithFYDataOrderByLastYearAmount() {
252 $inInd = $this->individualCreate();
253 $outInd = $this->individualCreate();
254 $this->contributionCreate(array('contact_id' => $inInd, 'receive_date' => '2014-10-01'));
255 $this->contributionCreate(array('contact_id' => $outInd, 'receive_date' => '2015-03-01', 'trxn_id' => NULL, 'invoice_id' => NULL));
256 $this->callAPISuccess('Setting', 'create', array('fiscalYearStart' => array('M' => 7, 'd' => 1)));
257 $rows = $this->callAPISuccess('report_template', 'getrows', array(
258 'report_id' => 'contribute/lybunt',
259 'yid_value' => 2015,
260 'yid_op' => 'fiscal',
261 'options' => array('metadata' => array('sql')),
262 'fields' => array('first_name'),
263 'order_bys' => array(
264 array(
265 'column' => 'last_year_total_amount',
266 'order' => 'ASC',
267 ),
268 ),
269 ));
270
271 $this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));
272 }
273
6a488035 274}