MediaWiki  REL1_21
SeleniumWebSettings.php
Go to the documentation of this file.
00001 <?php
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027         die( 1 );
00028 }
00029 
00030 require_once( "$IP/includes/GlobalFunctions.php" );
00031 
00032 $fname = 'SeleniumWebSettings.php';
00033 wfProfileIn( $fname );
00034 
00035 $cookiePrefix = $wgSitename . '-';
00036 $cookieName = $cookiePrefix . 'Selenium';
00037 
00038 // this is a fallback SQL file
00039 $testSqlFile = false;
00040 $testImageZip = false;
00041 
00042 // if we find a request parameter containing the test name, set a cookie with the test name
00043 if ( isset( $_GET['setupTestSuite'] ) ) {
00044         $setupTestSuiteName = $_GET['setupTestSuite'];
00045 
00046         if (
00047                 preg_match( '/[^a-zA-Z0-9_-]/', $setupTestSuiteName ) ||
00048                 !isset( $wgSeleniumTestConfigs[$setupTestSuiteName] )
00049         )
00050         {
00051                 return;
00052         }
00053         if ( strlen( $setupTestSuiteName ) > 0 ) {
00054                 $expire = time() + 600;
00055                 setcookie(
00056                         $cookieName,
00057                         $setupTestSuiteName,
00058                         $expire,
00059                         $wgCookiePath,
00060                         $wgCookieDomain,
00061                         $wgCookieSecure,
00062                         true
00063                 );
00064         }
00065 
00066         $testIncludes = array(); // array containing all the includes needed for this test
00067         $testGlobalConfigs = array(); // an array containing all the global configs needed for this test
00068         $testResourceFiles = array(); // an array containing all the resource files needed for this test
00069         $callback = $wgSeleniumTestConfigs[$setupTestSuiteName];
00070         call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
00071 
00072         if ( isset( $testResourceFiles['images'] ) ) {
00073                 $testImageZip = $testResourceFiles['images'];
00074         }
00075 
00076         if ( isset( $testResourceFiles['db'] ) ) {
00077                 $testSqlFile = $testResourceFiles['db'];
00078                 $testResourceName = getTestResourceNameFromTestSuiteName( $setupTestSuiteName );
00079 
00080                 switchToTestResources( $testResourceName, false ); // false means do not switch database yet
00081                 setupTestResources( $testResourceName, $testSqlFile, $testImageZip );
00082         }
00083 }
00084 
00085 // clear the cookie based on a request param
00086 if ( isset( $_GET['clearTestSuite'] ) ) {
00087         $testSuiteName = getTestSuiteNameFromCookie( $cookieName );
00088 
00089         $expire = time() - 600;
00090         setcookie(
00091                 $cookieName,
00092                 '',
00093                 $expire,
00094                 $wgCookiePath,
00095                 $wgCookieDomain,
00096                 $wgCookieSecure,
00097                 true
00098         );
00099 
00100         $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
00101         teardownTestResources( $testResourceName );
00102 }
00103 
00104 // if a cookie is found, run the appropriate callback to get the config params.
00105 if ( isset( $_COOKIE[$cookieName] ) ) {
00106         $testSuiteName = getTestSuiteNameFromCookie( $cookieName );
00107         if ( !isset( $wgSeleniumTestConfigs[$testSuiteName] ) ) {
00108                 return;
00109         }
00110 
00111         $testIncludes = array(); // array containing all the includes needed for this test
00112         $testGlobalConfigs = array(); // an array containing all the global configs needed for this test
00113         $testResourceFiles = array(); // an array containing all the resource files needed for this test
00114         $callback = $wgSeleniumTestConfigs[$testSuiteName];
00115         call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
00116 
00117         if ( isset( $testResourceFiles['db'] ) ) {
00118                 $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
00119                 switchToTestResources( $testResourceName );
00120         }
00121         foreach ( $testIncludes as $includeFile ) {
00122                 $file = $IP . '/' . $includeFile;
00123                 require_once( $file );
00124         }
00125         foreach ( $testGlobalConfigs as $key => $value ) {
00126                 if ( is_array( $value ) ) {
00127                         $GLOBALS[$key] = array_merge( $GLOBALS[$key], $value );
00128                 } else {
00129                         $GLOBALS[$key] = $value;
00130                 }
00131         }
00132 }
00133 
00134 wfProfileOut( $fname );
00135 
00136 function getTestSuiteNameFromCookie( $cookieName ) {
00137         $testSuiteName = null;
00138         if ( isset( $_COOKIE[$cookieName] ) ) {
00139                 $testSuiteName = $_COOKIE[$cookieName];
00140         }
00141         return $testSuiteName;
00142 }
00143 
00144 function getTestResourceNameFromTestSuiteName( $testSuiteName ) {
00145         $testResourceName = null;
00146         if ( isset( $testSuiteName ) ) {
00147                 $testResourceName = $testSuiteName;
00148         }
00149         return $testResourceName;
00150 }
00151 
00152 function getTestUploadPathFromResourceName( $testResourceName ) {
00153         global $IP;
00154         $testUploadPath = "$IP/images/$testResourceName";
00155         return $testUploadPath;
00156 }
00157 
00158 function setupTestResources( $testResourceName, $testSqlFile, $testImageZip ) {
00159         global $wgDBname;
00160 
00161         // Basic security. Do not allow to drop productive database.
00162         if ( $testResourceName == $wgDBname ) {
00163                 die( 'Cannot override productive database.' );
00164         }
00165         if ( $testResourceName == '' ) {
00166                 die( 'Cannot identify a test the resources should be installed for.' );
00167         }
00168 
00169         // create tables
00170         $dbw = wfGetDB( DB_MASTER );
00171         $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
00172         $dbw->query( 'CREATE DATABASE ' . $testResourceName );
00173 
00174         // do not set the new DB name before database is setup
00175         $wgDBname = $testResourceName;
00176         $dbw->selectDB( $testResourceName );
00177         // populate from SQL file
00178         if ( $testSqlFile ) {
00179                 $dbw->sourceFile( $testSqlFile );
00180         }
00181 
00182         // create test image dir
00183         $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
00184         if ( !file_exists( $testUploadPath ) ) {
00185                 mkdir( $testUploadPath );
00186         }
00187 
00188         if ( $testImageZip ) {
00189                 $zip = new ZipArchive();
00190                 $zip->open( $testImageZip );
00191                 $zip->extractTo( $testUploadPath );
00192                 $zip->close();
00193         }
00194 }
00195 
00196 function teardownTestResources( $testResourceName ) {
00197         // remove test database
00198         $dbw = wfGetDB( DB_MASTER );
00199         $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
00200 
00201         $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
00202         // remove test image dir
00203         if ( file_exists( $testUploadPath ) ) {
00204                 wfRecursiveRemoveDir( $testUploadPath );
00205         }
00206 }
00207 
00208 function switchToTestResources( $testResourceName, $switchDB = true ) {
00209         global $wgDBuser, $wgDBpassword, $wgDBname;
00210         global $wgDBtestuser, $wgDBtestpassword;
00211         global $wgUploadPath;
00212 
00213         if ( $switchDB ) {
00214                 $wgDBname = $testResourceName;
00215         }
00216         $wgDBuser = $wgDBtestuser;
00217         $wgDBpassword = $wgDBtestpassword;
00218 
00219         $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
00220         $wgUploadPath = $testUploadPath;
00221 }