From 8f416baead93a48e5799e44b8bd2e2c4859f4e04 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Fri, 14 Sep 2007 13:18:58 +0200 Subject: auf Version 1.11 aktualisiert; Login-Bug behoben --- tests/ArticleTest.php | 44 +--------------------- tests/DatabaseTest.php | 19 ++-------- tests/GlobalTest.php | 53 +++++++++++++------------- tests/ImageFunctionsTest.php | 48 +++++++++++++++++++++++ tests/LocalFileTest.php | 90 ++++++++++++++++++++++++++++++++++++++++++++ tests/Makefile | 6 ++- tests/MediaWiki_TestCase.php | 51 +++++++++++++++++++++++++ tests/README | 9 +++-- tests/SearchEngineTest.php | 37 +++++++++--------- tests/SearchMySQL4Test.php | 13 +++---- tests/run-test.php | 7 ++++ 11 files changed, 262 insertions(+), 115 deletions(-) create mode 100644 tests/ImageFunctionsTest.php create mode 100644 tests/LocalFileTest.php create mode 100644 tests/MediaWiki_TestCase.php create mode 100644 tests/run-test.php (limited to 'tests') diff --git a/tests/ArticleTest.php b/tests/ArticleTest.php index bd678565..a65e1ee3 100644 --- a/tests/ArticleTest.php +++ b/tests/ArticleTest.php @@ -1,20 +1,11 @@ PHPUnit_TestCase( $name ); - } - function setUp() { $globalSet = array( 'wgLegacyEncoding' => false, - 'wgUseLatin1' => false, 'wgCompressRevisions' => false, 'wgInputEncoding' => 'utf-8', 'wgOutputEncoding' => 'utf-8' ); @@ -101,20 +92,6 @@ class ArticleTest extends PHPUnit_TestCase { Revision::getRevisionText( $row ), "getRevisionText" ); } - function testCompressRevisionTextLatin1() { - $GLOBALS['wgUseLatin1'] = true; - $row->old_text = "Wiki est l'\xe9cole superieur !"; - $row->old_flags = Revision::compressRevisionText( $row->old_text ); - $this->assertFalse( false !== strpos( $row->old_flags, 'utf-8' ), - "Flags should not contain 'utf-8'" ); - $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ), - "Flags should not contain 'gzip'" ); - $this->assertEquals( "Wiki est l'\xe9cole superieur !", - $row->old_text, "Direct check" ); - $this->assertEquals( "Wiki est l'\xe9cole superieur !", - Revision::getRevisionText( $row ), "getRevisionText" ); - } - function testCompressRevisionTextUtf8Gzip() { $GLOBALS['wgCompressRevisions'] = true; $row->old_text = "Wiki est l'\xc3\xa9cole superieur !"; @@ -128,23 +105,6 @@ class ArticleTest extends PHPUnit_TestCase { $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !", Revision::getRevisionText( $row ), "getRevisionText" ); } - - function testCompressRevisionTextLatin1Gzip() { - $GLOBALS['wgCompressRevisions'] = true; - $GLOBALS['wgUseLatin1'] = true; - $row = new stdClass; - $row->old_text = "Wiki est l'\xe9cole superieur !"; - $row->old_flags = Revision::compressRevisionText( $row->old_text ); - $this->assertFalse( false !== strpos( $row->old_flags, 'utf-8' ), - "Flags should not contain 'utf-8'" ); - $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ), - "Flags should contain 'gzip'" ); - $this->assertEquals( "Wiki est l'\xe9cole superieur !", - gzinflate( $row->old_text ), "Direct check" ); - $this->assertEquals( "Wiki est l'\xe9cole superieur !", - Revision::getRevisionText( $row ), "getRevisionText" ); - } - } -?> \ No newline at end of file + diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index edb785dd..db46ad60 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -1,23 +1,10 @@ PHPUnit_TestCase( $name ); - } - function setUp() { - $this->db = new Database(); - } - - function tearDown() { - unset( $this->db ); + $this->db = wfGetDB( DB_SLAVE ); } function testAddQuotesNull() { @@ -90,4 +77,4 @@ class DatabaseTest extends PHPUnit_TestCase { } -?> + diff --git a/tests/GlobalTest.php b/tests/GlobalTest.php index 1567a189..ec694241 100644 --- a/tests/GlobalTest.php +++ b/tests/GlobalTest.php @@ -1,32 +1,21 @@ PHPUnit_TestCase( $name ); - } - +class GlobalTest extends PHPUnit_Framework_TestCase { function setUp() { - $this->save = array(); - $saveVars = array( 'wgReadOnlyFile' ); - foreach( $saveVars as $var ) { - if( isset( $GLOBALS[$var] ) ) { - $this->save[$var] = $GLOBALS[$var]; - } - } - $GLOBALS['wgReadOnlyFile'] = wfTempDir() . '/testReadOnly-' . mt_rand(); + global $wgReadOnlyFile; + $this->originals['wgReadOnlyFile'] = $wgReadOnlyFile; + $wgReadOnlyFile = tempnam(wfTempDir(), "mwtest_readonly"); + unlink( $wgReadOnlyFile ); } - + function tearDown() { - foreach( $this->save as $var => $data ) { - $GLOBALS[$var] = $data; + global $wgReadOnlyFile; + if( file_exists( $wgReadOnlyFile ) ) { + unlink( $wgReadOnlyFile ); } + $wgReadOnlyFile = $this->originals['wgReadOnlyFile']; } - + function testRandom() { # This could hypothetically fail, but it shouldn't ;) $this->assertFalse( @@ -40,16 +29,28 @@ class GlobalTest extends PHPUnit_TestCase { } function testReadOnlyEmpty() { + global $wgReadOnly; + $wgReadOnly = null; + + $this->assertFalse( wfReadOnly() ); $this->assertFalse( wfReadOnly() ); } function testReadOnlySet() { - $f = fopen( $GLOBALS['wgReadOnlyFile'], "wt" ); + global $wgReadOnly, $wgReadOnlyFile; + + $f = fopen( $wgReadOnlyFile, "wt" ); fwrite( $f, 'Message' ); fclose( $f ); + $wgReadOnly = null; + + $this->assertTrue( wfReadOnly() ); $this->assertTrue( wfReadOnly() ); - unlink( $GLOBALS['wgReadOnlyFile'] ); + unlink( $wgReadOnlyFile ); + $wgReadOnly = null; + + $this->assertFalse( wfReadOnly() ); $this->assertFalse( wfReadOnly() ); } @@ -61,7 +62,7 @@ class GlobalTest extends PHPUnit_TestCase { function testTime() { $start = wfTime(); - $this->assertType( 'double', $start ); + $this->assertType( 'float', $start ); $end = wfTime(); $this->assertTrue( $end > $start, "Time is running backwards!" ); } @@ -208,4 +209,4 @@ class GlobalTest extends PHPUnit_TestCase { /* TODO: many more! */ } -?> \ No newline at end of file + diff --git a/tests/ImageFunctionsTest.php b/tests/ImageFunctionsTest.php new file mode 100644 index 00000000..9794a2a2 --- /dev/null +++ b/tests/ImageFunctionsTest.php @@ -0,0 +1,48 @@ + 50, + 'height' => 50, + 'tests' => array( + 50 => 50, + 17 => 17, + 18 => 18 ) ), + array( + 'width' => 366, + 'height' => 300, + 'tests' => array( + 50 => 61, + 17 => 21, + 18 => 22 ) ), + array( + 'width' => 300, + 'height' => 366, + 'tests' => array( + 50 => 41, + 17 => 14, + 18 => 15 ) ), + array( + 'width' => 100, + 'height' => 400, + 'tests' => array( + 50 => 12, + 17 => 4, + 18 => 4 ) ) ); + foreach( $vals as $row ) { + extract( $row ); + foreach( $tests as $max => $expected ) { + $y = round( $expected * $height / $width ); + $result = wfFitBoxWidth( $width, $height, $max ); + $y2 = round( $result * $height / $width ); + $this->assertEquals( $expected, + $result, + "($width, $height, $max) wanted: {$expected}x$y, got: {$result}x$y2" ); + } + } + } +} + + diff --git a/tests/LocalFileTest.php b/tests/LocalFileTest.php new file mode 100644 index 00000000..335b8bbe --- /dev/null +++ b/tests/LocalFileTest.php @@ -0,0 +1,90 @@ + 'test', + 'directory' => '/testdir', + 'url' => '/testurl', + 'hashLevels' => 2, + 'transformVia404' => false, + ); + $this->repo_hl0 = new LocalRepo( array( 'hashLevels' => 0 ) + $info ); + $this->repo_hl2 = new LocalRepo( array( 'hashLevels' => 2 ) + $info ); + $this->repo_lc = new LocalRepo( array( 'initialCapital' => false ) + $info ); + $this->file_hl0 = $this->repo_hl0->newFile( 'test!' ); + $this->file_hl2 = $this->repo_hl2->newFile( 'test!' ); + $this->file_lc = $this->repo_lc->newFile( 'test!' ); + } + + function testGetHashPath() { + $this->assertEquals( '', $this->file_hl0->getHashPath() ); + $this->assertEquals( 'a/a2/', $this->file_hl2->getHashPath() ); + $this->assertEquals( 'c/c4/', $this->file_lc->getHashPath() ); + } + + function testGetRel() { + $this->assertEquals( 'Test!', $this->file_hl0->getRel() ); + $this->assertEquals( 'a/a2/Test!', $this->file_hl2->getRel() ); + $this->assertEquals( 'c/c4/test!', $this->file_lc->getRel() ); + } + + function testGetUrlRel() { + $this->assertEquals( 'Test%21', $this->file_hl0->getUrlRel() ); + $this->assertEquals( 'a/a2/Test%21', $this->file_hl2->getUrlRel() ); + $this->assertEquals( 'c/c4/test%21', $this->file_lc->getUrlRel() ); + } + + function testGetArchivePath() { + $this->assertEquals( '/testdir/archive', $this->file_hl0->getArchivePath() ); + $this->assertEquals( '/testdir/archive/a/a2', $this->file_hl2->getArchivePath() ); + $this->assertEquals( '/testdir/archive/!', $this->file_hl0->getArchivePath( '!' ) ); + $this->assertEquals( '/testdir/archive/a/a2/!', $this->file_hl2->getArchivePath( '!' ) ); + } + + function testGetThumbPath() { + $this->assertEquals( '/testdir/thumb/Test!', $this->file_hl0->getThumbPath() ); + $this->assertEquals( '/testdir/thumb/a/a2/Test!', $this->file_hl2->getThumbPath() ); + $this->assertEquals( '/testdir/thumb/Test!/x', $this->file_hl0->getThumbPath( 'x' ) ); + $this->assertEquals( '/testdir/thumb/a/a2/Test!/x', $this->file_hl2->getThumbPath( 'x' ) ); + } + + function testGetArchiveUrl() { + $this->assertEquals( '/testurl/archive', $this->file_hl0->getArchiveUrl() ); + $this->assertEquals( '/testurl/archive/a/a2', $this->file_hl2->getArchiveUrl() ); + $this->assertEquals( '/testurl/archive/%21', $this->file_hl0->getArchiveUrl( '!' ) ); + $this->assertEquals( '/testurl/archive/a/a2/%21', $this->file_hl2->getArchiveUrl( '!' ) ); + } + + function testGetThumbUrl() { + $this->assertEquals( '/testurl/thumb/Test%21', $this->file_hl0->getThumbUrl() ); + $this->assertEquals( '/testurl/thumb/a/a2/Test%21', $this->file_hl2->getThumbUrl() ); + $this->assertEquals( '/testurl/thumb/Test%21/x', $this->file_hl0->getThumbUrl( 'x' ) ); + $this->assertEquals( '/testurl/thumb/a/a2/Test%21/x', $this->file_hl2->getThumbUrl( 'x' ) ); + } + + function testGetArchiveVirtualUrl() { + $this->assertEquals( 'mwrepo://test/public/archive', $this->file_hl0->getArchiveVirtualUrl() ); + $this->assertEquals( 'mwrepo://test/public/archive/a/a2', $this->file_hl2->getArchiveVirtualUrl() ); + $this->assertEquals( 'mwrepo://test/public/archive/%21', $this->file_hl0->getArchiveVirtualUrl( '!' ) ); + $this->assertEquals( 'mwrepo://test/public/archive/a/a2/%21', $this->file_hl2->getArchiveVirtualUrl( '!' ) ); + } + + function testGetThumbVirtualUrl() { + $this->assertEquals( 'mwrepo://test/public/thumb/Test%21', $this->file_hl0->getThumbVirtualUrl() ); + $this->assertEquals( 'mwrepo://test/public/thumb/a/a2/Test%21', $this->file_hl2->getThumbVirtualUrl() ); + $this->assertEquals( 'mwrepo://test/public/thumb/Test%21/%21', $this->file_hl0->getThumbVirtualUrl( '!' ) ); + $this->assertEquals( 'mwrepo://test/public/thumb/a/a2/Test%21/%21', $this->file_hl2->getThumbVirtualUrl( '!' ) ); + } + + function testGetUrl() { + $this->assertEquals( '/testurl/Test%21', $this->file_hl0->getUrl() ); + $this->assertEquals( '/testurl/a/a2/Test%21', $this->file_hl2->getUrl() ); + } +} + + diff --git a/tests/Makefile b/tests/Makefile index 0a649927..25ccda35 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,6 +1,10 @@ .PHONY: help test all test: - php RunTests.php + php run-test.php ArticleTest.php + php run-test.php GlobalTest.php + php run-test.php DatabaseTest.php + php run-test.php ImageFunctionsTest.php + php run-test.php SearchMySQL4Test.php install: cvs -z9 -d:pserver:cvsread:@cvs.php.net:/repository/ co -P pear/PHPUnit mv pear/PHPUnit . diff --git a/tests/MediaWiki_TestCase.php b/tests/MediaWiki_TestCase.php new file mode 100644 index 00000000..3ca1dbdf --- /dev/null +++ b/tests/MediaWiki_TestCase.php @@ -0,0 +1,51 @@ +isOpen() ) { + if (!(strcmp($db->getServerVersion(), '4.1') < 0 and stristr($db->getSoftwareLink(), 'MySQL'))) { + # Database that supports CREATE TABLE ... LIKE + foreach ($tables as $tbl) { + $newTableName = $db->tableName( $tbl ); + #$tableName = $this->oldTableNames[$tbl]; + $tableName = $tbl; + $db->query("CREATE TEMPORARY TABLE $newTableName (LIKE $tableName)"); + } + } else { + # Hack for MySQL versions < 4.1, which don't support + # "CREATE TABLE ... LIKE". Note that + # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0" + # would not create the indexes we need.... + foreach ($tables as $tbl) { + $res = $db->query("SHOW CREATE TABLE $tbl"); + $row = $db->fetchRow($res); + $create = $row[1]; + $create_tmp = preg_replace('/CREATE TABLE `(.*?)`/', 'CREATE TEMPORARY TABLE `' + . $wgDBprefix . '\\1`', $create); + if ($create === $create_tmp) { + # Couldn't do replacement + wfDie( "could not create temporary table $tbl" ); + } + $db->query($create_tmp); + } + + } + return $db; + } else { + // Something amiss + return null; + } + } +} + diff --git a/tests/README b/tests/README index de153184..3bbf9704 100644 --- a/tests/README +++ b/tests/README @@ -1,8 +1,9 @@ Some quickie unit tests done with the PHPUnit testing framework. To run the test suite, run 'make test' in this dir or 'php RunTests.php' -You can install PHPUnit via pear like this: -# pear install PHPUnit +PHPUnit is no longer maintained by PEAR. To get the current version of +PHPUnit, first uninstall any old version of PHPUnit or PHPUnit2 from PEAR, +then install the current version from phpunit.de like this: -Or fetch and install it manually: -http://pear.php.net/package/PHPUnit +# pear channel-discover pear.phpunit.de +# pear install phpunit/PHPUnit diff --git a/tests/SearchEngineTest.php b/tests/SearchEngineTest.php index 0a02c49c..0ae14bdd 100644 --- a/tests/SearchEngineTest.php +++ b/tests/SearchEngineTest.php @@ -1,18 +1,9 @@ fetchObject() ) { - $matches[] = intval( $row->page_id ); + while( $row = $results->next() ) { + $matches[] = $row->getTitle()->getPrefixedText(); } $results->free(); # Search is not guaranteed to return results in a certain order; @@ -91,7 +82,7 @@ END $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." ); if( !is_null( $this->db ) ) { $this->assertEquals( - array( 3 ), + array( 'Smithee' ), $this->fetchIds( $this->search->searchText( 'smithee' ) ), "Plain search failed" ); } @@ -102,7 +93,10 @@ END if( !is_null( $this->db ) ) { $this->search->setNamespaces( array( 0, 1, 4 ) ); $this->assertEquals( - array( 2, 3 ), + array( + 'Smithee', + 'Talk:Main Page', + ), $this->fetchIds( $this->search->searchText( 'smithee' ) ), "Power search failed" ); } @@ -112,7 +106,10 @@ END $this->assertFalse( is_null( $this->db ), "Can't find a database to test with." ); if( !is_null( $this->db ) ) { $this->assertEquals( - array( 3, 9 ), + array( + 'Alan Smithee', + 'Smithee', + ), $this->fetchIds( $this->search->searchTitle( 'smithee' ) ), "Title search failed" ); } @@ -123,7 +120,11 @@ END if( !is_null( $this->db ) ) { $this->search->setNamespaces( array( 0, 1, 4 ) ); $this->assertEquals( - array( 3, 4, 9 ), + array( + 'Alan Smithee', + 'Smithee', + 'Talk:Smithee', + ), $this->fetchIds( $this->search->searchTitle( 'smithee' ) ), "Title power search failed" ); } @@ -132,4 +133,4 @@ END } -?> + diff --git a/tests/SearchMySQL4Test.php b/tests/SearchMySQL4Test.php index 6277e6ca..0f3a4c2c 100644 --- a/tests/SearchMySQL4Test.php +++ b/tests/SearchMySQL4Test.php @@ -1,19 +1,16 @@ PHPUnit_TestCase( $name ); + function __construct( $name ) { + parent::__construct( $name ); } function setUp() { $GLOBALS['wgContLang'] = new Language; - $this->db =& buildTestDatabase( - 'mysql4', + $this->db = $this->buildTestDatabase( array( 'page', 'revision', 'text', 'searchindex' ) ); if( $this->db ) { $this->insertSearchData(); @@ -31,4 +28,4 @@ class SearchMySQL4Test extends SearchEngine_TestCase { } -?> + diff --git a/tests/run-test.php b/tests/run-test.php new file mode 100644 index 00000000..37ca8519 --- /dev/null +++ b/tests/run-test.php @@ -0,0 +1,7 @@ +