Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 TO |
27 | require_once 'CiviTest/CiviSeleniumTestCase.php'; |
28 | class ExportCiviSeleniumTestCase extends CiviSeleniumTestCase { | |
29 | ||
30 | /** | |
31 | * Function to download CSV file. | |
32 | * | |
33 | * @params string $selector element selector(download button in most of the cases). | |
34 | * @params sting $fileName file name to be download. | |
35 | * @params string $downloadDir download dir. | |
36 | * | |
37 | * @return string downloaded file path. | |
38 | */ | |
39 | function downloadCSV($selector, $fileName = 'CiviCRM_Contact_Search.csv', $downloadDir = '/tmp') { | |
40 | // File download path. | |
41 | $file = "{$downloadDir}/{$fileName}"; | |
42 | ||
43 | // Delete file if already exists. | |
44 | if (file_exists($file)) { | |
45 | @unlink($file); | |
46 | } | |
47 | ||
48 | // Download file. | |
49 | // File will automatically download without confirmation. | |
50 | $this->click($selector); | |
efb29358 CW |
51 | // Because it tends to cause problems, all uses of sleep() must be justified in comments |
52 | // Sleep should never be used for wait for anything to load from the server | |
53 | // FIXME: consider doing the following assertion in a while loop | |
54 | // with a more reasonable sleep time of 2 seconds per loop iteration | |
6a488035 TO |
55 | sleep(20); |
56 | ||
57 | // File was downloaded? | |
58 | $this->assertTrue(file_exists($file), "CSV {$file} was not downloaded."); | |
59 | ||
60 | return $file; | |
61 | } | |
62 | ||
63 | /** | |
64 | * Function to read CSV file and fire provided assertions. | |
65 | * | |
66 | * @params string $file file path of CSV file. | |
67 | * @params array $checkHeaders check first row of csv | |
68 | * independent of index. | |
69 | * @params array $checkRows array of header and rows according to row index | |
70 | * eg: array( | |
71 | * 1 => array( | |
72 | // Row index 1 | |
73 | // column name 'First Name', value 'Jones' | |
74 | * 'First Name' => 'Jones', | |
75 | * 'Last Name' => 'Franklin' | |
76 | * ), | |
77 | * 2 => array( | |
78 | // Row index 2 | |
79 | * 'First Name' => 'Rajan', | |
80 | * 'Last Name' => 'mayekar' | |
81 | * ), | |
82 | * ); | |
83 | * @params int $rowCount count rows (excluding header row). | |
84 | * @params array $settings used for override settings. | |
85 | */ | |
86 | function reviewCSV($file, $checkColumns = array( | |
87 | ), $checkRows = array(), $rowCount = 0, $settings = array()) { | |
88 | // Check file exists before proceed. | |
89 | $this->assertTrue(($file && file_exists($file)), "Not able to locate {$file}."); | |
90 | ||
91 | // We are going to read downloaded file. | |
92 | $fd = fopen($file, 'r'); | |
93 | if (!$fd) { | |
94 | $this->fail("Could not read {$file}."); | |
95 | } | |
96 | ||
97 | // Default seperator ','. | |
98 | $fieldSeparator = !empty($settings['fieldSeparator']) ? $settings['fieldSeparator'] : ','; | |
99 | ||
100 | $allRows = array(); | |
101 | ||
102 | // Read header row. | |
103 | $headerRow = fgetcsv($fd, 0, $fieldSeparator); | |
104 | $allRows[] = $headerRow; | |
105 | ||
106 | // Read all other rows. | |
107 | while ($row = fgetcsv($fd, 0, $fieldSeparator)) { | |
108 | $allRows[] = $row; | |
109 | } | |
110 | ||
111 | // We have done with the CSV reading. | |
112 | fclose($fd); | |
113 | ||
114 | // Check header columns. | |
115 | if (!empty($checkColumns)) { | |
116 | foreach ($checkColumns as $column) { | |
117 | if (!in_array($column, $headerRow)) { | |
118 | $this->fail("Missing column {$column}."); | |
119 | } | |
120 | } | |
121 | } | |
122 | ||
123 | // Check row count, excluding header row. | |
124 | if ($rowCount && !($rowCount == (count($allRows) - 1))) { | |
125 | $this->fail("Mismatching row count"); | |
126 | } | |
127 | ||
128 | // Check all other rows. | |
129 | if (!empty($checkRows)) { | |
130 | foreach ($checkRows as $rowIndex => $row) { | |
131 | if ($rowIndex == 0) { | |
132 | // Skip checking header row, since we are already doing it above. | |
133 | continue; | |
134 | } | |
135 | ||
136 | foreach ($row as $column => $value) { | |
137 | $headerIndex = array_search($column, $headerRow); | |
138 | if ($headerIndex === FALSE) { | |
139 | $this->fail("Not able to locate column {$column} for row index {$rowIndex}."); | |
140 | } | |
141 | ||
142 | if (!isset($allRows[$rowIndex][$headerIndex]) || !($value == $allRows[$rowIndex][$headerIndex])) { | |
143 | $this->fail("Expected: {$value}, Got: {$allRows[$rowIndex][$headerIndex]}, for column {$column} for row index {$rowIndex}."); | |
144 | } | |
145 | } | |
146 | } | |
147 | } | |
148 | ||
149 | // Delete file, since we no longer need it. | |
150 | if (empty($settings['skipDeleteFile'])) { | |
151 | @unlink($file); | |
152 | } | |
153 | } | |
154 | } | |
155 |