MediaWiki  REL1_20
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 containg 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 containg 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                         
00129                 } else {
00130                         $GLOBALS[$key] = $value;
00131                 }
00132         }
00133 }
00134 
00135 wfProfileOut( $fname );
00136 
00137 function getTestSuiteNameFromCookie( $cookieName ) {
00138         $testSuiteName = null;
00139         if ( isset( $_COOKIE[$cookieName] ) ) {
00140                 $testSuiteName = $_COOKIE[$cookieName];
00141         }
00142         return $testSuiteName;
00143 }
00144 
00145 function getTestResourceNameFromTestSuiteName( $testSuiteName ) {
00146         $testResourceName = null;
00147         if ( isset( $testSuiteName ) ) {
00148                 $testResourceName = $testSuiteName;
00149         }
00150         return $testResourceName;
00151 }
00152 
00153 function getTestUploadPathFromResourceName( $testResourceName ) {
00154         global $IP;
00155         $testUploadPath = "$IP/images/$testResourceName";
00156         return $testUploadPath;
00157 }
00158 
00159 function setupTestResources( $testResourceName, $testSqlFile, $testImageZip ) {
00160         global $wgDBname;
00161 
00162         // Basic security. Do not allow to drop productive database.
00163         if ( $testResourceName == $wgDBname ) {
00164                 die( 'Cannot override productive database.' );
00165         }
00166         if ( $testResourceName == '' ) {
00167                 die( 'Cannot identify a test the resources should be installed for.' );
00168         }
00169         
00170         // create tables
00171         $dbw = wfGetDB( DB_MASTER );
00172         $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
00173         $dbw->query( 'CREATE DATABASE ' . $testResourceName );
00174 
00175         // do not set the new DB name before database is setup
00176         $wgDBname = $testResourceName;
00177         $dbw->selectDB( $testResourceName );
00178         // populate from SQL file
00179         if ( $testSqlFile ) {
00180                 $dbw->sourceFile( $testSqlFile );
00181         }
00182 
00183         // create test image dir
00184         $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
00185         if ( !file_exists( $testUploadPath ) ) {
00186                 mkdir( $testUploadPath );
00187         }
00188 
00189         if ( $testImageZip ) {
00190                 $zip = new ZipArchive();
00191                 $zip->open( $testImageZip );
00192                 $zip->extractTo( $testUploadPath );
00193                 $zip->close();
00194         }
00195 }
00196 
00197 function teardownTestResources( $testResourceName ) {
00198         // remove test database
00199         $dbw = wfGetDB( DB_MASTER );
00200         $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
00201 
00202         $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
00203         // remove test image dir
00204         if ( file_exists( $testUploadPath ) ) {
00205                 wfRecursiveRemoveDir( $testUploadPath );
00206         }
00207 }
00208 
00209 function switchToTestResources( $testResourceName, $switchDB = true ) {
00210         global $wgDBuser, $wgDBpassword, $wgDBname;
00211         global $wgDBtestuser, $wgDBtestpassword;
00212         global $wgUploadPath;
00213 
00214         if ( $switchDB ) {
00215                 $wgDBname = $testResourceName;
00216         }
00217         $wgDBuser = $wgDBtestuser;
00218         $wgDBpassword = $wgDBtestpassword;
00219 
00220         $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
00221         $wgUploadPath = $testUploadPath;
00222 }