| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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 along with this program; if not, contact CiviCRM LLC | |
| 21 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 22 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 23 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 24 | +--------------------------------------------------------------------+ |
| 25 | */ |
| 26 | |
| 27 | require_once 'CiviTest/CiviSeleniumTestCase.php'; |
| 28 | |
| 29 | /** |
| 30 | * Class ExportCiviSeleniumTestCase |
| 31 | */ |
| 32 | class ExportCiviSeleniumTestCase extends CiviSeleniumTestCase { |
| 33 | |
| 34 | /** |
| 35 | * Download CSV file. |
| 36 | * |
| 37 | * @param string $selector |
| 38 | * Element selector(download button in most of the cases). |
| 39 | * @param string $fileName |
| 40 | * File name to be download. |
| 41 | * @param string $downloadDir |
| 42 | * Download dir. |
| 43 | * |
| 44 | * @return string |
| 45 | * downloaded file path. |
| 46 | */ |
| 47 | public function downloadCSV($selector, $fileName = 'CiviCRM_Contact_Search.csv', $downloadDir = '/tmp') { |
| 48 | // File download path. |
| 49 | $file = "{$downloadDir}/{$fileName}"; |
| 50 | |
| 51 | // Delete file if already exists. |
| 52 | if (file_exists($file)) { |
| 53 | @unlink($file); |
| 54 | } |
| 55 | |
| 56 | $this->click($selector); |
| 57 | |
| 58 | // Wait for file to be downloaded |
| 59 | for ($i = 1; $i < 15; ++$i) { |
| 60 | sleep(2); |
| 61 | if (file_exists($file)) { |
| 62 | return $file; |
| 63 | } |
| 64 | } |
| 65 | // Timeout |
| 66 | $this->fail("CSV {$file} was not downloaded."); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Read CSV file and fire provided assertions. |
| 71 | * |
| 72 | * @param string $file |
| 73 | * File path of CSV file. |
| 74 | * @param array $checkColumns |
| 75 | * Check first row of csv. |
| 76 | * independent of index. |
| 77 | * @param array $checkRows |
| 78 | * Array of header and rows according to row index. |
| 79 | * eg: array( |
| 80 | * 1 => array( |
| 81 | * // Row index 1 |
| 82 | * // column name 'First Name', value 'Jones' |
| 83 | * 'First Name' => 'Jones', |
| 84 | * 'Last Name' => 'Franklin' |
| 85 | * ), |
| 86 | * 2 => array( |
| 87 | * // Row index 2 |
| 88 | * 'First Name' => 'Rajan', |
| 89 | * 'Last Name' => 'mayekar' |
| 90 | * ), |
| 91 | * ); |
| 92 | * @param int $rowCount |
| 93 | * Count rows (excluding header row). |
| 94 | * @param array $settings |
| 95 | * Used for override settings. |
| 96 | */ |
| 97 | public function reviewCSV($file, $checkColumns = array(), $checkRows = array(), $rowCount = 0, $settings = array()) { |
| 98 | // Check file exists before proceed. |
| 99 | $this->assertTrue(($file && file_exists($file)), "Not able to locate {$file}."); |
| 100 | |
| 101 | // We are going to read downloaded file. |
| 102 | $fd = fopen($file, 'r'); |
| 103 | if (!$fd) { |
| 104 | $this->fail("Could not read {$file}."); |
| 105 | } |
| 106 | |
| 107 | // Default seperator ','. |
| 108 | $fieldSeparator = !empty($settings['fieldSeparator']) ? $settings['fieldSeparator'] : ','; |
| 109 | |
| 110 | $allRows = array(); |
| 111 | |
| 112 | // Read header row. |
| 113 | $headerRow = fgetcsv($fd, 0, $fieldSeparator); |
| 114 | $allRows[] = $headerRow; |
| 115 | |
| 116 | // Read all other rows. |
| 117 | while ($row = fgetcsv($fd, 0, $fieldSeparator)) { |
| 118 | $allRows[] = $row; |
| 119 | } |
| 120 | |
| 121 | // We have done with the CSV reading. |
| 122 | fclose($fd); |
| 123 | |
| 124 | // Check header columns. |
| 125 | if (!empty($checkColumns)) { |
| 126 | foreach ($checkColumns as $column) { |
| 127 | if (!in_array($column, $headerRow)) { |
| 128 | $this->fail("Missing column {$column}."); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Check row count, excluding header row. |
| 134 | if ($rowCount && !($rowCount == (count($allRows) - 1))) { |
| 135 | $this->fail("Mismatching row count"); |
| 136 | } |
| 137 | |
| 138 | // Check all other rows. |
| 139 | if (!empty($checkRows)) { |
| 140 | foreach ($checkRows as $rowIndex => $row) { |
| 141 | if ($rowIndex == 0) { |
| 142 | // Skip checking header row, since we are already doing it above. |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | foreach ($row as $column => $value) { |
| 147 | $headerIndex = array_search($column, $headerRow); |
| 148 | if ($headerIndex === FALSE) { |
| 149 | $this->fail("Not able to locate column {$column} for row index {$rowIndex}."); |
| 150 | } |
| 151 | |
| 152 | if (!isset($allRows[$rowIndex][$headerIndex]) || !($value == $allRows[$rowIndex][$headerIndex])) { |
| 153 | $this->fail("Expected: {$value}, Got: {$allRows[$rowIndex][$headerIndex]}, for column {$column} for row index {$rowIndex}."); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Delete file, since we no longer need it. |
| 160 | if (empty($settings['skipDeleteFile'])) { |
| 161 | @unlink($file); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | } |