Skip to content

Commit

Permalink
Don't require developers to switch to the tests database manually
Browse files Browse the repository at this point in the history
Use an environment variable, and for integration tests, a $_SESSION variable, to get the config file to use test database values automatically
Closes ThinkUpLLC#463
  • Loading branch information
ginatrapani committed Mar 8, 2011
1 parent 908473c commit 8363336
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 5 deletions.
11 changes: 8 additions & 3 deletions tests/README.md
Expand Up @@ -12,18 +12,23 @@ clean, empty tests database.

## Running Tests

First, configure your test environment. Copy `tests/config.tests.sample.inc.php` to `tests/config.tests.inc.php` and
First, configure your test environment.

Copy `tests/config.tests.sample.inc.php` to `tests/config.tests.inc.php` and
set the appropriate values. You will need a clean, empty database to run your tests. By default, name it
`thinkup_tests` and set the `$TEST_DATABASE` config variable to that name.

In `webapp/config.inc.php`, in the DEVELOPER CONFIG section, set the name of your tests database, and the username and
password to access it. This database name should match the one you just set in `tests/config.tests.inc.php`.

### Test Assumptions

In order for the tests to pass, you must:

* Have a `tests/config.tests.inc.php` file with the correct values set
* Set the crawler log file in `webapp/config.inc.php` and make that file writable
* Set the database name to an empty tests database which the tests will destroy each run in `webapp/config.inc.php`
* Set the database user to a user with all privileges in the test database and global CREATE, DROP, and FILE privs
* Set the test database name to an empty tests database which the tests will destroy each run in `webapp/config.inc.php`
* Set the test database user to a user with all privileges in the test database and global CREATE, DROP, and FILE privs
* Set caching to false in `webapp/config.inc.php`
* Have a local installation of ThinkUp using your test database
* Have a working internet connection
Expand Down
14 changes: 13 additions & 1 deletion tests/WebTestOfUpgradeDatabase.php
Expand Up @@ -135,13 +135,25 @@ private function setUpApp($version, $MIGRATIONS) {
$this->assertFalse(file_exists($THINKUP_CFG['source_root_path'].
'webapp/test_installer/thinkup/config.inc.php'));

//Set test mode
$this->get($this->url.'/test_installer/thinkup/install/setmode.php?m=tests');
//Include config again to get test db credentials
require THINKUP_WEBAPP_PATH.'config.inc.php';
//$this->showText();

//Start installation process
$this->get($this->url.'/test_installer/thinkup/');
$this->assertTitle("ThinkUp");
$this->assertText('ThinkUp\'s configuration file does not exist! Try installing ThinkUp.');
$this->clickLink("installing ThinkUp.");
$this->assertText('Great! Your system has everything it needs to run ThinkUp. You may proceed to the next '.
'step.');

//Set test mode
putenv("MODE=TESTS");
//Include config again to get test db credentials
require THINKUP_WEBAPP_PATH.'config.inc.php';

$this->get('index.php?step=2');
$this->assertText('Create Your ThinkUp Account');

Expand All @@ -152,7 +164,7 @@ private function setUpApp($version, $MIGRATIONS) {
$this->setField('timezone', 'America/Los_Angeles');

$this->setField('db_host', $THINKUP_CFG['db_host']);
$this->setField('db_name', $this->test_database_name);
$this->setField('db_name', $THINKUP_CFG['db_name']);
$this->setField('db_user', $THINKUP_CFG['db_user']);
$this->setField('db_passwd', $THINKUP_CFG['db_password']);
$this->setField('db_socket', $THINKUP_CFG['db_socket']);
Expand Down
7 changes: 6 additions & 1 deletion tests/classes/class.ThinkUpBasicWebTestCase.php
Expand Up @@ -23,7 +23,7 @@
*
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2010 Gina Trapani
* @copyright 2009-2011 Gina Trapani
*/
class ThinkUpBasicWebTestCase extends WebTestCase {
/**
Expand All @@ -42,13 +42,18 @@ public function setUp() {
$this->url = $TEST_SERVER_DOMAIN;
$this->DEBUG = (getenv('TEST_DEBUG')!==false) ? true : false;

putenv("MODE=TESTS");
$this->get($this->url.'/install/setmode.php?m=tests');

self::isWebTestEnvironmentReady();

require THINKUP_ROOT_PATH.'tests/config.tests.inc.php';
$this->test_database_name = $TEST_DATABASE;
}

public function tearDown() {
putenv("MODE=PROD");
$this->get($this->url.'/install/setmode.php?m=prod');
}

public function debug($message) {
Expand Down
1 change: 1 addition & 0 deletions tests/init.tests.php
Expand Up @@ -24,6 +24,7 @@
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2010 Gina Trapani
*/
putenv("MODE=TESTS");
require_once 'config.tests.inc.php';

//set up 3 required constants
Expand Down
7 changes: 7 additions & 0 deletions webapp/config.sample.inc.php
Expand Up @@ -56,3 +56,10 @@
// Set this to true if you want your PDO object's database connection's charset to be explicitly set to utf8.
// If false (or unset), the database connection's charset will not be explicitly set.
$THINKUP_CFG['set_pdo_charset'] = false;

//Test database override: Set this to run tests against the tests database
if ((isset($_SESSION["MODE"]) && $_SESSION["MODE"] == "TESTS") || getenv("MODE")=="TESTS") {
$THINKUP_CFG['db_user'] = 'your_test_database_username';
$THINKUP_CFG['db_password'] = 'your_test_database_password';
$THINKUP_CFG['db_name'] = 'your_test_database_name'; //by default, thinkup_tests
}
45 changes: 45 additions & 0 deletions webapp/install/setmode.php
@@ -0,0 +1,45 @@
<?php
/**
*
* ThinkUp/webapp/install/setmode.php
*
* Copyright (c) 2011 Gina Trapani
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2011 Gina Trapani
*/
session_start();
if (strtolower($_GET['m']) == "tests") {
putenv("MODE=TESTS");
$_SESSION["MODE"] = "TESTS";
echo "Set to tests mode";
} elseif (strtolower($_GET['m']) == "prod") {
putenv("MODE=PROD");
$_SESSION["MODE"] = "PROD";
echo "Set to prod mode";
} else {
echo "Currently in ";
if (isset($_SESSION["MODE"])) {
echo strtolower($_SESSION["MODE"]);
} else {
echo " prod ";
}
echo " mode";
}

0 comments on commit 8363336

Please sign in to comment.