public function testGetHoursSinceLastCrawlerRun()
 {
     $dao = new InstanceMySQLDAO();
     $instance_builder = FixtureBuilder::build('instances', array('crawler_last_run' => '-3h'));
     $hours = $dao->getHoursSinceLastCrawlerRun();
     $this->assertEqual($hours, 3);
     $instance1_builder = FixtureBuilder::build('instances', array('crawler_last_run' => '-2h'));
     $hours = $dao->getHoursSinceLastCrawlerRun();
     $this->assertEqual($hours, 2);
 }
 public function testPerOwnerRefreshRate()
 {
     // $THINKUP_CFG['rss_crawler_refresh_rate'] should apply per owner
     $builders = $this->buildData();
     $controller = new RSSController(true);
     $_GET['un'] = '*****@*****.**';
     $_GET['as'] = 'c9089f3c9adaf0186f6ffb1ee8d6501c';
     $results = $controller->go();
     $this->assertPattern("/ThinkUp crawl started/", $results);
     $instanceDAO = new InstanceMySQLDAO();
     $instanceDAO->updateLastRun(1);
     // the crawler should start for the second owner despite a recent last
     // run time for instance 1 owned by owner 1
     $controller = new RSSController(true);
     $_GET['un'] = '*****@*****.**';
     $_GET['as'] = 'a34e120dc6807e0dffc0d2b973b9d55b';
     $results = $controller->go();
     $this->assertPattern("/ThinkUp crawl started/", $results);
 }
 public function save($instance_object, $user_xml_total_posts_by_owner, $logger = false)
 {
     parent::save($instance_object, $user_xml_total_posts_by_owner, $logger);
     if ($this->doesMetaDataExist($instance_object->id)) {
         if ($logger) {
             $status_message = "Updated " . $instance_object->network_username . "'s Twitter instance status.";
             $logger->logUserSuccess($status_message, __METHOD__ . ',' . __LINE__);
         }
         return $this->updateMetaData($instance_object);
     } else {
         if ($logger) {
             $status_message = "Updated " . $instance_object->network_username . "'s Twitter instance status (meta inserted).";
             $logger->logUserSuccess($status_message, __METHOD__ . ',' . __LINE__);
         }
         $this->insertMetaData($instance_object);
         return 1;
     }
 }
 public function testGetHoursSinceLastCrawlerRun()
 {
     $dao = new InstanceMySQLDAO();
     //set all existing instances to inactive first
     $dao->setActive(1, 0);
     $dao->setActive(2, 0);
     $dao->setActive(3, 0);
     $dao->setActive(4, 0);
     $dao->setActive(5, 0);
     $builders[] = FixtureBuilder::build('instances', array('crawler_last_run' => '-3h', 'is_active' => 1));
     $hours = $dao->getHoursSinceLastCrawlerRun();
     $this->assertEqual($hours, 3);
     $builders[] = FixtureBuilder::build('instances', array('crawler_last_run' => '-2h', 'is_active' => 1));
     $hours = $dao->getHoursSinceLastCrawlerRun();
     $this->assertEqual($hours, 3);
     // test that it ignores inactive instances
     $builders[] = FixtureBuilder::build('instances', array('crawler_last_run' => '-1h', 'is_active' => 0));
     $hours = $dao->getHoursSinceLastCrawlerRun();
     $this->assertEqual($hours, 3);
 }
    public function testReconnectAccount()  {
        $owner_instance_dao = new OwnerInstanceMySQLDAO();
        $instance_dao = new InstanceMySQLDAO();
        $owner_dao = new OwnerMySQLDAO();

        $_GET['p'] = 'facebook';
        $_GET['perms'] = 'offline_access,read_stream,user_likes,user_location,user_website,read_friendlists';
        $_GET['selected_profiles'] = '606837591';
        $_GET['session'] = '{"session_key":"new-faux-access-token","uid":"606837591","expires":0,"secret":'.
        '"itsasecret","access_token":"new-faux-access-token","sig":"siggysigsig"}';

        $options_arry = $this->buildPluginOptions();
        $this->simulateLogin('*****@*****.**', true);
        $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
        $controller = new FacebookPluginConfigurationController($owner, 'facebook');
        $output = $controller->go();

        $v_mgr = $controller->getViewManager();
        $this->assertEqual($v_mgr->getTemplateDataItem('successmsg'), "Success! You've reconnected your Facebook ".
        "account.");

        $instance = $instance_dao->getByUserIdOnNetwork('606837591', 'facebook');
        $this->assertTrue(isset($instance));

        $oinstance = $owner_instance_dao->get($owner->id, $instance->id);
        $this->assertTrue(isset($oinstance));
        $this->assertEqual($oinstance->oauth_access_token, 'new-faux-access-token');
    }
 public function testConnectAccountThatAlreadyExists()
 {
     self::buildInstanceData();
     $owner_instance_dao = new OwnerInstanceMySQLDAO();
     $instance_dao = new InstanceMySQLDAO();
     $owner_dao = new OwnerMySQLDAO();
     $config = Config::getInstance();
     $config->setValue('site_root_path', '/');
     $_SERVER['SERVER_NAME'] = "srvr";
     SessionCache::put('facebook_auth_csrf', '123');
     $_GET['p'] = 'facebook';
     $_GET['code'] = '456';
     $_GET['state'] = '123';
     $options_arry = $this->buildPluginOptions();
     $this->simulateLogin('*****@*****.**', true);
     $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
     $controller = new FacebookPluginConfigurationController($owner, 'facebook');
     $output = $controller->go();
     $v_mgr = $controller->getViewManager();
     $msgs = $v_mgr->getTemplateDataItem('success_msgs');
     $this->assertEqual($msgs['user_add'], "Success! You've reconnected your Facebook account. To connect " . "a different account, log  out of Facebook in a different browser tab and try again.");
     $this->debug(Utils::varDumpToString($msgs));
     $instance = $instance_dao->getByUserIdOnNetwork('606837591', 'facebook');
     $this->assertNotNull($instance);
     $owner_instance = $owner_instance_dao->get($owner->id, $instance->id);
     $this->assertNotNull($owner_instance);
     $this->assertEqual($owner_instance->oauth_access_token, 'newfauxaccesstoken11234567890');
 }
 public function testGetOAuthTokens()
 {
     $builders = $this->buildPluginOptions();
     $config = Config::getInstance();
     $config->setValue('site_root_path', '/');
     $plugin_options_dao = DAOFactory::getDAO("PluginOptionDAO");
     PluginOptionMySQLDAO::$cached_options = array();
     $builders[] = FixtureBuilder::build('owners', array('email' => '*****@*****.**', 'user_activated' => 1));
     $this->simulateLogin('*****@*****.**');
     $owner_dao = DAOFactory::getDAO('OwnerDAO');
     $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
     $controller = new GooglePlusPluginConfigurationController($owner);
     $_GET['code'] = 'test-google-provided-code';
     $results = $controller->go();
     $v_mgr = $controller->getViewManager();
     $msgs = $v_mgr->getTemplateDataItem('success_msgs');
     $this->assertEqual($msgs['user_add'], 'Success! Your Google+ account has been added to ThinkUp.');
     $this->debug(Utils::varDumpToString($msgs));
     $owner_instance_dao = new OwnerInstanceMySQLDAO();
     $instance_dao = new InstanceMySQLDAO();
     $instance = $instance_dao->getByUserIdOnNetwork('113612142759476883204', 'google+');
     $this->assertNotNull($instance);
     //Instance created
     $owner_instance = $owner_instance_dao->get($owner->id, $instance->id);
     $this->assertNotNull($owner_instance);
     //Owner Instance created
     //OAuth tokens set
     $this->assertEqual($owner_instance->oauth_access_token, 'faux-access-token');
     $this->assertEqual($owner_instance->oauth_access_token_secret, 'faux-refresh-token');
 }
 public function testAccountWithAuthError()
 {
     self::buildInstanceData();
     $owner_instance_dao = new OwnerInstanceMySQLDAO();
     $instance_dao = new InstanceMySQLDAO();
     $owner_dao = new OwnerMySQLDAO();
     $config = Config::getInstance();
     $config->setValue('site_root_path', '/');
     $_SERVER['SERVER_NAME'] = "srvr";
     $options_array = $this->buildPluginOptions();
     $this->simulateLogin('*****@*****.**', true);
     $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
     $instance = $instance_dao->getByUserIdOnNetwork('606837591', 'facebook');
     $this->assertNotNull($instance);
     //assert there is an auth error
     $owner_instance = $owner_instance_dao->get($owner->id, $instance->id);
     $this->assertEqual($owner_instance->auth_error, 'Token has expired.');
     $controller = new FacebookPluginConfigurationController($owner, 'facebook');
     $output = $controller->go();
     $this->debug($output);
     $this->assertPattern('/Facebook connection expired/', $output);
 }
 public function testCrawlCompletion()
 {
     $this->debug(__METHOD__);
     $builders = array();
     //Add instances
     $instance_builder_1 = FixtureBuilder::build('instances', array('id' => 1, 'network_username' => 'julie', 'network' => 'twitter', 'crawler_last_run' => '-5d', 'is_activated' => '1', 'is_public' => '1'));
     $instance_builder_2 = FixtureBuilder::build('instances', array('id' => 2, 'network_username' => 'john', 'network' => 'twitter', 'crawler_last_run' => '-5d', 'is_activated' => '1', 'is_public' => '1'));
     $builders[] = FixtureBuilder::build('instances_twitter', array('id' => 1));
     $builders[] = FixtureBuilder::build('instances_twitter', array('id' => 2));
     //Add owner
     $builders[] = FixtureBuilder::build('owners', array('id' => 1, 'full_name' => 'ThinkUp J. User', 'email' => '*****@*****.**', 'is_activated' => 1, 'is_admin' => 1));
     $builders[] = FixtureBuilder::build('owner_instances', array('owner_id' => 1, 'instance_id' => 1, 'auth_error' => ''));
     $builders[] = FixtureBuilder::build('owner_instances', array('owner_id' => 1, 'instance_id' => 2, 'auth_error' => ''));
     $this->simulateLogin('*****@*****.**', true, true);
     $test = new TwitterInstanceMySQLDAO();
     $twitter_plugin = new TwitterPlugin();
     $twitter_plugin->crawl();
     $instance_dao = new InstanceMySQLDAO();
     $updated_instance = $instance_dao->get(1);
     $this->debug(Utils::varDumpToString($updated_instance));
     // crawler_last_run should have been updated
     $this->assertNotEqual($instance_builder_1->columns['crawler_last_run'], $updated_instance->crawler_last_run);
 }
 public function testGetOAuthTokens()
 {
     // Set the plugin options
     $builders = $this->buildPluginOptions();
     // Get an instance
     $config = Config::getInstance();
     // Set the root path
     $config->setValue('site_root_path', '/');
     // Get a plugin options DAO
     $plugin_options_dao = DAOFactory::getDAO("PluginOptionDAO");
     //
     PluginOptionMySQLDAO::$cached_options = array();
     // Build an owner
     $builders[] = FixtureBuilder::build('owners', array('email' => '*****@*****.**', 'user_activated' => 1));
     $instance_builder = FixtureBuilder::build('instances', array('id' => 1, 'network_user_id' => '18127856', 'network_username' => '*****@*****.**', 'network' => 'foursquare', 'is_active' => 1));
     //Add owner instance_owner
     $owner_instance_builder = FixtureBuilder::build('owner_instances', array('owner_id' => 1, 'instance_id' => 1, 'oauth_access_token' => 'secret'));
     // Log them in
     $this->simulateLogin('*****@*****.**');
     // Get an owner DAO
     $owner_dao = DAOFactory::getDAO('OwnerDAO');
     // Get the logged in owners email
     $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
     // Create a new config controller for the owner
     $controller = new FoursquarePluginConfigurationController($owner);
     // Set the code foursquare would return from a real request
     $_GET['code'] = '5dn';
     // Check we get the tokens and tell the user it was a sucess
     $results = $controller->go();
     $v_mgr = $controller->getViewManager();
     $msgs = $v_mgr->getTemplateDataItem('success_msgs');
     $this->assertEqual($msgs['user_add'], 'Success! Your foursquare account has been added to ThinkUp.');
     // Get an owner instance DAO
     $owner_instance_dao = new OwnerInstanceMySQLDAO();
     // Get a instance DAO
     $instance_dao = new InstanceMySQLDAO();
     // Check we created a foursquare instance
     $instance = $instance_dao->getByUserIdOnNetwork('18127856', 'foursquare');
     $this->assertNotNull($instance);
     // Check a owner instance for this user with the instance ID was created
     $owner_instance = $owner_instance_dao->get($owner->id, $instance->id);
     $this->assertNotNull($owner_instance);
     // OAuth tokens set
     $this->assertEqual($owner_instance->oauth_access_token, 'secret');
 }
 public function testDeleteExistingInstanceNoPrivileges()
 {
     //Not admin without access privs (set error messages)
     $builders = array();
     $builders[] = FixtureBuilder::build('instances', array('id' => 2, 'network_user_id' => 12, 'network_username' => 'tuinstance', 'network' => 'twitter'));
     $builders[] = FixtureBuilder::build('owner_instances', array('owner_id' => 2, 'instance_id' => 2, 'oauth_access_token' => 'xxx', 'oauth_access_token_secret' => 'yyy'));
     $instance_dao = new InstanceMySQLDAO();
     $owner_instance_dao = new OwnerInstanceMySQLDAO();
     //Should delete the owner instance but leave the instance alone
     $this->simulateLogin('*****@*****.**');
     $_POST['action'] = "delete";
     $_POST["instance_id"] = 2;
     $controller = new AccountConfigurationController(true);
     //before
     $instance = $instance_dao->get(2);
     $this->assertNotNull($instance);
     $owner_instances = $owner_instance_dao->getByInstance(2);
     $this->assertNotNull($owner_instances);
     $this->assertIsA($owner_instances, 'Array');
     $this->assertEqual(sizeof($owner_instances), 1);
     //process controller
     $controller->go();
     //instance should NOT be deleted
     $instance = $instance_dao->get(2);
     $this->assertNotNull($instance);
     //owner instance should NOT be deleted
     $owner_instances = $owner_instance_dao->getByInstance(2);
     $this->assertIsA($owner_instances, 'Array');
     $this->assertEqual(sizeof($owner_instances), 1);
     $v_mgr = $controller->getViewManager();
     $this->assertNull($v_mgr->getTemplateDataItem('successmsg'));
     $this->assertNotNull($v_mgr->getTemplateDataItem('errormsg'));
     $this->assertEqual($v_mgr->getTemplateDataItem('errormsg'), 'Insufficient privileges.');
 }
 public function testDeleteExistingInstanceWithTweetSearchWithPrivilegesWithOtherOwners()
 {
     $this->debug(__METHOD__);
     //Not admin with access privs, with other owners (delete owner instance and NOT instance)
     $builders = array();
     $builders[] = FixtureBuilder::build('instances', array('id' => 2, 'network_user_id' => 12, 'network_username' => 'tuinstance', 'network' => 'twitter'));
     $builders[] = FixtureBuilder::build('owner_instances', array('owner_id' => 1, 'instance_id' => 2, 'oauth_access_token' => 'xxx', 'oauth_access_token_secret' => 'yyy'));
     $builders[] = FixtureBuilder::build('owner_instances', array('owner_id' => 2, 'instance_id' => 2, 'oauth_access_token' => 'xxx', 'oauth_access_token_secret' => 'yyy'));
     $instance_dao = new InstanceMySQLDAO();
     $owner_instance_dao = new OwnerInstanceMySQLDAO();
     $instance_hashtag_dao = new InstanceHashtagMySQLDAO();
     //before builder
     $instance = $instance_dao->get(2);
     $this->assertNotNull($instance);
     $instances_hashtags = $instance_hashtag_dao->getByInstance($instance->id);
     $this->assertEqual(sizeof($instances_hashtags), 0);
     $builder = $this->buildHashtagData(2);
     //after builder
     $hashtag_dao = new HashtagMySQLDAO();
     $hashtagpost_dao = new HashtagPostMySQLDAO();
     $hashtags = $hashtagpost_dao->getHashtagsForPost(1, 'twitter');
     $this->assertEqual(sizeof($hashtags), 1);
     $hashtags = $hashtagpost_dao->getHashtagsForPost(2, 'twitter');
     $this->assertEqual(sizeof($hashtags), 1);
     //instances hashtags should be deleted
     $instances_hashtags = $instance_hashtag_dao->getByInstance(2);
     $this->assertEqual(sizeof($instances_hashtags), 2);
     //hashtag should be deleted
     $hashtag = $hashtag_dao->getHashtagByID(1);
     $this->assertEqual(sizeof($hashtag), 1);
     $hashtag = $hashtag_dao->getHashtagByID(2);
     $this->assertEqual(sizeof($hashtag), 1);
     //Should delete the owner instance but leave the instance alone
     $this->simulateLogin('*****@*****.**', false, true);
     $_POST['action'] = "Delete";
     $_POST["instance_id"] = 2;
     $_POST['csrf_token'] = parent::CSRF_TOKEN;
     $controller = new AccountConfigurationController(true);
     //before
     $instance = $instance_dao->get(2);
     $this->assertNotNull($instance);
     $owner_instances = $owner_instance_dao->getByInstance(2);
     $this->assertNotNull($owner_instances);
     $this->assertIsA($owner_instances, 'Array');
     $this->assertEqual(sizeof($owner_instances), 2);
     //process controller
     $controller->go();
     //hashtags posts should be deleted
     $hashtags = $hashtagpost_dao->getHashtagsForPost(1, 'twitter');
     $this->assertEqual(sizeof($hashtags), 1);
     $hashtags = $hashtagpost_dao->getHashtagsForPost(2, 'twitter');
     $this->assertEqual(sizeof($hashtags), 1);
     //instances hashtags should NOT be deleted
     $instances_hashtags = $instance_hashtag_dao->getByInstance(2);
     $this->assertEqual(sizeof($instances_hashtags), 2);
     //hashtag should NOT be deleted
     $hashtag = $hashtag_dao->getHashtagByID(1);
     $this->assertEqual(sizeof($hashtag), 1);
     $hashtag = $hashtag_dao->getHashtagByID(2);
     $this->assertEqual(sizeof($hashtag), 1);
     $this->debug('Still running');
     //instance should NOT be deleted
     $instance = $instance_dao->get(2);
     $this->assertNotNull($instance);
     //just one owner_instance should be deleted
     $owner_instances = $owner_instance_dao->getByInstance(2);
     $this->assertIsA($owner_instances, 'Array');
     $this->assertEqual(sizeof($owner_instances), 1);
     $v_mgr = $controller->getViewManager();
     $success_msgs = $v_mgr->getTemplateDataItem('success_msgs');
     $this->assertNotNull($success_msgs);
     $this->assertEqual($success_msgs['account'], 'Account deleted.');
     $this->assertNull($v_mgr->getTemplateDataItem('error_msg'));
 }