public function testMandrillHTMLWithExceptions()
 {
     unlink(FileDataManager::getDataPath(Mailer::EMAIL));
     $plugin = new InsightsGeneratorPlugin();
     $config = Config::getInstance();
     $config->setValue('mandrill_api_key', '1234');
     $plugin_dao = DAOFactory::getDAO('PluginDAO');
     $plugin_id = $plugin_dao->getPluginId($plugin->folder_name);
     $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
     $plugin_option_dao->insertOption($plugin_id, 'mandrill_template', $template = 'my_template');
     $long_ago = date('Y-m-d', strtotime('last year'));
     // When in test mode, the mailHTMLViaMandrill method will throw a Template Not Found exception
     // if the email address contains "templateerror".
     $builders = array();
     $builders[] = FixtureBuilder::build('owners', array('id' => 1, 'full_name' => 'ThinkUp Q. User', 'is_admin' => 1, 'email' => '*****@*****.**', 'is_activated' => 1, 'email_notification_frequency' => 'daily'));
     $builders[] = FixtureBuilder::build('instances', array('network_username' => 'cdmoyer', 'id' => 6, 'network' => 'twitter', 'is_activated' => 1, 'is_public' => 1));
     $builders[] = FixtureBuilder::build('owner_instances', array('owner_id' => 1, 'instance_id' => 6, 'id' => 1));
     $builders[] = FixtureBuilder::build('insights', array('id' => 2, 'instance_id' => 6, 'slug' => 'new_group_memberships', 'prefix' => 'Made the List:', 'text' => 'Joe Test is on 1234 new lists', 'time_generated' => date('Y-m-d 03:00:00', strtotime('1am'))));
     $this->simulateLogin('*****@*****.**');
     $plugin->current_timestamp = strtotime('5pm');
     $exception = null;
     try {
         $plugin->crawl();
     } catch (Exception $e) {
         $exception = $e;
     }
     $this->assertNull($e, 'Should not get Mandrill template error');
     $sent = Mailer::getLastMail();
     //Sent plain text email
     $this->assertNotEqual($sent, '');
     $decoded = json_decode($sent);
     //Not HTML email via JSON
     $this->assertNull($decoded->global_merge_vars);
     //Check user got a log message
     $config = Config::getInstance();
     $logger_file = $config->getValue('log_location');
     $log = file($logger_file);
     $last_log = join("\n", array_slice($log, -10));
     $this->assertPattern('/invalid mandrill template/i', $last_log);
 }
 public function testNoTimezoneHandling()
 {
     $tz = date_default_timezone_get();
     unlink(FileDataManager::getDataPath(Mailer::EMAIL));
     $builders = array();
     $builders[] = FixtureBuilder::build('owners', array('id' => 1, 'full_name' => 'ThinkUp J. User', 'is_admin' => 1, 'email' => '*****@*****.**', 'is_activated' => 1, 'email_notification_frequency' => 'daily', 'timezone' => ''));
     $builders[] = FixtureBuilder::build('owner_instances', array('owner_id' => 1, 'instance_id' => 5, 'auth_error' => ''));
     $builders[] = FixtureBuilder::build('instances', array('network_username' => 'cdmoyer', 'id' => 5, 'network' => 'twitter', 'is_activated' => 1, 'is_public' => 1));
     $builders[] = FixtureBuilder::build('insights', array('id' => 1, 'instance_id' => 5, 'slug' => 'new_group_memberships', 'prefix' => 'Made the List:', 'text' => 'CDMoyer is on 29 new lists', 'time_generated' => date('Y-m-d 03:00:00')));
     $builders[] = FixtureBuilder::build('insights', array('id' => 2, 'instance_id' => 5, 'slug' => 'new_group_memberships', 'prefix' => 'Made the List:', 'text' => 'CDMoyer is on 99 new lists', 'time_generated' => date('Y-m-d 01:00:00')));
     $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
     $options = $plugin_option_dao->getOptionsHash($plugin->folder_name, true);
     $this->assertEqual(count($options), 0);
     $config = Config::getInstance();
     date_default_timezone_set($config->getValue('timezone'));
     $this->simulateLogin('*****@*****.**');
     $plugin = new InsightsGeneratorPlugin();
     $plugin->current_timestamp = strtotime('3am');
     // Should not set yet
     $plugin->crawl();
     $sent = Mailer::getLastMail();
     $this->assertEqual('', $sent);
     $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
     $options = $plugin_option_dao->getOptionsHash($plugin->folder_name, true);
     $this->assertEqual(count($options), 0);
     $plugin->current_timestamp = strtotime('5am');
     // SHould send
     $plugin->crawl();
     $sent = Mailer::getLastMail();
     $this->assertNotEqual('', $sent);
     $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
     $options = $plugin_option_dao->getOptionsHash($plugin->folder_name, true);
     $this->assertTrue(count($options) > 0);
     date_default_timezone_set($tz);
 }
 public function testHandlingLastSentTime()
 {
     $tz = date_default_timezone_get();
     unlink(FileDataManager::getDataPath(Mailer::EMAIL));
     $builders = array();
     $builders[] = FixtureBuilder::build('owners', array('id' => 1, 'full_name' => 'ThinkUp J. User', 'is_admin' => 1, 'email' => '*****@*****.**', 'is_activated' => 1, 'email_notification_frequency' => 'daily', 'timezone' => 'America/New_York'));
     $builders[] = FixtureBuilder::build('owner_instances', array('owner_id' => 1, 'instance_id' => 5, 'auth_error' => ''));
     $builders[] = FixtureBuilder::build('instances', array('network_username' => 'cdmoyer', 'id' => 5, 'network' => 'twitter', 'is_activated' => 1, 'is_public' => 1));
     $builders[] = FixtureBuilder::build('insights', array('id' => 1, 'instance_id' => 5, 'slug' => 'new_group_memberships', 'headline' => 'Made the List:', 'related_data' => null, 'text' => 'CDMoyer is on 29 new lists', 'time_generated' => date('Y-m-d 4:00', strtotime('-49 hour'))));
     $builders[] = FixtureBuilder::build('insights', array('id' => 2, 'instance_id' => 5, 'slug' => 'new_group_memberships', 'headline' => 'Made the List:', 'related_data' => null, 'text' => 'CDMoyer is on 99 new lists', 'time_generated' => date('Y-m-d H:00:00', strtotime('-45 hour'))));
     $builders[] = FixtureBuilder::build('insights', array('id' => 3, 'instance_id' => 5, 'slug' => 'new_group_memberships', 'headline' => 'Made the List:', 'related_data' => null, 'text' => 'CDMoyer is on 42 new lists', 'time_generated' => date('Y-m-d H:00:00', strtotime('-42 hour'))));
     $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
     $options = $plugin_option_dao->getOptionsHash($plugin->folder_name, true);
     $this->assertEqual(count($options), 0);
     $config = Config::getInstance();
     //date_default_timezone_set($config->getValue('timezone'));
     $this->simulateLogin('*****@*****.**');
     $plugin = new InsightsGeneratorPlugin();
     $plugin->current_timestamp = strtotime('Yesterday 4am');
     // Should send
     $plugin->crawl();
     $sent = Mailer::getLastMail();
     $this->assertNoPattern('/29 new lists/', $sent);
     unlink(FileDataManager::getDataPath(Mailer::EMAIL));
     $plugin->current_timestamp = strtotime('4am');
     // Should send
     $plugin_dao = DAOFactory::getDAO('PluginDAO');
     $plugin_id = $plugin_dao->getPluginId($plugin->folder_name);
     $options = $plugin_option_dao->getOptionsHash($plugin->folder_name, true);
     $plugin_option_dao->updateOption($options['last_daily_email']->id, 'last_daily_email', date('Y-m-d H:00:00', strtotime('-43 hour')));
     $options = $plugin_option_dao->getOptionsHash($plugin->folder_name, true);
     $plugin->crawl();
     $sent = Mailer::getLastMail();
     $this->assertNoPattern('/29 new lists/', $sent);
     $this->assertNoPattern('/99 new lists/', $sent);
     $this->assertPattern('/42 new lists/', $sent);
 }