3 * Dynamically change configuration variables based on the test suite name and a cookie value.
5 * For details on how to configure a wiki for a Selenium test, see:
6 * http://www.mediawiki.org/wiki/SeleniumFramework#Test_Wiki_configuration
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
30 require_once( "$IP/includes/GlobalFunctions.php" );
32 $fname = 'SeleniumWebSettings.php';
33 wfProfileIn( $fname );
35 $cookiePrefix = $wgSitename . '-';
36 $cookieName = $cookiePrefix . 'Selenium';
38 // this is a fallback SQL file
40 $testImageZip = false;
42 // if we find a request parameter containing the test name, set a cookie with the test name
43 if ( isset( $_GET['setupTestSuite'] ) ) {
44 $setupTestSuiteName = $_GET['setupTestSuite'];
47 preg_match( '/[^a-zA-Z0-9_-]/', $setupTestSuiteName ) ||
48 !isset( $wgSeleniumTestConfigs[$setupTestSuiteName] )
53 if ( strlen( $setupTestSuiteName ) > 0 ) {
54 $expire = time() +
600;
66 $testIncludes = array(); // array containing all the includes needed for this test
67 $testGlobalConfigs = array(); // an array containg all the global configs needed for this test
68 $testResourceFiles = array(); // an array containing all the resource files needed for this test
69 $callback = $wgSeleniumTestConfigs[$setupTestSuiteName];
70 call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
72 if ( isset( $testResourceFiles['images'] ) ) {
73 $testImageZip = $testResourceFiles['images'];
76 if ( isset( $testResourceFiles['db'] ) ) {
77 $testSqlFile = $testResourceFiles['db'];
78 $testResourceName = getTestResourceNameFromTestSuiteName( $setupTestSuiteName );
80 switchToTestResources( $testResourceName, false ); // false means do not switch database yet
81 setupTestResources( $testResourceName, $testSqlFile, $testImageZip );
85 // clear the cookie based on a request param
86 if ( isset( $_GET['clearTestSuite'] ) ) {
87 $testSuiteName = getTestSuiteNameFromCookie( $cookieName );
89 $expire = time() - 600;
100 $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
101 teardownTestResources( $testResourceName );
104 // if a cookie is found, run the appropriate callback to get the config params.
105 if ( isset( $_COOKIE[$cookieName] ) ) {
106 $testSuiteName = getTestSuiteNameFromCookie( $cookieName );
107 if ( !isset( $wgSeleniumTestConfigs[$testSuiteName] ) ) {
111 $testIncludes = array(); // array containing all the includes needed for this test
112 $testGlobalConfigs = array(); // an array containg all the global configs needed for this test
113 $testResourceFiles = array(); // an array containing all the resource files needed for this test
114 $callback = $wgSeleniumTestConfigs[$testSuiteName];
115 call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
117 if ( isset( $testResourceFiles['db'] ) ) {
118 $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
119 switchToTestResources( $testResourceName );
121 foreach ( $testIncludes as $includeFile ) {
122 $file = $IP . '/' . $includeFile;
123 require_once( $file );
125 foreach ( $testGlobalConfigs as $key => $value ) {
126 if ( is_array( $value ) ) {
127 $GLOBALS[$key] = array_merge( $GLOBALS[$key], $value );
130 $GLOBALS[$key] = $value;
135 wfProfileOut( $fname );
137 function getTestSuiteNameFromCookie( $cookieName ) {
138 $testSuiteName = null;
139 if ( isset( $_COOKIE[$cookieName] ) ) {
140 $testSuiteName = $_COOKIE[$cookieName];
142 return $testSuiteName;
145 function getTestResourceNameFromTestSuiteName( $testSuiteName ) {
146 $testResourceName = null;
147 if ( isset( $testSuiteName ) ) {
148 $testResourceName = $testSuiteName;
150 return $testResourceName;
153 function getTestUploadPathFromResourceName( $testResourceName ) {
155 $testUploadPath = "$IP/images/$testResourceName";
156 return $testUploadPath;
159 function setupTestResources( $testResourceName, $testSqlFile, $testImageZip ) {
162 // Basic security. Do not allow to drop productive database.
163 if ( $testResourceName == $wgDBname ) {
164 die( 'Cannot override productive database.' );
166 if ( $testResourceName == '' ) {
167 die( 'Cannot identify a test the resources should be installed for.' );
171 $dbw = wfGetDB( DB_MASTER
);
172 $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
173 $dbw->query( 'CREATE DATABASE ' . $testResourceName );
175 // do not set the new DB name before database is setup
176 $wgDBname = $testResourceName;
177 $dbw->selectDB( $testResourceName );
178 // populate from SQL file
179 if ( $testSqlFile ) {
180 $dbw->sourceFile( $testSqlFile );
183 // create test image dir
184 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
185 if ( !file_exists( $testUploadPath ) ) {
186 mkdir( $testUploadPath );
189 if ( $testImageZip ) {
190 $zip = new ZipArchive();
191 $zip->open( $testImageZip );
192 $zip->extractTo( $testUploadPath );
197 function teardownTestResources( $testResourceName ) {
198 // remove test database
199 $dbw = wfGetDB( DB_MASTER
);
200 $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
202 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
203 // remove test image dir
204 if ( file_exists( $testUploadPath ) ) {
205 wfRecursiveRemoveDir( $testUploadPath );
209 function switchToTestResources( $testResourceName, $switchDB = true ) {
210 global $wgDBuser, $wgDBpassword, $wgDBname;
211 global $wgDBtestuser, $wgDBtestpassword;
212 global $wgUploadPath;
215 $wgDBname = $testResourceName;
217 $wgDBuser = $wgDBtestuser;
218 $wgDBpassword = $wgDBtestpassword;
220 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
221 $wgUploadPath = $testUploadPath;