Renamed $pairs => $triples.
[lhc/web/wiklou.git] / includes / SeleniumWebSettings.php
1 <?php
2 /**
3 * Dynamically change configuration variables based on the test suite name and a cookie value.
4 *
5 * For details on how to configure a wiki for a Selenium test, see:
6 * http://www.mediawiki.org/wiki/SeleniumFramework#Test_Wiki_configuration
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 die( 1 );
28 }
29
30 require_once( "$IP/includes/GlobalFunctions.php" );
31
32 $fname = 'SeleniumWebSettings.php';
33 wfProfileIn( $fname );
34
35 $cookiePrefix = $wgSitename . '-';
36 $cookieName = $cookiePrefix . 'Selenium';
37
38 // this is a fallback SQL file
39 $testSqlFile = false;
40 $testImageZip = false;
41
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'];
45
46 if (
47 preg_match( '/[^a-zA-Z0-9_-]/', $setupTestSuiteName ) ||
48 !isset( $wgSeleniumTestConfigs[$setupTestSuiteName] )
49 )
50 {
51 return;
52 }
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
63 );
64 }
65
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));
71
72 if ( isset( $testResourceFiles['images'] ) ) {
73 $testImageZip = $testResourceFiles['images'];
74 }
75
76 if ( isset( $testResourceFiles['db'] ) ) {
77 $testSqlFile = $testResourceFiles['db'];
78 $testResourceName = getTestResourceNameFromTestSuiteName( $setupTestSuiteName );
79
80 switchToTestResources( $testResourceName, false ); // false means do not switch database yet
81 setupTestResources( $testResourceName, $testSqlFile, $testImageZip );
82 }
83 }
84
85 // clear the cookie based on a request param
86 if ( isset( $_GET['clearTestSuite'] ) ) {
87 $testSuiteName = getTestSuiteNameFromCookie( $cookieName );
88
89 $expire = time() - 600;
90 setcookie(
91 $cookieName,
92 '',
93 $expire,
94 $wgCookiePath,
95 $wgCookieDomain,
96 $wgCookieSecure,
97 true
98 );
99
100 $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
101 teardownTestResources( $testResourceName );
102 }
103
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;
109 }
110
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));
116
117 if ( isset( $testResourceFiles['db'] ) ) {
118 $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
119 switchToTestResources( $testResourceName );
120 }
121 foreach ( $testIncludes as $includeFile ) {
122 $file = $IP . '/' . $includeFile;
123 require_once( $file );
124 }
125 foreach ( $testGlobalConfigs as $key => $value ) {
126 if ( is_array( $value ) ) {
127 $GLOBALS[$key] = array_merge( $GLOBALS[$key], $value );
128
129 } else {
130 $GLOBALS[$key] = $value;
131 }
132 }
133 }
134
135 wfProfileOut( $fname );
136
137 function getTestSuiteNameFromCookie( $cookieName ) {
138 $testSuiteName = null;
139 if ( isset( $_COOKIE[$cookieName] ) ) {
140 $testSuiteName = $_COOKIE[$cookieName];
141 }
142 return $testSuiteName;
143 }
144
145 function getTestResourceNameFromTestSuiteName( $testSuiteName ) {
146 $testResourceName = null;
147 if ( isset( $testSuiteName ) ) {
148 $testResourceName = $testSuiteName;
149 }
150 return $testResourceName;
151 }
152
153 function getTestUploadPathFromResourceName( $testResourceName ) {
154 global $IP;
155 $testUploadPath = "$IP/images/$testResourceName";
156 return $testUploadPath;
157 }
158
159 function setupTestResources( $testResourceName, $testSqlFile, $testImageZip ) {
160 global $wgDBname;
161
162 // Basic security. Do not allow to drop productive database.
163 if ( $testResourceName == $wgDBname ) {
164 die( 'Cannot override productive database.' );
165 }
166 if ( $testResourceName == '' ) {
167 die( 'Cannot identify a test the resources should be installed for.' );
168 }
169
170 // create tables
171 $dbw = wfGetDB( DB_MASTER );
172 $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
173 $dbw->query( 'CREATE DATABASE ' . $testResourceName );
174
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 );
181 }
182
183 // create test image dir
184 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
185 if ( !file_exists( $testUploadPath ) ) {
186 mkdir( $testUploadPath );
187 }
188
189 if ( $testImageZip ) {
190 $zip = new ZipArchive();
191 $zip->open( $testImageZip );
192 $zip->extractTo( $testUploadPath );
193 $zip->close();
194 }
195 }
196
197 function teardownTestResources( $testResourceName ) {
198 // remove test database
199 $dbw = wfGetDB( DB_MASTER );
200 $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
201
202 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
203 // remove test image dir
204 if ( file_exists( $testUploadPath ) ) {
205 wfRecursiveRemoveDir( $testUploadPath );
206 }
207 }
208
209 function switchToTestResources( $testResourceName, $switchDB = true ) {
210 global $wgDBuser, $wgDBpassword, $wgDBname;
211 global $wgDBtestuser, $wgDBtestpassword;
212 global $wgUploadPath;
213
214 if ( $switchDB ) {
215 $wgDBname = $testResourceName;
216 }
217 $wgDBuser = $wgDBtestuser;
218 $wgDBpassword = $wgDBtestpassword;
219
220 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
221 $wgUploadPath = $testUploadPath;
222 }