Merge "(Bug 41169) Apply rtrim on on preSaveTransfrom."
authorDemon <chadh@wikimedia.org>
Thu, 18 Oct 2012 18:45:36 +0000 (18:45 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 18 Oct 2012 18:45:36 +0000 (18:45 +0000)
1  2 
includes/content/TextContent.php
tests/phpunit/includes/JavascriptContentTest.php
tests/phpunit/includes/TextContentTest.php

@@@ -116,6 -116,22 +116,22 @@@ class TextContent extends AbstractConte
                return $this->getNativeData();
        }
  
+       /**
+        * Returns a Content object with pre-save transformations applied.
+        * This implementation just trims trailing whitespace.
+        *
+        * @param $title Title
+        * @param $user User
+        * @param $popts ParserOptions
+        * @return Content
+        */
+       public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
+               $text = $this->getNativeData();
+               $pst = rtrim( $text );
+               return ( $text === $pst ) ? $this : new WikitextContent( $pst );
+       }
        /**
         * Diff this content object with another content object..
         *
                $revId = null,
                ParserOptions $options = null, $generateHtml = true
        ) {
 -              # Generic implementation, relying on $this->getHtml()
 +              global $wgParser, $wgTextModelsToParse;
 +
 +              if ( !$options ) {
 +                      //NOTE: use canonical options per default to produce cacheable output
 +                      $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
 +              }
 +
 +              if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
 +                      // parse just to get links etc into the database
 +                      $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
 +              } else {
 +                      $po = new ParserOutput();
 +              }
  
                if ( $generateHtml ) {
                        $html = $this->getHtml();
                        $html = '';
                }
  
 -              $po = new ParserOutput( $html );
 +              $po->setText( $html );
                return $po;
        }
  
@@@ -15,20 -15,8 +15,20 @@@ class JavascriptContentTest extends Tex
  
        public function dataGetParserOutput() {
                return array(
 -                      array("MediaWiki:Test.js", null, "hello <world>\n",
 -                                      "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>"),
 +                      array(
 +                              "MediaWiki:Test.js",
 +                              null,
 +                              "hello <world>\n",
 +                              "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>" ),
 +
 +                      array(
 +                              "MediaWiki:Test.js",
 +                              null,
 +                              "hello(); // [[world]]\n",
 +                              "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello(); // [[world]]\n\n</pre>",
 +                              array( 'Links' => array( // NOTE: assumes default settings for $wgTextModelsToParse
 +                                                      array( 'World' => 0 ) ) ) ),
 +
                        // @todo: more...?
                );
        }
                        array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
                                'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
                        ),
+                       array( " Foo \n ",
+                               " Foo",
+                       ),
                );
        }
  
@@@ -27,11 -27,7 +27,11 @@@ class TextContentTest extends MediaWiki
  
        public function dataGetParserOutput() {
                return array(
 -                      array("TextContentTest_testGetParserOutput", CONTENT_MODEL_TEXT, "hello ''world'' & stuff\n", "hello ''world'' &amp; stuff"),
 +                      array(
 +                              "TextContentTest_testGetParserOutput",
 +                              CONTENT_MODEL_TEXT,
 +                              "hello ''world'' & [[stuff]]\n", "hello ''world'' &amp; [[stuff]]",
 +                              array( 'Links' => array() ) ),
                        // @todo: more...?
                );
        }
@@@ -39,7 -35,7 +39,7 @@@
        /**
         * @dataProvider dataGetParserOutput
         */
 -      public function testGetParserOutput( $title, $model, $text, $expectedHtml ) {
 +      public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
                $title = Title::newFromText( $title );
                $content = ContentHandler::makeContent( $text, $title, $model );
  
                $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
  
                $this->assertEquals( $expectedHtml, trim( $html ) );
 +
 +              if ( $expectedFields ) {
 +                      foreach ( $expectedFields as $field => $exp ) {
 +                              $f = 'get' . ucfirst( $field );
 +                              $v = call_user_func( array( $po, $f ) );
 +
 +                              if ( is_array( $exp ) ) {
 +                                      $this->assertArrayEquals( $exp, $v );
 +                              } else {
 +                                      $this->assertEquals( $exp, $v );
 +                              }
 +                      }
 +              }
 +
                // @todo: assert more properties
        }
  
        public function dataPreSaveTransform() {
                return array(
-                       array( 'hello this is ~~~',
-                              "hello this is ~~~",
+                       array( #0: no signature resolution
+                               "hello this is ~~~",
+                               "hello this is ~~~",
+                       ),
+                       array( #1: rtrim
+                               " Foo \n ",
+                               " Foo",
                        ),
                );
        }