Merge pull request #4863 from totten/master-phpcbf4
[civicrm-core.git] / tests / phpunit / WebTest / Export / ExportCiviSeleniumTestCase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * 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 downloaded file path.
45 */
46 public function downloadCSV($selector, $fileName = 'CiviCRM_Contact_Search.csv', $downloadDir = '/tmp') {
47 // File download path.
48 $file = "{$downloadDir}/{$fileName}";
49
50 // Delete file if already exists.
51 if (file_exists($file)) {
52 @unlink($file);
53 }
54
55 $this->click($selector);
56
57 // Wait for file to be downloaded
58 for ($i = 1; $i < 15; ++$i) {
59 sleep(2);
60 if (file_exists($file)) {
61 return $file;
62 }
63 }
64 // Timeout
65 $this->fail("CSV {$file} was not downloaded.");
66 }
67
68 /**
69 * Read CSV file and fire provided assertions.
70 *
71 * @param string $file
72 * File path of CSV file.
73 * @param array $checkColumns
74 * Check first row of csv.
75 * independent of index.
76 * @param array $checkRows
77 * 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 * @param int $rowCount
92 * Count rows (excluding header row).
93 * @param array $settings
94 * Used for override settings.
95 */
96 public function reviewCSV($file, $checkColumns = array(), $checkRows = array(), $rowCount = 0, $settings = array()) {
97 // Check file exists before proceed.
98 $this->assertTrue(($file && file_exists($file)), "Not able to locate {$file}.");
99
100 // We are going to read downloaded file.
101 $fd = fopen($file, 'r');
102 if (!$fd) {
103 $this->fail("Could not read {$file}.");
104 }
105
106 // Default seperator ','.
107 $fieldSeparator = !empty($settings['fieldSeparator']) ? $settings['fieldSeparator'] : ',';
108
109 $allRows = array();
110
111 // Read header row.
112 $headerRow = fgetcsv($fd, 0, $fieldSeparator);
113 $allRows[] = $headerRow;
114
115 // Read all other rows.
116 while ($row = fgetcsv($fd, 0, $fieldSeparator)) {
117 $allRows[] = $row;
118 }
119
120 // We have done with the CSV reading.
121 fclose($fd);
122
123 // Check header columns.
124 if (!empty($checkColumns)) {
125 foreach ($checkColumns as $column) {
126 if (!in_array($column, $headerRow)) {
127 $this->fail("Missing column {$column}.");
128 }
129 }
130 }
131
132 // Check row count, excluding header row.
133 if ($rowCount && !($rowCount == (count($allRows) - 1))) {
134 $this->fail("Mismatching row count");
135 }
136
137 // Check all other rows.
138 if (!empty($checkRows)) {
139 foreach ($checkRows as $rowIndex => $row) {
140 if ($rowIndex == 0) {
141 // Skip checking header row, since we are already doing it above.
142 continue;
143 }
144
145 foreach ($row as $column => $value) {
146 $headerIndex = array_search($column, $headerRow);
147 if ($headerIndex === FALSE) {
148 $this->fail("Not able to locate column {$column} for row index {$rowIndex}.");
149 }
150
151 if (!isset($allRows[$rowIndex][$headerIndex]) || !($value == $allRows[$rowIndex][$headerIndex])) {
152 $this->fail("Expected: {$value}, Got: {$allRows[$rowIndex][$headerIndex]}, for column {$column} for row index {$rowIndex}.");
153 }
154 }
155 }
156 }
157
158 // Delete file, since we no longer need it.
159 if (empty($settings['skipDeleteFile'])) {
160 @unlink($file);
161 }
162 }
163 }