Mwjames has uploaded a new change for review.

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


Change subject: Support of redirects in \SMW\Api\BrowseBySubject
......................................................................

Support of redirects in \SMW\Api\BrowseBySubject

Change-Id: Iddf27ef3397bfd36b4b0d99ddcee972d9aea34c4
---
M includes/api/BrowseBySubject.php
M tests/phpunit/MockObjectRepository.php
M tests/phpunit/includes/api/BrowseBySubjectSerializationRoundtripTest.php
M tests/phpunit/includes/api/BrowseBySubjectTest.php
4 files changed, 146 insertions(+), 38 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki 
refs/changes/55/92855/1

diff --git a/includes/api/BrowseBySubject.php b/includes/api/BrowseBySubject.php
index 58569a3..b461d17 100644
--- a/includes/api/BrowseBySubject.php
+++ b/includes/api/BrowseBySubject.php
@@ -12,6 +12,12 @@
 /**
  * Api module to browse a subject
  *
+ * @note Browsing of a subobject subject is limited to its "parent" subject,
+ * meaning that a request for a "Foo#_ed5a9979db6609b32733eda3fb747d21" subject
+ * will produce information for "Foo" as a whole including its subobjects
+ * because MW's WebRequest (responsible for handling request data sent by a
+ * browser) will eliminate any fragments (indicated by "#") to an Api consumer
+ *
  * @ingroup Api
  *
  * @licence GNU GPL v2+
@@ -39,22 +45,44 @@
         * @since 1.9
         */
        protected function getSubject( $text ) {
-               return DIWikiPage::newFromTitle( $this->newFromText( $text ) );
+               return DIWikiPage::newFromTitle(
+                       $this->assertValidTitle( Title::newFromText( $text ) )
+               );
+       }
+
+       /**
+        * @since 1.9
+        *
+        * @param Title|null $title
+        *
+        * @return Title
+        */
+       protected function assertValidTitle( $title ) {
+
+               if ( $title instanceOf Title && $title->isRedirect() ) {
+
+                       $page = 
$this->withContext()->getDependencyBuilder()->newObject( 'WikiPage', array(
+                               'Title' => $title
+                       ) );
+
+                       $title = $this->assertValidTitle( 
$page->getRedirectTarget() );
+               }
+
+               if ( $title instanceof Title && $title->isValidRedirectTarget() 
) {
+                       return $title;
+               }
+
+               $this->isInvalidTitle( $title );
        }
 
        /**
         * @codeCoverageIgnore
+        *
         * @since 1.9
+        * @throws UsageException
         */
-       protected function newFromText( $text ) {
-
-               $title = Title::newFromText( $text );
-
-               if ( $title instanceOf Title ) {
-                       return $title;
-               }
-
-               $this->dieUsageMsg( array( 'invalidtitle', $text ) );
+       protected function isInvalidTitle( $title ) {
+               $this->dieUsageMsg( array( 'invalidtitle', $title ) );
        }
 
        /**
@@ -82,7 +110,7 @@
         *
         * @note If the subobject already exists within the current SemanticData
         * instance it will not be imported again (this avoids calling the Store
-        * again)
+        * repeatedly)
         *
         * @since 1.9
         */
diff --git a/tests/phpunit/MockObjectRepository.php 
b/tests/phpunit/MockObjectRepository.php
index 79bb0cc..bd7f2fa 100644
--- a/tests/phpunit/MockObjectRepository.php
+++ b/tests/phpunit/MockObjectRepository.php
@@ -318,25 +318,13 @@
                        ->disableOriginalConstructor()
                        ->getMock();
 
-               $wikiPage->expects( $this->any() )
-                       ->method( 'getTimestamp' )
-                       ->will( $this->returnValue( $this->builder->setValue( 
'getTimestamp' ) ) );
+               foreach ( $this->builder->getInvokedMethods() as $method ) {
 
-               $wikiPage->expects( $this->any() )
-                       ->method( 'getTitle' )
-                       ->will( $this->returnValue( $this->builder->setValue( 
'getTitle' ) ) );
+                       $wikiPage->expects( $this->any() )
+                               ->method( $method )
+                               ->will( $this->builder->setCallback( $method ) 
);
 
-               $wikiPage->expects( $this->any() )
-                       ->method( 'getRevision' )
-                       ->will( $this->returnValue( $this->builder->setValue( 
'getRevision' ) ) );
-
-               $wikiPage->expects( $this->any() )
-                       ->method( 'getParserOutput' )
-                       ->will( $this->returnValue( $this->builder->setValue( 
'getParserOutput' ) ) );
-
-               $wikiPage->expects( $this->any() )
-                       ->method( 'makeParserOptions' )
-                       ->will( $this->returnValue( $this->builder->setValue( 
'makeParserOptions' ) ) );
+               }
 
                return $wikiPage;
        }
diff --git 
a/tests/phpunit/includes/api/BrowseBySubjectSerializationRoundtripTest.php 
b/tests/phpunit/includes/api/BrowseBySubjectSerializationRoundtripTest.php
index 970eea9..c00cfe8 100644
--- a/tests/phpunit/includes/api/BrowseBySubjectSerializationRoundtripTest.php
+++ b/tests/phpunit/includes/api/BrowseBySubjectSerializationRoundtripTest.php
@@ -34,7 +34,9 @@
         * @since 1.9
         */
        private function newSemanticData( $text ) {
-               return $data = new SemanticData( DIWikiPage::newFromTitle( 
$this->newTitle( NS_MAIN, $text ) ) );
+               return $data = new SemanticData(
+                       DIWikiPage::newFromTitle( $this->newTitle( NS_MAIN, 
$text ) )
+               );
        }
 
        /**
@@ -45,13 +47,18 @@
         */
        public function testExecuteOnRawModeAndMockStore( $setup ) {
 
-               $api = new BrowseBySubject( $this->getApiMain( array( 'subject' 
=> $setup['subject'] ) ), 'browsebysubject' );
-               
$api->withContext()->getDependencyBuilder()->getContainer()->registerObject( 
'Store', $setup['store'] );
-               $api->getMain()->getResult()->setRawMode();
+               $apiMain  = $this->getApiMain( array( 'subject' => 
$setup['subject'] ) );
+               $instance = new BrowseBySubject( $apiMain, 'browsebysubject' );
 
-               $api->execute();
+               $instance->withContext()
+                       ->getDependencyBuilder()
+                       ->getContainer()
+                       ->registerObject( 'Store', $setup['store'] );
 
-               $this->assertStructuralIntegrity( $setup, $api->getResultData() 
);
+               $instance->getMain()->getResult()->setRawMode();
+               $instance->execute();
+
+               $this->assertStructuralIntegrity( $setup, 
$instance->getResultData() );
 
        }
 
@@ -62,11 +69,17 @@
         */
        public function testExecuteOnMockStore( $setup ) {
 
-               $api = new BrowseBySubject( $this->getApiMain( array( 'subject' 
=> $setup['subject'] ) ), 'browsebysubject' );
-               
$api->withContext()->getDependencyBuilder()->getContainer()->registerObject( 
'Store', $setup['store'] );
-               $api->execute();
+               $apiMain  = $this->getApiMain( array( 'subject' => 
$setup['subject'] ) );
+               $instance = new BrowseBySubject( $apiMain, 'browsebysubject' );
 
-               $result = $api->getResultData();
+               $instance->withContext()
+                       ->getDependencyBuilder()
+                       ->getContainer()
+                       ->registerObject( 'Store', $setup['store'] );
+
+               $instance->execute();
+
+               $result = $instance->getResultData();
 
                $this->assertStructuralIntegrity( $setup, $result );
 
diff --git a/tests/phpunit/includes/api/BrowseBySubjectTest.php 
b/tests/phpunit/includes/api/BrowseBySubjectTest.php
index 233462f..126c275 100644
--- a/tests/phpunit/includes/api/BrowseBySubjectTest.php
+++ b/tests/phpunit/includes/api/BrowseBySubjectTest.php
@@ -89,6 +89,38 @@
        }
 
        /**
+        * @dataProvider redirectDataProvider
+        *
+        * @since 1.9
+        */
+       public function testRedirectTitleOnMockStore( $setup ) {
+
+               $apiMain  = $this->getApiMain( array('subject' => 'Foo' ) );
+               $instance = new BrowseBySubject( $apiMain, 'browsebysubject' );
+
+               $container = 
$instance->withContext()->getDependencyBuilder()->getContainer();
+               $container->registerObject( 'Store', $setup['store'] );
+               $container->registerObject( 'WikiPage', $setup['wikiPage'] );
+
+               $reflector = $this->newReflector();
+               $assertValidTitle = $reflector->getMethod( 'assertValidTitle' );
+               $assertValidTitle->setAccessible( true );
+
+               try {
+
+                       $assertValidTitle->invoke( $instance, $setup['title'] );
+                       $instance->getMain()->getResult()->setRawMode();
+                       $instance->execute();
+
+               } catch ( \UsageException $e ) {
+                       $this->assertTrue( true );
+               };
+
+               $this->assertStructuralIntegrity( $setup, 
$instance->getResultData() );
+
+       }
+
+       /**
         * The Serializer enforces a specific output format therefore expected
         * elements are verified
         *
@@ -140,6 +172,53 @@
        /**
         * @return array
         */
+       public function redirectDataProvider() {
+
+               $mockStore = $this->newMockBuilder()->newObject( 'Store', array(
+                       'getSemanticData' => $this->newSemanticData( 
'Foo-redirect' )
+               ) );
+
+               $mockTitle = $this->newMockBuilder()->newObject( 'Title', array(
+                       'isRedirect' => true
+               ) );
+
+               $mockWikiPage = $this->newMockBuilder()->newObject( 'WikiPage', 
array(
+                       'getRedirectTarget' => $this->newTitle()
+               ) );
+
+               $provider = array();
+
+               // #0 Valid
+               $provider[] = array(
+                       array(
+                               'store'      => $mockStore,
+                               'wikiPage'   => $mockWikiPage,
+                               'title'      => $mockTitle,
+                               'hasSubject' => true,
+                               'hasResult'  => true
+                       )
+               );
+
+               // #1 Invalid, throws UsageException
+               $mockWikiPage = $this->newMockBuilder()->newObject( 'WikiPage', 
array(
+                       'getRedirectTarget'     => null,
+                       'isValidRedirectTarget' => false
+               ) );
+
+               $provider[] = array(
+                       array(
+                               'store'     => $mockStore,
+                               'wikiPage'  => $mockWikiPage,
+                               'title'     => $mockTitle,
+                       )
+               );
+
+               return $provider;
+       }
+
+       /**
+        * @return array
+        */
        public function invalidTitleDataProvider() {
 
                $provider = array();

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iddf27ef3397bfd36b4b0d99ddcee972d9aea34c4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>

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

Reply via email to