Bug 35034 - moved autocomment-prefix between the prefix and the arrow. Follow up...
[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 * For details on how to configure a wiki for a Selenium test, see:
5 * http://www.mediawiki.org/wiki/SeleniumFramework#Test_Wiki_configuration
6 */
7 if ( !defined( 'MEDIAWIKI' ) ) {
8 die( 1 );
9 }
10
11 require_once( "$IP/includes/GlobalFunctions.php" );
12
13 $fname = 'SeleniumWebSettings.php';
14 wfProfileIn( $fname );
15
16 $cookiePrefix = $wgSitename . '-';
17 $cookieName = $cookiePrefix . 'Selenium';
18
19 // this is a fallback SQL file
20 $testSqlFile = false;
21 $testImageZip = false;
22
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'];
26
27 if (
28 preg_match( '/[^a-zA-Z0-9_-]/', $setupTestSuiteName ) ||
29 !isset( $wgSeleniumTestConfigs[$setupTestSuiteName] )
30 )
31 {
32 return;
33 }
34 if ( strlen( $setupTestSuiteName ) > 0 ) {
35 $expire = time() + 600;
36 setcookie(
37 $cookieName,
38 $setupTestSuiteName,
39 $expire,
40 $wgCookiePath,
41 $wgCookieDomain,
42 $wgCookieSecure,
43 true
44 );
45 }
46
47 $testIncludes = array(); // array containing all the includes needed for this test
48 $testGlobalConfigs = array(); // an array containg all the global configs needed for this test
49 $testResourceFiles = array(); // an array containing all the resource files needed for this test
50 $callback = $wgSeleniumTestConfigs[$setupTestSuiteName];
51 call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
52
53 if ( isset( $testResourceFiles['images'] ) ) {
54 $testImageZip = $testResourceFiles['images'];
55 }
56
57 if ( isset( $testResourceFiles['db'] ) ) {
58 $testSqlFile = $testResourceFiles['db'];
59 $testResourceName = getTestResourceNameFromTestSuiteName( $setupTestSuiteName );
60
61 switchToTestResources( $testResourceName, false ); // false means do not switch database yet
62 setupTestResources( $testResourceName, $testSqlFile, $testImageZip );
63 }
64 }
65
66 // clear the cookie based on a request param
67 if ( isset( $_GET['clearTestSuite'] ) ) {
68 $testSuiteName = getTestSuiteNameFromCookie( $cookieName );
69
70 $expire = time() - 600;
71 setcookie(
72 $cookieName,
73 '',
74 $expire,
75 $wgCookiePath,
76 $wgCookieDomain,
77 $wgCookieSecure,
78 true
79 );
80
81 $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
82 teardownTestResources( $testResourceName );
83 }
84
85 // if a cookie is found, run the appropriate callback to get the config params.
86 if ( isset( $_COOKIE[$cookieName] ) ) {
87 $testSuiteName = getTestSuiteNameFromCookie( $cookieName );
88 if ( !isset( $wgSeleniumTestConfigs[$testSuiteName] ) ) {
89 return;
90 }
91
92 $testIncludes = array(); // array containing all the includes needed for this test
93 $testGlobalConfigs = array(); // an array containg all the global configs needed for this test
94 $testResourceFiles = array(); // an array containing all the resource files needed for this test
95 $callback = $wgSeleniumTestConfigs[$testSuiteName];
96 call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
97
98 if ( isset( $testResourceFiles['db'] ) ) {
99 $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
100 switchToTestResources( $testResourceName );
101 }
102 foreach ( $testIncludes as $includeFile ) {
103 $file = $IP . '/' . $includeFile;
104 require_once( $file );
105 }
106 foreach ( $testGlobalConfigs as $key => $value ) {
107 if ( is_array( $value ) ) {
108 $GLOBALS[$key] = array_merge( $GLOBALS[$key], $value );
109
110 } else {
111 $GLOBALS[$key] = $value;
112 }
113 }
114 }
115
116 wfProfileOut( $fname );
117
118 function getTestSuiteNameFromCookie( $cookieName ) {
119 $testSuiteName = null;
120 if ( isset( $_COOKIE[$cookieName] ) ) {
121 $testSuiteName = $_COOKIE[$cookieName];
122 }
123 return $testSuiteName;
124 }
125
126 function getTestResourceNameFromTestSuiteName( $testSuiteName ) {
127 $testResourceName = null;
128 if ( isset( $testSuiteName ) ) {
129 $testResourceName = $testSuiteName;
130 }
131 return $testResourceName;
132 }
133
134 function getTestUploadPathFromResourceName( $testResourceName ) {
135 global $IP;
136 $testUploadPath = "$IP/images/$testResourceName";
137 return $testUploadPath;
138 }
139
140 function setupTestResources( $testResourceName, $testSqlFile, $testImageZip ) {
141 global $wgDBname;
142
143 // Basic security. Do not allow to drop productive database.
144 if ( $testResourceName == $wgDBname ) {
145 die( 'Cannot override productive database.' );
146 }
147 if ( $testResourceName == '' ) {
148 die( 'Cannot identify a test the resources should be installed for.' );
149 }
150
151 // create tables
152 $dbw = wfGetDB( DB_MASTER );
153 $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
154 $dbw->query( 'CREATE DATABASE ' . $testResourceName );
155
156 // do not set the new DB name before database is setup
157 $wgDBname = $testResourceName;
158 $dbw->selectDB( $testResourceName );
159 // populate from SQL file
160 if ( $testSqlFile ) {
161 $dbw->sourceFile( $testSqlFile );
162 }
163
164 // create test image dir
165 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
166 if ( !file_exists( $testUploadPath ) ) {
167 mkdir( $testUploadPath );
168 }
169
170 if ( $testImageZip ) {
171 $zip = new ZipArchive();
172 $zip->open( $testImageZip );
173 $zip->extractTo( $testUploadPath );
174 $zip->close();
175 }
176 }
177
178 function teardownTestResources( $testResourceName ) {
179 // remove test database
180 $dbw = wfGetDB( DB_MASTER );
181 $dbw->query( 'DROP DATABASE IF EXISTS ' . $testResourceName );
182
183 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
184 // remove test image dir
185 if ( file_exists( $testUploadPath ) ) {
186 wfRecursiveRemoveDir( $testUploadPath );
187 }
188 }
189
190 function switchToTestResources( $testResourceName, $switchDB = true ) {
191 global $wgDBuser, $wgDBpassword, $wgDBname;
192 global $wgDBtestuser, $wgDBtestpassword;
193 global $wgUploadPath;
194
195 if ( $switchDB ) {
196 $wgDBname = $testResourceName;
197 }
198 $wgDBuser = $wgDBtestuser;
199 $wgDBpassword = $wgDBtestpassword;
200
201 $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
202 $wgUploadPath = $testUploadPath;
203 }