MaxSem has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/97783


Change subject: Basic mobileview test
......................................................................

Basic mobileview test

Change-Id: I841c284d83ca8fe78261c8f103b674e3847b0fcc
---
M includes/api/ApiMobileView.php
M tests/ApiMobileViewTest.php
2 files changed, 158 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/83/97783/1

diff --git a/includes/api/ApiMobileView.php b/includes/api/ApiMobileView.php
index eb1016c..647e3b8 100644
--- a/includes/api/ApiMobileView.php
+++ b/includes/api/ApiMobileView.php
@@ -27,7 +27,8 @@
                $this->getMain()->setCacheMode( 'anon-public-user-private' );
 
                // Enough '*' keys in JSON!!!
-               $textElement = $this->getMain()->getPrinter()->getFormat() == 
'XML' ? '*' : 'text';
+               $isXml = $this->getMain()->isInternalMode() || 
$this->getMain()->getPrinter()->getFormat() == 'XML';
+               $textElement = $isXml ? '*' : 'text';
                $params = $this->extractRequestParams();
 
                $prop = array_flip( $params['prop'] );
@@ -45,16 +46,7 @@
                        $this->maxlen = PHP_INT_MAX;
                }
 
-               $title = Title::newFromText( $params['page'] );
-               if ( !$title ) {
-                       $this->dieUsageMsg( array( 'invalidtitle', 
$params['page'] ) );
-               }
-               if ( $title->inNamespace( NS_FILE ) ) {
-                       $this->file = wfFindFile( $title );
-               }
-               if ( !$title->exists() && !$this->file ) {
-                       $this->dieUsageMsg( array( 'notanarticle', 
$params['page'] ) );
-               }
+               $title = $this->makeTitle( $params['page'] );
                $this->mainPage = $title->isMainPage();
                if ( $this->mainPage && $this->noHeadings ) {
                        $this->noHeadings = false;
@@ -143,6 +135,26 @@
                wfProfileOut( __METHOD__ );
        }
 
+       /**
+        * Creates and validates a title
+        *
+        * @param string$name
+        * @return Title
+        */
+       protected function makeTitle( $name ) {
+               $title = Title::newFromText( $name );
+               if ( !$title ) {
+                       $this->dieUsageMsg( array( 'invalidtitle', $name ) );
+               }
+               if ( $title->inNamespace( NS_FILE ) ) {
+                       $this->file = wfFindFile( $title );
+               }
+               if ( !$title->exists() && !$this->file ) {
+                       $this->dieUsageMsg( array( 'notanarticle', $name ) );
+               }
+               return $title;
+       }
+
        private function stringSplitter( $text ) {
                if ( $this->offset < 0  ) {
                        return $text; // NOOP - string splitting mode is off
@@ -220,15 +232,46 @@
                return array_flip( $ret );
        }
 
+       /**
+        * Performs a page parse
+        *
+        * @param WikiPage $wp
+        * @param ParserOptions $parserOptions
+        * @return ParserOutput
+        */
+       protected function getParserOutput( WikiPage $wp, ParserOptions 
$parserOptions ) {
+               wfProfileIn( __METHOD__ );
+               $time = microtime( true );
+               $parserOutput = $wp->getParserOutput( $parserOptions );
+               $time = microtime( true ) - $time;
+               if ( !$parserOutput ) {
+                       wfDebugLog( 'mobile', "Empty parser output on 
'{$wp->getTitle()->getPrefixedText()}': rev {$wp->getId()}, time $time" );
+                       throw new MWException( __METHOD__ . ": PoolCounter 
didn't return parser output" );
+               }
+               $parserOutput->setTOCEnabled( false );
+               wfProfileOut( __METHOD__ );
+               return $parserOutput;
+       }
+
+       /**
+        * Creates a WikiPage from title
+        *
+        * @param Title $title
+        * @return WikiPage
+        */
+       protected function makeWikiPage( Title $title ) {
+               return WikiPage::factory( $title );
+       }
+
        private function getData( Title $title, $noImages ) {
                global $wgMemc, $wgUseTidy, $wgMFMinCachedPageSize;
 
                wfProfileIn( __METHOD__ );
-               $wp = WikiPage::factory( $title );
+               $wp = $this->makeWikiPage( $title );
                if ( $this->followRedirects && $wp->isRedirect() ) {
                        $newTitle = $wp->getRedirectTarget();
                        if ( $newTitle ) {
-                               $wp = WikiPage::factory( $newTitle );
+                               $wp = $this->makeWikiPage( $newTitle );
                                $this->getResult()->addValue( null, 
$this->getModuleName(),
                                        array( 'redirected' => 
$newTitle->getPrefixedText() )
                                );
@@ -260,18 +303,9 @@
                if ( $this->file ) {
                        $html = $this->getFilePage( $title );
                } else {
-                       wfProfileIn( __METHOD__ . '-parserOutput' );
-                       $time = microtime( true );
-                       $parserOutput = $wp->getParserOutput( $parserOptions );
-                       $time = microtime( true ) - $time;
-                       if ( !$parserOutput ) {
-                               wfDebugLog( 'mobile', "Empty parser output on 
'{$title->getPrefixedText()}': rev $latest, time $time, cache key $key" );
-                               throw new MWException( __METHOD__ . ": 
PoolCounter didn't return parser output" );
-                       }
-                       $parserOutput->setTOCEnabled( false );
+                       $parserOutput = $this->getParserOutput( $wp, 
$parserOptions );
                        $html = $parserOutput->getText();
                        $cacheExpiry = $parserOutput->getCacheExpiry();
-                       wfProfileOut( __METHOD__ . '-parserOutput' );
                }
 
                wfProfileIn( __METHOD__ . '-MobileFormatter' );
diff --git a/tests/ApiMobileViewTest.php b/tests/ApiMobileViewTest.php
index 74dcbd7..427a302 100644
--- a/tests/ApiMobileViewTest.php
+++ b/tests/ApiMobileViewTest.php
@@ -1,9 +1,56 @@
 <?php
 
+class MockApiMobileView extends ApiMobileView {
+       protected function makeTitle( $name ) {
+               $t = Title::newFromText( $name );
+               $row = new stdClass();
+               $row->page_id = 1;
+               $row->page_title = $t->getDBkey();
+               $row->page_namespace = $t->getNamespace();
+               return Title::newFromRow( $row );
+       }
+
+       protected function getParserOutput( WikiPage $wp, ParserOptions 
$parserOptions ) {
+               $params = $this->extractRequestParams();
+               if ( !isset( $params['text'] ) ) {
+                       throw new MWException( 'Must specify page text' );
+               }
+               $parser = new Parser();
+               $po = $parser->parse( $params['text'], $wp->getTitle(), 
$parserOptions );
+               $po->setTOCEnabled( false );
+               return $po;
+       }
+
+       protected function makeWikiPage( Title $title ) {
+               return new MockWikiPage( $title );
+       }
+
+       public function getAllowedParams() {
+               return array_merge( parent::getAllowedParams(), array( 'text' 
=> null ) );
+       }
+}
+
+class MockWikiPage extends WikiPage {
+       public function getLatest() {
+               return 123;
+       }
+}
+
 /**
  * @group MobileFrontend
  */
 class ApiMobileViewTest extends MediaWikiTestCase {
+       private $savedGlobals;
+
+       public function setUp() {
+               $this->savedGlobals = $GLOBALS;
+               parent::setUp();
+       }
+
+       public function tearDown() {
+               parent::tearDown();
+               $GLOBALS = $this->savedGlobals;
+       }
        /**
         * @dataProvider provideSections
         */
@@ -41,4 +88,58 @@
                        array( array( 1, 3, 4, 5 ), array(), '1|3-5|4' ),
                );
        }
+
+       /**
+        * @dataProvider provideView
+        */
+       public function testView( array $input, array $expected ) {
+               global $wgAPIModules;
+               $wgAPIModules['mobileview'] = 'MockApiMobileView';
+
+               $request = new FauxRequest( $input );
+               $context = new RequestContext();
+               $context->setRequest( $request );
+               $api = new MockApiMobileView( new ApiMain( $context ), 
'mobileview' );
+               $api->execute();
+               $this->assertArrayEquals( $expected, $api->getResultData() );
+       }
+
+       public function provideView() {
+               $baseIn = array(
+                       'action' => 'mobileview',
+                       'page' => 'Foo',
+                       'sections' => '1-',
+                       'noheadings' => '',
+                       'text' => 'Lead
+== Section 1 ==
+Text 1
+== Section 2 ==
+Text 2
+',
+               );
+               return array(
+                       array(
+                               $baseIn,
+                               array(
+                                       'mobileview' => array(
+                                               'sections' => array(
+                                                       array( 'id' => 0 ),
+                                                       array(
+                                                               'toclevel' => 1,
+                                                               'line' => 
'Section 1',
+                                                               'id' => 1,
+                                                               '*' => '<p>Text 
1</p>'
+                                                       ),
+                                                       array(
+                                                               'toclevel' => 1,
+                                                               'line' => 
'Section 2',
+                                                               'id' => 2,
+                                                               '*' => '<p>Text 
2</p>'
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                       ),
+               );
+       }
 }
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I841c284d83ca8fe78261c8f103b674e3847b0fcc
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to