2010-10-14 16:38:40 +00:00
|
|
|
<?php
|
2011-06-25 23:27:05 +00:00
|
|
|
/**
|
2010-10-14 16:38:40 +00:00
|
|
|
* Dynamically change configuration variables based on the test suite name and a cookie value.
|
|
|
|
|
* For details on how to configure a wiki for a Selenium test, see:
|
|
|
|
|
* http://www.mediawiki.org/wiki/SeleniumFramework#Test_Wiki_configuration
|
|
|
|
|
*/
|
2010-10-18 18:15:13 +00:00
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) {
|
|
|
|
|
die( 1 );
|
2010-10-14 16:38:40 +00:00
|
|
|
}
|
2010-10-18 18:15:13 +00:00
|
|
|
|
2011-01-01 15:47:43 +00:00
|
|
|
require_once( "$IP/includes/GlobalFunctions.php" );
|
|
|
|
|
|
2010-10-18 18:15:13 +00:00
|
|
|
$fname = 'SeleniumWebSettings.php';
|
|
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
2011-06-25 23:27:05 +00:00
|
|
|
$cookiePrefix = $wgSitename . '-';
|
|
|
|
|
$cookieName = $cookiePrefix . 'Selenium';
|
2010-10-14 16:38:40 +00:00
|
|
|
|
2011-06-25 23:27:05 +00:00
|
|
|
// this is a fallback SQL file
|
2011-01-01 15:47:43 +00:00
|
|
|
$testSqlFile = false;
|
|
|
|
|
$testImageZip = false;
|
|
|
|
|
|
2011-06-25 23:27:05 +00:00
|
|
|
// if we find a request parameter containing the test name, set a cookie with the test name
|
2010-10-18 18:15:13 +00:00
|
|
|
if ( isset( $_GET['setupTestSuite'] ) ) {
|
2010-10-14 16:38:40 +00:00
|
|
|
$setupTestSuiteName = $_GET['setupTestSuite'];
|
2011-01-01 15:47:43 +00:00
|
|
|
|
2011-06-25 23:27:05 +00:00
|
|
|
if (
|
|
|
|
|
preg_match( '/[^a-zA-Z0-9_-]/', $setupTestSuiteName ) ||
|
|
|
|
|
!isset( $wgSeleniumTestConfigs[$setupTestSuiteName] )
|
|
|
|
|
)
|
|
|
|
|
{
|
2010-10-18 18:15:13 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2011-06-25 23:27:05 +00:00
|
|
|
if ( strlen( $setupTestSuiteName ) > 0 ) {
|
2010-10-14 16:38:40 +00:00
|
|
|
$expire = time() + 600;
|
2011-06-25 23:27:05 +00:00
|
|
|
setcookie(
|
|
|
|
|
$cookieName,
|
2010-10-14 16:38:40 +00:00
|
|
|
$setupTestSuiteName,
|
|
|
|
|
$expire,
|
|
|
|
|
$wgCookiePath,
|
|
|
|
|
$wgCookieDomain,
|
|
|
|
|
$wgCookieSecure,
|
2011-06-25 23:27:05 +00:00
|
|
|
true
|
|
|
|
|
);
|
2010-10-14 16:38:40 +00:00
|
|
|
}
|
2011-01-01 15:47:43 +00:00
|
|
|
|
2011-06-25 23:27:05 +00:00
|
|
|
$testIncludes = array(); // array containing all the includes needed for this test
|
|
|
|
|
$testGlobalConfigs = array(); // an array containg all the global configs needed for this test
|
2011-01-01 15:47:43 +00:00
|
|
|
$testResourceFiles = array(); // an array containing all the resource files needed for this test
|
|
|
|
|
$callback = $wgSeleniumTestConfigs[$setupTestSuiteName];
|
|
|
|
|
call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
|
2011-01-11 00:36:36 +00:00
|
|
|
|
2011-06-25 23:27:05 +00:00
|
|
|
if ( isset( $testResourceFiles['images'] ) ) {
|
2011-01-11 00:36:36 +00:00
|
|
|
$testImageZip = $testResourceFiles['images'];
|
|
|
|
|
}
|
2011-01-01 15:47:43 +00:00
|
|
|
|
|
|
|
|
if ( isset( $testResourceFiles['db'] ) ) {
|
|
|
|
|
$testSqlFile = $testResourceFiles['db'];
|
2011-01-11 00:36:36 +00:00
|
|
|
$testResourceName = getTestResourceNameFromTestSuiteName( $setupTestSuiteName );
|
|
|
|
|
|
|
|
|
|
switchToTestResources( $testResourceName, false ); // false means do not switch database yet
|
|
|
|
|
setupTestResources( $testResourceName, $testSqlFile, $testImageZip );
|
2011-01-01 15:47:43 +00:00
|
|
|
}
|
2010-10-14 16:38:40 +00:00
|
|
|
}
|
2011-01-01 15:47:43 +00:00
|
|
|
|
2011-06-25 23:27:05 +00:00
|
|
|
// clear the cookie based on a request param
|
2010-10-18 18:15:13 +00:00
|
|
|
if ( isset( $_GET['clearTestSuite'] ) ) {
|
2011-01-01 15:47:43 +00:00
|
|
|
$testSuiteName = getTestSuiteNameFromCookie( $cookieName );
|
|
|
|
|
|
|
|
|
|
$expire = time() - 600;
|
2011-06-25 23:27:05 +00:00
|
|
|
setcookie(
|
|
|
|
|
$cookieName,
|
2011-01-01 15:47:43 +00:00
|
|
|
'',
|
|
|
|
|
$expire,
|
|
|
|
|
$wgCookiePath,
|
|
|
|
|
$wgCookieDomain,
|
|
|
|
|
$wgCookieSecure,
|
2011-06-25 23:27:05 +00:00
|
|
|
true
|
|
|
|
|
);
|
2011-01-01 15:47:43 +00:00
|
|
|
|
|
|
|
|
$testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
|
|
|
|
|
teardownTestResources( $testResourceName );
|
2010-10-14 16:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-25 23:27:05 +00:00
|
|
|
// if a cookie is found, run the appropriate callback to get the config params.
|
2010-10-18 18:15:13 +00:00
|
|
|
if ( isset( $_COOKIE[$cookieName] ) ) {
|
2011-01-01 15:47:43 +00:00
|
|
|
$testSuiteName = getTestSuiteNameFromCookie( $cookieName );
|
2010-10-18 18:15:13 +00:00
|
|
|
if ( !isset( $wgSeleniumTestConfigs[$testSuiteName] ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-01-01 15:47:43 +00:00
|
|
|
|
2011-06-25 23:27:05 +00:00
|
|
|
$testIncludes = array(); // array containing all the includes needed for this test
|
|
|
|
|
$testGlobalConfigs = array(); // an array containg all the global configs needed for this test
|
2011-01-01 15:47:43 +00:00
|
|
|
$testResourceFiles = array(); // an array containing all the resource files needed for this test
|
2010-10-18 18:15:13 +00:00
|
|
|
$callback = $wgSeleniumTestConfigs[$testSuiteName];
|
2011-01-01 15:47:43 +00:00
|
|
|
call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
|
2011-01-11 00:36:36 +00:00
|
|
|
|
|
|
|
|
if ( isset( $testResourceFiles['db'] ) ) {
|
|
|
|
|
$testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
|
|
|
|
|
switchToTestResources( $testResourceName );
|
|
|
|
|
}
|
2010-10-14 16:38:40 +00:00
|
|
|
foreach ( $testIncludes as $includeFile ) {
|
|
|
|
|
$file = $IP . '/' . $includeFile;
|
|
|
|
|
require_once( $file );
|
|
|
|
|
}
|
|
|
|
|
foreach ( $testGlobalConfigs as $key => $value ) {
|
2010-10-18 18:15:13 +00:00
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
|
$GLOBALS[$key] = array_merge( $GLOBALS[$key], $value );
|
2010-10-14 16:38:40 +00:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
$GLOBALS[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-10-18 18:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wfProfileOut( $fname );
|
2011-01-01 15:47:43 +00:00
|
|
|
|
|
|
|
|
function getTestSuiteNameFromCookie( $cookieName ) {
|
|
|
|
|
$testSuiteName = null;
|
|
|
|
|
if ( isset( $_COOKIE[$cookieName] ) ) {
|
|
|
|
|
$testSuiteName = $_COOKIE[$cookieName];
|
|
|
|
|
}
|
|
|
|
|
return $testSuiteName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getTestResourceNameFromTestSuiteName( $testSuiteName ) {
|
|
|
|
|
$testResourceName = null;
|
|
|
|
|
if ( isset( $testSuiteName ) ) {
|
|
|
|
|
$testResourceName = $testSuiteName;
|
|
|
|
|
}
|
|
|
|
|
return $testResourceName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getTestUploadPathFromResourceName( $testResourceName ) {
|
|
|
|
|
global $IP;
|
|
|
|
|
$testUploadPath = "$IP/images/$testResourceName";
|
|
|
|
|
return $testUploadPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setupTestResources( $testResourceName, $testSqlFile, $testImageZip ) {
|
2011-01-11 00:36:36 +00:00
|
|
|
global $wgDBname;
|
|
|
|
|
|
2011-01-01 15:47:43 +00:00
|
|
|
// Basic security. Do not allow to drop productive database.
|
2011-01-11 00:36:36 +00:00
|
|
|
if ( $testResourceName == $wgDBname ) {
|
2011-06-25 23:27:05 +00:00
|
|
|
die( 'Cannot override productive database.' );
|
2011-01-01 15:47:43 +00:00
|
|
|
}
|
|
|
|
|
if ( $testResourceName == '' ) {
|
2011-06-25 23:27:05 +00:00
|
|
|
die( 'Cannot identify a test the resources should be installed for.' );
|
2011-01-01 15:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-25 23:27:05 +00:00
|
|
|
// create tables
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
|
|
|
|
|
$dbw->query( 'CREATE DATABASE ' . $testResourceName );
|
2011-01-01 15:47:43 +00:00
|
|
|
|
2011-06-25 23:27:05 +00:00
|
|
|
// do not set the new DB name before database is setup
|
2011-01-01 15:47:43 +00:00
|
|
|
$wgDBname = $testResourceName;
|
|
|
|
|
$dbw->selectDB( $testResourceName );
|
2011-06-25 23:27:05 +00:00
|
|
|
// populate from SQL file
|
2011-01-01 15:47:43 +00:00
|
|
|
if ( $testSqlFile ) {
|
|
|
|
|
$dbw->sourceFile( $testSqlFile );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create test image dir
|
|
|
|
|
$testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
|
|
|
|
|
if ( !file_exists( $testUploadPath ) ) {
|
|
|
|
|
mkdir( $testUploadPath );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $testImageZip ) {
|
|
|
|
|
$zip = new ZipArchive();
|
|
|
|
|
$zip->open( $testImageZip );
|
|
|
|
|
$zip->extractTo( $testUploadPath );
|
|
|
|
|
$zip->close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function teardownTestResources( $testResourceName ) {
|
|
|
|
|
// remove test database
|
2011-06-25 23:27:05 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
|
2011-01-01 15:47:43 +00:00
|
|
|
|
|
|
|
|
$testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
|
|
|
|
|
// remove test image dir
|
|
|
|
|
if ( file_exists( $testUploadPath ) ) {
|
|
|
|
|
wfRecursiveRemoveDir( $testUploadPath );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function switchToTestResources( $testResourceName, $switchDB = true ) {
|
2011-01-11 00:36:36 +00:00
|
|
|
global $wgDBuser, $wgDBpassword, $wgDBname;
|
|
|
|
|
global $wgDBtestuser, $wgDBtestpassword;
|
2011-01-01 15:47:43 +00:00
|
|
|
global $wgUploadPath;
|
|
|
|
|
|
|
|
|
|
if ( $switchDB ) {
|
|
|
|
|
$wgDBname = $testResourceName;
|
|
|
|
|
}
|
|
|
|
|
$wgDBuser = $wgDBtestuser;
|
|
|
|
|
$wgDBpassword = $wgDBtestpassword;
|
|
|
|
|
|
2011-06-25 23:27:05 +00:00
|
|
|
$testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
|
2011-01-01 15:47:43 +00:00
|
|
|
$wgUploadPath = $testUploadPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function wfRecursiveRemoveDir( $dir ) {
|
|
|
|
|
// taken from http://de3.php.net/manual/en/function.rmdir.php#98622
|
|
|
|
|
if ( is_dir( $dir ) ) {
|
|
|
|
|
$objects = scandir( $dir );
|
|
|
|
|
foreach ( $objects as $object ) {
|
|
|
|
|
if ( $object != "." && $object != ".." ) {
|
2011-06-25 23:27:05 +00:00
|
|
|
if ( filetype( $dir . '/' . $object ) == "dir" ) {
|
|
|
|
|
wfRecursiveRemoveDir( $dir . '/' . $object );
|
2011-01-01 15:47:43 +00:00
|
|
|
} else {
|
2011-06-25 23:27:05 +00:00
|
|
|
unlink( $dir . '/' . $object );
|
2011-01-01 15:47:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
reset( $objects );
|
|
|
|
|
rmdir( $dir );
|
|
|
|
|
}
|
|
|
|
|
}
|