PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/webapp/plugins/twitter/tests/TestOfTwitterAuthController.php

http://github.com/ginatrapani/ThinkUp
PHP | 146 lines | 96 code | 17 blank | 33 comment | 1 complexity | 3844f6ffcb31eb09fcc72f5b89bf3d91 MD5 | raw file
  1<?php
  2/**
  3 *
  4 * ThinkUp/webapp/plugins/twitter/tests/TestOfTwitterAuthController.php
  5 *
  6 * Copyright (c) 2009-2012 Gina Trapani
  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 * Test of TwitterAuthController
 24 *
 25 * @license http://www.gnu.org/licenses/gpl.html
 26 * @copyright 2009-2012 Gina Trapani
 27 * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
 28 *
 29 */
 30require_once dirname(__FILE__) . '/../../../../tests/init.tests.php';
 31require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
 32require_once THINKUP_WEBAPP_PATH.'config.inc.php';
 33if (!class_exists('twitterOAuth')) {
 34    require_once THINKUP_WEBAPP_PATH.'plugins/twitter/tests/classes/mock.TwitterOAuth.php';
 35}
 36require_once THINKUP_WEBAPP_PATH.'plugins/twitter/model/class.TwitterOAuthThinkUp.php';
 37require_once THINKUP_WEBAPP_PATH.'plugins/twitter/model/class.TwitterAPIAccessorOAuth.php';
 38require_once THINKUP_WEBAPP_PATH.'plugins/twitter/model/class.TwitterPlugin.php';
 39require_once THINKUP_WEBAPP_PATH.'plugins/twitter/controller/class.TwitterAuthController.php';
 40
 41class TestOfTwitterAuthController extends ThinkUpUnitTestCase {
 42
 43    public function setUp(){
 44        parent::setUp();
 45    }
 46
 47    public function testConstructor() {
 48        $controller = new TwitterAuthController(true);
 49        $this->assertTrue(isset($controller));
 50    }
 51    //Test not logged in
 52    public function testNotLoggedIn() {
 53        $controller = new TwitterAuthController(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    //Test no params
 62    public function testLoggedInMissingParams() {
 63        $this->simulateLogin('me@example.com');
 64        $controller = new TwitterAuthController(true);
 65        $results = $controller->go();
 66
 67        $v_mgr = $controller->getViewManager();
 68        $this->assertEqual('Secret token not set.', $v_mgr->getTemplateDataItem('info_msg'), "Info msg set");
 69    }
 70
 71    //Test Session param but no Get param
 72    public function testLoggedInMissingToken() {
 73        $this->simulateLogin('me@example.com');
 74        SessionCache::put('oauth_request_token_secret', 'XXX');
 75        $controller = new TwitterAuthController(true);
 76        $results = $controller->go();
 77
 78        $v_mgr = $controller->getViewManager();
 79        $this->assertEqual('No OAuth token specified.', $v_mgr->getTemplateDataItem('info_msg'), "Info msg set");
 80    }
 81
 82    //Test Session param but no Get param
 83    public function testLoggedInMissingSessionWithGet() {
 84        $this->simulateLogin('me@example.com');
 85        $_GET['oauth_token'] = 'XXX';
 86        $controller = new TwitterAuthController(true);
 87        $results = $controller->go();
 88
 89        $v_mgr = $controller->getViewManager();
 90        $this->assertEqual('Secret token not set.', $v_mgr->getTemplateDataItem('info_msg'), "Info msg set");
 91    }
 92
 93    public function testLoggedInAllParams() {
 94        $this->simulateLogin('me@example.com');
 95        $_GET['oauth_token'] = 'XXX';
 96        SessionCache::put('oauth_request_token_secret', 'XXX');
 97
 98        $owner_builder = FixtureBuilder::build('owners', array('id'=>'10', 'email'=>'me@example.com'));
 99        $namespace = OptionDAO::PLUGIN_OPTIONS . '-1';
100        $plugn_opt_builder1 = FixtureBuilder::build('options', array('namespace'=>$namespace,
101        'option_name'=>'oauth_consumer_key', 'option_value'=>'XXX'));
102        $plugn_opt_builder2 = FixtureBuilder::build('options', array('namespace'=>$namespace,
103        'option_name'=>'oauth_consumer_secret', 'option_value'=>'YYY'));
104        $plugn_opt_builder3 = FixtureBuilder::build('options', array('namespace'=>$namespace,
105        'option_name'=>'num_twitter_errors', 'option_value'=>'5'));
106        $plugn_opt_builder4 = FixtureBuilder::build('options', array('namespace'=>$namespace,
107        'option_name'=>'max_api_calls_per_crawl', 'option_value'=>'350'));
108
109        $controller = new TwitterAuthController(true);
110        $results = $controller->go();
111
112        //sleep(100);
113        $v_mgr = $controller->getViewManager();
114        $this->assertEqual('Success! dougw on Twitter has been added to ThinkUp!',
115        $v_mgr->getTemplateDataItem('success_msg'));
116    }
117
118    public function testLoggedInAllParamsServiceUserExists() {
119        $this->simulateLogin('me@example.com');
120        $_GET['oauth_token'] = 'XXX';
121        SessionCache::put('oauth_request_token_secret', 'XXX');
122
123        $builders[] = FixtureBuilder::build('owners', array('id'=>'10', 'email'=>'me@example.com'));
124        $namespace = OptionDAO::PLUGIN_OPTIONS . '-1';
125        $builders[] = FixtureBuilder::build('options', array('namespace'=>$namespace,
126        'option_name'=>'oauth_consumer_key', 'option_value'=>'XXX'));
127        $builders[] = FixtureBuilder::build('options', array('namespace'=>$namespace,
128        'option_name'=>'oauth_consumer_secret', 'option_value'=>'YYY'));
129        $builders[] = FixtureBuilder::build('options', array('namespace'=>$namespace,
130        'option_name'=>'num_twitter_errors', 'option_value'=>'5'));
131        $builders[] = FixtureBuilder::build('options', array('namespace'=>$namespace,
132        'option_name'=>'max_api_calls_per_crawl', 'option_value'=>'350'));
133        $builders[] = FixtureBuilder::build('instances', array('network_user_id'=>'1401881',
134        'network_username'=>'dougw', 'network'=>'twitter'));
135        $builders[] = FixtureBuilder::build('instances_twitter', array('last_page_fetched_replies'=>1));
136        $builders[] = FixtureBuilder::build('owner_instances', array('instance_id'=>1, 'owner_id'=>10));
137
138        $controller = new TwitterAuthController(true);
139        $results = $controller->go();
140
141        $v_mgr = $controller->getViewManager();
142        $this->debug($results);
143        $this->assertEqual('dougw on Twitter is already set up in ThinkUp! To add a different Twitter account, log '.
144        'out of Twitter.com in your browser and authorize ThinkUp again.', $v_mgr->getTemplateDataItem('success_msg'));
145    }
146}