Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / SeleniumWebSettings.php
blob26c2df54a49c88f32e96479042c9422bb6a7d547
1 <?php
2 /**
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
23 * @file
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 die( 1 );
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
39 $testSqlFile = false;
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'];
46 if (
47 preg_match( '/[^a-zA-Z0-9_-]/', $setupTestSuiteName ) ||
48 !isset( $wgSeleniumTestConfigs[$setupTestSuiteName] )
51 return;
53 if ( strlen( $setupTestSuiteName ) > 0 ) {
54 $expire = time() + 600;
55 setcookie(
56 $cookieName,
57 $setupTestSuiteName,
58 $expire,
59 $wgCookiePath,
60 $wgCookieDomain,
61 $wgCookieSecure,
62 true
66 $testIncludes = array(); // array containing all the includes needed for this test
67 $testGlobalConfigs = array(); // an array containing 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;
90 setcookie(
91 $cookieName,
92 '',
93 $expire,
94 $wgCookiePath,
95 $wgCookieDomain,
96 $wgCookieSecure,
97 true
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] ) ) {
108 return;
111 $testIncludes = array(); // array containing all the includes needed for this test
112 $testGlobalConfigs = array(); // an array containing 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 );
128 } else {
129 $GLOBALS[$key] = $value;
134 wfProfileOut( $fname );
136 function getTestSuiteNameFromCookie( $cookieName ) {
137 $testSuiteName = null;
138 if ( isset( $_COOKIE[$cookieName] ) ) {
139 $testSuiteName = $_COOKIE[$cookieName];
141 return $testSuiteName;
144 function getTestResourceNameFromTestSuiteName( $testSuiteName ) {
145 $testResourceName = null;
146 if ( isset( $testSuiteName ) ) {
147 $testResourceName = $testSuiteName;
149 return $testResourceName;
152 function getTestUploadPathFromResourceName( $testResourceName ) {
153 global $IP;
154 $testUploadPath = "$IP/images/$testResourceName";
155 return $testUploadPath;
158 function setupTestResources( $testResourceName, $testSqlFile, $testImageZip ) {
159 global $wgDBname;
161 // Basic security. Do not allow to drop productive database.
162 if ( $testResourceName == $wgDBname ) {
163 die( 'Cannot override productive database.' );
165 if ( $testResourceName == '' ) {
166 die( 'Cannot identify a test the resources should be installed for.' );
169 // create tables
170 $dbw = wfGetDB( DB_MASTER );
171 $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
172 $dbw->query( 'CREATE DATABASE ' . $testResourceName );
174 // do not set the new DB name before database is setup
175 $wgDBname = $testResourceName;
176 $dbw->selectDB( $testResourceName );
177 // populate from SQL file
178 if ( $testSqlFile ) {
179 $dbw->sourceFile( $testSqlFile );
182 // create test image dir
183 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
184 if ( !file_exists( $testUploadPath ) ) {
185 mkdir( $testUploadPath );
188 if ( $testImageZip ) {
189 $zip = new ZipArchive();
190 $zip->open( $testImageZip );
191 $zip->extractTo( $testUploadPath );
192 $zip->close();
196 function teardownTestResources( $testResourceName ) {
197 // remove test database
198 $dbw = wfGetDB( DB_MASTER );
199 $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
201 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
202 // remove test image dir
203 if ( file_exists( $testUploadPath ) ) {
204 wfRecursiveRemoveDir( $testUploadPath );
208 function switchToTestResources( $testResourceName, $switchDB = true ) {
209 global $wgDBuser, $wgDBpassword, $wgDBname;
210 global $wgDBtestuser, $wgDBtestpassword;
211 global $wgUploadPath;
213 if ( $switchDB ) {
214 $wgDBname = $testResourceName;
216 $wgDBuser = $wgDBtestuser;
217 $wgDBpassword = $wgDBtestpassword;
219 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
220 $wgUploadPath = $testUploadPath;