Merge pull request #4606 from johanv/CRM-15636-price_set_event_and_contribution
[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 element selector(download button in most of the cases).
38 * @param string $fileName file name to be download.
39 * @param string $downloadDir download dir.
40 *
41 * @return string downloaded file path.
42 */
43 public 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
52 $this->click($selector);
53
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.");
63 }
64
65 /**
66 * Read CSV file and fire provided assertions.
67 *
68 * @param string $file file path of CSV file.
69 * @param array $checkColumns check first row of csv
70 * independent of index.
71 * @param array $checkRows array of header and rows according to row index
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 * );
85 * @param int $rowCount count rows (excluding header row).
86 * @param array $settings used for override settings.
87 */
88 public function reviewCSV($file, $checkColumns = array(), $checkRows = array(), $rowCount = 0, $settings = array()) {
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 }