Merge pull request #2830 from eileenmcnaughton/patch-1
[civicrm-core.git] / tests / phpunit / api / v3 / ReportTemplateTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30
31/**
32 * Test APIv3 civicrm_report_instance_* functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_Report
36 */
37
38class api_v3_ReportTemplateTest extends CiviUnitTestCase {
e2779f6e 39 protected $_apiversion = 3;
b7c9bc4c 40
6a488035 41 function setUp() {
6a488035 42 parent::setUp();
e2779f6e 43 $this->_sethtmlGlobals();
6a488035
TO
44 }
45
46 function tearDown() {}
47
48 public function testReportTemplate() {
7fbb4198 49 $result = $this->callAPISuccess('ReportTemplate', 'create', array(
6a488035
TO
50 'label' => 'Example Form',
51 'description' => 'Longish description of the example form',
52 'class_name' => 'CRM_Report_Form_Examplez',
53 'report_url' => 'example/path',
54 'component' => 'CiviCase',
55 ));
1cbea43e 56 $this->assertAPISuccess($result);
6a488035
TO
57 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
58 $entityId = $result['id'];
59 $this->assertTrue(is_numeric($entityId), 'In line ' . __LINE__);
60 $this->assertEquals(7, $result['values'][$entityId]['component_id'], 'In line ' . __LINE__);
61 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
62 WHERE name = "CRM_Report_Form_Examplez"
46a29d7d 63 AND option_group_id = 41 ');
6a488035
TO
64 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
65 WHERE name = "CRM_Report_Form_Examplez"');
66
67 // change component to null
7fbb4198 68 $result = $this->callAPISuccess('ReportTemplate', 'create', array( 'id' => $entityId,
6a488035
TO
69 'component' => '',
70 ));
1cbea43e 71 $this->assertAPISuccess($result);
6a488035
TO
72 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
73 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
74 WHERE name = "CRM_Report_Form_Examplez"
46a29d7d 75 AND option_group_id = 41');
6a488035
TO
76 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
77 WHERE name = "CRM_Report_Form_Examplez"
78 AND component_id IS NULL');
79
80 // deactivate
7fbb4198 81 $result = $this->callAPISuccess('ReportTemplate', 'create', array( 'id' => $entityId,
6a488035
TO
82 'is_active' => 0,
83 ));
1cbea43e 84 $this->assertAPISuccess($result);
6a488035
TO
85 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
86 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
87 WHERE name = "CRM_Report_Form_Examplez"
46a29d7d 88 AND option_group_id = 41');
6a488035
TO
89 $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value
90 WHERE name = "CRM_Report_Form_Examplez"');
91
92 // activate
7fbb4198 93 $result = $this->callAPISuccess('ReportTemplate', 'create', array( 'id' => $entityId,
6a488035
TO
94 'is_active' => 1,
95 ));
1cbea43e 96 $this->assertAPISuccess($result);
6a488035
TO
97 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
98 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
99 WHERE name = "CRM_Report_Form_Examplez"
46a29d7d 100 AND option_group_id = 41');
6a488035
TO
101 $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value
102 WHERE name = "CRM_Report_Form_Examplez"');
103
7fbb4198 104 $result = $this->callAPISuccess('ReportTemplate', 'delete', array( 'id' => $entityId,
6a488035 105 ));
1cbea43e 106 $this->assertAPISuccess($result);
6a488035
TO
107 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
108 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value
109 WHERE name = "CRM_Report_Form_Examplez"
110 ');
111 }
e2779f6e
E
112
113 /**
114 *
115 */
116 function testReportTemplateGetRowsContactSummary() {
fee15d2a
E
117 $description = "Retrieve rows from a report template (optionally providing the instance_id)";
118 $result = $this->callAPIAndDocument('report_template', 'getrows', array(
e2779f6e 119 'report_id' => 'contact/summary',
781d91c8 120 'options' => array('metadata' => array('labels', 'title'))
fee15d2a 121 ), __FUNCTION__, __FILE__, $description, 'Getrows', 'getrows');
781d91c8 122 $this->assertEquals('Contact Name', $result['metadata']['labels']['civicrm_contact_sort_name']);
e2779f6e
E
123
124 //the second part of this test has been commented out because it relied on the db being reset to
125 // it's base state
126 //wasn't able to get that to work consistently
127 // however, when the db is in the base state the tests do pass
128 // and because the test covers 'all' contacts we can't create our own & assume the others don't exist
129 /*
130 $this->assertEquals(2, $result['count']);
131 $this->assertEquals('Default Organization', $result[0]['civicrm_contact_sort_name']);
132 $this->assertEquals('Second Domain', $result[1]['civicrm_contact_sort_name']);
133 $this->assertEquals('15 Main St', $result[1]['civicrm_address_street_address']);
134 */
135 }
136
137 /**
138 * @dataProvider getReportTemplates
139 */
140 function testReportTemplateGetRowsAllReports($reportID) {
141 if(stristr($reportID, 'has existing issues')) {
142 $this->markTestIncomplete($reportID);
143 }
144 $result = $this->callAPISuccess('report_template', 'getrows', array(
145 'report_id' => $reportID,
146 ));
147 }
148
149 /**
150 * @dataProvider getReportTemplates
151 */
152 function testReportTemplateGetStatisticsAllReports($reportID) {
153 if(stristr($reportID, 'has existing issues')) {
154 $this->markTestIncomplete($reportID);
155 }
156 if(in_array($reportID , array('contribute/softcredit', 'contribute/bookkeeping'))) {
157 $this->markTestIncomplete($reportID . " has non enotices when calling statistics fn");
158 }
fee15d2a
E
159 $description = "Get Statistics from a report (note there isn't much data to get in the test DB :-(";
160 $result = $this->callAPIAndDocument('report_template', 'getstatistics', array(
e2779f6e 161 'report_id' => $reportID,
fee15d2a 162 ), __FUNCTION__, __FILE__, $description, 'Getstatistics', 'getstatistics');
e2779f6e
E
163 }
164
165 /**
166 * Data provider function for getting all templates, note that the function needs to
167 * be static so cannot use $this->callAPISuccess
168 */
169 public static function getReportTemplates() {
170 $reportsToSkip = array(
171 'activity' => 'does not respect function signature on from clause',
172 'walklist' => 'Notice: Undefined index: type in CRM_Report_Form_Walklist_Walklist line 155.
173 (suspect the select function should be removed in favour of the parent (state province field)
174 also, type should be added to state province & others? & potentially getAddressColumns fn should be
175 used per other reports',
176 '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',
177 'contribute/organizationSummary' => 'Failure in api call for report_template getrows: Only variables should be assigned by reference line 381',
178 'contribute/householdSummary' => '(see contribute/repeat) Undefined property: CRM_Report_Form_Contribute_HouseholdSummary::$householdContact LINE 260, property should be declared on class, for api accessibility should be set in beginPreProcess common',
179 'contribute/topDonor' => 'construction of query in postprocess makes inaccessible ',
180 'contribute/sybunt' => 'e notice - (ui gives fatal error at civicrm/report/contribute/sybunt&reset=1&force=1
181 e-notice is on yid_valueContribute/Sybunt.php(214) because at the force url "yid_relative" not "yid_value" is defined',
182 'contribute/lybunt' => 'same as sybunt - fatals on force url & test identifies why',
183 'event/income' => 'I do no understant why but error is Call to undefined method CRM_Report_Form_Event_Income::from() in CRM/Report/Form.php on line 2120',
184 'contact/relationship' => '(see contribute/repeat), property declaration issue, Undefined property: CRM_Report_Form_Contact_Relationship::$relationType in /Contact/Relationship.php(486):',
e2779f6e
E
185 'activitySummary' => 'Undefined index: group_bys_freq m/ActivitySummary.php(191)',
186 'event/incomesummary' => 'Undefined index: title, Report/Form/Event/IncomeCountSummary.php(187)',
187 'logging/contact/summary' => '(likely to be test releated) probably logging off Undefined index: Form/Contact/LoggingSummary.php(231): PHP',
188 'logging/contact/detail' => '(likely to be test releated) probably logging off DB Error: no such table',
189 'logging/contribute/summary' => '(likely to be test releated) probably logging off DB Error: no such table',
190 'logging/contribute/detail' => '(likely to be test releated) probably logging off DB Error: no such table',
191 'survey/detail' => '(likely to be test releated) Undefined index: CiviCampaign civicrm CRM/Core/Component.php(196)',
192 'contribute/history' => 'Declaration of CRM_Report_Form_Contribute_History::buildRows() should be compatible with CRM_Report_Form::buildRows($sql, &$rows)',
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
206
207 return $reportTemplates;
208 }
6a488035 209}