Merge pull request #4185 from samuelsov/CRM-15330
[civicrm-core.git] / tests / phpunit / api / v3 / ReportTemplateTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 +--------------------------------------------------------------------+
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);
6a488035
TO
54 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
55 $entityId = $result['id'];
56 $this->assertTrue(is_numeric($entityId), 'In line ' . __LINE__);
57 $this->assertEquals(7, $result['values'][$entityId]['component_id'], 'In line ' . __LINE__);
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);
6a488035
TO
70 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
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);
6a488035
TO
84 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
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);
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"
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);
6a488035
TO
108 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
109 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value
110 WHERE name = "CRM_Report_Form_Examplez"
111 ');
112 }
e2779f6e
E
113
114 /**
e2779f6e 115 */
00be9182 116 public 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',
21dfd5f5 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']);
e70a7fc0 134 */
e2779f6e
E
135 }
136
137 /**
138 * @dataProvider getReportTemplates
1e1fdcf6
EM
139 * @param $reportID
140 * @throws \PHPUnit_Framework_IncompleteTestError
e2779f6e 141 */
00be9182 142 public function testReportTemplateGetRowsAllReports($reportID) {
22e263ad 143 if (stristr($reportID, 'has existing issues')) {
e2779f6e
E
144 $this->markTestIncomplete($reportID);
145 }
6c6e6187 146 $result = $this->callAPISuccess('report_template', 'getrows', array(
92915c55 147 'report_id' => $reportID,
e2779f6e
E
148 ));
149 }
150
151 /**
152 * @dataProvider getReportTemplates
1e1fdcf6
EM
153 * @param $reportID
154 * @throws \PHPUnit_Framework_IncompleteTestError
e2779f6e 155 */
00be9182 156 public function testReportTemplateGetStatisticsAllReports($reportID) {
22e263ad 157 if (stristr($reportID, 'has existing issues')) {
e2779f6e
E
158 $this->markTestIncomplete($reportID);
159 }
22e263ad 160 if (in_array($reportID, array('contribute/softcredit', 'contribute/bookkeeping'))) {
e2779f6e
E
161 $this->markTestIncomplete($reportID . " has non enotices when calling statistics fn");
162 }
fee15d2a
E
163 $description = "Get Statistics from a report (note there isn't much data to get in the test DB :-(";
164 $result = $this->callAPIAndDocument('report_template', 'getstatistics', array(
e2779f6e 165 'report_id' => $reportID,
fee15d2a 166 ), __FUNCTION__, __FILE__, $description, 'Getstatistics', 'getstatistics');
e2779f6e
E
167 }
168
169 /**
170 * Data provider function for getting all templates, note that the function needs to
171 * be static so cannot use $this->callAPISuccess
172 */
173 public static function getReportTemplates() {
174 $reportsToSkip = array(
92915c55 175 'activity' => 'does not respect function signature on from clause',
408b79bf 176 'walklist' => 'Notice: Undefined index: type in CRM_Report_Form_Walklist_Walklist line 155.(suspect the select function should be removed in favour of the parent (state province field) also, type should be added to state province & others? & potentially getAddressColumns fn should be used per other reports',
92915c55
TO
177 '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',
178 'contribute/topDonor' => 'construction of query in postProcess makes inaccessible ',
408b79bf 179 'contribute/sybunt' => 'e notice - (ui gives fatal error at civicrm/report/contribute/sybunt&reset=1&force=1 e-notice is on yid_valueContribute/Sybunt.php(214) because at the force url "yid_relative" not "yid_value" is defined',
92915c55
TO
180 'contribute/lybunt' => 'same as sybunt - fatals on force url & test identifies why',
181 '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',
182 'contact/relationship' => '(see contribute/repeat), property declaration issue, Undefined property: CRM_Report_Form_Contact_Relationship::$relationType in /Contact/Relationship.php(486):',
183 'logging/contact/summary' => '(likely to be test related) probably logging off Undefined index: Form/Contact/LoggingSummary.php(231): PHP',
184 'logging/contact/detail' => '(likely to be test related) probably logging off DB Error: no such table',
185 'logging/contribute/summary' => '(likely to be test related) probably logging off DB Error: no such table',
186 'logging/contribute/detail' => '(likely to be test related) probably logging off DB Error: no such table',
187 'survey/detail' => '(likely to be test related) Undefined index: CiviCampaign civicrm CRM/Core/Component.php(196)',
188 'contribute/history' => 'Declaration of CRM_Report_Form_Contribute_History::buildRows() should be compatible with CRM_Report_Form::buildRows($sql, &$rows)',
e2779f6e
E
189 );
190
191 $reports = civicrm_api3('report_template', 'get', array('return' => 'value', 'options' => array('limit' => 500)));
192 foreach ($reports['values'] as $report) {
22e263ad 193 if (empty($reportsToSkip[$report['value']])) {
e2779f6e
E
194 $reportTemplates[] = array($report['value']);
195 }
196 else {
86bfa4f6 197 $reportTemplates[] = array($report['value'] . " has existing issues : " . $reportsToSkip[$report['value']]);
e2779f6e
E
198 }
199 }
200
e2779f6e
E
201 return $reportTemplates;
202 }
96025800 203
6a488035 204}