wiki.techinc.nl/tests/phpunit/includes/GlobalFunctions/wfUrlencodeTest.php

127 lines
3.2 KiB
PHP
Raw Normal View History

<?php
/**
* The function only need a string parameter and might react to IIS7.0
*
* @group GlobalFunctions
* @covers ::wfUrlencode
*/
class WfUrlencodeTest extends MediaWikiTestCase {
#### TESTS ##############################################################
/**
* @dataProvider provideURLS
*/
public function testEncodingUrlWith( $input, $expected ) {
$this->verifyEncodingFor( 'Apache', $input, $expected );
}
/**
* @dataProvider provideURLS
*/
public function testEncodingUrlWithMicrosoftIis7( $input, $expected ) {
$this->verifyEncodingFor( 'Microsoft-IIS/7', $input, $expected );
}
#### HELPERS #############################################################
/**
* Internal helper that actually run the test.
* Called by the public methods testEncodingUrlWith...()
*
*/
private function verifyEncodingFor( $server, $input, $expectations ) {
$expected = $this->extractExpect( $server, $expectations );
// save up global
$old = isset( $_SERVER['SERVER_SOFTWARE'] )
? $_SERVER['SERVER_SOFTWARE']
: null;
$_SERVER['SERVER_SOFTWARE'] = $server;
wfUrlencode( null );
// do the requested test
$this->assertEquals(
$expected,
wfUrlencode( $input ),
"Encoding '$input' for server '$server' should be '$expected'"
);
// restore global
if ( $old === null ) {
unset( $_SERVER['SERVER_SOFTWARE'] );
} else {
$_SERVER['SERVER_SOFTWARE'] = $old;
}
wfUrlencode( null );
}
/**
* Interprets the provider array. Return expected value depending
* the HTTP server name.
*/
private function extractExpect( $server, $expectations ) {
if ( is_string( $expectations ) ) {
return $expectations;
} elseif ( is_array( $expectations ) ) {
if ( !array_key_exists( $server, $expectations ) ) {
throw new MWException( __METHOD__ . " expectation does not have any "
. "value for server name $server. Check the provider array.\n" );
} else {
return $expectations[$server];
}
} else {
throw new MWException( __METHOD__ . " given invalid expectation for "
. "'$server'. Should be a string or an array( <http server name> => <string> ).\n" );
}
}
#### PROVIDERS ###########################################################
/**
* Format is either:
* array( 'input', 'expected' );
* Or:
* array( 'input',
* array( 'Apache', 'expected' ),
* array( 'Microsoft-IIS/7', 'expected' ),
* ),
* If you want to add other HTTP server name, you will have to add a new
* testing method much like the testEncodingUrlWith() method above.
*/
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
public static function provideURLS() {
return array(
### RFC 1738 chars
// + is not safe
array( '+', '%2B' ),
// & and = not safe in queries
array( '&', '%26' ),
array( '=', '%3D' ),
array( ':', array(
'Apache' => ':',
'Microsoft-IIS/7' => '%3A',
) ),
// remaining chars do not need encoding
array(
';@$-_.!*',
';@$-_.!*',
),
### Other tests
// slash remain unchanged. %2F seems to break things
array( '/', '/' ),
// T105265
array( '~', '~' ),
// Other 'funnies' chars
array( '[]', '%5B%5D' ),
array( '<>', '%3C%3E' ),
// Apostrophe is encoded
array( '\'', '%27' ),
);
}
}