diff --git a/tests/TestOfMailer.php b/tests/TestOfMailer.php index b02e837ef1..8aba437d7c 100644 --- a/tests/TestOfMailer.php +++ b/tests/TestOfMailer.php @@ -29,7 +29,7 @@ require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php'; require_once THINKUP_WEBAPP_PATH.'config.inc.php'; -class TestOfMailer extends ThinkUpBasicUnitTestCase { +class TestOfMailer extends ThinkUpUnitTestCase { public function setUp() { parent::setUp(); @@ -59,12 +59,24 @@ public function testFromName() { $email_body); $config->setValue("app_title_prefix", "My Other Installation of "); - $_SERVER['HTTP_HOST'] = "my_other_hostname"; + $_SERVER['HTTP_HOST'] = null; + $_SERVER['SERVER_NAME'] = "my_other_hostname"; Mailer::mail('you@example.com', 'Testing 123', 'Me worky, yo?'); $email_body = Mailer::getLastMail(); $this->debug($email_body); $this->assertPattern('/From: "My Other Installation of ThinkUp" /', $email_body); + + $config->setValue("app_title_prefix", "Yet Another Installation of "); + $_SERVER['HTTP_HOST'] = null; + $_SERVER['SERVER_NAME'] = null; + $builder = FixtureBuilder::build('options', array('namespace'=>'application_options', + 'option_name'=>'server_name', 'option_value'=>'testservername') ); + Mailer::mail('you@example.com', 'Testing 123', 'Me worky, yo?'); + $email_body = Mailer::getLastMail(); + $this->debug($email_body); + $this->assertPattern('/From: "Yet Another Installation of ThinkUp" /', + $email_body); } public function testMandrill() { diff --git a/tests/TestOfUtils.php b/tests/TestOfUtils.php index b014232584..76f730da5c 100755 --- a/tests/TestOfUtils.php +++ b/tests/TestOfUtils.php @@ -218,6 +218,29 @@ public function testGetSiteRootPathFromFileSystem() { $this->assertEqual($filesystem_site_root_path, $cfg_site_root_path); } + public function testGetApplicationHostName() { + //no $_SERVER vars set, but with application setting set + $builder = FixtureBuilder::build('options', array('namespace'=>'application_options', + 'option_name'=>'server_name', 'option_value'=>'testservername') ); + $host_name = Utils::getApplicationHostName(); + $expected_host_name = 'testservername'; + $this->assertEqual($host_name, $expected_host_name); + + //SERVER_NAME, not HTTP_HOST + $_SERVER['HTTP_HOST'] = null; + $_SERVER['SERVER_NAME'] = 'mytestservername'; + $host_name = Utils::getApplicationHostName(); + $expected_host_name = 'mytestservername'; + $this->assertEqual($host_name, $expected_host_name); + + //HTTP_HOST, not SERVER_NAME + $_SERVER['HTTP_HOST'] = 'myothertestservername'; + $_SERVER['SERVER_NAME'] = null; + $host_name = Utils::getApplicationHostName(); + $expected_host_name = 'myothertestservername'; + $this->assertEqual($host_name, $expected_host_name); + } + public function testGetApplicationURL() { $cfg = Config::getInstance(); $cfg->setValue('site_root_path', '/my/path/to/thinkup/'); diff --git a/webapp/_lib/class.Mailer.php b/webapp/_lib/class.Mailer.php index d994caab06..a81c299d94 100644 --- a/webapp/_lib/class.Mailer.php +++ b/webapp/_lib/class.Mailer.php @@ -59,7 +59,7 @@ public static function mail($to, $subject, $message) { */ public static function mailHTMLViaMandrillTemplate($to, $subject, $template_name, $template_params) { $config = Config::getInstance(); - $host = self::getHost(); + $host = Utils::getApplicationHostName(); $app_title = $config->getValue('app_title_prefix'). "ThinkUp"; $mandrill_api_key = $config->getValue('mandrill_api_key'); @@ -92,19 +92,8 @@ public static function mailHTMLViaMandrillTemplate($to, $subject, $template_name // We want to be able to handle this specific error differently. throw $unknown_template_error; } catch (Mandrill_Error $e) { - throw new Exception('An error occurred while sending email via Mandrill. ' . get_class($e) . - ': ' . $e->getMessage()); - } - } - /** - * Return the current host's name, ie, $_SERVER['HTTP_HOST'] if it is set. - * @return str Host name - */ - private static function getHost() { - if (isset($_SERVER['HTTP_HOST'])) { - return $_SERVER['HTTP_HOST']; - } else { - return ""; + throw new Exception('An error occurred while sending email to '.$to.' from '.$from.' via Mandrill. ' + . get_class($e) . ': ' . $e->getMessage()); } } /** @@ -142,7 +131,7 @@ public static function mailViaPHP($to, $subject, $message) { $config = Config::getInstance(); $app_title = $config->getValue('app_title_prefix'). "ThinkUp"; - $host = self::getHost(); + $host = Utils::getApplicationHostName(); $mail_header = "From: \"{$app_title}\" \r\n"; $mail_header .= "X-Mailer: PHP/".phpversion(); @@ -168,7 +157,7 @@ public static function mailViaMandrill($to, $subject, $message) { $config = Config::getInstance(); $app_title = $config->getValue('app_title_prefix') . "ThinkUp"; - $host = self::getHost(); + $host = Utils::getApplicationHostName(); $mandrill_api_key = $config->getValue('mandrill_api_key'); try { diff --git a/webapp/_lib/class.Utils.php b/webapp/_lib/class.Utils.php index 793ae914ab..c9d9dde158 100644 --- a/webapp/_lib/class.Utils.php +++ b/webapp/_lib/class.Utils.php @@ -294,19 +294,18 @@ public static function getSiteRootPathFromFileSystem() { } /** - * Get the application's full URL, i.e., https://example.com/thinkup/ - * @param $replace_localhost_with_ip Default to false - * @return str + * Get the application's host name or server name, i.e., example.com. + * @return str Host name either set by PHP global vars or stored in the database */ - public static function getApplicationURL($replace_localhost_with_ip = false) { + public static function getApplicationHostName() { //First attempt to get the host name without querying the database //Try SERVER_NAME $server = empty($_SERVER['SERVER_NAME']) ? '' : $_SERVER['SERVER_NAME']; - //Try HTTP_HOST + //Second, try HTTP_HOST if ($server == '' ) { $server = empty($_SERVER['HTTP_HOST']) ? '' : $_SERVER['HTTP_HOST']; } - //Then fall back to stored application setting set by Installer::storeServerName + //Finally fall back to stored application setting set by Installer::storeServerName if ($server == '') { $option_dao = DAOFactory::getDAO('OptionDAO'); $server_app_setting = $option_dao->getOptionByName(OptionDAO::APP_OPTIONS, 'server_name'); @@ -314,11 +313,21 @@ public static function getApplicationURL($replace_localhost_with_ip = false) { $server = $server_app_setting->option_value; } } + //domain name is always lowercase + $server = strtolower($server); + return $server; + } + + /** + * Get the application's full URL, i.e., https://example.com/thinkup/ + * @param $replace_localhost_with_ip Default to false + * @return str + */ + public static function getApplicationURL($replace_localhost_with_ip = false) { + $server = self::getApplicationHostName(); if ($replace_localhost_with_ip) { $server = ($server == 'localhost')?'127.0.0.1':$server; } - //domain name is always lowercase - $server = strtolower($server); $site_root_path = Config::getInstance()->getValue('site_root_path'); if (!isset($site_root_path)) { //config file not written yet (during install) $site_root_path = self::getSiteRootPathFromFileSystem();