Index: trunk/phase3/includes/SeleniumWebSettings.php |
— | — | @@ -8,16 +8,22 @@ |
9 | 9 | die( 1 ); |
10 | 10 | } |
11 | 11 | |
| 12 | +require_once( "$IP/includes/GlobalFunctions.php" ); |
| 13 | + |
12 | 14 | $fname = 'SeleniumWebSettings.php'; |
13 | 15 | wfProfileIn( $fname ); |
14 | 16 | |
15 | 17 | $cookiePrefix = $wgSitename . "-"; |
16 | 18 | $cookieName = $cookiePrefix . "Selenium"; |
17 | 19 | |
| 20 | +// this is a fallback sql file |
| 21 | +$testSqlFile = false; |
| 22 | +$testImageZip = false; |
| 23 | + |
18 | 24 | //if we find a request parameter containing the test name, set a cookie with the test name |
19 | 25 | if ( isset( $_GET['setupTestSuite'] ) ) { |
20 | 26 | $setupTestSuiteName = $_GET['setupTestSuite']; |
21 | | - |
| 27 | + |
22 | 28 | if ( preg_match( '/[^a-zA-Z0-9_-]/', $setupTestSuiteName ) || !isset( $wgSeleniumTestConfigs[$setupTestSuiteName] ) ) { |
23 | 29 | return; |
24 | 30 | } |
— | — | @@ -31,29 +37,57 @@ |
32 | 38 | $wgCookieSecure, |
33 | 39 | true ); |
34 | 40 | } |
| 41 | + |
| 42 | + $testIncludes = array(); //array containing all the includes needed for this test |
| 43 | + $testGlobalConfigs = array(); //an array containg all the global configs needed for this test |
| 44 | + $testResourceFiles = array(); // an array containing all the resource files needed for this test |
| 45 | + $callback = $wgSeleniumTestConfigs[$setupTestSuiteName]; |
| 46 | + call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles)); |
| 47 | + |
| 48 | + if ( isset( $testResourceFiles['db'] ) ) { |
| 49 | + $testSqlFile = $testResourceFiles['db']; |
| 50 | + } |
| 51 | + if ( isset($testResourceFiles['images']) ) { |
| 52 | + $testImageZip = $testResourceFiles['images']; |
| 53 | + } |
| 54 | + |
| 55 | + $testResourceName = getTestResourceNameFromTestSuiteName( $setupTestSuiteName ); |
| 56 | + switchToTestResources( $testResourceName, false ); // false means do not switch database yet |
| 57 | + setupTestResources( $testResourceName, $testSqlFile, $testImageZip ); |
35 | 58 | } |
| 59 | + |
36 | 60 | //clear the cookie based on a request param |
37 | 61 | if ( isset( $_GET['clearTestSuite'] ) ) { |
38 | | - $expire = time() - 600; |
39 | | - setcookie( $cookieName, |
40 | | - '', |
41 | | - $expire, |
42 | | - $wgCookiePath, |
43 | | - $wgCookieDomain, |
44 | | - $wgCookieSecure, |
45 | | - true ); |
| 62 | + $testSuiteName = getTestSuiteNameFromCookie( $cookieName ); |
| 63 | + |
| 64 | + $expire = time() - 600; |
| 65 | + setcookie( $cookieName, |
| 66 | + '', |
| 67 | + $expire, |
| 68 | + $wgCookiePath, |
| 69 | + $wgCookieDomain, |
| 70 | + $wgCookieSecure, |
| 71 | + true ); |
| 72 | + |
| 73 | + $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName ); |
| 74 | + teardownTestResources( $testResourceName ); |
46 | 75 | } |
47 | 76 | |
48 | 77 | //if a cookie is found, run the appropriate callback to get the config params. |
49 | 78 | if ( isset( $_COOKIE[$cookieName] ) ) { |
50 | | - $testSuiteName = $_COOKIE[$cookieName]; |
| 79 | + $testSuiteName = getTestSuiteNameFromCookie( $cookieName ); |
51 | 80 | if ( !isset( $wgSeleniumTestConfigs[$testSuiteName] ) ) { |
52 | 81 | return; |
53 | 82 | } |
| 83 | + |
| 84 | + $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName ); |
| 85 | + switchToTestResources( $testResourceName ); |
| 86 | + |
54 | 87 | $testIncludes = array(); //array containing all the includes needed for this test |
55 | 88 | $testGlobalConfigs = array(); //an array containg all the global configs needed for this test |
| 89 | + $testResourceFiles = array(); // an array containing all the resource files needed for this test |
56 | 90 | $callback = $wgSeleniumTestConfigs[$testSuiteName]; |
57 | | - call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs)); |
| 91 | + call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles)); |
58 | 92 | |
59 | 93 | foreach ( $testIncludes as $includeFile ) { |
60 | 94 | $file = $IP . '/' . $includeFile; |
— | — | @@ -70,3 +104,122 @@ |
71 | 105 | } |
72 | 106 | |
73 | 107 | wfProfileOut( $fname ); |
| 108 | + |
| 109 | +function getTestSuiteNameFromCookie( $cookieName ) { |
| 110 | + $testSuiteName = null; |
| 111 | + if ( isset( $_COOKIE[$cookieName] ) ) { |
| 112 | + $testSuiteName = $_COOKIE[$cookieName]; |
| 113 | + } |
| 114 | + return $testSuiteName; |
| 115 | +} |
| 116 | + |
| 117 | +function getTestResourceNameFromTestSuiteName( $testSuiteName ) { |
| 118 | + $testResourceName = null; |
| 119 | + if ( isset( $testSuiteName ) ) { |
| 120 | + $testResourceName = $testSuiteName; |
| 121 | + } |
| 122 | + return $testResourceName; |
| 123 | +} |
| 124 | + |
| 125 | +function getTestUploadPathFromResourceName( $testResourceName ) { |
| 126 | + global $IP; |
| 127 | + $testUploadPath = "$IP/images/$testResourceName"; |
| 128 | + return $testUploadPath; |
| 129 | +} |
| 130 | + |
| 131 | +function setupTestResources( $testResourceName, $testSqlFile, $testImageZip ) { |
| 132 | + global $wgDBname, $wgSeleniumUseTestResources; |
| 133 | + |
| 134 | + if ( !$wgSeleniumUseTestResources ) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + // Basic security. Do not allow to drop productive database. |
| 138 | + if ( $testResouceName == $wgDBname ) { |
| 139 | + die( "Cannot override productive database." ); |
| 140 | + } |
| 141 | + if ( $testResourceName == '' ) { |
| 142 | + die( "Cannot identify a test the resources should be installed for." ); |
| 143 | + } |
| 144 | + |
| 145 | + //create tables |
| 146 | + $dbw =& wfGetDB( DB_MASTER ); |
| 147 | + $dbw->query( "DROP DATABASE IF EXISTS ".$testResourceName ); |
| 148 | + $dbw->query( "CREATE DATABASE ".$testResourceName ); |
| 149 | + |
| 150 | + // do not set the new db name before database is setup |
| 151 | + $wgDBname = $testResourceName; |
| 152 | + $dbw->selectDB( $testResourceName ); |
| 153 | + // populate from sql file |
| 154 | + if ( $testSqlFile ) { |
| 155 | + $dbw->sourceFile( $testSqlFile ); |
| 156 | + } |
| 157 | + |
| 158 | + // create test image dir |
| 159 | + $testUploadPath = getTestUploadPathFromResourceName( $testResourceName ); |
| 160 | + if ( !file_exists( $testUploadPath ) ) { |
| 161 | + mkdir( $testUploadPath ); |
| 162 | + } |
| 163 | + |
| 164 | + if ( $testImageZip ) { |
| 165 | + $zip = new ZipArchive(); |
| 166 | + $zip->open( $testImageZip ); |
| 167 | + $zip->extractTo( $testUploadPath ); |
| 168 | + $zip->close(); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +function teardownTestResources( $testResourceName ) { |
| 173 | + global $wgSeleniumUseTestResources; |
| 174 | + |
| 175 | + if ( !$wgSeleniumUseTestResources ) { |
| 176 | + return false; |
| 177 | + } |
| 178 | + // remove test database |
| 179 | + $dbw =& wfGetDB( DB_MASTER ); |
| 180 | + $dbw->query( "DROP DATABASE IF EXISTS ".$testResourceName ); |
| 181 | + |
| 182 | + $testUploadPath = getTestUploadPathFromResourceName( $testResourceName ); |
| 183 | + // remove test image dir |
| 184 | + if ( file_exists( $testUploadPath ) ) { |
| 185 | + wfRecursiveRemoveDir( $testUploadPath ); |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +function switchToTestResources( $testResourceName, $switchDB = true ) { |
| 190 | + global $wgDBuser, $wgDBpassword, $wgDBname, $wgDBprefix; |
| 191 | + global $wgDBtestuser, $wgDBtestpassword, $wgDBtestprefix; |
| 192 | + global $wgUploadPath; |
| 193 | + global $wgSeleniumUseTestResources; |
| 194 | + |
| 195 | + if ( !$wgSeleniumUseTestResources ) { |
| 196 | + return false; |
| 197 | + } |
| 198 | + |
| 199 | + if ( $switchDB ) { |
| 200 | + $wgDBname = $testResourceName; |
| 201 | + } |
| 202 | + $wgDBuser = $wgDBtestuser; |
| 203 | + $wgDBpassword = $wgDBtestpassword; |
| 204 | + //$wgDBprefix = $wgDBtestprefix; |
| 205 | + |
| 206 | + $testUploadPath = getTestUploadPathFromResourceName( $testResourceName ); |
| 207 | + $wgUploadPath = $testUploadPath; |
| 208 | +} |
| 209 | + |
| 210 | +function wfRecursiveRemoveDir( $dir ) { |
| 211 | + // taken from http://de3.php.net/manual/en/function.rmdir.php#98622 |
| 212 | + if ( is_dir( $dir ) ) { |
| 213 | + $objects = scandir( $dir ); |
| 214 | + foreach ( $objects as $object ) { |
| 215 | + if ( $object != "." && $object != ".." ) { |
| 216 | + if ( filetype( $dir."/".$object ) == "dir" ) { |
| 217 | + wfRecursiveRemoveDir( $dir."/".$object ); |
| 218 | + } else { |
| 219 | + unlink( $dir."/".$object ); |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + reset( $objects ); |
| 224 | + rmdir( $dir ); |
| 225 | + } |
| 226 | +} |
\ No newline at end of file |