Merge "Clean up Wikibase related code"

This commit is contained in:
jenkins-bot 2016-01-25 17:20:46 +00:00 committed by Gerrit Code Review
commit 0ea59c1526
3 changed files with 39 additions and 58 deletions

View File

@ -2,7 +2,6 @@
use DataValues\StringValue; use DataValues\StringValue;
use ValueFormatters\Exceptions\MismatchingDataValueTypeException; use ValueFormatters\Exceptions\MismatchingDataValueTypeException;
use ValueFormatters\FormattingException;
use ValueFormatters\ValueFormatter; use ValueFormatters\ValueFormatter;
use Wikibase\Lib\SnakFormatter; use Wikibase\Lib\SnakFormatter;
@ -17,22 +16,25 @@ use Wikibase\Lib\SnakFormatter;
class MathFormatter implements ValueFormatter { class MathFormatter implements ValueFormatter {
/**
* @var string One of the SnakFormatter::FORMAT_... constants.
*/
private $format; private $format;
/* /**
* Loads format to distinguish the type of formatting * Loads format to distinguish the type of formatting
* *
* @param string $format * @param string $format One of the SnakFormatter::FORMAT_... constants.
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function __construct( $format ) { public function __construct( $format ) {
switch ( $format ) { switch ( $format ) {
case ( SnakFormatter::FORMAT_HTML ): case SnakFormatter::FORMAT_PLAIN:
case ( SnakFormatter::FORMAT_HTML_DIFF ): case SnakFormatter::FORMAT_WIKI:
case ( SnakFormatter::FORMAT_HTML_WIDGET ): case SnakFormatter::FORMAT_HTML:
case ( SnakFormatter::FORMAT_WIKI ): case SnakFormatter::FORMAT_HTML_DIFF:
case ( SnakFormatter::FORMAT_PLAIN ): case SnakFormatter::FORMAT_HTML_WIDGET:
$this->format = $format; $this->format = $format;
break; break;
default: default:
@ -40,44 +42,40 @@ class MathFormatter implements ValueFormatter {
} }
} }
/* /**
*
* @param StringValue $value * @param StringValue $value
* *
* @throws MismatchingDataValueTypeException
* @return string * @return string
* @throws \ValueFormatters\Exceptions\MismatchingDataValueTypeException
*/ */
public function format( $value ) { public function format( $value ) {
if ( !( $value instanceof StringValue ) ) { if ( !( $value instanceof StringValue ) ) {
throw new MismatchingDataValueTypeException( 'StringValue', get_class( $value ) ); throw new MismatchingDataValueTypeException( 'StringValue', get_class( $value ) );
} }
$tex = $value->getValue(); $tex = $value->getValue();
switch ( $this->format ) { switch ( $this->format ) {
case ( SnakFormatter::FORMAT_PLAIN ): case SnakFormatter::FORMAT_PLAIN:
return "$tex"; return $tex;
case ( SnakFormatter::FORMAT_WIKI ): case SnakFormatter::FORMAT_WIKI:
return "<math>$tex</math>"; return "<math>$tex</math>";
case ( SnakFormatter::FORMAT_HTML ): default:
case ( SnakFormatter::FORMAT_HTML_WIDGET ):
case ( SnakFormatter::FORMAT_HTML_DIFF ):
$renderer = new MathMathML( $tex ); $renderer = new MathMathML( $tex );
if ( $renderer->checkTex() ) { if ( $renderer->checkTex() && $renderer->render() ) {
if ( $renderer->render() ) { return $renderer->getHtmlOutput();
return $renderer->getHtmlOutput();
}
} }
// TeX string is not valid or rendering failed // TeX string is not valid or rendering failed
return $renderer->getLastError(); return $renderer->getLastError();
} }
} }
/** /**
* * @return string One of the SnakFormatter::FORMAT_... constants.
* @return format
*/ */
public function getFormat() { public function getFormat() {
return $this->format; return $this->format;
} }
} }

View File

@ -9,8 +9,6 @@
* @author Moritz Schubotz * @author Moritz Schubotz
*/ */
use MediaWiki\Logger\LoggerFactory;
class MathInputCheckRestbase extends MathInputCheck { class MathInputCheckRestbase extends MathInputCheck {
private $restbaseInterface; private $restbaseInterface;

View File

@ -9,12 +9,8 @@ use DataValues\StringValue;
use DataValues\NumberValue; use DataValues\NumberValue;
class MathFormatterTest extends MediaWikiTestCase { class MathFormatterTest extends MediaWikiTestCase {
const SOME_TEX = "a^2+b^2=c^2";
const FORMAT_PLAIN = 'text/plain'; const SOME_TEX = 'a^2+b^2=c^2';
const FORMAT_HTML = 'text/html';
const FORMAT_XWIKI = 'text/x-wiki';
const FORMAT_UNKNOWN = 'unknown/unknown';
const FORMAT_VALUE = "";
protected static $hasRestbase; protected static $hasRestbase;
@ -23,19 +19,12 @@ class MathFormatterTest extends MediaWikiTestCase {
self::$hasRestbase = $rbi->checkBackend( true ); self::$hasRestbase = $rbi->checkBackend( true );
} }
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
if ( !self::$hasRestbase ) {
$this->markTestSkipped( "Can not connect to Restbase Math interface." );
}
}
protected function tearDown() { if ( !self::$hasRestbase ) {
parent::tearDown(); $this->markTestSkipped( 'Can not connect to Restbase Math interface.' );
}
} }
/** /**
@ -43,16 +32,16 @@ class MathFormatterTest extends MediaWikiTestCase {
* @covers MathFormatter::__construct() * @covers MathFormatter::__construct()
*/ */
public function testBasics() { public function testBasics() {
$formatter = new MathFormatter( self::FORMAT_PLAIN ); $formatter = new MathFormatter( 'text/plain' );
// check if the format input was corretly passed to the class // check if the format input was corretly passed to the class
$this->assertEquals( self::FORMAT_PLAIN, $formatter->getFormat(), 'test getFormat' ); $this->assertSame( 'text/plain', $formatter->getFormat(), 'test getFormat' );
} }
/** /**
* @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException * @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException
*/ */
public function testNotStringValue() { public function testNotStringValue() {
$formatter = new MathFormatter( self::FORMAT_PLAIN ); $formatter = new MathFormatter( 'text/plain' );
$formatter->format( new NumberValue( 0 ) ); $formatter->format( new NumberValue( 0 ) );
} }
@ -60,7 +49,7 @@ class MathFormatterTest extends MediaWikiTestCase {
* @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException * @expectedException ValueFormatters\Exceptions\MismatchingDataValueTypeException
*/ */
public function testNullValue() { public function testNullValue() {
$formatter = new MathFormatter( self::FORMAT_PLAIN ); $formatter = new MathFormatter( 'text/plain' );
$formatter->format( null ); $formatter->format( null );
} }
@ -68,33 +57,29 @@ class MathFormatterTest extends MediaWikiTestCase {
* @expectedException InvalidArgumentException * @expectedException InvalidArgumentException
*/ */
public function testUnknownFormat() { public function testUnknownFormat() {
$formatter = new MathFormatter( self::FORMAT_UNKNOWN ); new MathFormatter( 'unknown/unknown' );
} }
public function testFormatPlain() { public function testFormatPlain() {
$formatter = new MathFormatter( self::FORMAT_PLAIN ); $formatter = new MathFormatter( 'text/plain' );
$value = new StringValue( self::SOME_TEX ); $value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value ); $resultFormat = $formatter->format( $value );
$this->assertEquals( self::SOME_TEX, $resultFormat, $this->assertSame( self::SOME_TEX, $resultFormat );
'Results should be equal' );
} }
public function testFormatHtml() { public function testFormatHtml() {
$formatter = new MathFormatter( self::FORMAT_HTML ); $formatter = new MathFormatter( 'text/html' );
$value = new StringValue( self::SOME_TEX ); $value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value ); $resultFormat = $formatter->format( $value );
$this->assertContains( '</math>', $resultFormat, $this->assertContains( '</math>', $resultFormat, 'Result must contain math-tag' );
'Result must contain math-tag' );
} }
public function testFormatXWiki() { public function testFormatXWiki() {
$tex = self::SOME_TEX; $tex = self::SOME_TEX;
$formatter = new MathFormatter( self::FORMAT_XWIKI ); $formatter = new MathFormatter( 'text/x-wiki' );
$value = new StringValue( self::SOME_TEX ); $value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value ); $resultFormat = $formatter->format( $value );
$this->assertEquals( "<math>$tex</math>", $resultFormat, $this->assertSame( "<math>$tex</math>", $resultFormat, 'Tex wasn\'t properly wrapped' );
'Tex wasn\'t properly wrapped' );
} }
} }