Merge "Implement static public Parser::getExternalLinkRel"
[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 } else {
129 $GLOBALS[$key] = $value;
130 }
131 }
132 }
133
134 wfProfileOut( $fname );
135
136 function getTestSuiteNameFromCookie( $cookieName ) {
137 $testSuiteName = null;
138 if ( isset( $_COOKIE[$cookieName] ) ) {
139 $testSuiteName = $_COOKIE[$cookieName];
140 }
141 return $testSuiteName;
142 }
143
144 function getTestResourceNameFromTestSuiteName( $testSuiteName ) {
145 $testResourceName = null;
146 if ( isset( $testSuiteName ) ) {
147 $testResourceName = $testSuiteName;
148 }
149 return $testResourceName;
150 }
151
152 function getTestUploadPathFromResourceName( $testResourceName ) {
153 global $IP;
154 $testUploadPath = "$IP/images/$testResourceName";
155 return $testUploadPath;
156 }
157
158 function setupTestResources( $testResourceName, $testSqlFile, $testImageZip ) {
159 global $wgDBname;
160
161 // Basic security. Do not allow to drop productive database.
162 if ( $testResourceName == $wgDBname ) {
163 die( 'Cannot override productive database.' );
164 }
165 if ( $testResourceName == '' ) {
166 die( 'Cannot identify a test the resources should be installed for.' );
167 }
168
169 // create tables
170 $dbw = wfGetDB( DB_MASTER );
171 $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
172 $dbw->query( 'CREATE DATABASE ' . $testResourceName );
173
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 );
180 }
181
182 // create test image dir
183 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
184 if ( !file_exists( $testUploadPath ) ) {
185 mkdir( $testUploadPath );
186 }
187
188 if ( $testImageZip ) {
189 $zip = new ZipArchive();
190 $zip->open( $testImageZip );
191 $zip->extractTo( $testUploadPath );
192 $zip->close();
193 }
194 }
195
196 function teardownTestResources( $testResourceName ) {
197 // remove test database
198 $dbw = wfGetDB( DB_MASTER );
199 $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
200
201 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
202 // remove test image dir
203 if ( file_exists( $testUploadPath ) ) {
204 wfRecursiveRemoveDir( $testUploadPath );
205 }
206 }
207
208 function switchToTestResources( $testResourceName, $switchDB = true ) {
209 global $wgDBuser, $wgDBpassword, $wgDBname;
210 global $wgDBtestuser, $wgDBtestpassword;
211 global $wgUploadPath;
212
213 if ( $switchDB ) {
214 $wgDBname = $testResourceName;
215 }
216 $wgDBuser = $wgDBtestuser;
217 $wgDBpassword = $wgDBtestpassword;
218
219 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
220 $wgUploadPath = $testUploadPath;
221 }