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