Hoo man has uploaded a new change for review.

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

Change subject: Introduce 
DataAccessSnakFormatterFactory::newRichWikitextSnakFormatter
......................................................................

Introduce DataAccessSnakFormatterFactory::newRichWikitextSnakFormatter

This is a first step towards implementing new functionality
that uses FORMAT_WIKI, instead of escaped plain text.

This is not yet used anywhere, but we need this to a) test
the new output format and b) to introduce the new Lua functionality
and parser functions.

Note: We might want to consider renaming EscapingSnakFormatter to
WrappedSnakFormatter or something similar.

Bug: T142940
Change-Id: Id96d335bcbc207f0f7265b62b8483ed715931b3e
---
M client/includes/DataAccess/DataAccessSnakFormatterFactory.php
M 
client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php
M 
client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterOutputFormatTest.php
3 files changed, 110 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/82/315682/1

diff --git a/client/includes/DataAccess/DataAccessSnakFormatterFactory.php 
b/client/includes/DataAccess/DataAccessSnakFormatterFactory.php
index e8f97a9..b32a959 100644
--- a/client/includes/DataAccess/DataAccessSnakFormatterFactory.php
+++ b/client/includes/DataAccess/DataAccessSnakFormatterFactory.php
@@ -70,6 +70,36 @@
                Language $language,
                UsageAccumulator $usageAccumulator
        ) {
+               return $this->newSnakFormatter( 'escaped-plaintext', $language, 
$usageAccumulator );
+       }
+
+       /**
+        * This returns a SnakFormatter that will return "rich" wikitext.
+        *
+        * @param Language $language
+        * @param UsageAccumulator $usageAccumulator
+        *
+        * @return SnakFormatter
+        */
+       public function newRichWikitextSnakFormatter(
+               Language $language,
+               UsageAccumulator $usageAccumulator
+       ) {
+               return $this->newSnakFormatter( 'rich-wikitext', $language, 
$usageAccumulator );
+       }
+
+       /**
+        * @param string $type
+        * @param Language $language
+        * @param UsageAccumulator $usageAccumulator
+        *
+        * @return SnakFormatter
+        */
+       private function newSnakFormatter(
+               $type,
+               Language $language,
+               UsageAccumulator $usageAccumulator
+       ) {
                $fallbackChain = 
$this->languageFallbackChainFactory->newFromLanguage(
                        $language,
                        LanguageFallbackChainFactory::FALLBACK_ALL
@@ -80,7 +110,11 @@
                        SnakFormatter::OPT_LANG => $language->getCode(),
                ] );
 
-               $snakFormatter = $this->getPlainTextSnakFormatterForOptions( 
$options );
+               if ( $type === 'rich-wikitext' ) {
+                       $snakFormatter = 
$this->getRichWikitextSnakFormatterForOptions( $options );
+               } else {
+                       $snakFormatter = 
$this->getPlainTextSnakFormatterForOptions( $options );
+               }
 
                return new UsageTrackingSnakFormatter(
                        $snakFormatter,
@@ -96,6 +130,28 @@
         * @param FormatterOptions $options
         * @return BinaryOptionDispatchingSnakFormatter
         */
+       private function getRichWikitextSnakFormatterForOptions( 
FormatterOptions $options ) {
+               $snakFormatter = $this->snakFormatterFactory->getSnakFormatter(
+                       SnakFormatter::FORMAT_PLAIN,
+                       $options
+               );
+
+               return new EscapingSnakFormatter(
+                       SnakFormatter::FORMAT_PLAIN,
+                       $snakFormatter,
+                       function( $str ) {
+                               return "<span>$str</span>";
+                       }
+               );
+       }
+
+       /**
+        * Our output format is basically wikitext escaped plain text, except
+        * for URLs, these are not wikitext escaped.
+        *
+        * @param FormatterOptions $options
+        * @return BinaryOptionDispatchingSnakFormatter
+        */
        private function getPlainTextSnakFormatterForOptions( FormatterOptions 
$options ) {
                $plainTextSnakFormatter = 
$this->snakFormatterFactory->getSnakFormatter(
                        SnakFormatter::FORMAT_PLAIN,
diff --git 
a/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php
 
b/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php
index c1792aa..cac963b 100644
--- 
a/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php
+++ 
b/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterFactoryTest.php
@@ -26,10 +26,10 @@
  */
 class DataAccessSnakFormatterFactoryTest extends PHPUnit_Framework_TestCase {
 
-       private function getDataAccessSnakFormatterFactory() {
+       private function getDataAccessSnakFormatterFactory( $expectedFormat ) {
                return new DataAccessSnakFormatterFactory(
                        $this->getLanguageFallbackChainFactory(),
-                       $this->getOutputFormatSnakFormatterFactory(),
+                       $this->getOutputFormatSnakFormatterFactory( 
$expectedFormat ),
                        $this->getMock( PropertyDataTypeLookup::class )
                );
        }
@@ -53,27 +53,44 @@
        /**
         * @return OutputFormatSnakFormatterFactory
         */
-       private function getOutputFormatSnakFormatterFactory() {
+       private function getOutputFormatSnakFormatterFactory( $expectedFormat ) 
{
                $factory = $this->getMockBuilder( 
OutputFormatSnakFormatterFactory::class )
                        ->disableOriginalConstructor()
                        ->getMock();
 
+               $snakFormatter = $this->getMock( SnakFormatter::class );
+               $snakFormatter->expects( $this->any() )
+                       ->method( 'getFormat' )
+                       ->will( $this->returnValue( $expectedFormat ) );
+
                $factory->expects( $this->once() )
                        ->method( 'getSnakFormatter' )
-                       ->with( SnakFormatter::FORMAT_PLAIN, 
$this->isInstanceOf( FormatterOptions::class ) )
-                       ->will( $this->returnValue( $this->getMock( 
SnakFormatter::class ) ) );
+                       ->with( $expectedFormat, $this->isInstanceOf( 
FormatterOptions::class ) )
+                       ->will( $this->returnValue( $snakFormatter ) );
 
                return $factory;
        }
 
-       public function testNewSnakFormatterForLanguage() {
-               $factory = $this->getDataAccessSnakFormatterFactory();
+       public function testNewEscapedPlainTextSnakFormatter() {
+               $factory = $this->getDataAccessSnakFormatterFactory( 
SnakFormatter::FORMAT_PLAIN );
                $snakFormatter = $factory->newEscapedPlainTextSnakFormatter(
                        Language::factory( 'fr' ),
                        $this->getMock( UsageAccumulator::class )
                );
 
                $this->assertInstanceOf( SnakFormatter::class, $snakFormatter );
+               $this->assertSame( SnakFormatter::FORMAT_PLAIN, 
$snakFormatter->getFormat() );
+       }
+
+       public function testNewRichWikitextSnakFormatter() {
+               $factory = $this->getDataAccessSnakFormatterFactory( 
SnakFormatter::FORMAT_WIKI );
+               $snakFormatter = $factory->newRichWikitextSnakFormatter(
+                       Language::factory( 'fr' ),
+                       $this->getMock( UsageAccumulator::class )
+               );
+
+               $this->assertInstanceOf( SnakFormatter::class, $snakFormatter );
+               $this->assertSame( SnakFormatter::FORMAT_WIKI, 
$snakFormatter->getFormat() );
        }
 
 }
diff --git 
a/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterOutputFormatTest.php
 
b/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterOutputFormatTest.php
index 26c7cb5..19c18b7 100644
--- 
a/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterOutputFormatTest.php
+++ 
b/client/tests/phpunit/includes/DataAccess/DataAccessSnakFormatterOutputFormatTest.php
@@ -86,9 +86,35 @@
        }
 
        /**
-        * @dataProvider snakProvider
+        * @dataProvider richWikitextSnakProvider
         */
-       public function testOutput( $expected, $snak ) {
+       public function testRichWikitextOutput( $expected, $snak ) {
+               // This is an integration test, use the global factory
+               $factory = 
WikibaseClient::getDefaultInstance()->getDataAccessSnakFormatterFactory();
+               $formatter = $factory->newRichWikitextSnakFormatter(
+                       Language::factory( 'en' ),
+                       $this->getMock( UsageAccumulator::class )
+               );
+
+               $this->assertSame( $expected, $formatter->formatSnak( $snak ) );
+       }
+
+       public function richWikitextSnakProvider() {
+               return [
+                       'string' => [
+                               '<span>A string!</span>',
+                               new PropertyValueSnak(
+                                       new PropertyId( 'P5' ),
+                                       new StringValue( 'A string!' )
+                               )
+                       ]
+               ];
+       }
+
+       /**
+        * @dataProvider escapedPlainTextSnakProvider
+        */
+       public function testEscapedPlainTextOutput( $expected, $snak ) {
                // This is an integration test, use the global factory
                $factory = 
WikibaseClient::getDefaultInstance()->getDataAccessSnakFormatterFactory();
                $formatter = $factory->newEscapedPlainTextSnakFormatter(
@@ -99,7 +125,7 @@
                $this->assertSame( $expected, $formatter->formatSnak( $snak ) );
        }
 
-       public function snakProvider() {
+       public function escapedPlainTextSnakProvider() {
                $settings = WikibaseClient::getDefaultInstance()->getSettings();
                $repoConceptBaseUri = $settings->getSetting( 
'repoConceptBaseUri' );
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id96d335bcbc207f0f7265b62b8483ed715931b3e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <h...@online.de>

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

Reply via email to