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