From 3606cfa7f116b0e009ca734432e0a560a0a314d9 Mon Sep 17 00:00:00 2001 From: daniel Date: Mon, 23 Apr 2012 19:13:54 +0200 Subject: [PATCH] phpunit tests for JavascriptContent and CssContent --- includes/AutoLoader.php | 4 + tests/phpunit/includes/CssContentTest.php | 41 +++ .../includes/JavascriptContentTest.php | 233 ++++++++++++++++++ .../phpunit/includes/WikitextContentTest.php | 52 ++-- 4 files changed, 303 insertions(+), 27 deletions(-) create mode 100644 tests/phpunit/includes/CssContentTest.php create mode 100644 tests/phpunit/includes/JavascriptContentTest.php diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 0f55af2c6f..1da12751e2 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -972,6 +972,10 @@ $wgAutoloadLocalClasses = array( 'TestFileIterator' => 'tests/testHelpers.inc', 'TestRecorder' => 'tests/testHelpers.inc', + # tests/phpunit + 'WikitextContentTest' => 'tests/phpunit/includes/WikitextContentTest.php', + 'JavascriptContentTest' => 'tests/phpunit/includes/JavascriptContentTest.php', + # tests/parser 'ParserTest' => 'tests/parser/parserTest.inc', 'ParserTestParserHook' => 'tests/parser/parserTestsParserHook.php', diff --git a/tests/phpunit/includes/CssContentTest.php b/tests/phpunit/includes/CssContentTest.php new file mode 100644 index 0000000000..072a6b0224 --- /dev/null +++ b/tests/phpunit/includes/CssContentTest.php @@ -0,0 +1,41 @@ +\n", "
\nhello <world>\n\n
\n"), + // @todo: more...? + ); + } + + + # ================================================================================================================= + + public function getModelName() { + $content = $this->newContent( "hello world." ); + + $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModelName() ); + } + + public function getContentHandler() { + $content = $this->newContent( "hello world." ); + + $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelName() ); + } + + public function dataEquals( ) { + return array( + array( new CssContent( "hallo" ), null, false ), + array( new CssContent( "hallo" ), new CssContent( "hallo" ), true ), + array( new CssContent( "hallo" ), new WikitextContent( "hallo" ), false ), + array( new CssContent( "hallo" ), new CssContent( "HALLO" ), false ), + ); + } + +} diff --git a/tests/phpunit/includes/JavascriptContentTest.php b/tests/phpunit/includes/JavascriptContentTest.php new file mode 100644 index 0000000000..41f9f4ae64 --- /dev/null +++ b/tests/phpunit/includes/JavascriptContentTest.php @@ -0,0 +1,233 @@ +\n", "
\nhello <world>\n\n
\n"), + // @todo: more...? + ); + } + + public function dataGetSection() { + return array( + array( WikitextContentTest::$sections, + "0", + null + ), + array( WikitextContentTest::$sections, + "2", + null + ), + array( WikitextContentTest::$sections, + "8", + null + ), + ); + } + + public function dataReplaceSection() { + return array( + array( WikitextContentTest::$sections, + "0", + "No more", + null, + null + ), + array( WikitextContentTest::$sections, + "", + "No more", + null, + null + ), + array( WikitextContentTest::$sections, + "2", + "== TEST ==\nmore fun", + null, + null + ), + array( WikitextContentTest::$sections, + "8", + "No more", + null, + null + ), + array( WikitextContentTest::$sections, + "new", + "No more", + "New", + null + ), + ); + } + + public function testAddSectionHeader( ) { + $content = $this->newContent( 'hello world' ); + $c = $content->addSectionHeader( 'test' ); + + $this->assertTrue( $content->equals( $c ) ); + } + + public function dataPreSaveTransform() { + return array( + array( 'hello this is ~~~', + "hello this is ~~~", + ), + array( 'hello \'\'this\'\' is ~~~', + 'hello \'\'this\'\' is ~~~', + ), + ); + } + + public function dataPreloadTransform() { + return array( + array( 'hello this is ~~~', + "hello this is ~~~", + ), + array( 'hello \'\'this\'\' is foobar', + 'hello \'\'this\'\' is foobar', + ), + ); + } + + public function dataGetRedirectTarget() { + return array( + array( '#REDIRECT [[Test]]', + null, + ), + array( '#REDIRECT Test', + null, + ), + array( '* #REDIRECT [[Test]]', + null, + ), + ); + } + + /** + * @todo: test needs database! + */ + /* + public function getRedirectChain() { + $text = $this->getNativeData(); + return Title::newFromRedirectArray( $text ); + } + */ + + /** + * @todo: test needs database! + */ + /* + public function getUltimateRedirectTarget() { + $text = $this->getNativeData(); + return Title::newFromRedirectRecurse( $text ); + } + */ + + + public function dataIsCountable() { + return array( + array( '', + null, + 'any', + true + ), + array( 'Foo', + null, + 'any', + true + ), + array( 'Foo', + null, + 'comma', + false + ), + array( 'Foo, bar', + null, + 'comma', + false + ), + array( 'Foo', + null, + 'link', + false + ), + array( 'Foo [[bar]]', + null, + 'link', + false + ), + array( 'Foo', + true, + 'link', + false + ), + array( 'Foo [[bar]]', + false, + 'link', + false + ), + array( '#REDIRECT [[bar]]', + true, + 'any', + true + ), + array( '#REDIRECT [[bar]]', + true, + 'comma', + false + ), + array( '#REDIRECT [[bar]]', + true, + 'link', + false + ), + ); + } + + public function dataGetTextForSummary() { + return array( + array( "hello\nworld.", + 16, + 'hello world.', + ), + array( 'hello world.', + 8, + 'hello...', + ), + array( '[[hello world]].', + 8, + '[[hel...', + ), + ); + } + + # ================================================================================================================= + + public function getModelName() { + $content = $this->newContent( "hello world." ); + + $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModelName() ); + } + + public function getContentHandler() { + $content = $this->newContent( "hello world." ); + + $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelName() ); + } + + public function dataEquals( ) { + return array( + array( new JavascriptContent( "hallo" ), null, false ), + array( new JavascriptContent( "hallo" ), new JavascriptContent( "hallo" ), true ), + array( new JavascriptContent( "hallo" ), new CssContent( "hallo" ), false ), + array( new JavascriptContent( "hallo" ), new JavascriptContent( "HALLO" ), false ), + ); + } + +} diff --git a/tests/phpunit/includes/WikitextContentTest.php b/tests/phpunit/includes/WikitextContentTest.php index d029dafe07..c570024b6a 100644 --- a/tests/phpunit/includes/WikitextContentTest.php +++ b/tests/phpunit/includes/WikitextContentTest.php @@ -7,6 +7,10 @@ class WikitextContentTest extends MediaWikiTestCase { $this->context->setTitle( Title::newFromText( "Test" ) ); } + public function newContent( $text ) { + return new WikitextContent( $text ); + } + public function dataGetParserOutput() { return array( array("hello ''world''\n", "

hello world\n

"), @@ -18,7 +22,7 @@ class WikitextContentTest extends MediaWikiTestCase { * @dataProvider dataGetParserOutput */ public function testGetParserOutput( $text, $expectedHtml ) { - $content = new WikitextContent( $text ); + $content = $this->newContent( $text ); $po = $content->getParserOutput( $this->context ); @@ -62,11 +66,11 @@ just a test" * @dataProvider dataGetSection */ public function testGetSection( $text, $sectionId, $expectedText ) { - $content = new WikitextContent( $text ); + $content = $this->newContent( $text ); $sectionContent = $content->getSection( $sectionId ); - $this->assertEquals( $expectedText, $sectionContent->getNativeData() ); + $this->assertEquals( $expectedText, is_null( $sectionContent ) ? null : $sectionContent->getNativeData() ); } public function dataReplaceSection() { @@ -108,20 +112,14 @@ just a test" * @dataProvider dataReplaceSection */ public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) { - $content = new WikitextContent( $text ); - $c = $content->replaceSection( $section, new WikitextContent( $with ), $sectionTitle ); + $content = $this->newContent( $text ); + $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle ); - $this->assertEquals( $expected, $c->getNativeData() ); + $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() ); } - /** - * Returns a new WikitextContent object with the given section heading prepended. - * - * @param $header String - * @return Content - */ public function testAddSectionHeader( ) { - $content = new WikitextContent( 'hello world' ); + $content = $this->newContent( 'hello world' ); $content = $content->addSectionHeader( 'test' ); $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() ); @@ -142,7 +140,7 @@ just a test" * @dataProvider dataPreSaveTransform */ public function testPreSaveTransform( $text, $expected ) { - $content = new WikitextContent( $text ); + $content = $this->newContent( $text ); $content = $content->preSaveTransform( $this->context->getTitle(), $this->context->getUser() ); $this->assertEquals( $expected, $content->getNativeData() ); @@ -163,7 +161,7 @@ just a test" * @dataProvider dataPreloadTransform */ public function testPreloadTransform( $text, $expected ) { - $content = new WikitextContent( $text ); + $content = $this->newContent( $text ); $content = $content->preloadTransform( $this->context->getTitle() ); $this->assertEquals( $expected, $content->getNativeData() ); @@ -187,7 +185,7 @@ just a test" * @dataProvider dataGetRedirectTarget */ public function testGetRedirectTarget( $text, $expected ) { - $content = new WikitextContent( $text ); + $content = $this->newContent( $text ); $t = $content->getRedirectTarget( ); if ( is_null( $expected ) ) $this->assertNull( $t, "text should not have generated a redirect target: $text" ); @@ -198,7 +196,7 @@ just a test" * @dataProvider dataGetRedirectTarget */ public function isRedirect( $text, $expected ) { - $content = new WikitextContent( $text ); + $content = $this->newContent( $text ); $this->assertEquals( !is_null($expected), $content->isRedirect() ); } @@ -295,7 +293,7 @@ just a test" $old = $wgArticleCountMethod; $wgArticleCountMethod = $mode; - $content = new WikitextContent( $text ); + $content = $this->newContent( $text ); $v = $content->isCountable( $hasLinks, $this->context ); $wgArticleCountMethod = $old; @@ -325,20 +323,20 @@ just a test" * @dataProvider dataGetTextForSummary */ public function testGetTextForSummary( $text, $maxlength, $expected ) { - $content = new WikitextContent( $text ); + $content = $this->newContent( $text ); $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) ); } public function testGetTextForSearchIndex( ) { - $content = new WikitextContent( "hello world." ); + $content = $this->newContent( "hello world." ); $this->assertEquals( "hello world.", $content->getTextForSearchIndex() ); } public function testCopy() { - $content = new WikitextContent( "hello world." ); + $content = $this->newContent( "hello world." ); $copy = $content->copy(); $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" ); @@ -346,19 +344,19 @@ just a test" } public function testGetSize( ) { - $content = new WikitextContent( "hello world." ); + $content = $this->newContent( "hello world." ); $this->assertEquals( 12, $content->getSize() ); } public function testGetNativeData( ) { - $content = new WikitextContent( "hello world." ); + $content = $this->newContent( "hello world." ); $this->assertEquals( "hello world.", $content->getNativeData() ); } public function testGetWikitextForTransclusion( ) { - $content = new WikitextContent( "hello world." ); + $content = $this->newContent( "hello world." ); $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() ); } @@ -366,13 +364,13 @@ just a test" # ================================================================================================================= public function getModelName() { - $content = new WikitextContent( "hello world." ); + $content = $this->newContent( "hello world." ); $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModelName() ); } public function getContentHandler() { - $content = new WikitextContent( "hello world." ); + $content = $this->newContent( "hello world." ); $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelName() ); } @@ -390,7 +388,7 @@ just a test" * @dataProvider dataIsEmpty */ public function testIsEmpty( $text, $empty ) { - $content = new WikitextContent( $text ); + $content = $this->newContent( $text ); $this->assertEquals( $empty, $content->isEmpty() ); } -- 2.20.1