| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
| 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 | * @group headless |
| 34 | */ |
| 35 | class api_v3_ReportTemplateTest extends CiviUnitTestCase { |
| 36 | protected $_apiversion = 3; |
| 37 | |
| 38 | public function setUp() { |
| 39 | parent::setUp(); |
| 40 | $this->useTransaction(TRUE); |
| 41 | } |
| 42 | |
| 43 | public function testReportTemplate() { |
| 44 | $result = $this->callAPISuccess('ReportTemplate', 'create', array( |
| 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 | )); |
| 51 | $this->assertAPISuccess($result); |
| 52 | $this->assertEquals(1, $result['count']); |
| 53 | $entityId = $result['id']; |
| 54 | $this->assertTrue(is_numeric($entityId)); |
| 55 | $this->assertEquals(7, $result['values'][$entityId]['component_id']); |
| 56 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value |
| 57 | WHERE name = "CRM_Report_Form_Examplez" |
| 58 | AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") '); |
| 59 | $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value |
| 60 | WHERE name = "CRM_Report_Form_Examplez"'); |
| 61 | |
| 62 | // change component to null |
| 63 | $result = $this->callAPISuccess('ReportTemplate', 'create', array( |
| 64 | 'id' => $entityId, |
| 65 | 'component' => '', |
| 66 | )); |
| 67 | $this->assertAPISuccess($result); |
| 68 | $this->assertEquals(1, $result['count']); |
| 69 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value |
| 70 | WHERE name = "CRM_Report_Form_Examplez" |
| 71 | AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") '); |
| 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 |
| 77 | $result = $this->callAPISuccess('ReportTemplate', 'create', array( |
| 78 | 'id' => $entityId, |
| 79 | 'is_active' => 0, |
| 80 | )); |
| 81 | $this->assertAPISuccess($result); |
| 82 | $this->assertEquals(1, $result['count']); |
| 83 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value |
| 84 | WHERE name = "CRM_Report_Form_Examplez" |
| 85 | AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") '); |
| 86 | $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value |
| 87 | WHERE name = "CRM_Report_Form_Examplez"'); |
| 88 | |
| 89 | // activate |
| 90 | $result = $this->callAPISuccess('ReportTemplate', 'create', array( |
| 91 | 'id' => $entityId, |
| 92 | 'is_active' => 1, |
| 93 | )); |
| 94 | $this->assertAPISuccess($result); |
| 95 | $this->assertEquals(1, $result['count']); |
| 96 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value |
| 97 | WHERE name = "CRM_Report_Form_Examplez" |
| 98 | AND option_group_id IN (SELECT id from civicrm_option_group WHERE name = "report_template") '); |
| 99 | $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value |
| 100 | WHERE name = "CRM_Report_Form_Examplez"'); |
| 101 | |
| 102 | $result = $this->callAPISuccess('ReportTemplate', 'delete', array( |
| 103 | 'id' => $entityId, |
| 104 | )); |
| 105 | $this->assertAPISuccess($result); |
| 106 | $this->assertEquals(1, $result['count']); |
| 107 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value |
| 108 | WHERE name = "CRM_Report_Form_Examplez" |
| 109 | '); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Test getrows on contact summary report. |
| 114 | */ |
| 115 | public function testReportTemplateGetRowsContactSummary() { |
| 116 | $description = "Retrieve rows from a report template (optionally providing the instance_id)."; |
| 117 | $result = $this->callAPIAndDocument('report_template', 'getrows', array( |
| 118 | 'report_id' => 'contact/summary', |
| 119 | 'options' => array('metadata' => array('labels', 'title')), |
| 120 | ), __FUNCTION__, __FILE__, $description, 'Getrows', 'getrows'); |
| 121 | $this->assertEquals('Contact Name', $result['metadata']['labels']['civicrm_contact_sort_name']); |
| 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']); |
| 133 | */ |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Tet api to get rows from reports. |
| 138 | * |
| 139 | * @dataProvider getReportTemplates |
| 140 | * |
| 141 | * @param $reportID |
| 142 | * |
| 143 | * @throws \PHPUnit_Framework_IncompleteTestError |
| 144 | */ |
| 145 | public function testReportTemplateGetRowsAllReports($reportID) { |
| 146 | if (stristr($reportID, 'has existing issues')) { |
| 147 | $this->markTestIncomplete($reportID); |
| 148 | } |
| 149 | $this->callAPISuccess('report_template', 'getrows', array( |
| 150 | 'report_id' => $reportID, |
| 151 | )); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Test get statistics. |
| 156 | * |
| 157 | * @dataProvider getReportTemplates |
| 158 | * |
| 159 | * @param $reportID |
| 160 | * |
| 161 | * @throws \PHPUnit_Framework_IncompleteTestError |
| 162 | */ |
| 163 | public function testReportTemplateGetStatisticsAllReports($reportID) { |
| 164 | if (stristr($reportID, 'has existing issues')) { |
| 165 | $this->markTestIncomplete($reportID); |
| 166 | } |
| 167 | if (in_array($reportID, array('contribute/softcredit', 'contribute/bookkeeping'))) { |
| 168 | $this->markTestIncomplete($reportID . " has non enotices when calling statistics fn"); |
| 169 | } |
| 170 | $description = "Get Statistics from a report (note there isn't much data to get in the test DB)."; |
| 171 | $result = $this->callAPIAndDocument('report_template', 'getstatistics', array( |
| 172 | 'report_id' => $reportID, |
| 173 | ), __FUNCTION__, __FILE__, $description, 'Getstatistics', 'getstatistics'); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Data provider function for getting all templates. |
| 178 | * |
| 179 | * Note that the function needs to |
| 180 | * be static so cannot use $this->callAPISuccess |
| 181 | */ |
| 182 | public static function getReportTemplates() { |
| 183 | $reportsToSkip = array( |
| 184 | 'activity' => 'does not respect function signature on from clause', |
| 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/contribute/summary' => '(likely to be test related) probably logging off DB Error: no such table', |
| 189 | 'contribute/history' => 'Declaration of CRM_Report_Form_Contribute_History::buildRows() should be compatible with CRM_Report_Form::buildRows($sql, &$rows)', |
| 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.', |
| 191 | ); |
| 192 | |
| 193 | $reports = civicrm_api3('report_template', 'get', array('return' => 'value', 'options' => array('limit' => 500))); |
| 194 | foreach ($reports['values'] as $report) { |
| 195 | if (empty($reportsToSkip[$report['value']])) { |
| 196 | $reportTemplates[] = array($report['value']); |
| 197 | } |
| 198 | else { |
| 199 | $reportTemplates[] = array($report['value'] . " has existing issues : " . $reportsToSkip[$report['value']]); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return $reportTemplates; |
| 204 | } |
| 205 | |
| 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 | |
| 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 | |
| 274 | } |