-
Notifications
You must be signed in to change notification settings - Fork 676
/
TestOfGridExportController.php
119 lines (108 loc) · 4.41 KB
/
TestOfGridExportController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
*
* ThinkUp/tests/TestOfGridExportController.php
*
* Copyright (c) 2009-2015 Gina Trapani, Mark Wilkie
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkup.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
* @author Mark Wilkie <mark[at]bitterpill[dot]org>
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2015 Gina Trapani, Mark Wilkie
*/
require_once dirname(__FILE__).'/init.tests.php';
require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
require_once THINKUP_WEBAPP_PATH.'config.inc.php';
class TestOfGridExportController extends ThinkUpUnitTestCase {
public function testConstructor() {
$controller = new GridExportController(true);
$this->assertTrue(isset($controller));
}
public function testNotLoggedIn() {
$controller = new GridExportController(true);
$this->assertTrue(isset($controller));
$results = $controller->go();
$this->assertPattern( '/session\/login.php\?redirect\=/', $controller->redirect_destination);
}
public function testMissingParams() {
$this->simulateLogin('me@example.com');
$controller = new GridExportController(true);
$this->assertTrue(isset($controller));
ob_start();
$controller->control();
$results = ob_get_contents();
ob_end_clean();
$this->assertPattern("/No search data to export./", $results);
}
public function testNonExistentUser() {
$this->simulateLogin('me@example.com');
$_GET['u'] = 'idontexist';
$_GET['n'] = 'idontexist';
$controller = new GridExportController(true);
ob_start();
$controller->control();
$results = ob_get_contents();
ob_end_clean();
$this->assertPattern("/No search data to export./", $results);
}
public function testGridExport() {
$builders = $this->buildData();
$this->simulateLogin('me@example.com');
$data = array( array('name' => 'value1'), array('name' => 'value2'));
$json = json_encode($data);
//echo $json;
$_POST['grid_export_data'] = $json;
$controller = new GridExportController(true);
ob_start();
$controller->control();
$results = ob_get_contents();
ob_end_clean();
$data = split("\n", $results);
$this->assertEqual(3, count($data), 'we should have three lines, one blank');
$value = str_getcsv($data[0]);
$this->assertEqual($value[0], 'value1');
$value = str_getcsv($data[1]);
$this->assertEqual($value[0], 'value2');
$this->assertEqual($data[2], '');
}
private function buildData() {
$owner_builder = FixtureBuilder::build('owners', array('id'=>1, 'email'=>'me@example.com'));
$instance_builder = FixtureBuilder::build('instances', array('id'=>1, 'network_username'=>'someuser1',
'network'=>'twitter'));
$instance1_builder = FixtureBuilder::build('instances', array('id'=>2, 'network_username'=>'someuser2',
'network'=>'twitter'));
$owner_instance_builder = FixtureBuilder::build('owner_instances', array('instance_id'=>1, 'owner_id'=>1));
return array($owner_builder, $instance_builder, $instance1_builder, $owner_instance_builder);
}
}
/**
* The following PHP 5.2-compatible code courtesy of:
* http://www.php.net/manual/en/function.str-getcsv.php#88311
*/
if (!function_exists('str_getcsv')) {
function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\") {
$fiveMBs = 5 * 1024 * 1024;
$fp = fopen("php://temp/maxmemory:$fiveMBs", 'r+');
fputs($fp, $input);
rewind($fp);
$data = fgetcsv($fp, 1000, $delimiter, $enclosure); // $escape only got added in 5.3.0
fclose($fp);
return $data;
}
}