Skip to content

Commit

Permalink
Send insight notification emails in owner-specific timezone
Browse files Browse the repository at this point in the history
Closes #1792, closes #1790
  • Loading branch information
cdmoyer authored and ginatrapani committed Jan 14, 2014
1 parent f822abd commit fb99d44
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,21 @@ public function crawl() {
}

// Send email digest the first run after 4am
if ((int)date('G', $this->current_timestamp) >= 4) {
$tz = $current_owner->timezone;
if (empty($tz)) {
$config = Config::getInstance();
$tz = $config->getValue('timezone');
}

if (!empty($tz)) {
$original_tz = date_default_timezone_get();
date_default_timezone_set($tz);
$localized_hour = (int)date('G', $this->current_timestamp);
date_default_timezone_set($original_tz);
} else {
$localize_hour = (int)date('G', $this->current_timestamp);
}
if ($localized_hour >= 4) {
//Get plugin options
$plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
$options = $plugin_option_dao->getOptionsHash($this->folder_name, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class TestOfInsightsGeneratorPlugin extends ThinkUpUnitTestCase {

public function setUp(){
parent::setUp();
date_default_timezone_set('America/New_York');
}

public function tearDown() {
Expand All @@ -48,7 +49,7 @@ public function testDontSendAsNonAdmin() {
unlink(FileDataManager::getDataPath(Mailer::EMAIL));
$builders = array();
$builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp J. User',
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'never', 'is_admin' => 0));
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'never', 'is_admin' => 0, '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,
Expand All @@ -70,7 +71,9 @@ public function testNeverSendSetting() {
unlink(FileDataManager::getDataPath(Mailer::EMAIL));
$builders = array();
$builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp J. User','is_admin'=>1,
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'never'));
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'never',
'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,
Expand Down Expand Up @@ -118,7 +121,8 @@ public function testDailySendSetting() {
unlink(FileDataManager::getDataPath(Mailer::EMAIL));
$builders = array();
$builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp J. User','is_admin'=>1,
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'daily'));
'email'=>'[email protected]', '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,
Expand Down Expand Up @@ -163,7 +167,8 @@ public function testWeeklySendSetting() {

$builders = array();
$builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp J. User','is_admin'=>1,
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'weekly'));
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'weekly',
'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,
Expand Down Expand Up @@ -215,7 +220,8 @@ public function testBothSendSetting() {

$builders = array();
$builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp J. User','is_admin'=>1,
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'both'));
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'both',
'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,
Expand Down Expand Up @@ -269,7 +275,8 @@ public function testMultiUser() {

$builders = array();
$builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp J. User','is_admin'=>1,
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'daily'));
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'daily',
'timezone' => 'America/New_York'));
$builders[] = FixtureBuilder::build('owners', array('id'=>2, 'full_name'=>'ThinkUp J. User','is_admin'=>0,
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'daily'));
$builders[] = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>5, 'id'=>1));
Expand Down Expand Up @@ -328,7 +335,8 @@ public function testMandrillHTML() {

$builders = array();
$builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp Q. User','is_admin'=>1,
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'daily'));
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'daily',
'timezone' => 'America/New_York'));
$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));
Expand Down Expand Up @@ -401,7 +409,8 @@ public function testMandrillHTMLWithExceptions() {
// if the email address contains "templateerror".
$builders = array();
$builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'ThinkUp Q. User','is_admin'=>1,
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'daily'));
'email'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'daily',
'timezone' => 'America/New_York'));
$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));
Expand Down Expand Up @@ -434,4 +443,105 @@ public function testMandrillHTMLWithExceptions() {
$last_log = join("\n", array_slice($log, -10));
$this->assertPattern('/invalid mandrill template/i', $last_log);
}

public function testTimezoneHandling() {
$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'=>'[email protected]', 'is_activated'=>1, 'email_notification_frequency' => 'daily',
'timezone' => 'America/Los_Angeles'));
$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);

date_default_timezone_set('America/New_York');
$this->simulateLogin('[email protected]');
$plugin = new InsightsGeneratorPlugin();
$plugin->current_timestamp = strtotime('5am'); // Should not yet be 4am in America/Los_Angeles of owner
$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);

date_default_timezone_set('America/Los_Angeles');
$plugin->current_timestamp = strtotime('3am'); // Still not time
$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);

date_default_timezone_set('America/Los_Angeles');
$plugin->current_timestamp = strtotime('5am'); // Should be time now.
$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 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'=>'[email protected]', '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('[email protected]');
$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);
}
}

0 comments on commit fb99d44

Please sign in to comment.