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