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