Tpt has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/371649 )

Change subject: Moves edit validation to the Content interface implementation
......................................................................

Moves edit validation to the Content interface implementation

Change-Id: I4277bd4920aee599280109f448558c858a165ac8
---
M ProofreadPage.body.php
M extension.json
M i18n/en.json
M i18n/qqq.json
M includes/index/EditIndexPage.php
M includes/index/IndexContent.php
M includes/page/EditPagePage.php
M includes/page/PageContent.php
M tests/phpunit/index/IndexContentTest.php
M tests/phpunit/page/PageContentBuilderTest.php
M tests/phpunit/page/PageContentHandlerTest.php
M tests/phpunit/page/PageContentTest.php
12 files changed, 191 insertions(+), 201 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ProofreadPage 
refs/changes/49/371649/1

diff --git a/ProofreadPage.body.php b/ProofreadPage.body.php
index 56b01ed..72acd4c 100644
--- a/ProofreadPage.body.php
+++ b/ProofreadPage.body.php
@@ -607,58 +607,6 @@
        }
 
        /**
-        * Make validation of the content in the edit API
-        *
-        * @param IContextSource $context
-        * @param Content $content Content of the edit box.
-        * @param Status $status Status object to represent errors, etc.
-        * @param string $summary Edit summary for page.
-        * @param User $user User who is performing the edit.
-        * @param bool $minoredit Whether the edit was marked as minor by the 
user.
-        * @return bool
-        */
-       public static function onEditFilterMergedContent( IContextSource 
$context, Content $content,
-               Status $status, $summary, User $user, $minoredit
-       ) {
-               // If the content's model isn't ours, ignore this; there's 
nothing for us to do here.
-               if ( !( $content instanceof PageContent ) ) {
-                       return true;
-               }
-
-               // Fail if the content is invalid, or the level is being 
removed.
-               if ( !$content->isValid() ) {
-                       $status->fatal( 'proofreadpage_badpagetext' );
-                       return false;
-               }
-
-               $oldContent = $context->getWikiPage()->getContent( 
Revision::FOR_THIS_USER, $user );
-               if ( $oldContent === null ) {
-                       $oldContent = ContentHandler::getForModelID( 
CONTENT_MODEL_PROOFREAD_PAGE )
-                               ->makeEmptyContent();
-               }
-               if ( $oldContent->getModel() !== CONTENT_MODEL_PROOFREAD_PAGE ) 
{
-                       // Let's convert it to Page: page content
-                       $oldContent = $oldContent->convert( 
CONTENT_MODEL_PROOFREAD_PAGE );
-               }
-               if ( !( $oldContent instanceof PageContent ) ) {
-                       // We consider it as a page creation
-                       $oldContent = ContentHandler::getForModelID( 
CONTENT_MODEL_PROOFREAD_PAGE )
-                               ->makeEmptyContent();
-               }
-
-               $oldLevel = $oldContent->getLevel();
-               $newLevel = $content->getLevel();
-
-               // Fail if the user changed the level and the change isn't 
allowed
-               if ( !$oldLevel->isChangeAllowed( $newLevel ) ) {
-                       $status->fatal( 'proofreadpage_notallowedtext' );
-                       return false;
-               }
-
-               return true;
-       }
-
-       /**
         * Provides text for preload API
         *
         * @param string $text
diff --git a/extension.json b/extension.json
index 042b62b..20b2081 100644
--- a/extension.json
+++ b/extension.json
@@ -88,9 +88,6 @@
                "ApiQueryProofreadInfo": "ApiQueryProofreadInfo.php",
                "ProofreadPage\\FileProviderMock": 
"tests/phpunit/FileProviderMock.php",
                "ProofreadPageTestCase": 
"tests/phpunit/ProofreadPageTestCase.php",
-               "ProofreadIndexPageTest": 
"tests/phpunit/index/ProofreadIndexPageTest.php",
-               "ProofreadPagePageTest": 
"tests/phpunit/page/ProofreadPagePageTest.php",
-               "ProofreadPage\\Page\\PageContentTest": 
"tests/phpunit/page/PageContentTest.php",
                "FixProofreadPagePagesContentModel": 
"maintenance/fixProofreadPagePagesContentModel.php",
                "FixProofreadIndexPagesContentModel": 
"maintenance/fixProofreadIndexPagesContentModel.php"
        },
@@ -298,9 +295,6 @@
                ],
                "ContentHandlerDefaultModelFor": [
                        "ProofreadPage::onContentHandlerDefaultModelFor"
-               ],
-               "EditFilterMergedContent": [
-                       "ProofreadPage::onEditFilterMergedContent"
                ],
                "EditFormPreloadText": [
                        "ProofreadPage::onEditFormPreloadText"
diff --git a/i18n/en.json b/i18n/en.json
index c0551f5..e8b5b57 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -14,13 +14,9 @@
        "proofreadpage_index_expected": "Error: Index expected",
        "proofreadpage_nosuch_index": "Error: No such index",
        "proofreadpage_nosuch_file": "Error: No such file",
-       "proofreadpage_badpage": "Wrong format",
-       "proofreadpage_badpagetext": "The format of the page you attempted to 
save is incorrect.",
-       "proofreadpage_indexdupe": "Duplicate link",
        "proofreadpage_indexdupetext": "Pages cannot be listed more than once 
on an index page.",
        "proofreadpage_nologin": "Not logged in",
        "proofreadpage_nologintext": "You must be [[Special:UserLogin|logged 
in]] to modify the proofreading status of pages.",
-       "proofreadpage_notallowed": "Change not allowed",
        "proofreadpage_notallowedtext": "You are not allowed to change the 
proofreading status of this page.",
        "proofreadpage_dataconfig_badformatted": "Bug in data configuration",
        "proofreadpage_dataconfig_badformattedtext": "The page 
[[Mediawiki:Proofreadpage index data config]] is not in well-formatted JSON.",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index ff28c9d..fa818fa 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -40,14 +40,10 @@
        "proofreadpage_index_expected": "Used as error message",
        "proofreadpage_nosuch_index": "Used as error message",
        "proofreadpage_nosuch_file": "Used as error message",
-       "proofreadpage_badpage": "Seems unused inside the extension",
-       "proofreadpage_badpagetext": "Used as error message",
-       "proofreadpage_indexdupe": "Meaning: \"This is a duplicate link\"",
        "proofreadpage_indexdupetext": "Used as error message",
        "proofreadpage_nologin": "{{Identical|Not logged in}}",
        "proofreadpage_nologintext": "Used as error message",
-       "proofreadpage_notallowed": "Used as error title.\n\nThe body for this 
title is {{msg-mw|Proofreadpage notallowedtext}}.\n\nTranslate this as 
\"Changing the proofreading status is not allowed\".",
-       "proofreadpage_notallowedtext": "Used as error message.\n\nThe title 
for this error is {{msg-mw|Proofreadpage notallowed}}.",
+       "proofreadpage_notallowedtext": "Used as error message.",
        "proofreadpage_dataconfig_badformatted": "Title of the error page when 
[[MediaWiki:Proofreadpage index data config]] is not in well-formatted JSON",
        "proofreadpage_dataconfig_badformattedtext": "Content of the error page 
when [[MediaWiki:Proofreadpage index data config]] is not in well-formatted 
JSON",
        "proofreadpage_number_expected": "The place where the data entry should 
be in numeric form",
diff --git a/includes/index/EditIndexPage.php b/includes/index/EditIndexPage.php
index 9f0603e..5a87049 100644
--- a/includes/index/EditIndexPage.php
+++ b/includes/index/EditIndexPage.php
@@ -185,37 +185,4 @@
 
                return $value;
        }
-
-       /**
-        * Check the validity of the page
-        *
-        * @see EditPage::internalAttemptSave
-        */
-       public function internalAttemptSave( &$result, $bot = false ) {
-               $content = $this->toEditContent( $this->textbox1 );
-               if ( $content instanceof IndexContent ) {
-                       // Get list of pages titles
-                       $links = $content->getLinksToNamespace(
-                               $this->extContext->getPageNamespaceId(), 
$this->getTitle()
-                       );
-                       $linksTitle = [];
-                       foreach ( $links as $link ) {
-                               $linksTitle[] = $link->getTarget();
-                       }
-
-                       if ( count( $linksTitle ) !== count( array_unique( 
$linksTitle ) ) ) {
-                               $this->context->getOutput()->showErrorPage(
-                                       'proofreadpage_indexdupe',
-                                       'proofreadpage_indexdupetext'
-                               );
-                               $status = Status::newGood();
-                               $status->fatal( 'hookaborted' );
-                               $status->value = self::AS_HOOK_ERROR;
-
-                               return $status;
-                       }
-               }
-
-               return parent::internalAttemptSave( $result, $bot );
-       }
 }
diff --git a/includes/index/IndexContent.php b/includes/index/IndexContent.php
index 0e113e4..9ef128f 100644
--- a/includes/index/IndexContent.php
+++ b/includes/index/IndexContent.php
@@ -6,12 +6,15 @@
 use MagicWord;
 use MalformedTitleException;
 use ParserOptions;
+use ProofreadPage\Context;
 use ProofreadPage\Link;
 use ProofreadPage\Pagination\PageList;
 use Sanitizer;
+use Status;
 use TextContent;
 use Title;
 use User;
+use WikiPage;
 use WikitextContent;
 
 /**
@@ -131,6 +134,30 @@
        }
 
        /**
+        * @see Content::prepareSave
+        */
+       public function prepareSave( WikiPage $page, $flags, $parentRevId, User 
$user ) {
+               if ( !$this->isValid() ) {
+                       return Status::newFatal( 'invalid-content-data' );
+               }
+
+               // Get list of pages titles
+               $links = $this->getLinksToNamespace(
+                       Context::getDefaultContext()->getPageNamespaceId(), 
$page->getTitle()
+               );
+               $linksTitle = [];
+               foreach ( $links as $link ) {
+                       $linksTitle[] = $link->getTarget();
+               }
+
+               if ( count( $linksTitle ) !== count( array_unique( $linksTitle 
) ) ) {
+                       return Status::newFatal( 'proofreadpage_indexdupetext' 
);
+               }
+
+               return Status::newGood();
+       }
+
+       /**
         * @see Content::getSize
         */
        public function getSize() {
diff --git a/includes/page/EditPagePage.php b/includes/page/EditPagePage.php
index b6c4dc4..468ed1a 100644
--- a/includes/page/EditPagePage.php
+++ b/includes/page/EditPagePage.php
@@ -210,32 +210,4 @@
                        $currentContent
                )->serialize();
        }
-
-       /**
-        * Check the validity of the page
-        *
-        * @see EditPage::internalAttemptSave
-        */
-       public function internalAttemptSave( &$result, $bot = false ) {
-               $error = '';
-               $oldContent = $this->getCurrentContent();
-               $newContent = $this->toEditContent( $this->textbox1 );
-
-               if ( !$newContent->isValid() ) {
-                       $error = 'badpage';
-               } elseif ( !$oldContent->getLevel()->isChangeAllowed( 
$newContent->getLevel() ) ) {
-                       $error = 'notallowed';
-               }
-
-               if ( $error !== '' ) {
-                       $this->context->getOutput()->showErrorPage(
-                               'proofreadpage_' . $error, 'proofreadpage_' . 
$error . 'text'
-                       );
-                       $status = Status::newFatal( 'hookaborted' );
-                       $status->value = self::AS_HOOK_ERROR;
-                       return $status;
-               }
-
-               return parent::internalAttemptSave( $result, $bot );
-       }
 }
diff --git a/includes/page/PageContent.php b/includes/page/PageContent.php
index 9b3c1aa..157d3c1 100644
--- a/includes/page/PageContent.php
+++ b/includes/page/PageContent.php
@@ -6,9 +6,12 @@
 use Html;
 use MagicWord;
 use ParserOptions;
+use Revision;
+use Status;
 use TextContent;
 use Title;
 use User;
+use WikiPage;
 use WikitextContent;
 
 /**
@@ -167,6 +170,42 @@
        }
 
        /**
+        * @see Content::prepareSave
+        */
+       public function prepareSave( WikiPage $page, $flags, $parentRevId, User 
$user ) {
+               if ( !$this->isValid() ) {
+                       return Status::newFatal( 'invalid-content-data' );
+               }
+
+               $oldContent = $this->getContentForRevId( $parentRevId );
+               if ( $oldContent->getModel() !== CONTENT_MODEL_PROOFREAD_PAGE ) 
{
+                       // Let's convert it to Page: page content
+                       $oldContent = $oldContent->convert( 
CONTENT_MODEL_PROOFREAD_PAGE );
+               }
+               if ( !( $oldContent instanceof self ) ) {
+                       return Status::newFatal( 'invalid-content-data' );
+               }
+               if ( !$oldContent->getLevel()->isChangeAllowed( 
$this->getLevel() ) ) {
+                       return Status::newFatal( 'proofreadpage_notallowedtext' 
);
+               }
+
+               return Status::newGood();
+       }
+
+       private function getContentForRevId( $revId ) {
+               if ( $revId !== -1 ) {
+                       $revision = Revision::newFromId( $revId );
+                       if ( $revision !== null ) {
+                               $content = $revision->getContent();
+                               if ( $content !== null ) {
+                                       return $content;
+                               }
+                       }
+               }
+               return $this->getContentHandler()->makeEmptyContent();
+       }
+
+       /**
         * @see Content::getRedirectTarget
         */
        public function getRedirectTarget() {
diff --git a/tests/phpunit/index/IndexContentTest.php 
b/tests/phpunit/index/IndexContentTest.php
index d2a4081..ef00c24 100644
--- a/tests/phpunit/index/IndexContentTest.php
+++ b/tests/phpunit/index/IndexContentTest.php
@@ -9,6 +9,7 @@
 use ProofreadPage\Pagination\PageList;
 use ProofreadPageTestCase;
 use RequestContext;
+use Status;
 use Title;
 use User;
 use WikitextContent;
@@ -182,6 +183,30 @@
                $this->assertEquals( $expectedContent, $content );
        }
 
+       public function prepareSaveProvider() {
+               return [
+                       [
+                               Status::newGood(),
+                               new IndexContent( [] )
+                       ],
+                       [
+                               Status::newFatal( 'proofreadpage_indexdupetext' 
),
+                               new IndexContent( [
+                                       'page' => new WikitextContent( 
'[[Page:Foo]] [[Page:Bar]] [[Page:Foo]]' )
+                               ] )
+                       ]
+               ];
+       }
+
+       /**
+        * @dataProvider prepareSaveProvider
+        */
+       public function testPrepareSave( Status $expectedResult, IndexContent 
$content ) {
+               $this->assertEquals( $expectedResult, $content->prepareSave(
+                       $this->requestContext->getWikiPage(), 0, -1, 
$this->requestContext->getUser()
+               ) );
+       }
+
        public function testGetSize() {
                $content = new IndexContent( [ 'foo' => new WikitextContent( 
'bar' ) ] );
                $this->assertEquals( 3, $content->getSize() );
diff --git a/tests/phpunit/page/PageContentBuilderTest.php 
b/tests/phpunit/page/PageContentBuilderTest.php
index 8688f79..3202914 100644
--- a/tests/phpunit/page/PageContentBuilderTest.php
+++ b/tests/phpunit/page/PageContentBuilderTest.php
@@ -9,6 +9,7 @@
 use ProofreadPageTestCase;
 use RequestContext;
 use User;
+use WikitextContent;
 
 /**
  * @group ProofreadPage
@@ -26,6 +27,15 @@
 
                $this->context = new RequestContext();
                $this->context->setUser( User::newFromName( 'Test' ) );
+       }
+
+       private static function newContent(
+               $header = '', $body = '', $footer = '', $level = 1, 
$proofreader = null
+       ) {
+               return new PageContent(
+                       new WikitextContent( $header ), new WikitextContent( 
$body ), new WikitextContent( $footer ),
+                       new PageLevel( $level, PageLevel::getUserFromUserName( 
$proofreader ) )
+               );
        }
 
        /**
@@ -58,13 +68,13 @@
                                                'Test.djvu', "{{\n|Title=Test 
book\n|Header={{{title}}}\n}}"
                                        )
                                ),
-                               PageContentTest::newContent( 'Test book', '', 
'<references />', 1 ),
+                               self::newContent( 'Test book', '', '<references 
/>', 1 ),
                        ],
                        [
                                $this->newPagePage(
                                        'LoremIpsum.djvu/2'
                                ),
-                               PageContentTest::newContent( '', "Lorem ipsum 
\n2 \n", '<references/>', 1 ),
+                               self::newContent( '', "Lorem ipsum \n2 \n", 
'<references/>', 1 ),
                        ],
                        [
                                $this->newPagePage(
@@ -74,7 +84,7 @@
                                                "{{\n|Title=Test 
book\n|Pages=<pagelist/>\n|Header={{{pagenum}}}\n}}"
                                        )
                                ),
-                               PageContentTest::newContent( '2', "Lorem ipsum 
\n2 \n", '<references />', 1 ),
+                               self::newContent( '2', "Lorem ipsum \n2 \n", 
'<references />', 1 ),
                        ],
                        [
                                $this->newPagePage(
@@ -85,7 +95,7 @@
                                                        
"|Header={{{pagenum}}}\n}}"
                                        )
                                ),
-                               PageContentTest::newContent( 'ii', "Lorem ipsum 
\n2 \n", '<references />', 1 ),
+                               self::newContent( 'ii', "Lorem ipsum \n2 \n", 
'<references />', 1 ),
                        ],
                ];
        }
@@ -110,24 +120,24 @@
                                '42',
                                '42',
                                2,
-                               PageContentTest::newContent( '22', '22', '22', 
2, 'Test2' ),
-                               PageContentTest::newContent( '42', '42', '42', 
2, 'Test2' ),
+                               self::newContent( '22', '22', '22', 2, 'Test2' 
),
+                               self::newContent( '42', '42', '42', 2, 'Test2' 
),
                        ],
                        [
                                '42',
                                '42',
                                '42',
                                2,
-                               PageContentTest::newContent( '22', '22', '22', 
2, null ),
-                               PageContentTest::newContent( '42', '42', '42', 
2, 'Test' ),
+                               self::newContent( '22', '22', '22', 2, null ),
+                               self::newContent( '42', '42', '42', 2, 'Test' ),
                        ],
                        [
                                '42',
                                '42',
                                '42',
                                3,
-                               PageContentTest::newContent( '22', '22', '22', 
2, 'Test2' ),
-                               PageContentTest::newContent( '42', '42', '42', 
3, 'Test' ),
+                               self::newContent( '22', '22', '22', 2, 'Test2' 
),
+                               self::newContent( '42', '42', '42', 3, 'Test' ),
                        ],
                ];
        }
diff --git a/tests/phpunit/page/PageContentHandlerTest.php 
b/tests/phpunit/page/PageContentHandlerTest.php
index bd4cccb..6b49c2d 100644
--- a/tests/phpunit/page/PageContentHandlerTest.php
+++ b/tests/phpunit/page/PageContentHandlerTest.php
@@ -7,10 +7,11 @@
 use MWContentSerializationException;
 use ProofreadPageTestCase;
 use Title;
+use WikitextContent;
 
 /**
  * @group ProofreadPage
- * @covers ProofreadPageContentHandler
+ * @covers PageContentHandler
  */
 class PageContentHandlerTest extends ProofreadPageTestCase {
 
@@ -23,6 +24,15 @@
                parent::setUp();
 
                $this->handler = ContentHandler::getForModelID( 
CONTENT_MODEL_PROOFREAD_PAGE );
+       }
+
+       private static function newContent(
+               $header = '', $body = '', $footer = '', $level = 1, 
$proofreader = null
+       ) {
+               return new PageContent(
+                       new WikitextContent( $header ), new WikitextContent( 
$body ), new WikitextContent( $footer ),
+                       new PageLevel( $level, PageLevel::getUserFromUserName( 
$proofreader ) )
+               );
        }
 
        public function pageWikitextSerializationProvider() {
@@ -113,7 +123,7 @@
        public function testSerializeContentInWikitext(
                $header, $body, $footer, $level, $proofreader
        ) {
-               $pageContent = PageContentTest::newContent( $header, $body, 
$footer, $level, $proofreader );
+               $pageContent = self::newContent( $header, $body, $footer, 
$level, $proofreader );
 
                $serializedString = '<noinclude><pagequality level="' . $level 
. '" user="';
                $serializedString .= $proofreader;
@@ -131,7 +141,7 @@
                $header, $body, $footer, $level, $proofreader, $text
        ) {
                $this->assertEquals(
-                       PageContentTest::newContent( $header, $body, $footer, 
$level, $proofreader ),
+                       self::newContent( $header, $body, $footer, $level, 
$proofreader ),
                        $this->handler->unserializeContent( $text )
                );
        }
@@ -142,7 +152,7 @@
        public function testRoundTripSerializeContentInWikitext(
                $header, $body, $footer, $level, $proofreader, $text
        ) {
-               $content = PageContentTest::newContent( $header, $body, 
$footer, $level, $proofreader );
+               $content = self::newContent( $header, $body, $footer, $level, 
$proofreader );
                $this->assertEquals(
                        $content,
                        $this->handler->unserializeContent( 
$this->handler->serializeContent( $content ) )
@@ -150,7 +160,7 @@
        }
 
        public function testSerializeContentInJson() {
-               $pageContent = PageContentTest::newContent( 'Foo', 'Bar', 
'FooBar', 2, '1.2.3.4' );
+               $pageContent = self::newContent( 'Foo', 'Bar', 'FooBar', 2, 
'1.2.3.4' );
 
                $this->assertEquals(
                        FormatJson::encode( [
@@ -195,7 +205,7 @@
                $header, $body, $footer, $level, $proofreader, $text
        ) {
                $this->assertEquals(
-                       PageContentTest::newContent( $header, $body, $footer, 
$level, $proofreader ),
+                       self::newContent( $header, $body, $footer, $level, 
$proofreader ),
                        $this->handler->unserializeContent( $text, 
CONTENT_FORMAT_JSON )
                );
        }
@@ -241,7 +251,7 @@
        public function testRoundTripSerializeContentInJson(
                $header, $body, $footer, $level, $proofreader, $text
        ) {
-               $content = PageContentTest::newContent( $header, $body, 
$footer, $level, $proofreader );
+               $content = self::newContent( $header, $body, $footer, $level, 
$proofreader );
                $this->assertEquals(
                        $content,
                        $this->handler->unserializeContent(
@@ -259,27 +269,27 @@
        public static function merge3Provider() {
                return [
                        [
-                               PageContentTest::newContent( '', "first 
paragraph\n\nsecond paragraph\n" ),
-                               PageContentTest::newContent( '', "FIRST 
paragraph\n\nsecond paragraph\n" ),
-                               PageContentTest::newContent( '', "first 
paragraph\n\nSECOND paragraph\n" ),
-                               PageContentTest::newContent( '', "FIRST 
paragraph\n\nSECOND paragraph\n" )
+                               self::newContent( '', "first 
paragraph\n\nsecond paragraph\n" ),
+                               self::newContent( '', "FIRST 
paragraph\n\nsecond paragraph\n" ),
+                               self::newContent( '', "first 
paragraph\n\nSECOND paragraph\n" ),
+                               self::newContent( '', "FIRST 
paragraph\n\nSECOND paragraph\n" )
                        ],
                        [
-                               PageContentTest::newContent( '', "test\n" ),
-                               PageContentTest::newContent( '', "dddd\n" ),
-                               PageContentTest::newContent( '', "ffff\n" ),
+                               self::newContent( '', "test\n" ),
+                               self::newContent( '', "dddd\n" ),
+                               self::newContent( '', "ffff\n" ),
                                false
                        ],
                        [
-                               PageContentTest::newContent( '', "test\n", '', 
1, 'John' ),
-                               PageContentTest::newContent( '', "test2\n", '', 
2, 'Jack' ),
-                               PageContentTest::newContent( '', "test\n", '', 
2, 'Bob' ),
-                               PageContentTest::newContent( '', "test2\n", '', 
2, 'Bob' ),
+                               self::newContent( '', "test\n", '', 1, 'John' ),
+                               self::newContent( '', "test2\n", '', 2, 'Jack' 
),
+                               self::newContent( '', "test\n", '', 2, 'Bob' ),
+                               self::newContent( '', "test2\n", '', 2, 'Bob' ),
                        ],
                        [
-                               PageContentTest::newContent( '', "test\n", '', 
1, 'John' ),
-                               PageContentTest::newContent( '', "test\n", '', 
2, 'Jack' ),
-                               PageContentTest::newContent( '', "test\n", '', 
1, 'Bob' ),
+                               self::newContent( '', "test\n", '', 1, 'John' ),
+                               self::newContent( '', "test\n", '', 2, 'Jack' ),
+                               self::newContent( '', "test\n", '', 1, 'Bob' ),
                                false
                        ],
                ];
@@ -297,18 +307,18 @@
        public static function getAutosummaryProvider() {
                return [
                        [
-                               PageContentTest::newContent( '', '', '', 1 ),
-                               PageContentTest::newContent( 'aa', 'aa', 'aa', 
1 ),
+                               self::newContent( '', '', '', 1 ),
+                               self::newContent( 'aa', 'aa', 'aa', 1 ),
                                ''
                        ],
                        [
                                null,
-                               PageContentTest::newContent( '', 'aaa', '', 1 ),
+                               self::newContent( '', 'aaa', '', 1 ),
                                '/* Not proofread */'
                        ],
                        [
-                               PageContentTest::newContent( '', '', '', 2 ),
-                               PageContentTest::newContent( '', '', '', 1 ),
+                               self::newContent( '', '', '', 2 ),
+                               self::newContent( '', '', '', 1 ),
                                '/* Not proofread */'
                        ]
                ];
diff --git a/tests/phpunit/page/PageContentTest.php 
b/tests/phpunit/page/PageContentTest.php
index 14894a8..7f18235 100644
--- a/tests/phpunit/page/PageContentTest.php
+++ b/tests/phpunit/page/PageContentTest.php
@@ -6,13 +6,14 @@
 use ParserOptions;
 use ProofreadPageTestCase;
 use RequestContext;
+use Status;
 use Title;
 use User;
 use WikitextContent;
 
 /**
  * @group ProofreadPage
- * @covers ProofreadPageContent
+ * @covers \ProofreadPage\Page\PageContent
  */
 class PageContentTest extends ProofreadPageTestCase {
 
@@ -40,31 +41,13 @@
                $this->requestContext->setUser( $user );
        }
 
-       /**
-        * Constructor of a new PageContent
-        * @param WikitextContent|string $header
-        * @param WikitextContent|string $body
-        * @param WikitextContent|string $footer
-        * @param integer $level
-        * @param User|string $proofreader
-        * @return PageContent
-        */
-       public static function newContent(
+       private static function newContent(
                $header = '', $body = '', $footer = '', $level = 1, 
$proofreader = null
        ) {
-               if ( is_string( $header ) ) {
-                       $header = new WikitextContent( $header );
-               }
-               if ( is_string( $body ) ) {
-                       $body = new WikitextContent( $body );
-               }
-               if ( is_string( $footer ) ) {
-                       $footer = new WikitextContent( $footer );
-               }
-               if ( is_string( $proofreader ) ) {
-                       $proofreader = PageLevel::getUserFromUserName( 
$proofreader );
-               }
-               return new PageContent( $header, $body, $footer, new PageLevel( 
$level, $proofreader ) );
+               return new PageContent(
+                       new WikitextContent( $header ), new WikitextContent( 
$body ), new WikitextContent( $footer ),
+                       new PageLevel( $level, PageLevel::getUserFromUserName( 
$proofreader ) )
+               );
        }
 
        public function testGetModel() {
@@ -73,21 +56,18 @@
        }
 
        public function testGetHeader() {
-               $header = new WikitextContent( "testString" );
-               $pageContent = self::newContent( $header );
-               $this->assertEquals( $header, $pageContent->getHeader() );
+               $pageContent = self::newContent( 'testString' );
+               $this->assertEquals( new WikitextContent( 'testString' ), 
$pageContent->getHeader() );
        }
 
        public function testGetFooter() {
-               $footer = new WikitextContent( "testString" );
-               $pageContent = self::newContent( '', '', $footer );
-               $this->assertEquals( $footer, $pageContent->getFooter() );
+               $pageContent = self::newContent( '', '', 'testString' );
+               $this->assertEquals( new WikitextContent( 'testString' ), 
$pageContent->getFooter() );
        }
 
        public function testGetBody() {
-               $body = new WikitextContent( "testString" );
-               $pageContent = self::newContent( '', $body );
-               $this->assertEquals( $body, $pageContent->getBody() );
+               $pageContent = self::newContent( '', 'testString' );
+               $this->assertEquals( new WikitextContent( 'testString' ), 
$pageContent->getBody() );
        }
 
        public function testGetLevel() {
@@ -152,7 +132,7 @@
 
        public function testGetWikitextForTransclusion() {
                $content = self::newContent( 'aa', 'test', 'bb', 2, 'ater' );
-               return $this->assertEquals( 'test', 
$content->getWikitextForTransclusion() );
+               $this->assertEquals( 'test', 
$content->getWikitextForTransclusion() );
        }
 
        public function getTextForSummaryProvider() {
@@ -178,7 +158,7 @@
        /**
         * @dataProvider getTextForSummaryProvider
         */
-       public function testGetTextForSummary( $content, $length, $result ) {
+       public function testGetTextForSummary( PageContent $content, $length, 
$result ) {
                $this->assertEquals( $result, $content->getTextForSummary( 
$length ) );
        }
 
@@ -205,7 +185,7 @@
        /**
         * @dataProvider preSaveTransformProvider
         */
-       public function testPreSaveTransform( $content, $expectedContent ) {
+       public function testPreSaveTransform( PageContent $content, 
$expectedContent ) {
                global $wgContLang;
 
                $options = ParserOptions::newFromUserAndLang(
@@ -235,7 +215,7 @@
        /**
         * @dataProvider preloadTransformProvider
         */
-       public function testPreloadTransform( $content, $expectedContent ) {
+       public function testPreloadTransform( PageContent $content, 
$expectedContent ) {
                global $wgContLang;
 
                $options = ParserOptions::newFromUserAndLang(
@@ -247,6 +227,32 @@
                $this->assertEquals( $expectedContent, $content );
        }
 
+       public function prepareSaveProvider() {
+               return [
+                       [
+                               Status::newGood(),
+                               self::newContent()
+                       ],
+                       [
+                               Status::newFatal( 'invalid-content-data' ),
+                               self::newContent( '', '', '', 5 )
+                       ],
+                       [
+                               Status::newFatal( 
'proofreadpage_notallowedtext' ),
+                               self::newContent( '', '', '', 4 )
+                       ]
+               ];
+       }
+
+       /**
+        * @dataProvider prepareSaveProvider
+        */
+       public function testPrepareSave( Status $expectedResult, PageContent 
$content ) {
+               $this->assertEquals( $expectedResult, $content->prepareSave(
+                       $this->requestContext->getWikiPage(), 0, -1, 
$this->requestContext->getUser()
+               ) );
+       }
+
        public function testRedirectTarget() {
                $title = Title::makeTitle( NS_MAIN, 'Test' );
                $content = self::newContent( '', '#REDIRECT [[Test]]' );

-- 
To view, visit https://gerrit.wikimedia.org/r/371649
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4277bd4920aee599280109f448558c858a165ac8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ProofreadPage
Gerrit-Branch: master
Gerrit-Owner: Tpt <thoma...@hotmail.fr>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to