-
Notifications
You must be signed in to change notification settings - Fork 676
/
TestOfDashboardController.php
452 lines (380 loc) · 19.8 KB
/
TestOfDashboardController.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/**
*
* ThinkUp/tests/TestOfDashboardController.php
*
* Copyright (c) 2009-2012 Gina Trapani, Mark Wilkie
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.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/>.
*
* Test of DashboardController
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2012 Gina Trapani, Mark Wilkie
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*/
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';
require_once THINKUP_WEBAPP_PATH.'plugins/twitter/model/class.TwitterOAuthThinkUp.php';
require_once THINKUP_WEBAPP_PATH.'plugins/twitter/model/class.TwitterPlugin.php';
class TestOfDashboardController extends ThinkUpUnitTestCase {
public function setUp(){
parent::setUp();
$webapp_plugin_registrar = PluginRegistrarWebapp::getInstance();
$webapp_plugin_registrar->registerPlugin('twitter', 'TwitterPlugin');
$webapp_plugin_registrar->registerPlugin('facebook', 'FacebookPlugin');
$webapp_plugin_registrar->registerPlugin('google+', 'GooglePlusPlugin');
$webapp_plugin_registrar->registerPlugin('foursquare', 'FoursquarePlugin');
}
public function testConstructor() {
$controller = new DashboardController(true);
$this->assertTrue(isset($controller), 'constructor test');
$this->assertIsA($controller, 'DashboardController');
}
public function testNoInstanceNotLoggedIn() {
$controller = new DashboardController(true);
$results = $controller->go();
$v_mgr = $controller->getViewManager();
$this->assertEqual($v_mgr->getTemplateDataItem('info_msg'),
'There are no public accounts set up in this ThinkUp installation.');
$this->assertNoPattern("/Adjust Your Settings/", $results);
}
public function testNoInstancesLoggedIn() {
//Add owner
$owner_builder = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp J. User',
'email'=>'me@example.com', 'is_activated'=>1) );
$this->simulateLogin('me@example.com');
$controller = new DashboardController(true);
$results = $controller->go();
$v_mgr = $controller->getViewManager();
$this->assertPattern("/Welcome to ThinkUp. Let\'s get started./", $v_mgr->getTemplateDataItem('info_msg'));
$this->assertPattern("/Adjust Your Settings/", $results);
}
public function testNotLoggedInNoUserOrViewSpecifiedDefaultServiceUserSet() {
$builders = $this->buildData();
//Add another public instance
$instance_builder = FixtureBuilder::build('instances', array('id'=>6, 'network_user_id'=>14,
'network_username'=>'jack', 'is_public'=>1, 'crawler_last_run'=>'-2d'));
$instance_owner_builder = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>6));
//Set the default service user to jack, who is not last updated
$app_option_builder = FixtureBuilder::build('options', array('namespace'=>'application_options',
'option_name'=>'default_instance', 'option_value'=>'6'));
$controller = new DashboardController(true);
$results = $controller->go();
$v_mgr = $controller->getViewManager();
$this->assertEqual($v_mgr->getTemplateDataItem('controller_title'), "jack's Dashboard");
$instance = $v_mgr->getTemplateDataItem('instance');
$this->assertEqual($instance->network_username, 'jack');
$this->assertEqual($controller->getCacheKeyString(), '.htdashboard.tpl-', $controller->getCacheKeyString());
$this->assertFalse($v_mgr->getTemplateDataItem('is_searchable'));
}
public function testNotLoggedInNoUserOrViewSpecifiedNoDefaultServiceUserSet() {
$builders = $this->buildData();
//Add another public instance
$instance_builder = FixtureBuilder::build('instances', array('id'=>5, 'network_user_id'=>14,
'network_username'=>'jack', 'is_public'=>1, 'crawler_last_run'=>'-2d'));
$instance_owner_builder = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>5));
$controller = new DashboardController(true);
$results = $controller->go();
$v_mgr = $controller->getViewManager();
$this->assertEqual($v_mgr->getTemplateDataItem('controller_title'), "ev's Dashboard");
$instance = $v_mgr->getTemplateDataItem('instance');
$this->assertEqual($instance->network_username, 'ev');
$this->assertEqual($controller->getCacheKeyString(), '.htdashboard.tpl-', $controller->getCacheKeyString());
$this->assertFalse($v_mgr->getTemplateDataItem('is_searchable'));
}
public function testLoggedInUserSpecifiedNoViewSpecified() {
$builders = $this->buildData();
//must be logged in
$this->simulateLogin('me@example.com');
//required params
$_GET['u'] = 'ev';
$_GET['n'] = 'twitter';
//default view
$controller = new DashboardController(true);
$results = $controller->go();
//test if view variables were set correctly
$v_mgr = $controller->getViewManager();
$instance = $v_mgr->getTemplateDataItem('instance');
$this->assertEqual($instance->network_username, 'ev');
$this->assertEqual($controller->getCacheKeyString(), '.htdashboard.tpl-me@example.com-ev-twitter', 'Cache key');
}
public function testNotLoggedInPosts() {
$builders = $this->buildData();
$_GET['u'] = 'ev';
$_GET['n'] = 'twitter';
$_GET['v'] = 'tweets-all';
$controller = new DashboardController(true);
$results = $controller->go();
//test if view variables were set correctly
$v_mgr = $controller->getViewManager();
$this->assertEqual($v_mgr->getTemplateDataItem('header'), 'Your tweets');
$this->assertEqual($v_mgr->getTemplateDataItem('description'), 'All your tweets');
$this->assertIsA($v_mgr->getTemplateDataItem('all_tweets'), 'array', 'Array of tweets');
$this->assertEqual(sizeof($v_mgr->getTemplateDataItem('all_tweets')), 15, '15 posts in listing');
$this->assertFalse($v_mgr->getTemplateDataItem('is_searchable'));
}
public function testNonExistentUsername() {
$builders = $this->buildData();
$_GET['u'] = 'idontexist';
$_GET['n'] = 'twitter';
$_GET['v'] = 'tweets-all';
$controller = new DashboardController(true);
$results = $controller->go();
//test if view variables were set correctly
$v_mgr = $controller->getViewManager();
$error_msg = $v_mgr->getTemplateDataItem('error_msg');
$this->assertNotNull($error_msg);
$this->assertEqual($error_msg, 'idontexist on Twitter is not in ThinkUp.');
}
public function testLoggedInPosts() {
$builders = $this->buildData();
$this->simulateLogin('me@example.com');
//required params
$_GET['u'] = 'ev';
$_GET['n'] = 'twitter';
$_GET['v'] = 'tweets-all';
$controller = new DashboardController(true);
$results = $controller->go();
$this->debug($results);
//test if view variables were set correctly
$v_mgr = $controller->getViewManager();
$this->assertEqual($v_mgr->getTemplateDataItem('header'), 'Your tweets');
$this->assertEqual($v_mgr->getTemplateDataItem('description'), 'All your tweets');
$this->assertIsA($v_mgr->getTemplateDataItem('all_tweets'), 'array', 'Array of tweets');
$this->assertEqual(sizeof($v_mgr->getTemplateDataItem('all_tweets')), 15, '15 posts in listing');
$config = Config::getInstance();
$this->assertEqual($controller->getCacheKeyString(), '.htdashboard.tpl-me@example.com-ev-twitter-tweets-all');
$this->assertTrue($v_mgr->getTemplateDataItem('is_searchable'));
$this->assertPattern('/Export/', $results);
}
public function testLoggedInPostsFacebook() {
$builders = $this->buildData();
$this->simulateLogin('me@example.com');
//required params
$_GET['u'] ="Joe O\'Malley";
$_GET['n'] = 'facebook';
$_GET['v'] = 'posts-all';
$controller = new DashboardController(true);
$results = $controller->go();
$this->debug($results);
$config = Config::getInstance();
$this->assertPattern('/Export/', $results);
}
public function testFacebookAuthError() {
$builders = $this->buildData();
//required params
$_GET['u'] ="Joe O\'Malley";
$_GET['n'] = 'facebook';
$_GET['v'] = '';
$controller = new DashboardController(true);
//not logged in, shouldn't show auth error alert
$results = $controller->go();
$this->debug($results);
$this->assertNoPattern('/ThinkUp can\'t connect to your Facebook account./', $results);
//logged in, should show auth error alert
$this->simulateLogin('me@example.com');
$results = $controller->go();
$this->debug($results);
$this->assertPattern('/ThinkUp can\'t connect to your Facebook account./', $results);
}
public function testLoggedInPostsGooglePlus() {
$builders = $this->buildData();
$this->simulateLogin('me@example.com');
//required params
$_GET['u'] ='Kim';
$_GET['n'] = 'google+';
$_GET['v'] = 'posts-all';
$controller = new DashboardController(true);
$results = $controller->go();
$config = Config::getInstance();
$this->assertPattern('/Export/', $results);
}
public function testLoggedInPostsFoursquare() {
$builders = $this->buildData();
$this->simulateLogin('me@example.com');
//required params
$_GET['u'] ='kim@kim.com';
$_GET['n'] = 'foursquare';
$_GET['v'] = 'posts';
$controller = new DashboardController(true);
$results = $controller->go();
$config = Config::getInstance();
$this->assertPattern('/kim@kim.com on Foursquare/', $results);
}
public function testLoggedInPostsWithUsernameApostrophe() {
$builders = $this->buildData();
$this->simulateLogin('me@example.com');
//required params
$_GET['u'] ="Joe O\'Malley";
$_GET['n'] = 'facebook';
$_GET['v'] = 'posts-all';
$controller = new DashboardController(true);
$results = $controller->go();
//test if view variables were set correctly
$v_mgr = $controller->getViewManager();
$this->assertEqual($v_mgr->getTemplateDataItem('header'), 'All posts');
$this->assertEqual($v_mgr->getTemplateDataItem('description'), 'All your status updates');
$config = Config::getInstance();
$this->assertEqual($controller->getCacheKeyString(),
".htdashboard.tpl-me@example.com-Joe O\'Malley-facebook-posts-all");
$this->assertTrue($v_mgr->getTemplateDataItem('is_searchable'));
$this->assertPattern('/Export/', $results);
}
public function testNotLoggedInNotPublicInstance() {
$builders = $this->buildData();
//required params
$_GET['u'] = "Joe O\'Malley";
$_GET['n'] = 'facebook';
$_GET['v'] = 'posts-all';
$controller = new DashboardController(true);
$results = $controller->go();
//test if view variables were set correctly
$v_mgr = $controller->getViewManager();
$error_msg = $v_mgr->getTemplateDataItem('error_msg');
$this->assertNotNull($error_msg);
$this->assertEqual($error_msg, 'Insufficient privileges');
}
public function testLoggedInPeople() {
$builders = $this->buildData();
//first, add some people
$user1_builder = FixtureBuilder::build('users', array(
'user_name'=>'ginatrapani',
'user_id'=>'930061',
'network'=>'twitter')
);
$user2_builder = FixtureBuilder::build('users', array(
'user_name'=>'anildash',
'user_id'=>'123456',
'network'=>'twitter')
);
$follower_builders = array();
$follower_builders[] = FixtureBuilder::build('follows', array('user_id'=>'930061', 'follower_id'=>'13'));
$follower_builders[] = FixtureBuilder::build('follows', array('user_id'=>'123456', 'follower_id'=>'13'));
//must be logged in
$this->simulateLogin('me@example.com');
//required params
$_GET['u'] = 'ev';
$_GET['n'] = 'twitter';
$_GET['v'] = 'friends-mostactive';
$controller = new DashboardController(true);
$results = $controller->go();
//test if view variables were set correctly
$v_mgr = $controller->getViewManager();
$this->assertEqual($v_mgr->getTemplateDataItem('header'), 'Chatterboxes', 'Header');
$this->assertEqual($v_mgr->getTemplateDataItem('description'), 'People you follow who tweet the most',
'Description');
$this->assertIsA($v_mgr->getTemplateDataItem('people'), 'array', 'Array of users');
$this->assertEqual(sizeof($v_mgr->getTemplateDataItem('people')), 2, '2 users in listing');
$config = Config::getInstance();
$this->assertEqual($controller->getCacheKeyString(),
'.htdashboard.tpl-me@example.com-ev-twitter-friends-mostactive');
}
public function testNonexistentPluginIsActive() {
$builders = $this->buildData();
//add a plugin which is activatd, but doesn't exist on the file system
$plugin_builder = FixtureBuilder::build('plugins', array(
'name'=>'Flickr Thumbnails',
'folder_name'=>'flickrthumbnails',
'is_active'=>1)
);
$controller = new DashboardController(true);
$results = $controller->go();
//make sure there's no fatal error because the plugin files don't exist
}
public function testCleanXSS() {
$with_xss = true;
$builders = $this->buildData($with_xss);
$this->simulateLogin('me@example.com');
//required params
$_GET['u'] = 'ev';
$_GET['n'] = 'twitter';
$_GET['v'] = 'tweets-all';
$controller = new DashboardController(true);
$results = $controller->go();
$this->assertNoPattern("/This is post <script>alert\('wa'\);<\/script>\d+/", $results);
$this->assertPattern("/This is post <script>alert\('wa'\);<\/script>\d+/", $results);
}
private function buildData($with_xss = false) {
//Add owner
$owner_builder = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp J. User',
'email'=>'me@example.com', 'is_activated'=>1) );
//Add instance_owner
$instance_owner_builder_1 = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>1));
$instance_owner_builder_2 = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>2));
$instance_owner_builder_3 = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>3,
'auth_error'=>'Error validating access token: Session has expired at unix time SOME_TIME. The current unix '.
'time is SOME_TIME.'));
$instance_owner_builder_4 = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>4));
//Insert test data into test table
$user_builders = array();
$user_builders[] = FixtureBuilder::build('users', array('user_id'=>'13', 'user_name'=>'ev',
'full_name'=>'Ev Williams', 'network'=>'twitter', 'last_updated'=>'-1d'));
$user_builders[] = FixtureBuilder::build('users', array('user_id'=>'12', 'user_name'=>'jack',
'network'=>'twitter', 'last_updated'=>'-5d'));
$user_builders[] = FixtureBuilder::build('users', array('user_id'=>'14', 'user_name'=>"Joe O'Malley",
'last_updated'=>'-5d', 'network'=>'facebook'));
$user_builders[] = FixtureBuilder::build('users', array('user_id'=>'16', 'user_name'=>"Kim",
'last_updated'=>'-5d', 'network'=>'google+'));
$user_builders[] = FixtureBuilder::build('users', array('user_id'=>'17', 'user_name'=>"kim@kim.com",
'last_updated'=>'-5d', 'network'=>'foursquare'));
//Make public
$instance_builder_1 = FixtureBuilder::build('instances', array('id'=>1, 'network_user_id'=>'13',
'network_username'=>'ev', 'is_public'=>1, 'crawler_last_run'=>'-1d', 'network'=>'twitter'));
$instance_builder_2 = FixtureBuilder::build('instances', array('id'=>2, 'network_user_id'=>'16',
'network_username'=>"Kim", 'is_public'=>0, 'crawler_last_run'=>'-1d', 'network'=>'google+'));
$instance_builder_3 = FixtureBuilder::build('instances', array('id'=>3, 'network_user_id'=>'14',
'network_username'=>"Joe O'Malley", 'is_public'=>0, 'crawler_last_run'=>'-1d', 'network'=>'facebook'));
$instance_builder_4 = FixtureBuilder::build('instances', array('id'=>4, 'network_user_id'=>'17',
'network_username'=>"kim@kim.com", 'is_public'=>1, 'crawler_last_run'=>'-1d',
'network'=>'foursquare'));
$post_builders = array();
//Add a bunch of posts
$counter = 0;
$post_data = 'This is post ';
if ($with_xss) { $post_data .= "<script>alert('wa');</script>"; }
while ($counter < 40) {
$pseudo_minute = str_pad($counter, 2, "0", STR_PAD_LEFT);
$post_builders[] = FixtureBuilder::build('posts', array('post_id'=>$counter, 'author_user_id'=>13,
'author_username'=>'ev', 'author_fullname'=>'Ev Williams', 'post_text'=>$post_data.$counter,
'pub_date'=>'2006-01-01 00:'.$pseudo_minute.':00'));
$counter++;
}
$post_builders[] = FixtureBuilder::build('posts', array('post_id'=>41, 'author_user_id'=>'13',
'author_username'=>'ev', 'author_fullname'=>'Ev Williams',
'post_text'=>'This post is in reply to jacks post 50', 'in_reply_to_post_id'=>'50', 'network'=>'twitter',
'in_reply_to_user_id'=>'12'));
$post_builders[] = FixtureBuilder::build('posts', array('post_id'=>42, 'author_user_id'=>'12',
'author_username'=>'jack', 'author_fullname'=>'Jack Dorsey', 'post_text'=>'Ev replies to this post',
'network'=>'twitter'));
$post_builders[] = FixtureBuilder::build('posts', array('post_id'=>43, 'author_user_id'=>'14',
'author_username'=>"Joe O'Malley", 'author_fullname'=>"Joe O\'Malley", 'post_text'=>"Joe's post",
'network'=>'facebook'));
$counter = 0;
$post_data = 'This is post ';
while ($counter < 40) {
$pseudo_minute = str_pad($counter, 2, "0", STR_PAD_LEFT);
$post_builders[] = FixtureBuilder::build('posts', array('post_id'=>($counter+45), 'author_user_id'=>'14',
'author_username'=>"Joe O'Malley", 'author_fullname'=>"Joe O'Malley", 'post_text'=>$post_data.($counter+45),
'pub_date'=>'2006-01-01 00:'.$pseudo_minute.':00', 'network'=>'facebook'));
$counter++;
}
return array($owner_builder, $instance_owner_builder_1, $instance_owner_builder_2, $instance_owner_builder_3,
$instance_owner_builder_4, $user_builders, $instance_builder_1, $instance_builder_2, $instance_builder_3,
$instance_builder_4, $post_builders);
}
}