TestHelper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. require_once "AirtimeInstall.php";
  3. class TestHelper
  4. {
  5. public static function loginUser()
  6. {
  7. $authAdapter = Application_Model_Auth::getAuthAdapter();
  8. //pass to the adapter the submitted username and password
  9. $authAdapter->setIdentity('admin')
  10. ->setCredential('admin');
  11. $auth = Zend_Auth::getInstance();
  12. $result = $auth->authenticate($authAdapter);
  13. if ($result->isValid()) {
  14. //all info about this user from the login table omit only the password
  15. $userInfo = $authAdapter->getResultRowObject(null, 'password');
  16. //the default storage is a session with namespace Zend_Auth
  17. $authStorage = $auth->getStorage();
  18. $authStorage->write($userInfo);
  19. }
  20. }
  21. public static function getDbZendConfig()
  22. {
  23. return new Zend_Config(
  24. array(
  25. 'host' => '127.0.0.1',
  26. 'dbname' => 'airtime_test',
  27. 'username' => 'airtime',
  28. 'password' => 'airtime'
  29. )
  30. );
  31. }
  32. public static function installTestDatabase()
  33. {
  34. //We need to load the config before our app bootstrap runs. The config
  35. //is normally
  36. $CC_CONFIG = Config::getConfig();
  37. $dbuser = $CC_CONFIG['dsn']['username'];
  38. $dbpasswd = $CC_CONFIG['dsn']['password'];
  39. $dbname = $CC_CONFIG['dsn']['database'];
  40. $dbhost = $CC_CONFIG['dsn']['hostspec'];
  41. $databaseAlreadyExists = AirtimeInstall::createDatabase();
  42. if ($databaseAlreadyExists)
  43. {
  44. //Truncate all the tables
  45. $con = Propel::getConnection();
  46. $sql = "select * from pg_tables where tableowner = 'airtime'";
  47. try {
  48. $rows = $con->query($sql)->fetchAll();
  49. } catch (Exception $e) {
  50. $rows = array();
  51. }
  52. //Add any tables that shouldn't be cleared here.
  53. // cc_subjs - Most of Airtime requires an admin account to work, which has id=1,
  54. // so don't clear it.
  55. // cc_music_dirs - Has foreign key constraints against cc_files, so we clear cc_files
  56. // first and clear cc_music_dirs after
  57. $tablesToNotClear = array("cc_subjs", "cc_music_dirs");
  58. $con->beginTransaction();
  59. foreach ($rows as $row) {
  60. $tablename = $row["tablename"];
  61. if (in_array($tablename, $tablesToNotClear))
  62. {
  63. continue;
  64. }
  65. //echo " * Clearing database table $tablename...";
  66. //TRUNCATE is actually slower than DELETE in many cases:
  67. //http://stackoverflow.com/questions/11419536/postgresql-truncation-speed
  68. //$sql = "TRUNCATE TABLE $tablename CASCADE";
  69. $sql = "DELETE FROM $tablename";
  70. AirtimeInstall::InstallQuery($sql, false);
  71. }
  72. //Now that cc_files is empty, clearing cc_music_dirs should work
  73. $sql = "DELETE FROM cc_music_dirs";
  74. AirtimeInstall::InstallQuery($sql, false);
  75. $con->commit();
  76. //Because we're DELETEing all the rows instead of using TRUNCATE (for speed),
  77. //we have to reset the sequences so the auto-increment columns (like primary keys)
  78. //all start at 1 again. This is hacky but it still lets all of this execute fast.
  79. $sql = "SELECT c.relname FROM pg_class c WHERE c.relkind = 'S'";
  80. try {
  81. $rows = $con->query($sql)->fetchAll();
  82. } catch (Exception $e) {
  83. $rows = array();
  84. }
  85. $con->beginTransaction();
  86. foreach ($rows as $row) {
  87. $seqrelname= $row["relname"];
  88. $sql = "ALTER SEQUENCE ${seqrelname} RESTART WITH 1";
  89. AirtimeInstall::InstallQuery($sql, false);
  90. }
  91. $con->commit();
  92. }
  93. else
  94. {
  95. //Create all the database tables
  96. AirtimeInstall::createDatabaseTables($dbuser, $dbpasswd, $dbname, $dbhost);
  97. }
  98. }
  99. public static function setupZendBootstrap()
  100. {
  101. $application = new Zend_Application(APPLICATION_ENV, CONFIG_PATH . 'application.ini');
  102. $application->bootstrap();
  103. return $application;
  104. }
  105. }