PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/TestOfGridController.php

http://github.com/ginatrapani/ThinkUp
PHP | 303 lines | 236 code | 33 blank | 34 comment | 0 complexity | 5c069227caca8afad341ba2e25bfc7c5 MD5 | raw file
  1<?php
  2/**
  3 *
  4 * ThinkUp/tests/TestOfGridController.php
  5 *
  6 * Copyright (c) 2009-2012 Mark Wilkie, Gina Trapani, Guillaume Boudreau
  7 *
  8 * LICENSE:
  9 *
 10 * This file is part of ThinkUp (http://thinkupapp.com).
 11 *
 12 * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 13 * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
 14 * later version.
 15 *
 16 * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 17 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 18 * details.
 19 *
 20 * You should have received a copy of the GNU General Public License along with ThinkUp.  If not, see
 21 * <http://www.gnu.org/licenses/>.
 22 *
 23 *
 24 * @author Mark Wilkie <mark[at]bitterpill[dot]org>
 25 * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
 26 * @author Guillaume Boudreau <gboudreau[at]pommepause[dot]com>
 27 * @license http://www.gnu.org/licenses/gpl.html
 28 * @copyright 2009-2012 Mark Wilkie, Gina Trapani, Guillaume Boudreau
 29 */
 30require_once dirname(__FILE__).'/init.tests.php';
 31require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
 32require_once THINKUP_WEBAPP_PATH.'config.inc.php';
 33
 34class TestOfGridController extends ThinkUpUnitTestCase {
 35
 36    public function setUp(){
 37        parent::setUp();
 38        $webapp = Webapp::getInstance();
 39        $webapp->registerPlugin('twitter', 'TwitterPlugin');
 40    }
 41
 42    public function tearDown() {
 43        parent::tearDown();
 44        GridController::$MAX_ROWS = 5000;
 45    }
 46
 47    public function testConstructor() {
 48        $controller = new GridController(true);
 49        $this->assertTrue(isset($controller));
 50    }
 51
 52    public function testNotLoggedIn() {
 53        $controller = new GridController(true);
 54        $results = $controller->go();
 55        $v_mgr = $controller->getViewManager();
 56        $config = Config::getInstance();
 57        $this->assertEqual('You must <a href="'.$config->getValue('site_root_path').
 58        'session/login.php">log in</a> to do this.', $v_mgr->getTemplateDataItem('error_msg'));
 59    }
 60
 61    public function testMissingParams() {
 62        $this->simulateLogin('me@example.com');
 63        $controller = new GridController(true);
 64        ob_start();
 65        $controller->control();
 66        $results = ob_get_contents();
 67        ob_end_clean();
 68        $ob = json_decode($results);
 69        $this->assertEqual($ob->status, 'failed');
 70        $this->assertEqual($ob->message, 'Missing Parameters');
 71    }
 72
 73    public function testNonExistentUser() {
 74        $this->simulateLogin('me@example.com');
 75        $_GET['u'] = 'idontexist';
 76        $_GET['n'] = 'idontexist';
 77        $controller = new GridController(true);
 78
 79        ob_start();
 80        $controller->control();
 81        $results = ob_get_contents();
 82        ob_end_clean();
 83        $ob = json_decode($results);
 84        $this->assertEqual($ob->status, 'failed');
 85        $this->assertEqual($ob->message, 'idontexistis not configured.');
 86    }
 87
 88    public function testOwnerWithoutAccess() {
 89        $builders = $this->buildData();
 90        $this->simulateLogin('me@example.com');
 91        $_GET['u'] = 'someuser2';
 92        $_GET['n'] = 'twitter';
 93        ob_start();
 94        $controller = new GridController(true);
 95        $controller->control();
 96        $results = ob_get_contents();
 97        ob_end_clean();
 98        $ob = json_decode($results);
 99        $this->assertEqual($ob->status, 'failed');
100        $this->assertEqual($ob->message, 'Insufficient privileges.');
101    }
102
103    public function testOwnerWithAccessTweetsAll() {
104        $builders = $this->buildData();
105        $this->simulateLogin('me@example.com');
106        $_GET['u'] = 'someuser1';
107        $_GET['n'] = 'twitter';
108        $_GET['d'] = 'tweets-all';
109        $controller = new GridController(true);
110        $this->assertTrue(isset($controller));
111        ob_start();
112        $controller->control();
113        $results = ob_get_contents();
114        ob_end_clean();
115        $json = substr($results, 29, strrpos($results, ';') - 30);
116        $ob = json_decode( $json );
117        $this->assertEqual($ob->status, 'success');
118        $this->assertEqual(count($ob->posts), 3);
119    }
120
121    public function testOwnerWithAccessTweetsAllMaxLimit() {
122        $builders = $this->buildData();
123        GridController::$MAX_ROWS = 1;
124        $this->simulateLogin('me@example.com');
125        $_GET['u'] = 'someuser1';
126        $_GET['n'] = 'twitter';
127        $_GET['d'] = 'tweets-all';
128        $controller = new GridController(true);
129        $this->assertTrue(isset($controller));
130        ob_start();
131        $controller->control();
132        $results = ob_get_contents();
133        ob_end_clean();
134        $json = substr($results, 29, strrpos($results, ';') - 30);
135        $ob = json_decode( $json );
136        $this->assertEqual($ob->status, 'success');
137        $this->assertEqual(count($ob->posts), 2);
138    }
139
140    public function testOwnerWithAccessTweetsAllMaxNoLimit() {
141        $builders = $this->buildData();
142        GridController::$MAX_ROWS = 0;
143        $this->simulateLogin('me@example.com');
144        $_GET['u'] = 'someuser1';
145        $_GET['n'] = 'twitter';
146        $_GET['d'] = 'tweets-all';
147        $_GET['nolimit'] = '1';
148        $controller = new GridController(true);
149        $this->assertTrue(isset($controller));
150        ob_start();
151        $controller->control();
152        $results = ob_get_contents();
153        ob_end_clean();
154        $json = substr($results, 29, strrpos($results, ';') - 30);
155        $ob = json_decode( $json );
156        $this->assertEqual($ob->status, 'success');
157        $this->assertEqual(count($ob->posts), 3);
158    }
159
160    public function testReplyToSearch() {
161        $builders = $this->buildData(0,0);
162        $this->simulateLogin('me@example.com');
163        $_GET['u'] = 'someuser1';
164        $_GET['n'] = 'twitter';
165        $_GET['t'] = '10765432100123456781';
166        $controller = new GridController(true);
167        $this->assertTrue(isset($controller));
168        ob_start();
169        $controller->control();
170        $results = ob_get_contents();
171        ob_end_clean();
172        $json = substr($results, 29, strrpos($results, ';') - 30);
173        $ob = json_decode( $json );
174        $this->assertEqual($ob->status, 'success');
175        $this->assertEqual(count($ob->posts), 2);
176        $this->assertEqual($ob->posts[0]->text, 'Reply to a post');
177        $this->assertEqual($ob->posts[0]->post_id_str, '10765432100123456783_str');
178
179    }
180
181    public function testReplyToSearchNotLoggedIn() {
182        $builders = $this->buildData();
183        //$this->simulateLogin('me@example.com');
184        $_GET['u'] = 'someuser1';
185        $_GET['n'] = 'twitter';
186        $_GET['t'] = '10765432100123456781';
187
188        // a private instance
189        $controller = new GridController(true);
190        try {
191            $controller->control();
192            $this->fail("should throw auth exception");
193        } catch(Exception $e) {
194            $this->assertPattern('/You must.*log in/',$e->getMessage());
195        }
196
197        // public instance, but protected posts
198        $builders = null;
199        $public = 1;
200        $builders = $this->buildData($public);
201        $this->assertTrue(isset($controller));
202        ob_start();
203        $controller->control();
204        $results = ob_get_contents();
205        ob_end_clean();
206        $json = substr($results, 29, strrpos($results, ';') - 30);
207        $ob = json_decode( $json );
208        $this->assertEqual($ob->status, 'success');
209        $this->assertEqual(count($ob->posts), 1);
210
211        // public instance, and not protected posts
212        $builders = null;
213        $public = 1;
214        $protected = 0;
215        $builders = $this->buildData($public, $protected);
216        $this->assertTrue(isset($controller));
217        ob_start();
218        $controller->control();
219        $results = ob_get_contents();
220        ob_end_clean();
221        $json = substr($results, 29, strrpos($results, ';') - 30);
222        $ob = json_decode( $json );
223        $this->assertEqual($ob->status, 'success');
224        $this->assertEqual(count($ob->posts), 2);
225        $this->assertEqual($ob->posts[0]->text, 'Reply to a post');
226        $this->assertEqual($ob->posts[0]->post_id_str, '10765432100123456783_str');
227
228    }
229
230    public function testReplyToSearchFilterOutProtected() {
231        $builders = $this->buildData();
232        $this->simulateLogin('me@example.com');
233        $_GET['u'] = 'someuser1';
234        $_GET['n'] = 'twitter';
235        $_GET['t'] = '10765432100123456781';
236        $controller = new GridController(true);
237        $this->assertTrue(isset($controller));
238        ob_start();
239        $controller->control();
240        $results = ob_get_contents();
241        ob_end_clean();
242        $json = substr($results, 29, strrpos($results, ';') - 30);
243        $ob = json_decode( $json );
244        $this->assertEqual($ob->status, 'success');
245        $this->assertEqual(count($ob->posts), 1);
246    }
247
248    public function testNoProfilerOutput() {
249        // Enable profiler
250        $config = Config::getInstance();
251        $config->setValue('enable_profiler', true);
252        $_SERVER['HTTP_HOST'] = 'something';
253
254        $builders = $this->buildData();
255        $this->simulateLogin('me@example.com');
256        $_GET['u'] = 'someuser1';
257        $_GET['n'] = 'twitter';
258        $_GET['d'] = 'tweets-all';
259        $controller = new GridController(true);
260        $this->assertTrue(isset($controller));
261
262        ob_start();
263        $results = $controller->go();
264        $results .= ob_get_contents();
265        ob_end_clean();
266        $json = substr($results, 29, strrpos($results, ';') - 30);
267        $ob = json_decode($json);
268        // If the profiler outputs HTML (it shouldn't), the following will fail
269        $this->assertIsA($ob, 'stdClass');
270        unset($_SERVER['HTTP_HOST']);
271    }
272
273    private function buildData($is_public = 0, $is_protected = 1) {
274        $owner_builder = FixtureBuilder::build('owners', array('id'=>1, 'email'=>'me@example.com'));
275
276        $user_builder = FixtureBuilder::build('users', array('user_id'=>123, 'user_name'=>'someuser1',
277        'network'=>'twitter'));
278
279        $user_builder2 = FixtureBuilder::build('users', array('user_id'=>1234, 'user_name'=>'someuser2',
280        'network'=>'twitter'));
281
282        $instance_builder = FixtureBuilder::build('instances', array('id'=>1, 'network_username'=>'someuser1',
283        'network'=>'twitter', 'network_user_id' => 123, 'is_public' => $is_public));
284
285        $instance_builder2 = FixtureBuilder::build('instances', array('id'=>2, 'network_username'=>'someuser2',
286        'network'=>'twitter'));
287
288        $owner_instance_builder = FixtureBuilder::build('owner_instances', array('instance_id'=>1, 'owner_id'=>1));
289
290        $posts1_builder = FixtureBuilder::build('posts', array('author_username'=>'someuser1','author_user_id' => 123,
291        'post_text'=>'@someuser1 My first post', 'network'=>'twitter', 'post_id' => '10765432100123456781'));
292
293        $posts2_builder = FixtureBuilder::build('posts', array('author_username'=>'someuser1','author_user_id' => 123,
294        'post_text'=>'My second @someuser1 post', 'network'=>'twitter', 'post_id' => '10765432100123456782'));
295
296        $reply_builder = FixtureBuilder::build('posts', array('post_id' => '10765432100123456783',
297        'author_username'=>'reply_user', 'post_text'=>'Reply to a post', 'network'=>'twitter',
298        'in_reply_to_post_id' => '10765432100123456781', 'author_user_id'=>'1234','is_protected' => $is_protected));
299
300        return array($owner_builder, $instance_builder, $owner_instance_builder, $posts1_builder,
301        $posts2_builder, $user_builder, $user_builder2, $instance_builder2, $reply_builder);
302    }
303}