diff --git a/airtime_mvc/application/models/Preference.php b/airtime_mvc/application/models/Preference.php index 803a516fe..96ee08609 100644 --- a/airtime_mvc/application/models/Preference.php +++ b/airtime_mvc/application/models/Preference.php @@ -221,6 +221,7 @@ class Application_Model_Preference */ public static function SetShowsPopulatedUntil($dateTime) { + $dateTime->setTimezone(new DateTimeZone("UTC")); self::setValue("shows_populated_until", $dateTime->format("Y-m-d H:i:s")); } diff --git a/airtime_mvc/tests/README.txt b/airtime_mvc/tests/README.txt index 9489d7c6d..adc5beb83 100644 --- a/airtime_mvc/tests/README.txt +++ b/airtime_mvc/tests/README.txt @@ -19,4 +19,24 @@ DbUnit overwrites a file that's actually part of the PHPUnit package, with a version that's incompatible and gives an error for us. +2) Running the unit tests: + 1. To run all the unit tests, run: + + $ sudo ./runtests.sh + + (It has to be run as root to access the database for now.) + + 2. To run one specific test, you can do something like: + + $ export AIRTIME_UNIT_TEST="1" + $ sudo -E phpunit --filter testEditReatingShowInstance application/services/database/ShowServiceDbTest.php + + IMPORTANT: Make sure you use "sudo" with the "-E" flag so it preserves the environment variable we set before that. + + + +FAQ +==== +- If you get errors about an AMPQ fwrite failing, it means your RabbitMQ credentials are wrong in airtime.conf. + (That's the airtime.conf in this directory.) diff --git a/airtime_mvc/tests/TODO.txt b/airtime_mvc/tests/TODO.txt new file mode 100644 index 000000000..751b596d8 --- /dev/null +++ b/airtime_mvc/tests/TODO.txt @@ -0,0 +1,2 @@ +TODO: +- Set up a RabbitMQ airtime_test user that is hardcoded diff --git a/airtime_mvc/tests/airtime.conf b/airtime_mvc/tests/airtime.conf index 21275e80a..703880ba0 100755 --- a/airtime_mvc/tests/airtime.conf +++ b/airtime_mvc/tests/airtime.conf @@ -8,7 +8,7 @@ dbpass = airtime host = 127.0.0.1 port = 5672 user = airtime -password = GTUR1HVMU7GPIO3FETKY +password = GEN7GWIOB66FFKY30ERF vhost = /airtime [general] diff --git a/airtime_mvc/tests/application/bootstrap.php b/airtime_mvc/tests/application/bootstrap.php index 66f008f1b..55b1408da 100644 --- a/airtime_mvc/tests/application/bootstrap.php +++ b/airtime_mvc/tests/application/bootstrap.php @@ -59,6 +59,7 @@ if (file_exists('/usr/share/php/libzend-framework-php')) { require_once 'Zend/Application.php'; require_once 'Zend/Config.php'; +//require_once 'helpers/TestHelper.php'; require_once APPLICATION_PATH.'/configs/conf.php'; require_once 'propel/runtime/lib/Propel.php'; @@ -67,3 +68,5 @@ Propel::init("../application/configs/airtime-conf-production.php"); #require_once 'DatabaseTestCase.php'; require_once 'Zend/Session.php'; Zend_Session::start(); + +//TestHelper::installTestDatabase(); diff --git a/airtime_mvc/tests/application/helpers/TestHelper.php b/airtime_mvc/tests/application/helpers/TestHelper.php index b5880c1ba..283f8b895 100644 --- a/airtime_mvc/tests/application/helpers/TestHelper.php +++ b/airtime_mvc/tests/application/helpers/TestHelper.php @@ -45,9 +45,70 @@ class TestHelper $dbpasswd = $CC_CONFIG['dsn']['password']; $dbname = $CC_CONFIG['dsn']['database']; $dbhost = $CC_CONFIG['dsn']['hostspec']; + + $databaseAlreadyExists = AirtimeInstall::createDatabase(); + if ($databaseAlreadyExists) + { + //Truncate all the tables + $con = Propel::getConnection(); + $sql = "select * from pg_tables where tableowner = 'airtime'"; + try { + $rows = $con->query($sql)->fetchAll(); + } catch (Exception $e) { + $rows = array(); + } + + //Add any tables that shouldn't be cleared here. + // cc_subjs - Most of Airtime requires an admin account to work, which has id=1, + // so don't clear it. + $tablesToNotClear = array("cc_subjs"); - AirtimeInstall::createDatabase(); - AirtimeInstall::createDatabaseTables($dbuser, $dbpasswd, $dbname, $dbhost); + $con->beginTransaction(); + foreach ($rows as $row) { + $tablename = $row["tablename"]; + if (in_array($tablename, $tablesToNotClear)) + { + continue; + } + //echo " * Clearing database table $tablename..."; + + //TRUNCATE is actually slower than DELETE in many cases: + //http://stackoverflow.com/questions/11419536/postgresql-truncation-speed + //$sql = "TRUNCATE TABLE $tablename CASCADE"; + $sql = "DELETE FROM $tablename"; + AirtimeInstall::InstallQuery($sql, false); + } + $con->commit(); + + //Because we're DELETEing all the rows instead of using TRUNCATE (for speed), + //we have to reset the sequences so the auto-increment columns (like primary keys) + //all start at 1 again. This is hacky but it still lets all of this execute fast. + $sql = "SELECT c.relname FROM pg_class c WHERE c.relkind = 'S'"; + try { + $rows = $con->query($sql)->fetchAll(); + } catch (Exception $e) { + $rows = array(); + } + $con->beginTransaction(); + foreach ($rows as $row) { + $seqrelname= $row["relname"]; + $sql = "ALTER SEQUENCE ${seqrelname} RESTART WITH 1"; + AirtimeInstall::InstallQuery($sql, false); + } + $con->commit(); + } + else + { + //Create all the database tables + AirtimeInstall::createDatabaseTables($dbuser, $dbpasswd, $dbname, $dbhost); + } AirtimeInstall::SetDefaultTimezone(); } -} \ No newline at end of file + + public static function setupZendBootstrap() + { + $application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH .'/configs/application.ini'); + $application->bootstrap(); + return $application; + } +} diff --git a/airtime_mvc/tests/application/models/unit/PreferenceUnitTest.php b/airtime_mvc/tests/application/models/unit/PreferenceUnitTest.php new file mode 100644 index 000000000..264ddda48 --- /dev/null +++ b/airtime_mvc/tests/application/models/unit/PreferenceUnitTest.php @@ -0,0 +1,29 @@ +assertEquals(Application_Model_Preference::GetHeadTitle(), $title); + } + + public function testSetShowsPopulatedUntil() + { + $date = new DateTime(); + Application_Model_Preference::SetShowsPopulatedUntil($date); + $this->assertEquals(Application_Model_Preference::GetShowsPopulatedUntil(), $date); + } + +} diff --git a/airtime_mvc/tests/application/services/database/ShowServiceDbTest.php b/airtime_mvc/tests/application/services/database/ShowServiceDbTest.php index b4eaa14a2..7519b55c1 100644 --- a/airtime_mvc/tests/application/services/database/ShowServiceDbTest.php +++ b/airtime_mvc/tests/application/services/database/ShowServiceDbTest.php @@ -22,23 +22,13 @@ class ShowServiceDbTest extends Zend_Test_PHPUnit_DatabaseTestCase public function setUp() { TestHelper::installTestDatabase(); - - //XXX: Zend_Test_PHPUnit_DatabaseTestCase doesn't use this for whatever reason: - //$this->bootstrap = array($this, 'appBootstrap'); - //So instead we just manually call the appBootstrap here: - $this->appBootstrap(); + TestHelper::setupZendBootstrap(); //$this->_nowDT = new DateTime("now", new DateTimeZone("UTC")); parent::setUp(); } - public function appBootstrap() - { - $this->application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH .'/configs/application.ini'); - $this->application->bootstrap(); - } - public function getConnection() { if ($this->_connectionMock == null) { diff --git a/install_minimal/include/AirtimeInstall.php b/install_minimal/include/AirtimeInstall.php index ac44297bd..7f1523137 100644 --- a/install_minimal/include/AirtimeInstall.php +++ b/install_minimal/include/AirtimeInstall.php @@ -117,7 +117,8 @@ class AirtimeInstall { AirtimeInstall::InstallQuery("DROP SEQUENCE IF EXISTS $p_sequenceName", false); } - + + /** * Try to connect to the database. Return true on success, false on failure. * @param boolean $p_exitOnError @@ -210,17 +211,20 @@ class AirtimeInstall { $CC_CONFIG = Config::getConfig(); - echo " * Creating Airtime database".PHP_EOL; $database = $CC_CONFIG['dsn']['database']; $username = $CC_CONFIG['dsn']['username']; #$command = "echo \"CREATE DATABASE $database OWNER $username\" | su postgres -c psql 2>/dev/null"; - - $command = "su postgres -c \"psql -l | cut -f2 -d' ' | grep -w 'airtime'\";"; + + echo " * Creating Airtime database: " . $database . PHP_EOL; + + putenv("LC_ALL=en_CA.UTF-8"); //Squash warnings when running unit tests + $command = "su postgres -c \"psql -l | cut -f2 -d' ' | grep -w '{$database}'\";"; exec($command, $output, $rv); if ($rv == 0) { //database already exists + echo "Database already exists." . PHP_EOL; return true; }