3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
27 require_once 'CiviTest/CiviSeleniumTestCase.php';
30 * Class ExportCiviSeleniumTestCase
32 class ExportCiviSeleniumTestCase
extends CiviSeleniumTestCase
{
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
44 * @return string downloaded file path.
46 public function downloadCSV($selector, $fileName = 'CiviCRM_Contact_Search.csv', $downloadDir = '/tmp') {
47 // File download path.
48 $file = "{$downloadDir}/{$fileName}";
50 // Delete file if already exists.
51 if (file_exists($file)) {
55 $this->click($selector);
57 // Wait for file to be downloaded
58 for ($i = 1; $i < 15; ++
$i) {
60 if (file_exists($file)) {
65 $this->fail("CSV {$file} was not downloaded.");
69 * Read CSV file and fire provided assertions.
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.
81 // column name 'First Name', value 'Jones'
82 * 'First Name' => 'Jones',
83 * 'Last Name' => 'Franklin'
87 * 'First Name' => 'Rajan',
88 * 'Last Name' => 'mayekar'
91 * @param int $rowCount
92 * Count rows (excluding header row).
93 * @param array $settings
94 * Used for override settings.
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}.");
100 // We are going to read downloaded file.
101 $fd = fopen($file, 'r');
103 $this->fail("Could not read {$file}.");
106 // Default seperator ','.
107 $fieldSeparator = !empty($settings['fieldSeparator']) ?
$settings['fieldSeparator'] : ',';
112 $headerRow = fgetcsv($fd, 0, $fieldSeparator);
113 $allRows[] = $headerRow;
115 // Read all other rows.
116 while ($row = fgetcsv($fd, 0, $fieldSeparator)) {
120 // We have done with the CSV reading.
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}.");
132 // Check row count, excluding header row.
133 if ($rowCount && !($rowCount == (count($allRows) - 1))) {
134 $this->fail("Mismatching row count");
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.
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}.");
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}.");
158 // Delete file, since we no longer need it.
159 if (empty($settings['skipDeleteFile'])) {