r79421 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79420‎ | r79421 | r79422 >
Date:15:47, 1 January 2011
Author:mglaser
Status:deferred (Comments)
Tags:
Comment:
now uses separate database and images dir for selenium tests if $wgSeleniumUseTestResources is set
Modified paths:
  • /trunk/phase3/includes/SeleniumWebSettings.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SeleniumWebSettings.php
@@ -8,16 +8,22 @@
99 die( 1 );
1010 }
1111
 12+require_once( "$IP/includes/GlobalFunctions.php" );
 13+
1214 $fname = 'SeleniumWebSettings.php';
1315 wfProfileIn( $fname );
1416
1517 $cookiePrefix = $wgSitename . "-";
1618 $cookieName = $cookiePrefix . "Selenium";
1719
 20+// this is a fallback sql file
 21+$testSqlFile = false;
 22+$testImageZip = false;
 23+
1824 //if we find a request parameter containing the test name, set a cookie with the test name
1925 if ( isset( $_GET['setupTestSuite'] ) ) {
2026 $setupTestSuiteName = $_GET['setupTestSuite'];
21 -
 27+
2228 if ( preg_match( '/[^a-zA-Z0-9_-]/', $setupTestSuiteName ) || !isset( $wgSeleniumTestConfigs[$setupTestSuiteName] ) ) {
2329 return;
2430 }
@@ -31,29 +37,57 @@
3238 $wgCookieSecure,
3339 true );
3440 }
 41+
 42+ $testIncludes = array(); //array containing all the includes needed for this test
 43+ $testGlobalConfigs = array(); //an array containg all the global configs needed for this test
 44+ $testResourceFiles = array(); // an array containing all the resource files needed for this test
 45+ $callback = $wgSeleniumTestConfigs[$setupTestSuiteName];
 46+ call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
 47+
 48+ if ( isset( $testResourceFiles['db'] ) ) {
 49+ $testSqlFile = $testResourceFiles['db'];
 50+ }
 51+ if ( isset($testResourceFiles['images']) ) {
 52+ $testImageZip = $testResourceFiles['images'];
 53+ }
 54+
 55+ $testResourceName = getTestResourceNameFromTestSuiteName( $setupTestSuiteName );
 56+ switchToTestResources( $testResourceName, false ); // false means do not switch database yet
 57+ setupTestResources( $testResourceName, $testSqlFile, $testImageZip );
3558 }
 59+
3660 //clear the cookie based on a request param
3761 if ( isset( $_GET['clearTestSuite'] ) ) {
38 - $expire = time() - 600;
39 - setcookie( $cookieName,
40 - '',
41 - $expire,
42 - $wgCookiePath,
43 - $wgCookieDomain,
44 - $wgCookieSecure,
45 - true );
 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 );
4675 }
4776
4877 //if a cookie is found, run the appropriate callback to get the config params.
4978 if ( isset( $_COOKIE[$cookieName] ) ) {
50 - $testSuiteName = $_COOKIE[$cookieName];
 79+ $testSuiteName = getTestSuiteNameFromCookie( $cookieName );
5180 if ( !isset( $wgSeleniumTestConfigs[$testSuiteName] ) ) {
5281 return;
5382 }
 83+
 84+ $testResourceName = getTestResourceNameFromTestSuiteName( $testSuiteName );
 85+ switchToTestResources( $testResourceName );
 86+
5487 $testIncludes = array(); //array containing all the includes needed for this test
5588 $testGlobalConfigs = array(); //an array containg all the global configs needed for this test
 89+ $testResourceFiles = array(); // an array containing all the resource files needed for this test
5690 $callback = $wgSeleniumTestConfigs[$testSuiteName];
57 - call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs));
 91+ call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs, &$testResourceFiles));
5892
5993 foreach ( $testIncludes as $includeFile ) {
6094 $file = $IP . '/' . $includeFile;
@@ -70,3 +104,122 @@
71105 }
72106
73107 wfProfileOut( $fname );
 108+
 109+function getTestSuiteNameFromCookie( $cookieName ) {
 110+ $testSuiteName = null;
 111+ if ( isset( $_COOKIE[$cookieName] ) ) {
 112+ $testSuiteName = $_COOKIE[$cookieName];
 113+ }
 114+ return $testSuiteName;
 115+}
 116+
 117+function getTestResourceNameFromTestSuiteName( $testSuiteName ) {
 118+ $testResourceName = null;
 119+ if ( isset( $testSuiteName ) ) {
 120+ $testResourceName = $testSuiteName;
 121+ }
 122+ return $testResourceName;
 123+}
 124+
 125+function getTestUploadPathFromResourceName( $testResourceName ) {
 126+ global $IP;
 127+ $testUploadPath = "$IP/images/$testResourceName";
 128+ return $testUploadPath;
 129+}
 130+
 131+function setupTestResources( $testResourceName, $testSqlFile, $testImageZip ) {
 132+ global $wgDBname, $wgSeleniumUseTestResources;
 133+
 134+ if ( !$wgSeleniumUseTestResources ) {
 135+ return false;
 136+ }
 137+ // Basic security. Do not allow to drop productive database.
 138+ if ( $testResouceName == $wgDBname ) {
 139+ die( "Cannot override productive database." );
 140+ }
 141+ if ( $testResourceName == '' ) {
 142+ die( "Cannot identify a test the resources should be installed for." );
 143+ }
 144+
 145+ //create tables
 146+ $dbw =& wfGetDB( DB_MASTER );
 147+ $dbw->query( "DROP DATABASE IF EXISTS ".$testResourceName );
 148+ $dbw->query( "CREATE DATABASE ".$testResourceName );
 149+
 150+ // do not set the new db name before database is setup
 151+ $wgDBname = $testResourceName;
 152+ $dbw->selectDB( $testResourceName );
 153+ // populate from sql file
 154+ if ( $testSqlFile ) {
 155+ $dbw->sourceFile( $testSqlFile );
 156+ }
 157+
 158+ // create test image dir
 159+ $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
 160+ if ( !file_exists( $testUploadPath ) ) {
 161+ mkdir( $testUploadPath );
 162+ }
 163+
 164+ if ( $testImageZip ) {
 165+ $zip = new ZipArchive();
 166+ $zip->open( $testImageZip );
 167+ $zip->extractTo( $testUploadPath );
 168+ $zip->close();
 169+ }
 170+}
 171+
 172+function teardownTestResources( $testResourceName ) {
 173+ global $wgSeleniumUseTestResources;
 174+
 175+ if ( !$wgSeleniumUseTestResources ) {
 176+ return false;
 177+ }
 178+ // remove test database
 179+ $dbw =& wfGetDB( DB_MASTER );
 180+ $dbw->query( "DROP DATABASE IF EXISTS ".$testResourceName );
 181+
 182+ $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
 183+ // remove test image dir
 184+ if ( file_exists( $testUploadPath ) ) {
 185+ wfRecursiveRemoveDir( $testUploadPath );
 186+ }
 187+}
 188+
 189+function switchToTestResources( $testResourceName, $switchDB = true ) {
 190+ global $wgDBuser, $wgDBpassword, $wgDBname, $wgDBprefix;
 191+ global $wgDBtestuser, $wgDBtestpassword, $wgDBtestprefix;
 192+ global $wgUploadPath;
 193+ global $wgSeleniumUseTestResources;
 194+
 195+ if ( !$wgSeleniumUseTestResources ) {
 196+ return false;
 197+ }
 198+
 199+ if ( $switchDB ) {
 200+ $wgDBname = $testResourceName;
 201+ }
 202+ $wgDBuser = $wgDBtestuser;
 203+ $wgDBpassword = $wgDBtestpassword;
 204+ //$wgDBprefix = $wgDBtestprefix;
 205+
 206+ $testUploadPath = getTestUploadPathFromResourceName( $testResourceName );
 207+ $wgUploadPath = $testUploadPath;
 208+}
 209+
 210+function wfRecursiveRemoveDir( $dir ) {
 211+ // taken from http://de3.php.net/manual/en/function.rmdir.php#98622
 212+ if ( is_dir( $dir ) ) {
 213+ $objects = scandir( $dir );
 214+ foreach ( $objects as $object ) {
 215+ if ( $object != "." && $object != ".." ) {
 216+ if ( filetype( $dir."/".$object ) == "dir" ) {
 217+ wfRecursiveRemoveDir( $dir."/".$object );
 218+ } else {
 219+ unlink( $dir."/".$object );
 220+ }
 221+ }
 222+ }
 223+ reset( $objects );
 224+ rmdir( $dir );
 225+ }
 226+}
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r79984New test database resource file with some test configuration changes for it. ...pdhanda01:01, 11 January 2011

Comments

#Comment by Platonides (talk | contribs)   21:20, 1 January 2011

Global variable $wgSeleniumUseTestResources is not present in DefaultSettings

Global variable $wgSeleniumUseTestResources is not present in DefaultSettings
Global variable $wgDBtestuser is not present in DefaultSettings
Global variable $wgDBtestpassword is not present in DefaultSettings
Global variable $wgDBtestprefix is not present in DefaultSettings
Global variable $wgSeleniumUseTestResources is not present in DefaultSettings
Unused global $wgDBprefix in function switchToTestResources line 189
Unused global $wgDBtestprefix in function switchToTestResources line 190
#Comment by Krinkle (talk | contribs)   00:38, 11 January 2011

(Follow-up: r79982)

#Comment by Pdhanda (talk | contribs)   00:47, 11 January 2011

Thanks Krinkle :)

Status & tagging log