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