MediaWiki  REL1_22
WikitextContentHandlerTest.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class WikitextContentHandlerTest extends MediaWikiLangTestCase {
00007 
00011     var $handler;
00012 
00013     public function setUp() {
00014         parent::setUp();
00015 
00016         $this->handler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT );
00017     }
00018 
00022     public function testSerializeContent() {
00023         $content = new WikitextContent( 'hello world' );
00024 
00025         $this->assertEquals( 'hello world', $this->handler->serializeContent( $content ) );
00026         $this->assertEquals( 'hello world', $this->handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT ) );
00027 
00028         try {
00029             $this->handler->serializeContent( $content, 'dummy/foo' );
00030             $this->fail( "serializeContent() should have failed on unknown format" );
00031         } catch ( MWException $e ) {
00032             // ok, as expected
00033         }
00034     }
00035 
00039     public function testUnserializeContent() {
00040         $content = $this->handler->unserializeContent( 'hello world' );
00041         $this->assertEquals( 'hello world', $content->getNativeData() );
00042 
00043         $content = $this->handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
00044         $this->assertEquals( 'hello world', $content->getNativeData() );
00045 
00046         try {
00047             $this->handler->unserializeContent( 'hello world', 'dummy/foo' );
00048             $this->fail( "unserializeContent() should have failed on unknown format" );
00049         } catch ( MWException $e ) {
00050             // ok, as expected
00051         }
00052     }
00053 
00057     public function testMakeEmptyContent() {
00058         $content = $this->handler->makeEmptyContent();
00059 
00060         $this->assertTrue( $content->isEmpty() );
00061         $this->assertEquals( '', $content->getNativeData() );
00062     }
00063 
00064     public static function dataIsSupportedFormat() {
00065         return array(
00066             array( null, true ),
00067             array( CONTENT_FORMAT_WIKITEXT, true ),
00068             array( 99887766, false ),
00069         );
00070     }
00071 
00078     public function testMakeRedirectContent( $title, $expected ) {
00079         global $wgContLang;
00080         $wgContLang->resetNamespaces();
00081 
00082         if ( is_string( $title ) ) {
00083             $title = Title::newFromText( $title );
00084         }
00085         $content = $this->handler->makeRedirectContent( $title );
00086         $this->assertEquals( $expected, $content->serialize() );
00087     }
00088 
00089     public static function provideMakeRedirectContent() {
00090         return array(
00091             array( 'Hello', '#REDIRECT [[Hello]]' ),
00092             array( 'Template:Hello', '#REDIRECT [[Template:Hello]]' ),
00093             array( 'Hello#section', '#REDIRECT [[Hello#section]]' ),
00094             array( 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ),
00095             array( 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ),
00096             array( 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ),
00097             array( Title::makeTitle( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ),
00098             array( Title::makeTitle( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ),
00099             array( Title::makeTitle( NS_MAIN, 'Bar', 'fragment', 'google' ), '#REDIRECT [[google:Bar#fragment]]' ),
00100         );
00101     }
00102 
00107     public function testIsSupportedFormat( $format, $supported ) {
00108         $this->assertEquals( $supported, $this->handler->isSupportedFormat( $format ) );
00109     }
00110 
00111     public static function dataMerge3() {
00112         return array(
00113             array(
00114                 "first paragraph
00115 
00116                     second paragraph\n",
00117 
00118                 "FIRST paragraph
00119 
00120                     second paragraph\n",
00121 
00122                 "first paragraph
00123 
00124                     SECOND paragraph\n",
00125 
00126                 "FIRST paragraph
00127 
00128                     SECOND paragraph\n",
00129             ),
00130 
00131             array( "first paragraph
00132                     second paragraph\n",
00133 
00134                 "Bla bla\n",
00135 
00136                 "Blubberdibla\n",
00137 
00138                 false,
00139             ),
00140         );
00141     }
00142 
00147     public function testMerge3( $old, $mine, $yours, $expected ) {
00148         $this->checkHasDiff3();
00149 
00150         // test merge
00151         $oldContent = new WikitextContent( $old );
00152         $myContent = new WikitextContent( $mine );
00153         $yourContent = new WikitextContent( $yours );
00154 
00155         $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
00156 
00157         $this->assertEquals( $expected, $merged ? $merged->getNativeData() : $merged );
00158     }
00159 
00160     public static function dataGetAutosummary() {
00161         return array(
00162             array(
00163                 'Hello there, world!',
00164                 '#REDIRECT [[Foo]]',
00165                 0,
00166                 '/^Redirected page .*Foo/'
00167             ),
00168 
00169             array(
00170                 null,
00171                 'Hello world!',
00172                 EDIT_NEW,
00173                 '/^Created page .*Hello/'
00174             ),
00175 
00176             array(
00177                 'Hello there, world!',
00178                 '',
00179                 0,
00180                 '/^Blanked/'
00181             ),
00182 
00183             array(
00184                 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
00185                 labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et
00186                 ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
00187                 'Hello world!',
00188                 0,
00189                 '/^Replaced .*Hello/'
00190             ),
00191 
00192             array(
00193                 'foo',
00194                 'bar',
00195                 0,
00196                 '/^$/'
00197             ),
00198         );
00199     }
00200 
00205     public function testGetAutosummary( $old, $new, $flags, $expected ) {
00206         $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
00207         $newContent = is_null( $new ) ? null : new WikitextContent( $new );
00208 
00209         $summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
00210 
00211         $this->assertTrue( (bool)preg_match( $expected, $summary ), "Autosummary didn't match expected pattern $expected: $summary" );
00212     }
00213 
00217     /*
00218     public function testGetAutoDeleteReason( Title $title, &$hasHistory ) {}
00219     */
00220 
00224     /*
00225     public function testGetUndoContent( Revision $current, Revision $undo, Revision $undoafter = null ) {}
00226     */
00227 }