jenkins-bot has submitted this change and it was merged.

Change subject: Register #ask as callback
......................................................................


Register #ask as callback

Eliminate static AskParserFunction::render()

Change-Id: I7af42a18cc09aaace497eb160aee453c6b2e2533
---
M SemanticMediaWiki.hooks.php
M includes/Setup.php
M includes/dic/SharedDependencyContainer.php
M includes/parserhooks/AskParserFunction.php
M tests/phpunit/MockObjectRepository.php
M tests/phpunit/includes/SetupTest.php
M tests/phpunit/includes/dic/SharedDependencyContainerTest.php
M tests/phpunit/includes/parserhooks/AskParserFunctionTest.php
8 files changed, 180 insertions(+), 63 deletions(-)

Approvals:
  Mwjames: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/SemanticMediaWiki.hooks.php b/SemanticMediaWiki.hooks.php
index 2b1556a..9507d87 100644
--- a/SemanticMediaWiki.hooks.php
+++ b/SemanticMediaWiki.hooks.php
@@ -191,7 +191,6 @@
         * @return boolean
         */
        public static function onParserFirstCallInit( Parser &$parser ) {
-               $parser->setFunctionHook( 'ask', array( 
'SMW\AskParserFunction', 'render' ) );
                $parser->setFunctionHook( 'show', array( 
'SMW\ShowParserFunction', 'render' ) );
                $parser->setFunctionHook( 'subobject', array( 
'SMW\SubobjectParserFunction', 'render' ) );
                $parser->setFunctionHook( 'concept', array( 
'SMW\ConceptParserFunction', 'render' ) );
diff --git a/includes/Setup.php b/includes/Setup.php
index 81e6f5b..453daf9 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -335,19 +335,33 @@
         */
        protected function registerParserHooks() {
 
-               // FIXME Registration should follow
+               $settings = $this->settings;
 
-               // $objectBuilder = $this->getDependencyBuilder();
-               // $settings = $this->settings;
+               // Inject the ObjectBuilder using the DefaultContext
+               $objectBuilder = new SimpleDependencyBuilder( new 
SharedDependencyContainer() );
+               $objectBuilder->getContainer()->registerObject( 'Settings', 
$settings );
 
-               // $this->globals['wgHooks']['ParserFirstCallInit'][] = 
function ( Parser &$parser ) use ( $objectBuilder, $settings ) {
-               //
-               //      $ask = $objectBuilder->newObject( 'AskParserFunction' , 
array( 'Parser', $parser );
-               //
-               //      $parser->setFunctionHook( 'ask', function( $parser ) 
use ( $ask, $settings ) {
-               //              return $settings->get( 'smwgQEnabled' ) ? 
$ask->parse( func_get_args() ) : $ask->disabled();
-               //      } );
-               // };
+               /**
+                * Called when the parser initialises for the first time
+                *
+                * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit
+                *
+                * @since  1.9
+                */
+               $this->globals['wgHooks']['ParserFirstCallInit'][] = function ( 
\Parser &$parser ) use ( $objectBuilder, $settings ) {
+
+                       /**
+                        * {{#ask}}
+                        *
+                        * @since  1.9
+                        */
+                       $parser->setFunctionHook( 'ask', function( $parser ) 
use ( $objectBuilder, $settings ) {
+                               $ask = $objectBuilder->newObject( 
'AskParserFunction', array( 'Parser' => $parser ) );
+                               return $settings->get( 'smwgQEnabled' ) ? 
$ask->parse( func_get_args() ) : $ask->disabled();
+                       } );
+
+                       return true;
+               };
 
                $this->globals['wgHooks']['ParserFirstCallInit'][] = 
'SMW\DocumentationParserFunction::staticInit';
                $this->globals['wgHooks']['ParserFirstCallInit'][] = 
'SMW\InfoParserFunction::staticInit';
diff --git a/includes/dic/SharedDependencyContainer.php 
b/includes/dic/SharedDependencyContainer.php
index 11d4995..c61e958 100644
--- a/includes/dic/SharedDependencyContainer.php
+++ b/includes/dic/SharedDependencyContainer.php
@@ -121,11 +121,11 @@
                        },
 
                        /**
-                        * IContextSource object definition
+                        * RequestContext object definition
                         *
                         * @since  1.9
                         *
-                        * @return IContextSource
+                        * @return RequestContext
                         */
                        'RequestContext' => function ( DependencyBuilder 
$builder ) {
 
@@ -151,6 +151,64 @@
                         */
                        'WikiPage' => function ( DependencyBuilder $builder ) {
                                return \WikiPage::factory( 
$builder->getArgument( 'Title' ) );
+                       },
+
+                       /**
+                        * MessageFormatter object definition
+                        *
+                        * @since  1.9
+                        *
+                        * @return MessageFormatter
+                        */
+                       'MessageFormatter' => function ( DependencyBuilder 
$builder ) {
+                               return new MessageFormatter( 
$builder->getArgument( 'Language' ) );
+                       },
+
+                       /**
+                        * QueryData object definition
+                        *
+                        * @since  1.9
+                        *
+                        * @return QueryData
+                        */
+                       'QueryData' => function ( DependencyBuilder $builder ) {
+                               return new QueryData( $builder->getArgument( 
'Title' ) );
+                       },
+
+                       /**
+                        * AskParserFunction object definition
+                        *
+                        * @since  1.9
+                        *
+                        * @return AskParserFunction
+                        */
+                       'AskParserFunction' => function ( DependencyBuilder 
$builder ) {
+
+                               $parser = $builder->getArgument( 'Parser' );
+
+                               $parserData = $builder->newObject( 
'ParserData', array(
+                                       'Title'        => $parser->getTitle(),
+                                       'ParserOutput' => $parser->getOutput()
+                               ) );
+
+                               // FIXME Inject a Context instead so that 
QueryData
+                               // and MessageFormatter are only instantiated 
when
+                               // requested
+
+                               // $context = $builder->getArgument( 
'BaseContext' );
+                               // $context->setObjectBuilder( $builder );
+
+                               $queryData = $builder->newObject( 'QueryData', 
array(
+                                       'Title' => $parser->getTitle()
+                               ) );
+
+                               $messageFormatter = $builder->newObject( 
'MessageFormatter', array(
+                                       'Language' => 
$parser->getTargetLanguage()
+                               ) );
+
+                               $instance = new AskParserFunction( $parserData, 
$queryData, $messageFormatter );
+
+                               return $instance;
                        }
 
                );
diff --git a/includes/parserhooks/AskParserFunction.php 
b/includes/parserhooks/AskParserFunction.php
index 88c9f9b..801bc85 100644
--- a/includes/parserhooks/AskParserFunction.php
+++ b/includes/parserhooks/AskParserFunction.php
@@ -144,21 +144,4 @@
                return $this->result;
        }
 
-       /**
-        * Parser::setFunctionHook {{#ask}} handler method
-        *
-        * @since 1.9
-        *
-        * @param Parser $parser
-        *
-        * @return string
-        */
-       public static function render( Parser &$parser ) {
-               $ask = new self(
-                       new ParserData( $parser->getTitle(), 
$parser->getOutput() ),
-                       new QueryData( $parser->getTitle() ),
-                       new MessageFormatter( $parser->getTargetLanguage() )
-               );
-               return $GLOBALS['smwgQEnabled'] ? $ask->parse( func_get_args() 
) : $ask->disabled();
-       }
 }
diff --git a/tests/phpunit/MockObjectRepository.php 
b/tests/phpunit/MockObjectRepository.php
index 9c2fee1..bf7abd7 100644
--- a/tests/phpunit/MockObjectRepository.php
+++ b/tests/phpunit/MockObjectRepository.php
@@ -866,13 +866,17 @@
                        ->getMock();
 
                $parser->expects( $this->any() )
-                       ->method( 'getParserOutput' )
-                       ->will( $this->returnValue( $this->builder->setValue( 
'getParserOutput' ) ) );
+                       ->method( 'getOutput' )
+                       ->will( $this->returnValue( $this->builder->setValue( 
'getOutput' ) ) );
 
                $parser->expects( $this->any() )
                        ->method( 'getTitle' )
                        ->will( $this->returnValue( $this->builder->setValue( 
'getTitle' ) ) );
 
+               $parser->expects( $this->any() )
+                       ->method( 'getTargetLanguage' )
+                       ->will( $this->returnValue( $this->builder->setValue( 
'getTargetLanguage' ) ) );
+
                return $parser;
        }
 
diff --git a/tests/phpunit/includes/SetupTest.php 
b/tests/phpunit/includes/SetupTest.php
index d57a5e9..7a3923c 100644
--- a/tests/phpunit/includes/SetupTest.php
+++ b/tests/phpunit/includes/SetupTest.php
@@ -48,11 +48,50 @@
 
        /**
         * @test Setup::run
-        * @dataProvider hooksDataProvider
+        * @dataProvider functionHooksProvider
         *
         * @since 1.9
         */
-       public function testRegisterFunctionHooksAndParserHooks( $hook, $setup 
) {
+       public function testRegisterFunctionHooks( $hook, $setup ) {
+
+               $this->assertHook( $hook, $setup, 1 );
+
+       }
+
+       /**
+        * @test Setup::run
+        * @dataProvider parserHooksProvider
+        *
+        * @since 1.9
+        */
+       public function testParserHooks( $hook, $setup ) {
+
+               // 4 because of having hooks registered without using a 
callback, after
+               // all parser hooks being registered using a callback this can 
be
+               // reduced to 1
+               $this->assertHook( $hook, $setup, 4 );
+
+               // Verify that registered closures are executable otherwise
+               // malicious code could hide if not properly checked
+               $result = $this->executeHookOnMock( $hook, 
$setup['wgHooks'][$hook][0] );
+
+               if ( $result !== null ) {
+                       $this->assertTrue( $result );
+               } else {
+                       $this->markTestIncomplete( "Test is incomplete because 
of a missing {$hook} closure verification" );
+               }
+
+       }
+
+       /**
+        * Asserts a hook
+        *
+        * @since  1.9
+        *
+        * @param $hook
+        * @param $object
+        */
+       private function assertHook( $hook, &$setup, $count ) {
 
                $instance = $this->newInstance( $setup );
 
@@ -64,24 +103,11 @@
 
                $instance->run();
 
-               // A bit of a heck for now until the ParserHook use callbacks
-               $count = $hook === 'ParserFirstCallInit' ? 3 : 1;
-
                $this->assertCount(
                        $count,
                        $setup['wgHooks'][$hook],
                        "Asserts that after run() the entry counts {$count}"
                );
-
-               // Verify that registered closures are executable otherwise
-               // malicious code could hide if not properly checked
-               // $result = $this->callFunctionHookOnMock( $hook, 
$setup['wgHooks'][$hook][0] );
-
-               //if ( $result !== null ) {
-               //      $this->assertTrue( $result );
-               //} else {
-               //      $this->markTestIncomplete( "Test is incomplete because 
of a missing {$hook} closure verification" );
-               //}
 
        }
 
@@ -93,15 +119,15 @@
         * @param $hook
         * @param $object
         */
-       private function callFunctionHookOnMock( $hook, $object ) {
+       private function executeHookOnMock( $hook, $object ) {
 
                $empty = '';
 
                // Evade execution by setting the title object as isSpecialPage
                // the hook class should always ensure that isSpecialPage is 
checked
-               $title = $this->newMockObject( array(
+               $title =  $this->newMockBuilder()->newObject( 'Title', array(
                        'isSpecialPage' => true
-               ) )->getMockTitle();
+               ) );
 
                $outputPage = $this->newMockBuilder()->newObject( 'OutputPage', 
array(
                        'getTitle' => $title
@@ -405,7 +431,7 @@
        /**
         * @since 1.9
         */
-       public function hooksDataProvider() {
+       public function functionHooksProvider() {
 
                $provider = array();
 
@@ -434,6 +460,26 @@
                        'SkinAfterContent',
                        'OutputPageParserOutput',
                        'ExtensionTypes',
+               );
+
+               foreach ( $hooks as $hook ) {
+                       $provider[] = array(
+                               $hook,
+                               array( 'wgHooks' => array( $hook => array() ) ),
+                       );
+               }
+
+               return $provider;
+       }
+
+       /**
+        * @since 1.9
+        */
+       public function parserHooksProvider() {
+
+               $provider = array();
+
+               $hooks = array(
                        'ParserFirstCallInit'
                );
 
diff --git a/tests/phpunit/includes/dic/SharedDependencyContainerTest.php 
b/tests/phpunit/includes/dic/SharedDependencyContainerTest.php
index 212a219..d2019ba 100644
--- a/tests/phpunit/includes/dic/SharedDependencyContainerTest.php
+++ b/tests/phpunit/includes/dic/SharedDependencyContainerTest.php
@@ -176,6 +176,30 @@
                        )
                );
 
+               $provider[] = array( 'QueryData', array( '\SMW\QueryData' => 
array(
+                               'Title' => $this->newMockBuilder()->newObject( 
'Title' )
+                               )
+                       )
+               );
+
+               $provider[] = array( 'MessageFormatter', array( 
'\SMW\MessageFormatter' => array(
+                               'Language' => 
$this->newMockBuilder()->newObject( 'Language' )
+                               )
+                       )
+               );
+
+               $parser = $this->newMockBuilder()->newObject( 'Parser', array(
+                       'getTitle'          => 
$this->newMockBuilder()->newObject( 'Title' ),
+                       'getOutput'         => 
$this->newMockBuilder()->newObject( 'ParserOutput' ),
+                       'getTargetLanguage' => 
$this->newMockBuilder()->newObject( 'Language' )
+               ) );
+
+               $provider[] = array( 'AskParserFunction', array( 
'\SMW\AskParserFunction' => array(
+                               'Parser' => $parser
+                               )
+                       )
+               );
+
                return $provider;
        }
 }
diff --git a/tests/phpunit/includes/parserhooks/AskParserFunctionTest.php 
b/tests/phpunit/includes/parserhooks/AskParserFunctionTest.php
index b24e900..4b03734 100644
--- a/tests/phpunit/includes/parserhooks/AskParserFunctionTest.php
+++ b/tests/phpunit/includes/parserhooks/AskParserFunctionTest.php
@@ -186,17 +186,6 @@
        }
 
        /**
-        * @test AskParserFunction::render
-        *
-        * @since 1.9
-        */
-       public function testStaticRender() {
-               $parser = $this->getParser( $this->newTitle(), $this->getUser() 
);
-               $result = AskParserFunction::render( $parser );
-               $this->assertInternalType( 'string', $result );
-       }
-
-       /**
         * Provides sample data usually found in {{#ask}} queries
         *
         * @return array

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I7af42a18cc09aaace497eb160aee453c6b2e2533
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Mwjames <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to