r105809 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105808‎ | r105809 | r105810 >
Date:18:25, 11 December 2011
Author:dantman
Status:resolved (Comments)
Tags:core 
Comment:
Update wfArrayToCGI and wfCgiToArray:
- 'foo' => '' now outputs "&foo=" instead of the key being omitted
- 'foo' => null and 'foo' => false now omit the key instead of outputting "&foo="
- Added a test to make sure that 'foo' => true outputs "&foo=1"
- Fixed a php notice caused when passing a =value-less bit like "&qwerty" to wfCgiToArray by treating it like php and extracting it as 'qwerty' => ''
- Updated tests
Modified paths:
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/GlobalFunctions/GlobalTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/GlobalFunctions/GlobalTest.php
@@ -96,9 +96,9 @@
9797
9898 function testArrayToCGI() {
9999 $this->assertEquals(
100 - "baz=AT%26T&foo=bar",
 100+ "baz=AT%26T&empty=&true=1&foo=bar",
101101 wfArrayToCGI(
102 - array( 'baz' => 'AT&T', 'ignore' => '' ),
 102+ array( 'baz' => 'AT&T', 'empty' => '', 'ignored' => null, 'ignored2' => false, 'true' => true ),
103103 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
104104 $this->assertEquals(
105105 "path%5B0%5D=wiki&path%5B1%5D=test&cfg%5Bservers%5D%5Bhttp%5D=localhost",
@@ -110,8 +110,9 @@
111111 function testCgiToArray() {
112112 $this->assertEquals(
113113 array( 'path' => array( 'wiki', 'test' ),
114 - 'cfg' => array( 'servers' => array( 'http' => 'localhost' ) ) ),
115 - wfCgiToArray( 'path%5B0%5D=wiki&path%5B1%5D=test&cfg%5Bservers%5D%5Bhttp%5D=localhost' ) );
 114+ 'cfg' => array( 'servers' => array( 'http' => 'localhost' ) ),
 115+ 'qwerty' => '' ),
 116+ wfCgiToArray( 'path%5B0%5D=wiki&path%5B1%5D=test&cfg%5Bservers%5D%5Bhttp%5D=localhost&qwerty' ) );
116117 }
117118
118119 function testMimeTypeMatch() {
Index: trunk/phase3/includes/GlobalFunctions.php
@@ -322,7 +322,7 @@
323323 /**
324324 * This function takes two arrays as input, and returns a CGI-style string, e.g.
325325 * "days=7&limit=100". Options in the first array override options in the second.
326 - * Options set to "" will not be output.
 326+ * Options set to null or false will not be output.
327327 *
328328 * @param $array1 Array ( String|Array )
329329 * @param $array2 Array ( String|Array )
@@ -336,7 +336,7 @@
337337
338338 $cgi = '';
339339 foreach ( $array1 as $key => $value ) {
340 - if ( $value !== '' ) {
 340+ if ( !is_null($value) && $value !== false ) {
341341 if ( $cgi != '' ) {
342342 $cgi .= '&';
343343 }
@@ -369,8 +369,7 @@
370370 * This is the logical opposite of wfArrayToCGI(): it accepts a query string as
371371 * its argument and returns the same string in array form. This allows compa-
372372 * tibility with legacy functions that accept raw query strings instead of nice
373 - * arrays. Of course, keys and values are urldecode()d. Don't try passing in-
374 - * valid query strings, or it will explode.
 373+ * arrays. Of course, keys and values are urldecode()d.
375374 *
376375 * @param $query String: query string
377376 * @return array Array version of input
@@ -385,7 +384,13 @@
386385 if ( $bit === '' ) {
387386 continue;
388387 }
389 - list( $key, $value ) = explode( '=', $bit );
 388+ if ( strpos( $bit, '=' ) === false ) {
 389+ // Pieces like &qwerty become 'qwerty' => '' (at least this is what php does)
 390+ $key = $bit;
 391+ $value = '';
 392+ } else {
 393+ list( $key, $value ) = explode( '=', $bit );
 394+ }
390395 $key = urldecode( $key );
391396 $value = urldecode( $value );
392397 if ( strpos( $key, '[' ) !== false ) {

Sign-offs

UserFlagDate
Abigortested07:48, 12 December 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r108104Followup r105809; Split up the tests with a dataProvider and add a round trip...dantman00:26, 5 January 2012
r112474(bug 34736) empty limit on special pages causes navigation issues...hashar10:17, 27 February 2012

Comments

#Comment by Hashar (talk | contribs)   12:03, 12 December 2011

You really want to add one test by expectation. In the above case, if some break, you don't know what is broken exactly since everything is tested in the same assertion.

Make sure to test CGI to Array and the same issue with Array to CGI

Consider adding a test per type:

'StringEmpty' => 

Then another one for:

'StringNull' =>null

And a last one for:

'BooleanFalse' => false



What does '?empty=&some=1' gives when using CGI to Array? Will $empty be null ?

#Comment by Dantman (talk | contribs)   17:53, 12 December 2011
empty=&some=1 => { empty: '', some: '1' } => empty=&some=1 => { empty: '', some: '1' } => ...
#Comment by Brion VIBBER (talk | contribs)   23:10, 20 December 2011
  • use @dataProvider on the tests so you can loop over multiple inputs/outputs
  • as noted above, separate out the various items being tested
  • maybe throw in a round-trip test
#Comment by Hashar (talk | contribs)   10:17, 27 February 2012

Caused Bug 34736 - empty limit on special pages causes navigation issues Fixed by r112474

Status & tagging log