r91108 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91107‎ | r91108 | r91109 >
Date:20:28, 29 June 2011
Author:hashar
Status:ok (Comments)
Tags:todo 
Comment:
basic tests for wfUrlencode

Please note this function use a static variable based on the value
of $GLOBALS[’SERVER_SOFTWARE']. With PHP 5.2 and PHPUnit 3.5.x, there
is currently now way to reset the static variable :-/ Tests relying on
a change of SERVER_SOFTWARE are thus skipped, still the framework is there
for later use.
Modified paths:
  • /trunk/phase3/tests/phpunit/includes/GlobalFunctions.php/wfUrlencodeTest.php (added) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/GlobalFunctions.php/wfUrlencodeTest.php
@@ -0,0 +1,123 @@
 2+<?php
 3+/**
 4+ * Tests for includes/GlobalFunctions.php -> wfUrlencode()
 5+ *
 6+ * The function only need a string parameter and might react to IIS7.0
 7+ */
 8+
 9+class wfUrlencodeTest extends MediaWikiTestCase {
 10+
 11+ #### TESTS ##############################################################
 12+
 13+ /** @dataProvider provideURLS */
 14+ public function testEncodingUrlWith( $input, $expected ) {
 15+ $this->verifyEncodingFor( 'Apache', $input, $expected );
 16+ }
 17+
 18+ /** @dataProvider provideURLS */
 19+ public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
 20+ $this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
 21+ }
 22+
 23+ #### HELPERS #############################################################
 24+
 25+ /**
 26+ * Internal helper that actually run the test.
 27+ * Called by the public methods testEncodingUrlWith...()
 28+ *
 29+ */
 30+ private function verifyEncodingFor( $server, $input, $expectations ) {
 31+ $expected = $this->extractExpect( $server, $expectations );
 32+
 33+ // save up global
 34+ $old = isset($_SERVER['SERVER_SOFTWARE'])
 35+ ? $_SERVER['SERVER_SOFTWARE']
 36+ : null
 37+ ;
 38+ $_SERVER['SERVER_SOFTWARE'] = $server;
 39+
 40+ // do the requested test
 41+ $this->assertEquals(
 42+ $expected,
 43+ wfUrlencode( $input ),
 44+ "Encoding '$input' for server '$server' should be '$expected'"
 45+ );
 46+
 47+ // restore global
 48+ if( $old === null ) {
 49+ unset( $_SERVER['SERVER_SOFTWARE'] );
 50+ } else {
 51+ $_SERVER['SERVER_SOFTWARE'] = $old;
 52+ }
 53+ }
 54+
 55+ /**
 56+ * Interprets the provider array. Return expected value depending
 57+ * the HTTP server name.
 58+ */
 59+ private function extractExpect( $server, $expectations ) {
 60+ if( is_string( $expectations ) ) {
 61+ return $expectations;
 62+ } elseif( is_array( $expectations ) ) {
 63+
 64+ /**
 65+ * FIXME FIXME FIXME FIXME
 66+ * wfUrlencode use a static variable so we can not just
 67+ * change the $GLOBALS server name :(
 68+ */
 69+ $this->markTestSkipped( 'FIXME: wfUrlencode() use a static, thus changing $GLOBALS[SERVER_SOFTWARE] is useless' );
 70+
 71+ if( !array_key_exists( $server, $expectations ) ) {
 72+ throw new MWException( __METHOD__ . " expectation does not have any value for server name $server. Check the provider array.\n" );
 73+ } else {
 74+ return $expectations[$server];
 75+ }
 76+ } else {
 77+ throw new MWException( __METHOD__ . " given invalid expectation for '$server'. Should be a string or an array( <http server name> => <string> ).\n" );
 78+ }
 79+ }
 80+
 81+
 82+ #### PROVIDERS ###########################################################
 83+
 84+ /**
 85+ * Format is either:
 86+ * array( 'input', 'expected' );
 87+ * Or:
 88+ * array( 'input',
 89+ * array( 'Apache', 'expected' ),
 90+ * array( 'Microsoft-IIS/7', 'expected' ),
 91+ * ),
 92+ * If you want to add other HTTP server name, you will have to add a new
 93+ * testing method much like the testEncodingUrlWith() method above.
 94+ */
 95+ public function provideURLS() {
 96+ return array(
 97+ ### RFC 1738 chars
 98+ // + is not safe
 99+ array( '+', '%2B' ),
 100+ // & and = not safe in queries
 101+ array( '&', '%26' ),
 102+ array( '=', '%3D' ),
 103+
 104+ array( ':', array(
 105+ 'Apache' => ':',
 106+ 'Microsoft-IIS/7' => '%3A',
 107+ ) ),
 108+
 109+ // remaining chars do not need encoding
 110+ array(
 111+ ';@$-_.!*',
 112+ ';@$-_.!*',
 113+ ),
 114+
 115+ ### Other tests
 116+ // slash remain unchanged. %2F seems to break things
 117+ array( '/', '/' ),
 118+
 119+ // Other 'funnies' chars
 120+ array( '[]', '%5B%5D' ),
 121+ array( '<>', '%3C%3E' ),
 122+ );
 123+ }
 124+}
Property changes on: trunk/phase3/tests/phpunit/includes/GlobalFunctions.php/wfUrlencodeTest.php
___________________________________________________________________
Added: svn:eol-style
1125 + native

Comments

#Comment by Hashar (talk | contribs)   20:53, 29 June 2011

Maybe I should mark relevant tests as incompletes.

#Comment by Aaron Schulz (talk | contribs)   21:29, 2 August 2011

Yes.

Status & tagging log