r74955 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74954‎ | r74955 | r74956 >
Date:18:15, 18 October 2010
Author:pdhanda
Status:ok (Comments)
Tags:
Comment:
Follow up to r74780. Set default for . did some sanity check around testSuiteNames. Some more code cleanup
Modified paths:
  • /trunk/phase3/includes/SeleniumWebSettings.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SeleniumWebSettings.php
@@ -4,19 +4,26 @@
55 * For details on how to configure a wiki for a Selenium test, see:
66 * http://www.mediawiki.org/wiki/SeleniumFramework#Test_Wiki_configuration
77 */
8 -if ( !$wgEnableSelenium ) {
9 - return;
 8+if ( !defined( 'MEDIAWIKI' ) ) {
 9+ die( 1 );
1010 }
 11+
 12+$fname = 'SeleniumWebSettings.php';
 13+wfProfileIn( $fname );
 14+
1115 $cookiePrefix = $wgSitename . "-";
12 -$name = $cookiePrefix . "Selenium";
 16+$cookieName = $cookiePrefix . "Selenium";
1317
1418 //if we find a request parameter containing the test name, set a cookie with the test name
15 -if ( array_key_exists( 'setupTestSuite', $_GET) ) {
16 - //TODO: do a check for valid testsuite names
 19+if ( isset( $_GET['setupTestSuite'] ) ) {
1720 $setupTestSuiteName = $_GET['setupTestSuite'];
 21+
 22+ if ( preg_match( '/[^a-zA-Z0-9_-]/', $setupTestSuiteName ) || !isset( $wgSeleniumTestConfigs[$setupTestSuiteName] ) ) {
 23+ return;
 24+ }
1825 if ( strlen( $setupTestSuiteName) > 0 ) {
1926 $expire = time() + 600;
20 - setcookie( $name,
 27+ setcookie( $cookieName,
2128 $setupTestSuiteName,
2229 $expire,
2330 $wgCookiePath,
@@ -26,9 +33,9 @@
2734 }
2835 }
2936 //clear the cookie based on a request param
30 -if ( array_key_exists( 'clearTestSuite', $_GET) ) {
 37+if ( isset( $_GET['clearTestSuite'] ) ) {
3138 $expire = time() - 600;
32 - setcookie( $name,
 39+ setcookie( $cookieName,
3340 '',
3441 $expire,
3542 $wgCookiePath,
@@ -38,32 +45,28 @@
3946 }
4047
4148 //if a cookie is found, run the appropriate callback to get the config params.
42 -if ( array_key_exists( $name, $_COOKIE) ) {
43 - $testSuiteName = $_COOKIE[$name];
 49+if ( isset( $_COOKIE[$cookieName] ) ) {
 50+ $testSuiteName = $_COOKIE[$cookieName];
 51+ if ( !isset( $wgSeleniumTestConfigs[$testSuiteName] ) ) {
 52+ return;
 53+ }
4454 $testIncludes = array(); //array containing all the includes needed for this test
45 - $testGlobalConfigs = array(); //an array containg all the global configs needed for this test
46 - if ( isset( $wgSeleniumTestConfigs ) && array_key_exists($testSuiteName, $wgSeleniumTestConfigs) ) {
47 - $callback = $wgSeleniumTestConfigs[$testSuiteName];
48 - call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs));
49 - }
 55+ $testGlobalConfigs = array(); //an array containg all the global configs needed for this test
 56+ $callback = $wgSeleniumTestConfigs[$testSuiteName];
 57+ call_user_func_array( $callback, array( &$testIncludes, &$testGlobalConfigs));
5058
5159 foreach ( $testIncludes as $includeFile ) {
5260 $file = $IP . '/' . $includeFile;
5361 require_once( $file );
5462 }
5563 foreach ( $testGlobalConfigs as $key => $value ) {
56 - if ( is_array( $value ) ) {
 64+ if ( is_array( $value ) ) {
 65+ $GLOBALS[$key] = array_merge( $GLOBALS[$key], $value );
5766
58 - $configArray = array();
59 - if ( isset( $GLOBALS[$key] ) ) {
60 - $configArray = $GLOBALS[$key];
61 - }
62 - foreach ( $value as $configKey => $configValue ) {
63 - $configArray[$configKey] = $configValue;
64 - }
65 - $GLOBALS[$key] = $configArray;
6667 } else {
6768 $GLOBALS[$key] = $value;
6869 }
6970 }
70 -}
\ No newline at end of file
 71+}
 72+
 73+wfProfileOut( $fname );

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r74780* WebStart.php and SeleniumWebSettings.php allow include files and global con...pdhanda16:38, 14 October 2010

Comments

#Comment by Platonides (talk | contribs)   21:57, 20 October 2010

I would set $cookieName in one line.

You have no guarantee that $wgSitename makes a legal cookie name. You should be using $wgCookiePrefix, (but it is set in Setup.php, after SeleniumWebSettings.php inclusion, some code needs to be moved IMHO).

You only call wfProfileOut on the end of file, not when exiting with the return;

Space missing before the closing bracket of strlen

Usage of $wgRequest->response()->setcookie would be preferable to raw setcookie()

Nonetheless, this looks better :)

#Comment by Zakgreant (talk | contribs)   18:48, 3 December 2010

Should we be using:

if( $wgRequest->getVal( 'clearTestSuite' ) ) { $setupTestSuiteName = $wgRequest->getVal( 'clearTestSuite' );

instead of:

if ( isset( $_GET['setupTestSuite'] ) ) {

		$setupTestSuiteName = $_GET['setupTestSuite'];

Using $wgRequest will let you pass data via GET or POST requests. The various WebRequest methods also handles various edge cases related to encoding, typing, etc.

#Comment by Catrope (talk | contribs)   19:01, 3 December 2010

This has been discussed extensively before being implemented; $wgRequest is not yet set up at the stage in which SeleniumWebSettings.php , so $_GET has to be used (with extra care).

#Comment by Zakgreant (talk | contribs)   19:12, 3 December 2010

Ahai. Thanks!

Would we still want to use FauxRequest for setting the cookie?

#Comment by Platonides (talk | contribs)   22:01, 3 December 2010

It should be noted at the top of the file, so this doesn't confuse more developers in the future.

#Comment by Zakgreant (talk | contribs)   22:54, 3 December 2010

I've added a note recommending the non-obvious choices be commented at Security_for_developers#Reviewer_anxiety. I'll add similar notes to Manual:Pre-commit_checklist and Manual:Coding conventions.

Status & tagging log