| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\Math\Tests; |
| 4 | |
| 5 | use DataValues\StringValue; |
| 6 | use MediaWiki\Config\ServiceOptions; |
| 7 | use MediaWiki\Extension\Math\MathFormatter; |
| 8 | use MediaWiki\Extension\Math\MathWikibaseConnector; |
| 9 | use MediaWiki\Language\Language; |
| 10 | use MediaWiki\Languages\LanguageFactory; |
| 11 | use MediaWiki\Languages\LanguageNameUtils; |
| 12 | use MediaWiki\Registration\ExtensionRegistry; |
| 13 | use MediaWiki\Site\Site; |
| 14 | use MediaWikiUnitTestCase; |
| 15 | use Psr\Log\LoggerInterface; |
| 16 | use TestLogger; |
| 17 | use Wikibase\Client\RepoLinker; |
| 18 | use Wikibase\DataModel\Entity\BasicEntityIdParser; |
| 19 | use Wikibase\DataModel\Entity\EntityId; |
| 20 | use Wikibase\DataModel\Entity\EntityIdParser; |
| 21 | use Wikibase\DataModel\Entity\EntityIdValue; |
| 22 | use Wikibase\DataModel\Entity\Item; |
| 23 | use Wikibase\DataModel\Entity\ItemId; |
| 24 | use Wikibase\DataModel\Entity\NumericPropertyId; |
| 25 | use Wikibase\DataModel\SiteLink; |
| 26 | use Wikibase\DataModel\Snak\PropertyValueSnak; |
| 27 | use Wikibase\DataModel\Snak\SnakList; |
| 28 | use Wikibase\DataModel\Statement\Statement; |
| 29 | use Wikibase\DataModel\Statement\StatementList; |
| 30 | use Wikibase\DataModel\Term\Term; |
| 31 | use Wikibase\Lib\Store\EntityRevision; |
| 32 | use Wikibase\Lib\Store\EntityRevisionLookup; |
| 33 | use Wikibase\Lib\Store\FallbackLabelDescriptionLookup; |
| 34 | use Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory; |
| 35 | use Wikibase\Lib\Store\StorageException; |
| 36 | |
| 37 | class MathWikibaseConnectorTestFactory extends MediaWikiUnitTestCase { |
| 38 | public const EXAMPLE_URL = 'https://example.com/'; |
| 39 | |
| 40 | public const TEST_ITEMS = [ |
| 41 | 'Q1' => [ 'mass–energy equivalence', 'physical law relating mass to energy', 'E = mc^2' ], |
| 42 | 'Q2' => [ 'energy', 'measure for the ability of a system to do work', 'E' ], |
| 43 | 'Q3' => [ |
| 44 | 'speed of light', |
| 45 | 'speed at which all massless particles and associated fields travel in vacuum', |
| 46 | 'c' |
| 47 | ], |
| 48 | 'Q4' => [ |
| 49 | 'mass', |
| 50 | 'property of matter to resist changes of the state of motion and to attract other bodies', |
| 51 | 'm' |
| 52 | ] |
| 53 | ]; |
| 54 | |
| 55 | public static function setUpBeforeClass(): void { |
| 56 | ExtensionRegistry::enableForTest(); |
| 57 | if ( !ExtensionRegistry::getInstance()->isLoaded( 'WikibaseClient' ) ) { |
| 58 | self::markTestSkipped( 'WikibaseClient is not installed. Skipping tests.' ); |
| 59 | } |
| 60 | ExtensionRegistry::disableForTest(); |
| 61 | parent::setUpBeforeClass(); |
| 62 | } |
| 63 | |
| 64 | public function getWikibaseConnectorWithExistingItems( |
| 65 | EntityRevision $entityRevision, |
| 66 | bool $storageExceptionOnQ3 = false, |
| 67 | ?LoggerInterface $logger = null, |
| 68 | ?EntityIdParser $parser = null |
| 69 | ): MathWikibaseConnector { |
| 70 | $revisionLookupMock = $this->createMock( EntityRevisionLookup::class ); |
| 71 | $revisionLookupMock->method( 'getEntityRevision' )->willReturnCallback( |
| 72 | static function ( EntityId $entityId ) use ( $entityRevision, $storageExceptionOnQ3 ) { |
| 73 | if ( $storageExceptionOnQ3 && $entityId->getSerialization() === 'Q3' ) { |
| 74 | throw new StorageException( 'Test Exception' ); |
| 75 | } else { |
| 76 | return $entityRevision; |
| 77 | } |
| 78 | } |
| 79 | ); |
| 80 | $revisionLookupMock->expects( self::atLeastOnce() ) |
| 81 | ->method( 'getEntityRevision' ); |
| 82 | |
| 83 | $fallbackLabelDescriptionLookupFactoryMock = $this->createMock( FallbackLabelDescriptionLookupFactory::class ); |
| 84 | $languageMock = $this->createMock( Language::class ); |
| 85 | $languageFactoryMock = $this->createMock( LanguageFactory::class ); |
| 86 | $languageFactoryMock->method( 'getLanguage' ) |
| 87 | ->with( 'en' ) |
| 88 | ->willReturn( $languageMock ); |
| 89 | $languageNameUtilsMock = $this->createMock( LanguageNameUtils::class ); |
| 90 | $languageNameUtilsMock->method( 'isValidCode' ) |
| 91 | ->with( 'en' ) |
| 92 | ->willReturn( true ); |
| 93 | $fallbackLabelDescriptionLookupFactoryMock->method( 'newLabelDescriptionLookup' ) |
| 94 | ->with( $languageMock ) |
| 95 | ->willReturnCallback( [ $this, 'newLabelDescriptionLookup' ] ); |
| 96 | |
| 97 | return self::getWikibaseConnector( |
| 98 | $languageFactoryMock, |
| 99 | $languageNameUtilsMock, |
| 100 | $fallbackLabelDescriptionLookupFactoryMock, |
| 101 | $revisionLookupMock, |
| 102 | $logger, |
| 103 | $parser |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | public function getWikibaseConnector( |
| 108 | ?LanguageFactory $languageFactory = null, |
| 109 | ?LanguageNameUtils $languageNameUtils = null, |
| 110 | ?FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory = null, |
| 111 | ?EntityRevisionLookup $entityRevisionLookupMock = null, |
| 112 | ?LoggerInterface $logger = null, |
| 113 | ?EntityIdParser $parser = null |
| 114 | ): MathWikibaseConnector { |
| 115 | $labelDescriptionLookupFactory = $labelDescriptionLookupFactory ?: |
| 116 | $this->createMock( FallbackLabelDescriptionLookupFactory::class ); |
| 117 | |
| 118 | $entityRevisionLookup = $entityRevisionLookupMock ?: |
| 119 | $this->createMock( EntityRevisionLookup::class ); |
| 120 | |
| 121 | $languageFactory = $languageFactory ?: $this->createMock( LanguageFactory::class ); |
| 122 | if ( !$languageNameUtils ) { |
| 123 | $languageNameUtils = $this->createMock( LanguageNameUtils::class ); |
| 124 | $languageNameUtils->method( 'isValidCode' )->willReturn( true ); |
| 125 | } |
| 126 | |
| 127 | $site = $this->createMock( Site::class ); |
| 128 | $site->method( 'getGlobalId' )->willReturn( '' ); |
| 129 | $site->method( 'getPageUrl' )->willReturn( self::EXAMPLE_URL ); |
| 130 | |
| 131 | $repoConnector = $this->createMock( RepoLinker::class ); |
| 132 | $repoConnector->method( 'getEntityUrl' ) |
| 133 | ->willReturnCallback( static function ( ItemId $itemId ) { |
| 134 | return self::EXAMPLE_URL . 'wiki/Special:EntityPage/' . $itemId->getSerialization(); |
| 135 | } ); |
| 136 | |
| 137 | $mathFormatter = $this->createMock( MathFormatter::class ); |
| 138 | $mathFormatter->method( 'format' ) |
| 139 | ->willReturnCallback( static function ( StringValue $value ) { |
| 140 | return self::getExpectedMathML( $value->getValue() ); |
| 141 | } ); |
| 142 | |
| 143 | return new MathWikibaseConnector( |
| 144 | new ServiceOptions( MathWikibaseConnector::CONSTRUCTOR_OPTIONS, [ |
| 145 | 'MathWikibasePropertyIdHasPart' => 'P1', |
| 146 | 'MathWikibasePropertyIdDefiningFormula' => 'P2', |
| 147 | 'MathWikibasePropertyIdQuantitySymbol' => 'P3', |
| 148 | 'MathWikibasePropertyIdInDefiningFormula' => 'P4', |
| 149 | 'MathWikibasePropertyIdSymbolRepresents' => 'P5' |
| 150 | ] ), |
| 151 | $repoConnector, |
| 152 | $languageFactory, |
| 153 | $languageNameUtils, |
| 154 | $entityRevisionLookup, |
| 155 | $labelDescriptionLookupFactory, |
| 156 | $site, |
| 157 | $parser ?: new BasicEntityIdParser(), |
| 158 | $mathFormatter, |
| 159 | $logger ?: new TestLogger() |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | public function setupMassEnergyEquivalenceItem( |
| 164 | bool $hasPartMode |
| 165 | ) { |
| 166 | $partPropertyId = new NumericPropertyId( $hasPartMode ? 'P1' : 'P4' ); |
| 167 | $symbolPropertyId = new NumericPropertyId( $hasPartMode ? 'P3' : 'P5' ); |
| 168 | $items = []; |
| 169 | $statements = []; |
| 170 | foreach ( self::TEST_ITEMS as $key => $itemInfo ) { |
| 171 | $itemId = new ItemId( $key ); |
| 172 | $items[ $key ] = new Item( $itemId ); |
| 173 | |
| 174 | $siteLinkMock = $this->createMock( SiteLink::class ); |
| 175 | $siteLinkMock->method( 'getSiteId' )->willReturn( '' ); |
| 176 | $siteLinkMock->method( 'getPageName' )->willReturn( '' ); |
| 177 | $items[ $key ]->addSiteLink( $siteLinkMock ); |
| 178 | |
| 179 | if ( $key === 'Q1' ) { |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | $partSnak = new PropertyValueSnak( |
| 184 | $partPropertyId, |
| 185 | $hasPartMode ? new EntityIdValue( $items[ $key ]->getId() ) : new StringValue( $itemInfo[2] ) |
| 186 | ); |
| 187 | $partQualifier = new PropertyValueSnak( |
| 188 | $symbolPropertyId, |
| 189 | $hasPartMode ? new StringValue( $itemInfo[2] ) : new EntityIdValue( $items[ $key ]->getId() ) |
| 190 | ); |
| 191 | |
| 192 | $statement = new Statement( $partSnak ); |
| 193 | $statement->setQualifiers( new SnakList( [ $partQualifier ] ) ); |
| 194 | $statements[] = $statement; |
| 195 | } |
| 196 | |
| 197 | $mainFormulaValue = new StringValue( self::TEST_ITEMS[ 'Q1' ][2] ); |
| 198 | $definingFormulaStatement = new Statement( new PropertyValueSnak( |
| 199 | new NumericPropertyId( 'P2' ), |
| 200 | $mainFormulaValue |
| 201 | ) ); |
| 202 | |
| 203 | $statementList = new StatementList( ...$statements ); |
| 204 | $statementList->addStatement( $definingFormulaStatement ); |
| 205 | $items[ 'Q1' ]->setStatements( $statementList ); |
| 206 | return $items[ 'Q1' ]; |
| 207 | } |
| 208 | |
| 209 | public function newLabelDescriptionLookup(): FallbackLabelDescriptionLookup { |
| 210 | $lookup = $this->createMock( FallbackLabelDescriptionLookup::class ); |
| 211 | |
| 212 | $lookup->method( 'getLabel' ) |
| 213 | ->willReturnCallback( static function ( EntityId $entityId ) { |
| 214 | if ( self::TEST_ITEMS[ $entityId->getSerialization() ] !== null ) { |
| 215 | return new Term( 'en', self::TEST_ITEMS[ $entityId->getSerialization() ][0] ); |
| 216 | } else { |
| 217 | return null; |
| 218 | } |
| 219 | } ); |
| 220 | |
| 221 | $lookup->method( 'getDescription' ) |
| 222 | ->willReturnCallback( static function ( EntityId $entityId ) { |
| 223 | if ( self::TEST_ITEMS[ $entityId->getSerialization() ] !== null ) { |
| 224 | return new Term( 'en', self::TEST_ITEMS[ $entityId->getSerialization() ][1] ); |
| 225 | } else { |
| 226 | return null; |
| 227 | } |
| 228 | } ); |
| 229 | |
| 230 | return $lookup; |
| 231 | } |
| 232 | |
| 233 | public static function getExpectedMathML( $str ) { |
| 234 | return '<math>' . $str . '</math>'; |
| 235 | } |
| 236 | } |