3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
45 * downloaded file path.
47 public function downloadCSV($selector, $fileName = 'CiviCRM_Contact_Search.csv', $downloadDir = '/tmp') {
48 // File download path.
49 $file = "{$downloadDir}/{$fileName}";
51 // Delete file if already exists.
52 if (file_exists($file)) {
56 $this->click($selector);
58 // Wait for file to be downloaded
59 for ($i = 1; $i < 15; ++
$i) {
61 if (file_exists($file)) {
66 $this->fail("CSV {$file} was not downloaded.");
70 * Read CSV file and fire provided assertions.
73 * File path of CSV file.
74 * @param array $checkColumns
75 * Check first row of csv.
76 * independent of index.
77 * @param array $checkRows
78 * Array of header and rows according to row index.
82 * // column name 'First Name', value 'Jones'
83 * 'First Name' => 'Jones',
84 * 'Last Name' => 'Franklin'
88 * 'First Name' => 'Rajan',
89 * 'Last Name' => 'mayekar'
92 * @param int $rowCount
93 * Count rows (excluding header row).
94 * @param array $settings
95 * Used for override settings.
97 public function reviewCSV($file, $checkColumns = array(), $checkRows = array(), $rowCount = 0, $settings = array()) {
98 // Check file exists before proceed.
99 $this->assertTrue(($file && file_exists($file)), "Not able to locate {$file}.");
101 // We are going to read downloaded file.
102 $fd = fopen($file, 'r');
104 $this->fail("Could not read {$file}.");
107 // Default seperator ','.
108 $fieldSeparator = !empty($settings['fieldSeparator']) ?
$settings['fieldSeparator'] : ',';
113 $headerRow = fgetcsv($fd, 0, $fieldSeparator);
114 $allRows[] = $headerRow;
116 // Read all other rows.
117 while ($row = fgetcsv($fd, 0, $fieldSeparator)) {
121 // We have done with the CSV reading.
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}.");
133 // Check row count, excluding header row.
134 if ($rowCount && !($rowCount == (count($allRows) - 1))) {
135 $this->fail("Mismatching row count");
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.
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}.");
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}.");
159 // Delete file, since we no longer need it.
160 if (empty($settings['skipDeleteFile'])) {