Debian Codegraph @

extensions/Math/tests/phpunit/unit/WikiTexVC/MMLFullCoverageTest.php

raw symbols SCIP
1 <?php
2 namespace MediaWiki\Extension\Math\WikiTexVC;
3
4 use InvalidArgumentException;
5 use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLComparator;
6 use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLTestUtil;
7 use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLTestUtilHTML;
8 use MediaWikiUnitTestCase;
9
10 /**
11 * This is a test which checks the WikiTexVC (LaTeX to MathML) converter capabilities
12 * It uses the Full-Coverage definition of tests from:
13 * https://www.mediawiki.org/wiki/Extension:Math/CoverageTest
14 *
15 * The json test-files for this can be updated with:
16 * 'MathSearch-Extension/maintenance/UpdateMath.php --mode mathml --exportmml /var/www/html/extensions/MathSearch'
17 *
18 * WIP:
19 * Currently this is just checking that texVC can generate MathML
20 * for the specified tests, not how the MathML looks like.
21 *
22 * @covers \MediaWiki\Extension\Math\WikiTexVC\TexVC
23 */
24 final class MMLFullCoverageTest extends MediaWikiUnitTestCase {
25 /** @var float */
26 private static $SIMILARITYTRESH = 0.7;
27 /** @var bool */
28 private static $SKIPXMLVALIDATION = true;
29 /** @var string */
30 private static $FILENAMELATEXML = __DIR__ . "/mmlRes-latexml-FullCoverage.json";
31 /** @var string */
32 private static $FILENAMEMATHOID = __DIR__ . "/mmlRes-mathml-FullCoverage.json";
33 /** @var bool */
34 private static $APPLYFILTER = false;
35 /** @var int */
36 private static $FILTERSTART = 0;
37 /** @var int */
38 private static $FILTERLENGTH = 60;
39 /** @var bool */
40 private static $GENERATEHTML = false;
41 /** @var string */
42 private static $GENERATEDHTMLFILE = __DIR__ . "/MMLFullCoverageTest-Output.html";
43 /** @var bool */
44 private static $GENERATEEVAL = false;
45 /** @var string */
46 private static $GENERATEDEVALFILE = __DIR__ . "/MMLFullCoverageEval.json";
47 /** @var int[] */
48 private static $SKIPPEDINDICES = [];
49
50 /** @var bool */
51 private static $FILTERMML = true;
52
53 public static function setUpBeforeClass(): void {
54 MMLTestUtilHTML::generateHTMLstart( self::$GENERATEDHTMLFILE, [ "name", "TeX-Input", "MathML(LaTeXML)",
55 "MathML(Mathoid)", "MathML(WikiTexVC)", "F-Similarity" ], self::$GENERATEHTML );
56 if ( self::$GENERATEEVAL ) {
57 MMLTestUtil::deleteFile( self::$GENERATEDEVALFILE );
58 MMLTestUtil::createJSONstartEnd( true, self::$GENERATEDEVALFILE );
59 }
60 }
61
62 public static function tearDownAfterClass(): void {
63 MMLTestUtilHTML::generateHTMLEnd( self::$GENERATEDHTMLFILE, self::$GENERATEHTML );
64 if ( self::$GENERATEEVAL ) {
65 MMLTestUtil::createJSONstartEnd( false, self::$GENERATEDEVALFILE );
66 }
67 }
68
69 /**
70 * @dataProvider provideTestCases
71 */
72 public function testTexVC( $title, $tc ) {
73 $texVC = new TexVC();
74
75 if ( in_array( $tc->ctr, self::$SKIPPEDINDICES, true ) ) {
76 MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $tc->ctr, $tc->tex,
77 "skipped", "skipped", "skipped" ], false, self::$GENERATEHTML );
78 $this->addToAssertionCount( 1 );
79 return;
80 }
81 # Fetch result from WikiTexVC(PHP)
82 $resultT = $texVC->check( $tc->tex, [
83 'debug' => false,
84 'usemathrm' => $tc->usemathrm ?? false,
85 'oldtexvc' => $tc->oldtexvc ?? false
86 ] );
87
88 $mml_latexml = self::$FILTERMML ? self::loadXMLandDeleteAttrs( $tc->mml_latexml ) : $tc->mml_latexml;
89 $mathMLtexVC = MMLTestUtil::getMMLwrapped( $resultT["input"] );
90 $this->assertStringNotContainsString( 'merror', $mathMLtexVC,
91 "tc $$tc->tex$: MathML $mathMLtexVC contains error" );
92 $mmlComparator = new MMLComparator();
93 $compRes = $mmlComparator->compareMathML( $tc->mml_mathoid, $mathMLtexVC );
94
95 if ( self::$GENERATEEVAL ) {
96 $entry = [
97 "testname" => "FullCoverageTest",
98 "ctr" => $tc->ctr,
99 "tex" => $tc->tex,
100 "mml_latcompareMathMLexml" => $mml_latexml,
101 "mml_mathoid" => $tc->mml_mathoid,
102 "mml_wikitexvc" => $mathMLtexVC,
103 "tree_bracket_wikitexvc" => MMLComparator::functionObtainTreeInBrackets( $mathMLtexVC ),
104 "tree_bracket_latexml" => MMLComparator::functionObtainTreeInBrackets( $mml_latexml ),
105 "tree_bracket_mathoid" => MMLComparator::functionObtainTreeInBrackets( $tc->mml_mathoid )
106 ];
107 MMLTestUtil::appendToJSONFile( $entry, self::$GENERATEDEVALFILE );
108 }
109
110 MMLTestUtilHTML::generateHTMLtableRow( self::$GENERATEDHTMLFILE, [ $tc->ctr, $tc->tex, $mml_latexml,
111 $tc->mml_mathoid, $mathMLtexVC, $compRes['similarityF'] ], false, self::$GENERATEHTML );
112
113 if ( !self::$SKIPXMLVALIDATION ) {
114 if ( !$tc->mml_mathoid ) {
115 $this->fail( "No Mathoid reference found for: " . $tc->tex );
116 }
117 if ( $compRes['similarityF'] >= self::$SIMILARITYTRESH ) {
118 $this->addToAssertionCount( 1 );
119 } else {
120 $this->assertXmlStringEqualsXmlString( $tc->mml_mathoid, $mathMLtexVC );
121 }
122 } else {
123 $this->addToAssertionCount( 1 );
124 }
125 }
126
127 /**
128 * Deletes some attributes from the mathml which are not necessary for comparisons.
129 * @param string $mml mathml as string
130 * @return bool|string false if problem, mathml as xml string without the specified attributes if ok
131 */
132 public static function loadXMLandDeleteAttrs( $mml ) {
133 $xml = simplexml_load_string( $mml );
134 self::unsetAttrs( $xml );
135 // Recursive call deleting attributes
136 self::deleteAttributes( $xml );
137 return $xml->asXML();
138 }
139
140 public static function deleteAttributes( &$xml ) {
141 foreach ( $xml as $node ) {
142 self::unsetAttrs( $node );
143 self::deleteAttributes( $node );
144 }
145 }
146
147 public static function unsetAttrs( $node ): void {
148 $attrs = $node->attributes();
149 unset( $attrs['id'] );
150 unset( $attrs['xref'] );
151 }
152
153 public static function provideTestCases() {
154 $resMathoid = MMLTestUtil::getJSON( self::$FILENAMEMATHOID );
155 $resLaTeXML = MMLTestUtil::getJSON( self::$FILENAMELATEXML );
156 if ( count( $resMathoid ) != count( $resLaTeXML ) ) {
157 throw new InvalidArgumentException( "Test files dont have the same number of entries." );
158 }
159 $f = [];
160 // Adding running indices for location of tests.
161 foreach ( $resMathoid as $index => $tcMathoid ) {
162 $tcLaTeXML = $resLaTeXML[$index];
163 $tc = [
164 "ctr" => $index,
165 "tex" => $tcMathoid->tex,
166 "type" => $tcMathoid->type,
167 "mml_mathoid" => $tcMathoid->mml,
168 "mml_latexml" => $tcLaTeXML->mml,
169 ];
170 array_push( $f, [ "title N/A", (object)$tc ] );
171 }
172 // Filtering results by index if necessary
173 if ( self::$APPLYFILTER ) {
174 $f = array_slice( $f, self::$FILTERSTART, self::$FILTERLENGTH );
175 }
176 return $f;
177 }
178 }