Skip to content

Commit

Permalink
Standardize test mode check
Browse files Browse the repository at this point in the history
Use a cookie rather than SESSION for web-based test flag
Use single Utils function to check test mode everywhere
Stop using global TESTS_RUNNING variable
  • Loading branch information
ginatrapani committed Feb 28, 2014
1 parent 9e59f87 commit 7404a1f
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 30 deletions.
6 changes: 3 additions & 3 deletions extras/dev/config/config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
$THINKUP_CFG['set_pdo_charset'] = false;

//TESTS OVERRIDE: Run against the tests database and use unpackaged developer /thinkup/webapp/ folder structure
if ((isset($_SESSION["MODE"]) && $_SESSION["MODE"] == "TESTS") && ! isset($_SESSION["RD_MODE"])
|| (getenv("MODE")=="TESTS" && ! getenv("RD_MODE")=="1")) {
if ((isset($_COOKIE['TU_MODE']) && $_COOKIE['TU_MODE']=='TESTS') && !isset($_SESSION["RD_MODE"])
|| (getenv("MODE")=="TESTS" && !getenv("RD_MODE")=="1")) {
// Full server path to /thinkup/ source code folder.
// $THINKUP_CFG['source_root_path'] = '/your-server-path-to/thinkup/';
// $THINKUP_CFG['db_user'] = 'your_test_database_username';
Expand All @@ -89,4 +89,4 @@
$THINKUP_CFG['db_user'] = 'your_ram_disk_test_database_username';
$THINKUP_CFG['db_password'] = 'your_ram_disk_test_database_password';
$THINKUP_CFG['db_name'] = $THINKUP_CFG['db_name'] . '_rd';
}
}
3 changes: 3 additions & 0 deletions tests/classes/class.ThinkUpBasicUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public function setUp() {
$crawler_plugin_registrar = PluginRegistrarCrawler::getInstance();
$this->DEBUG = (getenv('TEST_DEBUG')!==false) ? true : false;

ini_set('session.use_cookies', 0);
session_cache_limiter('');

self::isTestEnvironmentReady();
}

Expand Down
4 changes: 0 additions & 4 deletions tests/init.tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
define('THINKUP_WEBAPP_PATH', THINKUP_ROOT_PATH . 'webapp/');
}

if ( !defined('TESTS_RUNNING') ) {
define('TESTS_RUNNING', true);
}

//Register our lazy class loader
require_once THINKUP_WEBAPP_PATH.'_lib/class.Loader.php';

Expand Down
2 changes: 1 addition & 1 deletion webapp/_lib/class.Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function generate() {
*/
public function doesTextMatchImage() {
//if in test mode, assume check is good if user_code is set to 123456
if ((isset($_SESSION["MODE"]) && $_SESSION["MODE"] == "TESTS") || getenv("MODE")=="TESTS") {
if (Utils::isTest()) {
if (isset($_POST['user_code']) && $_POST['user_code'] == '123456') {
return true;
} else {
Expand Down
10 changes: 5 additions & 5 deletions webapp/_lib/class.Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static function getInstance() {
*/
public function checkVersion($ver = '') {
// when testing
if ( defined('TESTS_RUNNING') && TESTS_RUNNING && !empty($ver) ) {
if ( Utils::isTest() && !empty($ver) ) {
$version = $ver;
} else {
$version = PHP_VERSION;
Expand Down Expand Up @@ -207,7 +207,7 @@ public function checkDependency($libs = array()) {
$ret['ZipArchive'] = true;
}
// when testing
if ( defined('TESTS_RUNNING') && TESTS_RUNNING && !empty($libs) ) {
if ( Utils::isTest() && !empty($libs) ) {
$ret = $libs;
}
return $ret;
Expand Down Expand Up @@ -248,7 +248,7 @@ public function checkPermission($perms = array()) {
}

// when testing
if ( defined('TESTS_RUNNING') && TESTS_RUNNING && !empty($perms) ) {
if ( Utils::isTest() && !empty($perms) ) {
$ret = $perms;
}
return $ret;
Expand Down Expand Up @@ -304,7 +304,7 @@ public function checkStep1($pass = true) {
$writable_session_permission = $this->isSessionDirectoryWritable();

// when testing
if ( defined('TESTS_RUNNING') && TESTS_RUNNING && !empty($pass) ) {
if ( Utils::isTest() && !empty($pass) ) {
$ret = $pass;
} else {
$ret = ($version_compat && $lib_depends_ret && $writable_permission_ret && $writable_session_permission);
Expand Down Expand Up @@ -457,7 +457,7 @@ public function isThinkUpInstalled($config) {
// check version is met
$version_met = self::checkStep1();
// when testing
if ( defined('TESTS_RUNNING') && TESTS_RUNNING && !empty($pass) ) {
if ( Utils::isTest() && !empty($pass) ) {
$version_met = $pass;
}
if ( !$version_met ) {
Expand Down
3 changes: 1 addition & 2 deletions webapp/_lib/class.Reporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public static function reportVersion(Instance $instance) {
$referer_url .= "?u=".urlencode($instance->network_username)."&n=". urlencode($instance->network);
}

$in_test_mode = ((isset($_SESSION["MODE"]) && $_SESSION["MODE"] == "TESTS") || getenv("MODE")=="TESTS");
if (!$in_test_mode) { //only make live request if we're not running the test suite
if (!Utils::isTest()) { //only make live request if we're not running the test suite
//Make the cURL request
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $report_back_url);
Expand Down
2 changes: 1 addition & 1 deletion webapp/_lib/class.Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function completeLogin($owner) {
SessionCache::put('user_is_admin', $owner->is_admin);
// set a CSRF token
SessionCache::put('csrf_token', uniqid(mt_rand(), true));
if (isset($_SESSION["MODE"]) && $_SESSION["MODE"] == 'TESTS') {
if (Utils::isTest()) {
SessionCache::put('csrf_token', 'TEST_CSRF_TOKEN');
}
}
Expand Down
2 changes: 1 addition & 1 deletion webapp/_lib/class.Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,6 @@ public static function getLastSaturday($from_date = null ) {
* @return bool Whether in test mode
*/
public static function isTest() {
return (isset($_SESSION["MODE"]) && $_SESSION["MODE"] == "TESTS") || getenv("MODE")=="TESTS";
return (getenv("MODE")=="TESTS" || (isset($_COOKIE['TU_MODE']) && $_COOKIE['TU_MODE']=='TESTS'));
}
}
2 changes: 1 addition & 1 deletion webapp/_lib/controller/class.InstallerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ private function repairInstallation($to_repair) {

// check $THINKUP_CFG['repair'] is set to true
// bypass this security check when running tests
if ( defined('TESTS_RUNNING') && TESTS_RUNNING ) {
if ( Utils::isTest() ) {
$THINKUP_CFG['repair'] = true;
}
$this->installer->repairerIsDefined($THINKUP_CFG);
Expand Down
2 changes: 1 addition & 1 deletion webapp/config.sample.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
$THINKUP_CFG['set_pdo_charset'] = false;

//TESTS OVERRIDE: Assign variables below to use different settings during test builds
if ((isset($_SESSION["MODE"]) && $_SESSION["MODE"] == "TESTS") && ! isset($_SESSION["RD_MODE"])
if ((isset($_COOKIE['TU_MODE']) && $_COOKIE['TU_MODE']=='TESTS') && ! isset($_SESSION["RD_MODE"])
|| (getenv("MODE")=="TESTS" && ! getenv("RD_MODE")=="1")) {
// $THINKUP_CFG['source_root_path'] = '/your-server-path-to/thinkup/';
// $THINKUP_CFG['db_user'] = 'your_test_database_username';
Expand Down
8 changes: 4 additions & 4 deletions webapp/install/setmode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
*
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2011-2013 Gina Trapani
* @copyright 2011-2014 Gina Trapani
*/
session_start();

if (strtolower($_GET['m']) == "tests") {
putenv("MODE=TESTS");
$_SESSION["MODE"] = "TESTS";
setcookie('TU_MODE', 'TESTS', 0, '/');
echo "Set to tests mode";
} elseif (strtolower($_GET['m']) == "prod") {
putenv("MODE=PROD");
$_SESSION["MODE"] = "PROD";
setcookie('TU_MODE', 'PROD', 0, '/');
echo "Set to prod mode";
} else {
echo "Currently in ";
Expand Down
3 changes: 1 addition & 2 deletions webapp/plugins/insightsgenerator/insights/linkprompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public function generateInsight(Instance $instance, $last_week_of_posts, $number
public function shouldGenerateInsight($slug, Instance $instance, $insight_date=null,
$regenerate_existing_insight=false, $day_of_week=null, $count_last_week_of_posts=null,
$excluded_networks=null, $alternate_day=true) {
$in_test_mode = ((isset($_SESSION["MODE"]) && $_SESSION["MODE"] == "TESTS") || getenv("MODE") == "TESTS");
if ($in_test_mode) {
if (Utils::isTest()) {
return true;
} else {
return $alternate_day && parent::shouldGenerateInsight($slug, $instance, $insight_date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ public function shouldGenerateInsight($slug, Instance $instance, $insight_date=n
}

// Check whether testing
$in_test_mode = ((isset($_SESSION["MODE"]) && $_SESSION["MODE"] == "TESTS") || getenv("MODE") == "TESTS");
if ($in_test_mode) {
return ($run && $in_test_mode);
if (Utils::isTest()) {
return ($run && Utils::isTest());
}

// Check the day of the week (0 for Sunday through 6 for Saturday) on which the insight should run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public function authControl() {
/* Request tokens from twitter */
$token_array = $twitter_oauth->getRequestToken(Utils::getApplicationURL(true)."account/?p=twitter");

if (isset($token_array['oauth_token'])
|| (isset($_SESSION["MODE"]) && $_SESSION["MODE"] == "TESTS") || getenv("MODE")=="TESTS") { //testing
if (isset($token_array['oauth_token']) || Utils::isTest()) {
$token = $token_array['oauth_token'];
SessionCache::put('oauth_request_token_secret', $token_array['oauth_token_secret']);

Expand Down

0 comments on commit 7404a1f

Please sign in to comment.