2014-01-08 21:51:11 +01:00
|
|
|
<?php
|
2014-01-23 17:37:20 +01:00
|
|
|
|
2014-01-08 21:51:11 +01:00
|
|
|
class TestHelper
|
|
|
|
{
|
|
|
|
public static function loginUser()
|
|
|
|
{
|
|
|
|
$authAdapter = Application_Model_Auth::getAuthAdapter();
|
|
|
|
|
2022-03-14 11:15:04 +01:00
|
|
|
// pass to the adapter the submitted username and password
|
2014-01-08 21:51:11 +01:00
|
|
|
$authAdapter->setIdentity('admin')
|
2022-01-23 19:15:55 +01:00
|
|
|
->setCredential('admin');
|
2014-01-08 21:51:11 +01:00
|
|
|
|
|
|
|
$auth = Zend_Auth::getInstance();
|
|
|
|
$result = $auth->authenticate($authAdapter);
|
|
|
|
if ($result->isValid()) {
|
2022-03-14 11:15:04 +01:00
|
|
|
// all info about this user from the login table omit only the password
|
2014-01-08 21:51:11 +01:00
|
|
|
$userInfo = $authAdapter->getResultRowObject(null, 'password');
|
|
|
|
|
2022-03-14 11:15:04 +01:00
|
|
|
// the default storage is a session with namespace Zend_Auth
|
2014-01-08 21:51:11 +01:00
|
|
|
$authStorage = $auth->getStorage();
|
|
|
|
$authStorage->write($userInfo);
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 17:37:20 +01:00
|
|
|
|
|
|
|
public static function getDbZendConfig()
|
|
|
|
{
|
2017-02-20 21:47:53 +01:00
|
|
|
$config = Config::getConfig();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-01-23 17:37:20 +01:00
|
|
|
return new Zend_Config(
|
2021-10-11 16:10:47 +02:00
|
|
|
[
|
2022-02-04 15:03:01 +01:00
|
|
|
'host' => $config['dsn']['host'],
|
|
|
|
'port' => $config['dsn']['port'],
|
2021-10-11 16:10:47 +02:00
|
|
|
'dbname' => $config['dsn']['database'],
|
2017-02-20 21:47:53 +01:00
|
|
|
'username' => $config['dsn']['username'],
|
2021-10-11 16:10:47 +02:00
|
|
|
'password' => $config['dsn']['password'],
|
|
|
|
]
|
2014-01-23 17:37:20 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function installTestDatabase()
|
|
|
|
{
|
2022-03-14 11:15:04 +01:00
|
|
|
// We need to load the config before our app bootstrap runs. The config
|
|
|
|
// is normally
|
2014-01-23 17:37:20 +01:00
|
|
|
$CC_CONFIG = Config::getConfig();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2022-02-04 15:03:01 +01:00
|
|
|
$dbhost = $CC_CONFIG['dsn']['host'];
|
|
|
|
$dbport = $CC_CONFIG['dsn']['port'];
|
|
|
|
$dbname = $CC_CONFIG['dsn']['database'];
|
2014-01-23 17:37:20 +01:00
|
|
|
$dbuser = $CC_CONFIG['dsn']['username'];
|
|
|
|
$dbpasswd = $CC_CONFIG['dsn']['password'];
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-01-23 23:04:29 +01:00
|
|
|
$databaseAlreadyExists = AirtimeInstall::createDatabase();
|
2021-10-11 16:10:47 +02:00
|
|
|
if ($databaseAlreadyExists) {
|
2022-03-14 11:15:04 +01:00
|
|
|
// Truncate all the tables
|
2014-01-23 23:04:29 +01:00
|
|
|
$con = Propel::getConnection();
|
2021-10-11 16:10:47 +02:00
|
|
|
$sql = "select * from pg_tables where tableowner = '{$dbuser}'";
|
|
|
|
|
2014-01-23 23:04:29 +01:00
|
|
|
try {
|
|
|
|
$rows = $con->query($sql)->fetchAll();
|
|
|
|
} catch (Exception $e) {
|
2021-10-11 16:10:47 +02:00
|
|
|
$rows = [];
|
2014-01-23 23:04:29 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2022-03-14 11:15:04 +01:00
|
|
|
// Add any tables that shouldn't be cleared here.
|
2014-01-23 23:04:29 +01:00
|
|
|
// cc_subjs - Most of Airtime requires an admin account to work, which has id=1,
|
|
|
|
// so don't clear it.
|
2021-10-11 16:10:47 +02:00
|
|
|
// cc_music_dirs - Has foreign key constraints against cc_files, so we clear cc_files
|
2015-01-28 19:19:50 +01:00
|
|
|
// first and clear cc_music_dirs after
|
2021-10-11 16:10:47 +02:00
|
|
|
$tablesToNotClear = ['cc_subjs', 'cc_music_dirs'];
|
2014-01-23 17:37:20 +01:00
|
|
|
|
2014-01-23 23:04:29 +01:00
|
|
|
$con->beginTransaction();
|
|
|
|
foreach ($rows as $row) {
|
2021-10-11 16:10:47 +02:00
|
|
|
$tablename = $row['tablename'];
|
|
|
|
if (in_array($tablename, $tablesToNotClear)) {
|
2014-01-23 23:04:29 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-14 11:15:04 +01:00
|
|
|
// echo " * Clearing database table $tablename...";
|
2014-01-23 23:04:29 +01:00
|
|
|
|
2022-03-14 11:15:04 +01:00
|
|
|
// TRUNCATE is actually slower than DELETE in many cases:
|
|
|
|
// http://stackoverflow.com/questions/11419536/postgresql-truncation-speed
|
|
|
|
// $sql = "TRUNCATE TABLE $tablename CASCADE";
|
2021-10-11 16:10:47 +02:00
|
|
|
$sql = "DELETE FROM {$tablename}";
|
2014-01-23 23:04:29 +01:00
|
|
|
AirtimeInstall::InstallQuery($sql, false);
|
|
|
|
}
|
2014-02-05 21:37:47 +01:00
|
|
|
|
2022-03-14 11:15:04 +01:00
|
|
|
// Now that cc_files is empty, clearing cc_music_dirs should work
|
2021-10-11 16:10:47 +02:00
|
|
|
$sql = 'DELETE FROM cc_music_dirs';
|
2014-02-05 21:37:47 +01:00
|
|
|
AirtimeInstall::InstallQuery($sql, false);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-10-09 15:39:50 +02:00
|
|
|
// Because files are stored relative to their watch directory,
|
|
|
|
// we need to set the "stor" path before we can successfully
|
|
|
|
// create a fake file in the database.
|
2022-03-14 11:15:04 +01:00
|
|
|
// Copy paste from airtime-db-install.php:
|
2021-10-11 16:10:47 +02:00
|
|
|
$stor_dir = '/tmp';
|
2014-10-09 15:39:50 +02:00
|
|
|
$con = Propel::getConnection();
|
2021-10-11 16:10:47 +02:00
|
|
|
$sql = "INSERT INTO cc_music_dirs (directory, type) VALUES ('{$stor_dir}', 'stor')";
|
|
|
|
|
2014-10-09 15:39:50 +02:00
|
|
|
try {
|
|
|
|
$con->exec($sql);
|
|
|
|
} catch (Exception $e) {
|
2021-10-11 16:10:47 +02:00
|
|
|
echo " * Failed inserting {$stor_dir} in cc_music_dirs" . PHP_EOL;
|
|
|
|
echo " * Message {$e->getMessage()}" . PHP_EOL;
|
|
|
|
|
2014-10-09 15:39:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-05 21:37:47 +01:00
|
|
|
|
2014-01-23 23:04:29 +01:00
|
|
|
$con->commit();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2022-03-14 11:15:04 +01:00
|
|
|
// 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.
|
2014-01-23 23:04:29 +01:00
|
|
|
$sql = "SELECT c.relname FROM pg_class c WHERE c.relkind = 'S'";
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-01-23 23:04:29 +01:00
|
|
|
try {
|
|
|
|
$rows = $con->query($sql)->fetchAll();
|
|
|
|
} catch (Exception $e) {
|
2021-10-11 16:10:47 +02:00
|
|
|
$rows = [];
|
2014-01-23 23:04:29 +01:00
|
|
|
}
|
|
|
|
$con->beginTransaction();
|
|
|
|
foreach ($rows as $row) {
|
2021-10-11 16:10:47 +02:00
|
|
|
$seqrelname = $row['relname'];
|
|
|
|
$sql = "ALTER SEQUENCE {$seqrelname} RESTART WITH 1";
|
2014-01-23 23:04:29 +01:00
|
|
|
AirtimeInstall::InstallQuery($sql, false);
|
|
|
|
}
|
|
|
|
$con->commit();
|
2021-10-11 16:10:47 +02:00
|
|
|
} else {
|
2022-03-14 11:15:04 +01:00
|
|
|
// Create all the database tables
|
2022-02-04 15:03:01 +01:00
|
|
|
AirtimeInstall::CreateDatabaseTables($dbuser, $dbpasswd, $dbname, $dbhost, $dbport);
|
2014-01-23 23:04:29 +01:00
|
|
|
}
|
2014-01-23 17:37:20 +01:00
|
|
|
}
|
2014-01-23 23:04:29 +01:00
|
|
|
|
|
|
|
public static function setupZendBootstrap()
|
|
|
|
{
|
2022-02-04 11:00:41 +01:00
|
|
|
$application = new Zend_Application(APPLICATION_ENV, CONFIG_PATH . '/application.ini');
|
2014-01-23 23:04:29 +01:00
|
|
|
$application->bootstrap();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-01-23 23:04:29 +01:00
|
|
|
return $application;
|
|
|
|
}
|
|
|
|
}
|