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