| 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 | require_once 'CiviTest/CiviReportTestCase.php'; |
| 29 | |
| 30 | /** |
| 31 | * Verify that the CiviReportTestCase provides a working set of |
| 32 | * primitives for tests. Do this by running various scenarios |
| 33 | * that should yield positive and negative results. |
| 34 | * |
| 35 | * Note: We need some report class to use as an example. |
| 36 | * CRM_Report_Form_Contribute_DetailTest is chosen arbitrarily. |
| 37 | * |
| 38 | * @package CiviCRM |
| 39 | */ |
| 40 | class CRM_Report_Form_TestCaseTest extends CiviReportTestCase { |
| 41 | protected $_tablesToTruncate = array( |
| 42 | 'civicrm_contact', |
| 43 | 'civicrm_email', |
| 44 | 'civicrm_phone', |
| 45 | 'civicrm_address', |
| 46 | 'civicrm_contribution', |
| 47 | ); |
| 48 | |
| 49 | /** |
| 50 | * @return array |
| 51 | */ |
| 52 | public function dataProvider() { |
| 53 | $testCaseA = array( |
| 54 | 'CRM_Report_Form_Contribute_Detail', |
| 55 | array( |
| 56 | 'fields' => array( |
| 57 | 'sort_name', |
| 58 | 'first_name', |
| 59 | 'email', |
| 60 | 'total_amount', |
| 61 | ), |
| 62 | 'filters' => array( |
| 63 | 'total_amount_op' => 'gte', |
| 64 | 'total_amount_value' => 50, |
| 65 | ), |
| 66 | // FIXME: add filters |
| 67 | ), |
| 68 | 'Contribute/fixtures/dataset-ascii.sql', |
| 69 | 'Contribute/fixtures/report-ascii.csv', |
| 70 | ); |
| 71 | |
| 72 | return array( |
| 73 | $testCaseA, |
| 74 | $testCaseA, |
| 75 | $testCaseA, |
| 76 | // We repeat the test a second time to |
| 77 | // ensure that CiviReportTestCase can |
| 78 | // clean up sufficiently to run |
| 79 | // multiple tests. |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @return array |
| 85 | */ |
| 86 | public function badDataProvider() { |
| 87 | return array( |
| 88 | // This test-case is bad because the dataset-ascii.sql does not match the |
| 89 | // report.csv (due to differences in international chars) |
| 90 | array( |
| 91 | 'CRM_Report_Form_Contribute_Detail', |
| 92 | array( |
| 93 | 'fields' => array( |
| 94 | 'sort_name', |
| 95 | 'first_name', |
| 96 | 'email', |
| 97 | 'total_amount', |
| 98 | ), |
| 99 | 'filters' => array( |
| 100 | 'total_amount_op' => 'gte', |
| 101 | 'total_amount_value' => 50, |
| 102 | ), |
| 103 | // FIXME: add filters |
| 104 | ), |
| 105 | 'Contribute/fixtures/dataset-ascii.sql', |
| 106 | 'Contribute/fixtures/report.csv', |
| 107 | ), |
| 108 | // This test-case is bad because the filters check for |
| 109 | // an amount >= $100, but the test data includes records |
| 110 | // for $50. |
| 111 | array( |
| 112 | 'CRM_Report_Form_Contribute_Detail', |
| 113 | array( |
| 114 | 'fields' => array( |
| 115 | 'sort_name', |
| 116 | 'first_name', |
| 117 | 'email', |
| 118 | 'total_amount', |
| 119 | ), |
| 120 | 'filters' => array( |
| 121 | 'total_amount_op' => 'gte', |
| 122 | 'total_amount_value' => 100, |
| 123 | ), |
| 124 | // FIXME: add filters |
| 125 | ), |
| 126 | 'Contribute/fixtures/dataset-ascii.sql', |
| 127 | 'Contribute/fixtures/report.csv', |
| 128 | ), |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | public function setUp() { |
| 133 | parent::setUp(); |
| 134 | $this->foreignKeyChecksOff(); |
| 135 | $this->quickCleanup($this->_tablesToTruncate); |
| 136 | } |
| 137 | |
| 138 | public function tearDown() { |
| 139 | parent::tearDown(); |
| 140 | CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp1'); |
| 141 | CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp2'); |
| 142 | CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp3'); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @dataProvider dataProvider |
| 147 | * @param $reportClass |
| 148 | * @param $inputParams |
| 149 | * @param $dataSet |
| 150 | * @param $expectedOutputCsvFile |
| 151 | * @throws \Exception |
| 152 | */ |
| 153 | public function testReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile) { |
| 154 | $config = CRM_Core_Config::singleton(); |
| 155 | CRM_Utils_File::sourceSQLFile($config->dsn, dirname(__FILE__) . "/{$dataSet}"); |
| 156 | |
| 157 | $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams); |
| 158 | $reportCsvArray = $this->getArrayFromCsv($reportCsvFile); |
| 159 | |
| 160 | $expectedOutputCsvArray = $this->getArrayFromCsv(dirname(__FILE__) . "/{$expectedOutputCsvFile}"); |
| 161 | $this->assertCsvArraysEqual($expectedOutputCsvArray, $reportCsvArray); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @expectedException PHPUnit_Framework_AssertionFailedError |
| 166 | * @dataProvider badDataProvider |
| 167 | * @param $reportClass |
| 168 | * @param $inputParams |
| 169 | * @param $dataSet |
| 170 | * @param $expectedOutputCsvFile |
| 171 | * @throws \Exception |
| 172 | */ |
| 173 | public function testBadReportOutput($reportClass, $inputParams, $dataSet, $expectedOutputCsvFile) { |
| 174 | $config = CRM_Core_Config::singleton(); |
| 175 | CRM_Utils_File::sourceSQLFile($config->dsn, dirname(__FILE__) . "/{$dataSet}"); |
| 176 | |
| 177 | $reportCsvFile = $this->getReportOutputAsCsv($reportClass, $inputParams); |
| 178 | $reportCsvArray = $this->getArrayFromCsv($reportCsvFile); |
| 179 | |
| 180 | $expectedOutputCsvArray = $this->getArrayFromCsv(dirname(__FILE__) . "/{$expectedOutputCsvFile}"); |
| 181 | $this->assertCsvArraysEqual($expectedOutputCsvArray, $reportCsvArray); |
| 182 | } |
| 183 | |
| 184 | } |