diff options
Diffstat (limited to 'tests/phpunit/includes/GlobalFunctions')
4 files changed, 237 insertions, 55 deletions
diff --git a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php index 3acc48e2..1e30273e 100644 --- a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php +++ b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php @@ -7,7 +7,7 @@ class GlobalTest extends MediaWikiTestCase { protected function setUp() { parent::setUp(); - $readOnlyFile = tempnam( wfTempDir(), "mwtest_readonly" ); + $readOnlyFile = $this->getNewTempFile(); unlink( $readOnlyFile ); $this->setMwGlobals( array( @@ -22,16 +22,6 @@ class GlobalTest extends MediaWikiTestCase { ) ); } - protected function tearDown() { - global $wgReadOnlyFile; - - if ( file_exists( $wgReadOnlyFile ) ) { - unlink( $wgReadOnlyFile ); - } - - parent::tearDown(); - } - /** * @dataProvider provideForWfArrayDiff2 * @covers ::wfArrayDiff2 @@ -312,46 +302,42 @@ class GlobalTest extends MediaWikiTestCase { * @covers ::wfDebugMem */ public function testDebugFunctionTest() { + $debugLogFile = $this->getNewTempFile(); - global $wgDebugLogFile, $wgDebugTimestamps; - - $old_log_file = $wgDebugLogFile; - $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' ); - # @todo FIXME: $wgDebugTimestamps should be tested - $old_wgDebugTimestamps = $wgDebugTimestamps; - $wgDebugTimestamps = false; + $this->setMwGlobals( array( + 'wgDebugLogFile' => $debugLogFile, + # @todo FIXME: $wgDebugTimestamps should be tested + 'wgDebugTimestamps' => false + ) ); wfDebug( "This is a normal string" ); - $this->assertEquals( "This is a normal string", file_get_contents( $wgDebugLogFile ) ); - unlink( $wgDebugLogFile ); + $this->assertEquals( "This is a normal string\n", file_get_contents( $debugLogFile ) ); + unlink( $debugLogFile ); wfDebug( "This is nöt an ASCII string" ); - $this->assertEquals( "This is nöt an ASCII string", file_get_contents( $wgDebugLogFile ) ); - unlink( $wgDebugLogFile ); + $this->assertEquals( "This is nöt an ASCII string\n", file_get_contents( $debugLogFile ) ); + unlink( $debugLogFile ); wfDebug( "\00305This has böth UTF and control chars\003" ); $this->assertEquals( - " 05This has böth UTF and control chars ", - file_get_contents( $wgDebugLogFile ) + " 05This has böth UTF and control chars \n", + file_get_contents( $debugLogFile ) ); - unlink( $wgDebugLogFile ); + unlink( $debugLogFile ); wfDebugMem(); $this->assertGreaterThan( 1000, - preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) + preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) ) ); - unlink( $wgDebugLogFile ); + unlink( $debugLogFile ); wfDebugMem( true ); $this->assertGreaterThan( 1000000, - preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) + preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) ) ); - unlink( $wgDebugLogFile ); - - $wgDebugLogFile = $old_log_file; - $wgDebugTimestamps = $old_wgDebugTimestamps; + unlink( $debugLogFile ); } /** @@ -389,24 +375,6 @@ class GlobalTest extends MediaWikiTestCase { } /** - * @covers ::swap - */ - public function testSwapVarsTest() { - $this->hideDeprecated( 'swap' ); - - $var1 = 1; - $var2 = 2; - - $this->assertEquals( $var1, 1, 'var1 is set originally' ); - $this->assertEquals( $var2, 2, 'var1 is set originally' ); - - swap( $var1, $var2 ); - - $this->assertEquals( $var1, 2, 'var1 is swapped' ); - $this->assertEquals( $var2, 1, 'var2 is swapped' ); - } - - /** * @covers ::wfPercent */ public function testWfPercentTest() { @@ -705,21 +673,21 @@ class GlobalTest extends MediaWikiTestCase { } /** - * @dataProvider provideWfShellMaintenanceCmdList - * @covers ::wfShellMaintenanceCmd + * @dataProvider provideWfShellWikiCmdList + * @covers ::wfShellWikiCmd */ - public function testWfShellMaintenanceCmd( $script, $parameters, $options, + public function testWfShellWikiCmd( $script, $parameters, $options, $expected, $description ) { if ( wfIsWindows() ) { // Approximation that's good enough for our purposes just now $expected = str_replace( "'", '"', $expected ); } - $actual = wfShellMaintenanceCmd( $script, $parameters, $options ); + $actual = wfShellWikiCmd( $script, $parameters, $options ); $this->assertEquals( $expected, $actual, $description ); } - public static function provideWfShellMaintenanceCmdList() { + public static function provideWfShellWikiCmdList() { global $wgPhpCli; return array( diff --git a/tests/phpunit/includes/GlobalFunctions/wfAppendQueryTest.php b/tests/phpunit/includes/GlobalFunctions/wfAppendQueryTest.php new file mode 100644 index 00000000..54e1f896 --- /dev/null +++ b/tests/phpunit/includes/GlobalFunctions/wfAppendQueryTest.php @@ -0,0 +1,67 @@ +<?php + +/** + * @group GlobalFunctions + * @covers ::wfAppendQuery + */ +class WfAppendQueryTest extends MediaWikiTestCase { + /** + * @dataProvider provideAppendQuery + */ + public function testAppendQuery( $url, $query, $expected, $message = null ) { + $this->assertEquals( $expected, wfAppendQuery( $url, $query ), $message ); + } + + public static function provideAppendQuery() { + return array( + array( + 'http://www.example.org/index.php', + '', + 'http://www.example.org/index.php', + 'No query' + ), + array( + 'http://www.example.org/index.php', + array( 'foo' => 'bar' ), + 'http://www.example.org/index.php?foo=bar', + 'Set query array' + ), + array( + 'http://www.example.org/index.php?foz=baz', + 'foo=bar', + 'http://www.example.org/index.php?foz=baz&foo=bar', + 'Set query string' + ), + array( + 'http://www.example.org/index.php?foo=bar', + '', + 'http://www.example.org/index.php?foo=bar', + 'Empty string with query' + ), + array( + 'http://www.example.org/index.php?foo=bar', + array( 'baz' => 'quux' ), + 'http://www.example.org/index.php?foo=bar&baz=quux', + 'Add query array' + ), + array( + 'http://www.example.org/index.php?foo=bar', + 'baz=quux', + 'http://www.example.org/index.php?foo=bar&baz=quux', + 'Add query string' + ), + array( + 'http://www.example.org/index.php?foo=bar', + array( 'baz' => 'quux', 'foo' => 'baz' ), + 'http://www.example.org/index.php?foo=bar&baz=quux&foo=baz', + 'Modify query array' + ), + array( + 'http://www.example.org/index.php?foo=bar', + 'baz=quux&foo=baz', + 'http://www.example.org/index.php?foo=bar&baz=quux&foo=baz', + 'Modify query string' + ) + ); + } +} diff --git a/tests/phpunit/includes/GlobalFunctions/wfEscapeShellArgTest.php b/tests/phpunit/includes/GlobalFunctions/wfEscapeShellArgTest.php new file mode 100644 index 00000000..cb334d2f --- /dev/null +++ b/tests/phpunit/includes/GlobalFunctions/wfEscapeShellArgTest.php @@ -0,0 +1,43 @@ +<?php + +/** + * @group GlobalFunctions + * @covers ::wfEscapeShellArg + */ +class wfEscapeShellArgTest extends MediaWikiTestCase { + public function testSingleInput() { + if ( wfIsWindows() ) { + $expected = '"blah"'; + } else { + $expected = "'blah'"; + } + + $actual = wfEscapeShellArg( 'blah' ); + + $this->assertEquals( $expected, $actual ); + } + + public function testMultipleArgs() { + if ( wfIsWindows() ) { + $expected = '"foo" "bar" "baz"'; + } else { + $expected = "'foo' 'bar' 'baz'"; + } + + $actual = wfEscapeShellArg( 'foo', 'bar', 'baz' ); + + $this->assertEquals( $expected, $actual ); + } + + public function testMultipleArgsAsArray() { + if ( wfIsWindows() ) { + $expected = '"foo" "bar" "baz"'; + } else { + $expected = "'foo' 'bar' 'baz'"; + } + + $actual = wfEscapeShellArg( array( 'foo', 'bar', 'baz' ) ); + + $this->assertEquals( $expected, $actual ); + } +} diff --git a/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php b/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php new file mode 100644 index 00000000..448250a6 --- /dev/null +++ b/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php @@ -0,0 +1,104 @@ +<?php + +/** + * @group GlobalFunctions + * @covers ::wfThumbIsStandard + */ +class WfThumbIsStandardTest extends MediaWikiTestCase { + + protected function setUp() { + parent::setUp(); + + $this->setMwGlobals( array( + 'wgThumbLimits' => array( + 100, + 401 + ), + 'wgImageLimits' => array( + array( 300, 225 ), + array( 800, 600 ), + ), + 'wgMediaHandlers' => array( + 'unknown/unknown' => 'MockBitmapHandler', + ), + ) ); + } + + public static function provideThumbParams() { + return array( + // Thumb limits + array( + 'Standard thumb width', + true, + array( 'width' => 100 ), + ), + array( + 'Standard thumb width', + true, + array( 'width' => 401 ), + ), + // wfThumbIsStandard should match Linker::processResponsiveImages + // in its rounding behaviour. + array( + 'Standard thumb width (HiDPI 1.5x) - incorrect rounding', + false, + array( 'width' => 601 ), + ), + array( + 'Standard thumb width (HiDPI 1.5x)', + true, + array( 'width' => 602 ), + ), + array( + 'Standard thumb width (HiDPI 2x)', + true, + array( 'width' => 802 ), + ), + array( + 'Non-standard thumb width', + false, + array( 'width' => 300 ), + ), + // Image limits + // Note: Image limits are measured as pairs. Individual values + // may be non-standard based on the aspect ratio. + array( + 'Standard image width/height pair', + true, + array( 'width' => 250, 'height' => 225 ), + ), + array( + 'Standard image width/height pair', + true, + array( 'width' => 667, 'height' => 600 ), + ), + array( + 'Standard image width where image does not fit aspect ratio', + false, + array( 'width' => 300 ), + ), + array( + 'Implicit width from image width/height pair aspect ratio fit', + true, + // 2000x1800 fit inside 300x225 makes w=250 + array( 'width' => 250 ), + ), + array( + 'Height-only is always non-standard', + false, + array( 'height' => 225 ), + ), + ); + } + + /** + * @dataProvider provideThumbParams + */ + public function testIsStandard( $message, $expected, $params ) { + $this->assertSame( + $expected, + wfThumbIsStandard( new FakeDimensionFile( array( 2000, 1800 ) ), $params ), + $message + ); + } +} |