3 * Dynamically change configuration variables based on the test suite name and a cookie value.
4 * For details on how to configure a wiki for a Selenium test, see:
5 * http://www.mediawiki.org/wiki/SeleniumFramework#Test_Wiki_configuration
7 if ( !defined( 'MEDIAWIKI' ) ) {
11 require_once( "$IP/includes/GlobalFunctions.php" );
13 $fname = 'SeleniumWebSettings.php';
14 wfProfileIn( $fname );
16 $cookiePrefix = $wgSitename . "-";
17 $cookieName = $cookiePrefix . "Selenium";
19 // this is a fallback sql file
21 $testImageZip = false;
23 //if we find a request parameter containing the test name, set a cookie with the test name
24 if ( isset( $_GET['setupTestSuite'] ) ) {
25 $setupTestSuiteName = $_GET['setupTestSuite'];
27 if ( preg_match( '/[^a-zA-Z0-9_-]/', $setupTestSuiteName ) ||
!isset( $wgSeleniumTestConfigs[$setupTestSuiteName] ) ) {
30 if ( strlen( $setupTestSuiteName) > 0 ) {
31 $expire = time() +
600;
32 setcookie( $cookieName,
41 $testIncludes = array(); //array containing all the includes needed for this test
42 $testGlobalConfigs = array(); //an array containg all the global configs needed for this test
43 $testResourceFiles = array(); // an array containing all the resource files needed for this test
44 $callback = $wgSeleniumTestConfigs[$setupTestSuiteName];
45 call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
47 if ( isset( $testResourceFiles['db'] ) ) {
48 $testSqlFile = $testResourceFiles['db'];
50 if ( isset($testResourceFiles['images']) ) {
51 $testImageZip = $testResourceFiles['images'];
54 $testResourceName = getTestResourceNameFromTestSuiteName( $setupTestSuiteName );
55 switchToTestResources( $testResourceName, false ); // false means do not switch database yet
56 setupTestResources( $testResourceName, $testSqlFile, $testImageZip );
59 //clear the cookie based on a request param
60 if ( isset( $_GET['clearTestSuite'] ) ) {
61 $testSuiteName = getTestSuiteNameFromCookie( $cookieName );
63 $expire = time() - 600;
64 setcookie( $cookieName,
72 $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
73 teardownTestResources( $testResourceName );
76 //if a cookie is found, run the appropriate callback to get the config params.
77 if ( isset( $_COOKIE[$cookieName] ) ) {
78 $testSuiteName = getTestSuiteNameFromCookie( $cookieName );
79 if ( !isset( $wgSeleniumTestConfigs[$testSuiteName] ) ) {
83 $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
84 switchToTestResources( $testResourceName );
86 $testIncludes = array(); //array containing all the includes needed for this test
87 $testGlobalConfigs = array(); //an array containg all the global configs needed for this test
88 $testResourceFiles = array(); // an array containing all the resource files needed for this test
89 $callback = $wgSeleniumTestConfigs[$testSuiteName];
90 call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
92 foreach ( $testIncludes as $includeFile ) {
93 $file = $IP . '/' . $includeFile;
94 require_once( $file );
96 foreach ( $testGlobalConfigs as $key => $value ) {
97 if ( is_array( $value ) ) {
98 $GLOBALS[$key] = array_merge( $GLOBALS[$key], $value );
101 $GLOBALS[$key] = $value;
106 wfProfileOut( $fname );
108 function getTestSuiteNameFromCookie( $cookieName ) {
109 $testSuiteName = null;
110 if ( isset( $_COOKIE[$cookieName] ) ) {
111 $testSuiteName = $_COOKIE[$cookieName];
113 return $testSuiteName;
116 function getTestResourceNameFromTestSuiteName( $testSuiteName ) {
117 $testResourceName = null;
118 if ( isset( $testSuiteName ) ) {
119 $testResourceName = $testSuiteName;
121 return $testResourceName;
124 function getTestUploadPathFromResourceName( $testResourceName ) {
126 $testUploadPath = "$IP/images/$testResourceName";
127 return $testUploadPath;
130 function setupTestResources( $testResourceName, $testSqlFile, $testImageZip ) {
131 global $wgDBname, $wgSeleniumUseTestResources;
133 if ( !$wgSeleniumUseTestResources ) {
136 // Basic security. Do not allow to drop productive database.
137 if ( $testResouceName == $wgDBname ) {
138 die( "Cannot override productive database." );
140 if ( $testResourceName == '' ) {
141 die( "Cannot identify a test the resources should be installed for." );
145 $dbw =& wfGetDB( DB_MASTER
);
146 $dbw->query( "DROP DATABASE IF EXISTS ".$testResourceName );
147 $dbw->query( "CREATE DATABASE ".$testResourceName );
149 // do not set the new db name before database is setup
150 $wgDBname = $testResourceName;
151 $dbw->selectDB( $testResourceName );
152 // populate from sql file
153 if ( $testSqlFile ) {
154 $dbw->sourceFile( $testSqlFile );
157 // create test image dir
158 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
159 if ( !file_exists( $testUploadPath ) ) {
160 mkdir( $testUploadPath );
163 if ( $testImageZip ) {
164 $zip = new ZipArchive();
165 $zip->open( $testImageZip );
166 $zip->extractTo( $testUploadPath );
171 function teardownTestResources( $testResourceName ) {
172 global $wgSeleniumUseTestResources;
174 if ( !$wgSeleniumUseTestResources ) {
177 // remove test database
178 $dbw =& wfGetDB( DB_MASTER
);
179 $dbw->query( "DROP DATABASE IF EXISTS ".$testResourceName );
181 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
182 // remove test image dir
183 if ( file_exists( $testUploadPath ) ) {
184 wfRecursiveRemoveDir( $testUploadPath );
188 function switchToTestResources( $testResourceName, $switchDB = true ) {
189 global $wgDBuser, $wgDBpassword, $wgDBname, $wgDBprefix;
190 global $wgDBtestuser, $wgDBtestpassword, $wgDBtestprefix;
191 global $wgUploadPath;
192 global $wgSeleniumUseTestResources;
194 if ( !$wgSeleniumUseTestResources ) {
199 $wgDBname = $testResourceName;
201 $wgDBuser = $wgDBtestuser;
202 $wgDBpassword = $wgDBtestpassword;
203 //$wgDBprefix = $wgDBtestprefix;
205 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
206 $wgUploadPath = $testUploadPath;
209 function wfRecursiveRemoveDir( $dir ) {
210 // taken from http://de3.php.net/manual/en/function.rmdir.php#98622
211 if ( is_dir( $dir ) ) {
212 $objects = scandir( $dir );
213 foreach ( $objects as $object ) {
214 if ( $object != "." && $object != ".." ) {
215 if ( filetype( $dir."/".$object ) == "dir" ) {
216 wfRecursiveRemoveDir( $dir."/".$object );
218 unlink( $dir."/".$object );