Merge pull request #8525 from twomice/CRM-18251b
[civicrm-core.git] / tests / phpunit / WebTest / Export / ExportCiviSeleniumTestCase.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
d25dd0ee 25 */
6a488035 26
6a488035 27require_once 'CiviTest/CiviSeleniumTestCase.php';
e9479dcf
EM
28
29/**
30 * Class ExportCiviSeleniumTestCase
31 */
6a488035
TO
32class ExportCiviSeleniumTestCase extends CiviSeleniumTestCase {
33
34 /**
100fef9d 35 * Download CSV file.
6a488035 36 *
e16033b4
TO
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.
2a6da8d7 43 *
a6c01b45
CW
44 * @return string
45 * downloaded file path.
6a488035 46 */
00be9182 47 public function downloadCSV($selector, $fileName = 'CiviCRM_Contact_Search.csv', $downloadDir = '/tmp') {
6a488035
TO
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 58 // Wait for file to be downloaded
6c6e6187 59 for ($i = 1; $i < 15; ++$i) {
beea38fc
CW
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 /**
100fef9d 70 * Read CSV file and fire provided assertions.
6a488035 71 *
e16033b4
TO
72 * @param string $file
73 * File path of CSV file.
74 * @param array $checkColumns
75 * Check first row of csv.
6a488035 76 * independent of index.
e16033b4
TO
77 * @param array $checkRows
78 * Array of header and rows according to row index.
6a488035
TO
79 * eg: array(
80 * 1 => array(
92915c55
TO
81 * // Row index 1
82 * // column name 'First Name', value 'Jones'
6a488035
TO
83 * 'First Name' => 'Jones',
84 * 'Last Name' => 'Franklin'
85 * ),
86 * 2 => array(
92915c55 87 * // Row index 2
6a488035
TO
88 * 'First Name' => 'Rajan',
89 * 'Last Name' => 'mayekar'
90 * ),
91 * );
e16033b4
TO
92 * @param int $rowCount
93 * Count rows (excluding header row).
94 * @param array $settings
95 * Used for override settings.
6a488035 96 */
00be9182 97 public function reviewCSV($file, $checkColumns = array(), $checkRows = array(), $rowCount = 0, $settings = array()) {
6a488035
TO
98 // Check file exists before proceed.
99 $this->assertTrue(($file && file_exists($file)), "Not able to locate {$file}.");
100
101 // We are going to read downloaded file.
102 $fd = fopen($file, 'r');
103 if (!$fd) {
104 $this->fail("Could not read {$file}.");
105 }
106
107 // Default seperator ','.
108 $fieldSeparator = !empty($settings['fieldSeparator']) ? $settings['fieldSeparator'] : ',';
109
110 $allRows = array();
111
112 // Read header row.
113 $headerRow = fgetcsv($fd, 0, $fieldSeparator);
114 $allRows[] = $headerRow;
115
116 // Read all other rows.
117 while ($row = fgetcsv($fd, 0, $fieldSeparator)) {
118 $allRows[] = $row;
119 }
120
121 // We have done with the CSV reading.
122 fclose($fd);
123
124 // Check header columns.
125 if (!empty($checkColumns)) {
126 foreach ($checkColumns as $column) {
127 if (!in_array($column, $headerRow)) {
128 $this->fail("Missing column {$column}.");
129 }
130 }
131 }
132
133 // Check row count, excluding header row.
134 if ($rowCount && !($rowCount == (count($allRows) - 1))) {
135 $this->fail("Mismatching row count");
136 }
137
138 // Check all other rows.
139 if (!empty($checkRows)) {
140 foreach ($checkRows as $rowIndex => $row) {
141 if ($rowIndex == 0) {
142 // Skip checking header row, since we are already doing it above.
143 continue;
144 }
145
146 foreach ($row as $column => $value) {
147 $headerIndex = array_search($column, $headerRow);
148 if ($headerIndex === FALSE) {
149 $this->fail("Not able to locate column {$column} for row index {$rowIndex}.");
150 }
151
152 if (!isset($allRows[$rowIndex][$headerIndex]) || !($value == $allRows[$rowIndex][$headerIndex])) {
153 $this->fail("Expected: {$value}, Got: {$allRows[$rowIndex][$headerIndex]}, for column {$column} for row index {$rowIndex}.");
154 }
155 }
156 }
157 }
158
159 // Delete file, since we no longer need it.
160 if (empty($settings['skipDeleteFile'])) {
161 @unlink($file);
162 }
163 }
96025800 164
6a488035 165}