Commit | Line | Data |
---|---|---|
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 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 | ||
6a488035 | 27 | require_once 'CiviTest/CiviSeleniumTestCase.php'; |
e9479dcf EM |
28 | |
29 | /** | |
30 | * Class ExportCiviSeleniumTestCase | |
31 | */ | |
6a488035 TO |
32 | class ExportCiviSeleniumTestCase extends CiviSeleniumTestCase { |
33 | ||
34 | /** | |
35 | * Function to download CSV file. | |
36 | * | |
37 | * @params string $selector element selector(download button in most of the cases). | |
38 | * @params sting $fileName file name to be download. | |
39 | * @params string $downloadDir download dir. | |
40 | * | |
2a6da8d7 EM |
41 | * @param $selector |
42 | * @param string $fileName | |
43 | * @param string $downloadDir | |
44 | * | |
6a488035 TO |
45 | * @return string downloaded file path. |
46 | */ | |
47 | 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 | ||
6a488035 | 56 | $this->click($selector); |
6a488035 | 57 | |
beea38fc CW |
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."); | |
6a488035 TO |
67 | } |
68 | ||
69 | /** | |
70 | * Function to read CSV file and fire provided assertions. | |
71 | * | |
72 | * @params string $file file path of CSV file. | |
73 | * @params array $checkHeaders check first row of csv | |
74 | * independent of index. | |
75 | * @params array $checkRows array of header and rows according to row index | |
76 | * eg: array( | |
77 | * 1 => array( | |
78 | // Row index 1 | |
79 | // column name 'First Name', value 'Jones' | |
80 | * 'First Name' => 'Jones', | |
81 | * 'Last Name' => 'Franklin' | |
82 | * ), | |
83 | * 2 => array( | |
84 | // Row index 2 | |
85 | * 'First Name' => 'Rajan', | |
86 | * 'Last Name' => 'mayekar' | |
87 | * ), | |
88 | * ); | |
89 | * @params int $rowCount count rows (excluding header row). | |
90 | * @params array $settings used for override settings. | |
91 | */ | |
92 | function reviewCSV($file, $checkColumns = array( | |
93 | ), $checkRows = array(), $rowCount = 0, $settings = array()) { | |
94 | // Check file exists before proceed. | |
95 | $this->assertTrue(($file && file_exists($file)), "Not able to locate {$file}."); | |
96 | ||
97 | // We are going to read downloaded file. | |
98 | $fd = fopen($file, 'r'); | |
99 | if (!$fd) { | |
100 | $this->fail("Could not read {$file}."); | |
101 | } | |
102 | ||
103 | // Default seperator ','. | |
104 | $fieldSeparator = !empty($settings['fieldSeparator']) ? $settings['fieldSeparator'] : ','; | |
105 | ||
106 | $allRows = array(); | |
107 | ||
108 | // Read header row. | |
109 | $headerRow = fgetcsv($fd, 0, $fieldSeparator); | |
110 | $allRows[] = $headerRow; | |
111 | ||
112 | // Read all other rows. | |
113 | while ($row = fgetcsv($fd, 0, $fieldSeparator)) { | |
114 | $allRows[] = $row; | |
115 | } | |
116 | ||
117 | // We have done with the CSV reading. | |
118 | fclose($fd); | |
119 | ||
120 | // Check header columns. | |
121 | if (!empty($checkColumns)) { | |
122 | foreach ($checkColumns as $column) { | |
123 | if (!in_array($column, $headerRow)) { | |
124 | $this->fail("Missing column {$column}."); | |
125 | } | |
126 | } | |
127 | } | |
128 | ||
129 | // Check row count, excluding header row. | |
130 | if ($rowCount && !($rowCount == (count($allRows) - 1))) { | |
131 | $this->fail("Mismatching row count"); | |
132 | } | |
133 | ||
134 | // Check all other rows. | |
135 | if (!empty($checkRows)) { | |
136 | foreach ($checkRows as $rowIndex => $row) { | |
137 | if ($rowIndex == 0) { | |
138 | // Skip checking header row, since we are already doing it above. | |
139 | continue; | |
140 | } | |
141 | ||
142 | foreach ($row as $column => $value) { | |
143 | $headerIndex = array_search($column, $headerRow); | |
144 | if ($headerIndex === FALSE) { | |
145 | $this->fail("Not able to locate column {$column} for row index {$rowIndex}."); | |
146 | } | |
147 | ||
148 | if (!isset($allRows[$rowIndex][$headerIndex]) || !($value == $allRows[$rowIndex][$headerIndex])) { | |
149 | $this->fail("Expected: {$value}, Got: {$allRows[$rowIndex][$headerIndex]}, for column {$column} for row index {$rowIndex}."); | |
150 | } | |
151 | } | |
152 | } | |
153 | } | |
154 | ||
155 | // Delete file, since we no longer need it. | |
156 | if (empty($settings['skipDeleteFile'])) { | |
157 | @unlink($file); | |
158 | } | |
159 | } | |
160 | } | |
161 |