Open Journal Systems  3.3.0
PKPTestHelper.inc.php
1 <?php
15 define('PKP_TEST_ENTIRE_DB', 1);
16 
17 abstract class PKPTestHelper {
18 
19  //
20  // Public helper methods
21  //
27  public static function backupTables($tables, $test) {
28  $dao = new DAO();
29  $driver = Config::getVar('database', 'driver');
30  foreach ($tables as $table) {
31  switch ($driver) {
32  case 'mysql':
33  case 'mysqli':
34  $createLikeSql = "CREATE TABLE backup_$table LIKE $table";
35  break;
36  case 'postgres':
37  case 'postgres64':
38  case 'postgres7':
39  case 'postgres8':
40  case 'postgres9':
41  $createLikeSql = "CREATE TABLE backup_$table (LIKE $table)";
42  break;
43  default:
44  $test->fail("Unknown driver \"$driver\"");
45  return;
46  }
47 
48  $sqls = array(
49  "DROP TABLE IF EXISTS backup_$table",
50  $createLikeSql,
51  "INSERT INTO backup_$table SELECT * FROM $table"
52  );
53  foreach ($sqls as $sql) {
54  if (!$dao->update($sql, false, true, false)) {
55  $test->fail("Error while backing up $table: offending SQL is '$sql'");
56  }
57  }
58  }
59  }
60 
66  public static function restoreTables($tables, $test) {
67  $dao = new DAO();
68  foreach ($tables as $table) {
69  $sqls = array(
70  "TRUNCATE TABLE $table",
71  "INSERT INTO $table SELECT * FROM backup_$table",
72  "DROP TABLE backup_$table"
73  );
74  foreach ($sqls as $sql) {
75  if (!$dao->update($sql, false, true, false)) {
76  $test->fail("Error while restoring $table: offending SQL is '$sql'");
77  }
78  }
79  }
80  }
81 
85  public static function restoreDB($test) {
86  $filename = getenv('DATABASEDUMP');
87  if (!$filename || !file_exists($filename)) {
88  $test->fail('Database dump filename needs to be specified in env variable DATABASEDUMP!');
89  return;
90  }
91 
92  $output = $status = null; // For PHP scrutinizer
93  switch (Config::getVar('database', 'driver')) {
94  case 'mysql':
95  case 'mysqli':
96  exec($cmd = 'zcat ' .
97  escapeshellarg($filename) .
98  ' | /usr/bin/mysql --user=' .
99  escapeshellarg(Config::getVar('database', 'username')) .
100  ' --password=' .
101  escapeshellarg(Config::getVar('database', 'password')) .
102  ' --host=' .
103  escapeshellarg(Config::getVar('database', 'host')) .
104  ' ' .
105  escapeshellarg(Config::getVar('database', 'name')),
106  $output,
107  $status
108  );
109  if ($status !== 0) {
110  $test->fail("Error while restoring database from \"$filename\" (command: \"$cmd\").");
111  }
112  break;
113  case 'postgres':
114  case 'postgres64':
115  case 'postgres7':
116  case 'postgres8':
117  case 'postgres9':
118  // WARNING: Does not send a password.
119  exec($cmd = 'zcat ' .
120  escapeshellarg($filename) .
121  ' | /usr/bin/psql --username=' .
122  escapeshellarg(Config::getVar('database', 'username')) .
123  ' --no-password' .
124  ' --host=' .
125  escapeshellarg(Config::getVar('database', 'host')) .
126  ' ' .
127  escapeshellarg(Config::getVar('database', 'name')),
128  $output,
129  $status
130  );
131  if ($status !== 0) {
132  $test->fail("Error while restoring database from \"$filename\" (command: \"$cmd\".");
133  }
134  break;
135  }
136  }
137 
150  public static function xdebugScream($scream) {
151  if (extension_loaded('xdebug')) {
152  static $previous = null;
153  if ($scream) {
154  assert(!is_null($previous));
155  ini_set('xdebug.scream', $previous);
156  } else {
157  $previous = ini_get('xdebug.scream');
158  ini_set('xdebug.scream', false);
159  }
160  }
161  }
162 }
163 
PKPTestHelper\backupTables
static backupTables($tables, $test)
Definition: PKPTestHelper.inc.php:27
PKPTestHelper
Definition: PKPTestHelper.inc.php:17
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
PKPTestHelper\xdebugScream
static xdebugScream($scream)
Definition: PKPTestHelper.inc.php:150
PKPTestHelper\restoreTables
static restoreTables($tables, $test)
Definition: PKPTestHelper.inc.php:66
PKPTestHelper\restoreDB
static restoreDB($test)
Definition: PKPTestHelper.inc.php:85
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31