Debian Codegraph @

extensions/Math/tests/phpunit/MathFormatterTest.php

raw symbols SCIP
1 <?php
2
3 use DataValues\NumberValue;
4 use DataValues\StringValue;
5 use MediaWiki\Extension\Math\MathFormatter;
6 use MediaWiki\Extension\Math\Tests\MathMockHttpTrait;
7 use Wikibase\Lib\Formatters\SnakFormatter;
8 use Wikimedia\Rdbms\IDatabase;
9 use Wikimedia\Rdbms\LBFactory;
10
11 /**
12 * Test the results of MathFormatter
13 *
14 * @covers \MediaWiki\Extension\Math\MathFormatter
15 *
16 * @group Math
17 *
18 * @license GPL-2.0-or-later
19 */
20 class MathFormatterTest extends MediaWikiIntegrationTestCase {
21 use MathMockHttpTrait;
22
23 private const SOME_TEX = '\sin x^2';
24
25 protected function setUp(): void {
26 $this->markTestSkippedIfExtensionNotLoaded( 'WikibaseClient' );
27 $db = $this->createMock( IDatabase::class );
28 $db->method( 'selectRow' )->willReturn( false );
29 $lbFactory = $this->createMock( LBFactory::class );
30 $lbFactory->method( 'getReplicaDatabase' )->willReturn( $db );
31 $this->setService( 'DBLoadBalancerFactory', $lbFactory );
32 parent::setUp();
33 }
34
35 /**
36 * Checks the
37 * @covers \MediaWiki\Extension\Math\MathFormatter::__construct
38 */
39 public function testBasics() {
40 $formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
41 // check if the format input was corretly passed to the class
42 $this->assertSame( SnakFormatter::FORMAT_PLAIN, $formatter->getFormat(), 'test getFormat' );
43 }
44
45 public function testNotStringValue() {
46 $formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
47 $this->expectException( InvalidArgumentException::class );
48 $formatter->format( new NumberValue( 0 ) );
49 }
50
51 public function testNullValue() {
52 $formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
53 $this->expectException( InvalidArgumentException::class );
54 $formatter->format( null );
55 }
56
57 public function testUnknownFormatFallsBackToMathMl() {
58 $this->setupGoodMathRestBaseMockHttp( true );
59
60 $formatter = new MathFormatter( 'unknown/unknown' );
61 $value = new StringValue( self::SOME_TEX );
62 $resultFormat = $formatter->format( $value );
63 $this->assertStringContainsString( '</math>', $resultFormat );
64 }
65
66 /**
67 * @covers \MediaWiki\Extension\Math\MathFormatter::format
68 */
69 public function testUnknownFormatFailure() {
70 $this->setupBadMathRestBaseMockHttp();
71
72 $formatter = new MathFormatter( 'unknown/unknown' );
73 $value = new StringValue( '\newcommand' );
74 $resultFormat = $formatter->format( $value );
75 $this->assertStringContainsString( 'unknown function', $resultFormat );
76 }
77
78 public function testFormatPlain() {
79 $formatter = new MathFormatter( SnakFormatter::FORMAT_PLAIN );
80 $value = new StringValue( self::SOME_TEX );
81 $resultFormat = $formatter->format( $value );
82 $this->assertSame( self::SOME_TEX, $resultFormat );
83 }
84
85 public function testFormatHtml() {
86 $this->setupGoodMathRestBaseMockHttp( true );
87
88 $formatter = new MathFormatter( SnakFormatter::FORMAT_HTML );
89 $value = new StringValue( self::SOME_TEX );
90 $resultFormat = $formatter->format( $value );
91 $this->assertStringContainsString( '</math>', $resultFormat, 'Result must contain math-tag' );
92 }
93
94 public function testFormatDiffHtml() {
95 $this->setupGoodMathRestBaseMockHttp( true );
96
97 $formatter = new MathFormatter( SnakFormatter::FORMAT_HTML_DIFF );
98 $value = new StringValue( self::SOME_TEX );
99 $resultFormat = $formatter->format( $value );
100 $this->assertStringContainsString( '</math>', $resultFormat, 'Result must contain math-tag' );
101 $this->assertStringContainsString( '</h4>', $resultFormat, 'Result must contain a <h4> tag' );
102 $this->assertStringContainsString( '</code>', $resultFormat, 'Result must contain a <code> tag' );
103 $this->assertStringContainsString(
104 'wb-details',
105 $resultFormat,
106 'Result must contain wb-details class'
107 );
108 $this->assertStringContainsString(
109 htmlspecialchars( self::SOME_TEX ),
110 $resultFormat,
111 'Result must contain the TeX source'
112 );
113 }
114
115 public function testFormatXWiki() {
116 $tex = self::SOME_TEX;
117 $formatter = new MathFormatter( SnakFormatter::FORMAT_WIKI );
118 $value = new StringValue( self::SOME_TEX );
119 $resultFormat = $formatter->format( $value );
120 $this->assertSame( "<math>$tex</math>", $resultFormat, 'Tex wasn\'t properly wrapped' );
121 }
122
123 }