phpunit tests for JavascriptContent and CssContent
authordaniel <daniel.kinzler@wikimedia.de>
Mon, 23 Apr 2012 17:13:54 +0000 (19:13 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Mon, 23 Apr 2012 19:21:22 +0000 (21:21 +0200)
includes/AutoLoader.php
tests/phpunit/includes/CssContentTest.php [new file with mode: 0644]
tests/phpunit/includes/JavascriptContentTest.php [new file with mode: 0644]
tests/phpunit/includes/WikitextContentTest.php

index 0f55af2..1da1275 100644 (file)
@@ -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 (file)
index 0000000..072a6b0
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+class CssContentTest extends JavascriptContentTest {
+
+       public function newContent( $text ) {
+               return new CssContent( $text );
+       }
+
+
+       public function dataGetParserOutput() {
+               return array(
+                       array("hello <world>\n", "<pre class=\"mw-code mw-css\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>\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 (file)
index 0000000..41f9f4a
--- /dev/null
@@ -0,0 +1,233 @@
+<?php
+
+class JavascriptContentTest extends WikitextContentTest {
+
+       public function newContent( $text ) {
+               return new JavascriptContent( $text );
+       }
+
+
+       public function dataGetParserOutput() {
+               return array(
+                       array("hello <world>\n", "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>\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 <nowiki>~~~</nowiki>',
+                              'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
+                       ),
+               );
+       }
+
+       public function dataPreloadTransform() {
+               return array(
+                       array( 'hello this is ~~~',
+                              "hello this is ~~~",
+                       ),
+                       array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
+                              'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
+                       ),
+               );
+       }
+
+       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 ),
+               );
+       }
+
+}
index d029daf..c570024 100644 (file)
@@ -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", "<p>hello <i>world</i>\n</p>"),
@@ -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() );
        }