Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Send insights emails at timezone-adjusted time. #1792

Closed
wants to merge 2 commits into from

2 participants

@cdmoyer

Based on the owner/admins TimeZone.

References #1790

@cdmoyer

Hmm. Maybe this will fix the tests. Didn't add a timezone to all the original fixtures, so it was falling back to the server time instead of america/new_york like my laptop and failing.

@ginatrapani
Owner

Excellent, thanks.

@ecucurella ecucurella referenced this pull request from a commit in ecucurella/ThinkUp
@cdmoyer cdmoyer Send insight notification emails in owner-specific timezone
Closes #1792, closes #1790
443aebd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 13, 2014
  1. @cdmoyer

    Send insights emails at timezone-adjusted time.

    cdmoyer authored
    Based on the owner/admins TimeZone.
Commits on Jan 14, 2014
  1. @cdmoyer
This page is out of date. Refresh to see the latest.
View
16 webapp/plugins/insightsgenerator/model/class.InsightsGeneratorPlugin.php
@@ -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);
View
126 webapp/plugins/insightsgenerator/tests/TestOfInsightsGeneratorPlugin.php
@@ -38,6 +38,7 @@ class TestOfInsightsGeneratorPlugin extends ThinkUpUnitTestCase {
public function setUp(){
parent::setUp();
+ date_default_timezone_set('America/New_York');
}
public function tearDown() {
@@ -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'=>'never@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'never', 'is_admin' => 0));
+ 'email'=>'never@example.com', '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,
@@ -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'=>'never@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'never'));
+ 'email'=>'never@example.com', '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,
@@ -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'=>'daily@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'daily'));
+ 'email'=>'daily@example.com', '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,
@@ -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'=>'weekly@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'weekly'));
+ 'email'=>'weekly@example.com', '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,
@@ -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'=>'both@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'both'));
+ 'email'=>'both@example.com', '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,
@@ -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'=>'admin@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'daily'));
+ 'email'=>'admin@example.com', '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'=>'normal@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'daily'));
$builders[] = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>5, 'id'=>1));
@@ -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'=>'admin@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'daily'));
+ 'email'=>'admin@example.com', '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));
@@ -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'=>'templateerror@example.com', 'is_activated'=>1, 'email_notification_frequency' => 'daily'));
+ 'email'=>'templateerror@example.com', '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));
@@ -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'=>'daily@example.com', '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('daily@example.com');
+ $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'=>'daily@example.com', '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('daily@example.com');
+ $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);
+ }
}
Something went wrong with that request. Please try again.