Debian Codegraph @

extensions/Math/tests/phpunit/unit/MathWikibaseConnectorTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Math\Tests;
4
5 use DataValues\StringValue;
6 use MediaWiki\Config\ConfigException;
7 use MediaWiki\Languages\LanguageNameUtils;
8 use MediaWiki\Logger\LoggerFactory;
9 use Psr\Log\LoggerInterface;
10 use Wikibase\DataModel\Entity\BasicEntityIdParser;
11 use Wikibase\DataModel\Entity\EntityIdParsingException;
12 use Wikibase\DataModel\Entity\Item;
13 use Wikibase\DataModel\Entity\ItemId;
14 use Wikibase\DataModel\Entity\NumericPropertyId;
15 use Wikibase\DataModel\Snak\PropertyValueSnak;
16 use Wikibase\DataModel\Statement\Statement;
17 use Wikibase\DataModel\Statement\StatementList;
18 use Wikibase\Lib\Store\EntityRevision;
19 use Wikibase\Lib\Store\EntityRevisionLookup;
20 use Wikibase\Lib\Store\StorageException;
21
22 /**
23 * @covers \MediaWiki\Extension\Math\MathWikibaseConnector
24 */
25 class MathWikibaseConnectorTest extends MathWikibaseConnectorTestFactory {
26
27 public function testGetUrl() {
28 $mathWikibase = $this->getWikibaseConnector();
29 $this->assertEquals( self::EXAMPLE_URL . 'wiki/Special:EntityPage/Q42',
30 $mathWikibase->buildURL( 'Q42' ) );
31 }
32
33 public function testFetchInvalidLanguage() {
34 $languageNameUtils = $this->createMock( LanguageNameUtils::class );
35 $languageNameUtils->method( 'isValidCode' )
36 ->willReturn( false );
37 $mathWikibase = $this->getWikibaseConnector( null, $languageNameUtils );
38
39 $this->expectException( 'InvalidArgumentException' );
40 $this->expectExceptionMessage( 'Invalid language code specified.' );
41 $mathWikibase->fetchWikibaseFromId( 'Q1', '&' );
42 }
43
44 public function testFetchWithStorageIssue() {
45 $entityRevisionLookup = $this->createMock( EntityRevisionLookup::class );
46 $entityRevisionLookup->method( 'getEntityRevision' )
47 ->willThrowException( new StorageException( 'Invalid code' ) );
48 $mathWikibase = $this->getWikibaseConnector( null, null, null, $entityRevisionLookup );
49
50 $this->expectException( 'InvalidArgumentException' );
51 $this->expectExceptionMessage( 'Non-existing Wikibase ID.' );
52 $mathWikibase->fetchWikibaseFromId( 'Q1', '&' );
53 }
54
55 public function testFetchNonExistingId() {
56 $mathWikibase = $this->getWikibaseConnector();
57 $this->expectException( 'InvalidArgumentException' );
58 $this->expectExceptionMessage( 'Non-existing Wikibase ID.' );
59 $mathWikibase->fetchWikibaseFromId( 'Q1', 'en' );
60 }
61
62 public function testFailSafeFaultyPropertySetup() {
63 $dummyItemId = new ItemId( 'Q1' );
64 $parserMock = $this->createMock( BasicEntityIdParser::class );
65 $parserMock->method( 'parse' )
66 ->willReturnCallback(
67 static function ( string $id ) {
68 if ( $id === 'Q1' ) {
69 return new ItemId( 'Q1' );
70 } else {
71 throw new ConfigException();
72 }
73 } );
74
75 $revisionLookupMock = $this->createMock( EntityRevisionLookup::class );
76 $revisionLookupMock->expects( $this->once() )
77 ->method( 'getEntityRevision' )
78 ->with( $dummyItemId )
79 ->willReturn( null );
80
81 // non-existing properties should not result in errors on initialization
82 $mathWikibase = $this->getWikibaseConnector(
83 null,
84 null,
85 null,
86 $revisionLookupMock,
87 null,
88 $parserMock
89 );
90
91 // but obviously on non-existing errors when trying to fetch information
92 $this->expectException( 'InvalidArgumentException' );
93 $this->expectExceptionMessage( 'Non-existing Wikibase ID.' );
94 $mathWikibase->fetchWikibaseFromId( 'Q1', 'en' );
95 }
96
97 public function testFetchMalformedId() {
98 $parserMock = $this->createMock( BasicEntityIdParser::class );
99 $parserMock->method( 'parse' )
100 ->willReturnCallback(
101 static function ( string $id ) {
102 if ( $id === '1' ) {
103 throw new EntityIdParsingException();
104 } else {
105 return null;
106 }
107 } );
108
109 $mathWikibase = $this->getWikibaseConnector( null, null, null, null, null, $parserMock );
110 $this->expectException( 'InvalidArgumentException' );
111 $this->expectExceptionMessage( 'Invalid Wikibase ID.' );
112 $mathWikibase->fetchWikibaseFromId( '1', 'en' );
113 }
114
115 public function testFetchNonItem() {
116 // a mocked Item does not pass instanceof, hence the InvalidArgumentException
117 $entityRevisionMock = $this->createMock( EntityRevision::class );
118 $wikibaseConnector = $this->getWikibaseConnectorWithExistingItems( $entityRevisionMock );
119 $this->expectException( 'InvalidArgumentException' );
120 $this->expectExceptionMessage( 'The specified Wikibase ID does not represented an item.' );
121 $wikibaseConnector->fetchWikibaseFromId( 'Q1', 'en' );
122 }
123
124 public function testFetchEmptyItem() {
125 $itemId = new ItemId( 'Q1' );
126 $item = new Item( $itemId );
127 $revision = new EntityRevision( $item );
128
129 $parserMock = $this->createMock( BasicEntityIdParser::class );
130 $parserMock->method( 'parse' )
131 ->willReturnCallback(
132 static function ( string $id ) {
133 if ( str_starts_with( $id, 'Q' ) ) {
134 return new ItemId( $id );
135 } else {
136 throw new ConfigException();
137 }
138 } );
139
140 $wikibaseConnector = $this->getWikibaseConnectorWithExistingItems(
141 $revision,
142 false,
143 null,
144 $parserMock
145 );
146 $wikibaseInfo = $wikibaseConnector->fetchWikibaseFromId( 'Q1', 'en' );
147 $this->assertEquals( $itemId, $wikibaseInfo->getId() );
148 $this->assertEquals( self::TEST_ITEMS[ 'Q1' ][0], $wikibaseInfo->getLabel() );
149 $this->assertEquals( self::TEST_ITEMS[ 'Q1' ][1], $wikibaseInfo->getDescription() );
150 $this->assertCount( 0, $wikibaseInfo->getParts() );
151 $this->assertFalse( $wikibaseInfo->hasParts() );
152 $this->assertNull( $wikibaseInfo->getSymbol() );
153 $this->assertNull( $wikibaseInfo->getFormattedSymbol() );
154 }
155
156 public function testFetchItemWithFormula() {
157 $itemId = new ItemId( 'Q1' );
158 $item = new Item( $itemId );
159 $revision = new EntityRevision( $item );
160
161 $formulaValue = new StringValue( self::TEST_ITEMS[ 'Q1' ][2] );
162 $definingFormulaStatement = new Statement( new PropertyValueSnak(
163 new NumericPropertyId( 'P2' ),
164 $formulaValue
165 ) );
166
167 $item->setStatements( new StatementList( $definingFormulaStatement ) );
168
169 $wikibaseConnector = $this->getWikibaseConnectorWithExistingItems( $revision );
170 $wikibaseInfo = $wikibaseConnector->fetchWikibaseFromId( 'Q1', 'en' );
171 $this->assertFalse( $wikibaseInfo->hasParts() );
172 $this->assertEquals( $formulaValue, $wikibaseInfo->getSymbol() );
173 $this->assertEquals(
174 $this->getExpectedMathML( $formulaValue->getValue() ),
175 $wikibaseInfo->getFormattedSymbol()
176 );
177 }
178
179 /**
180 * @dataProvider provideItemSetups
181 */
182 public function testFetchMassEnergyEquivalenceHasPartsItem( bool $hasPart ) {
183 $item = $this->setupMassEnergyEquivalenceItem( $hasPart );
184 $wikibaseConnector = $this->getWikibaseConnectorWithExistingItems( new EntityRevision( $item ) );
185 $wikibaseInfo = $wikibaseConnector->fetchWikibaseFromId( 'Q1', 'en' );
186
187 $this->assertEquals( $item->getId(), $wikibaseInfo->getId() );
188 $this->assertEquals( self::TEST_ITEMS[ 'Q1' ][0], $wikibaseInfo->getLabel() );
189 $this->assertEquals( self::TEST_ITEMS[ 'Q1' ][1], $wikibaseInfo->getDescription() );
190 $mathFormula = self::TEST_ITEMS[ 'Q1' ][2];
191 $this->assertEquals( $mathFormula, $wikibaseInfo->getSymbol()->getValue() );
192 $this->assertEquals( $this->getExpectedMathML( $mathFormula ), $wikibaseInfo->getFormattedSymbol() );
193
194 $this->assertTrue( $wikibaseInfo->hasParts() );
195 $parts = $wikibaseInfo->getParts();
196 $this->assertCount( 3, $parts );
197 foreach ( $parts as $part ) {
198 $key = $part->getId()->getSerialization();
199 $this->assertEquals( self::TEST_ITEMS[ $key ][0], $part->getLabel() );
200 $this->assertEquals( self::TEST_ITEMS[ $key ][1], $part->getDescription() );
201 $mathFormula = self::TEST_ITEMS[ $key ][2];
202 $this->assertEquals( $mathFormula, $part->getSymbol()->getValue() );
203 $this->assertEquals( $this->getExpectedMathML( $mathFormula ), $part->getFormattedSymbol() );
204 $this->assertEquals( self::EXAMPLE_URL, $part->getUrl() );
205 }
206 }
207
208 /**
209 * @dataProvider provideItemSetups
210 */
211 public function testFetchMassEnergyWithStorageExceptionLogging( bool $hasPart ) {
212 $logger = $this->createMock( LoggerInterface::class );
213 $logger->expects( $this->once() )
214 ->method( 'warning' )
215 ->with( 'Cannot fetch URL for EntityId Q3. Reason: Test Exception' );
216
217 $item = $this->setupMassEnergyEquivalenceItem( $hasPart );
218 $wikibaseConnector = $this->getWikibaseConnectorWithExistingItems(
219 new EntityRevision( $item ),
220 true,
221 $logger
222 );
223
224 $wikibaseConnector->fetchWikibaseFromId( 'Q1', 'en' );
225 }
226
227 /**
228 * @dataProvider provideItemSetups
229 */
230 public function testFetchMassEnergyWithStorageException( bool $hasPart ) {
231 $item = $this->setupMassEnergyEquivalenceItem( $hasPart );
232 $wikibaseConnector = $this->getWikibaseConnectorWithExistingItems(
233 new EntityRevision( $item ),
234 true,
235 LoggerFactory::getInstance( 'Math' )
236 );
237
238 $wikibaseInfo = $wikibaseConnector->fetchWikibaseFromId( 'Q1', 'en' );
239 $this->assertTrue( $wikibaseInfo->hasParts() );
240 $parts = $wikibaseInfo->getParts();
241 $this->assertCount( 3, $parts );
242 foreach ( $parts as $part ) {
243 $key = $part->getId()->getSerialization();
244 if ( $key === 'Q3' ) {
245 $this->assertNull( $part->getUrl() );
246 } else {
247 $this->assertEquals( self::EXAMPLE_URL, $part->getUrl() );
248 }
249 }
250 }
251
252 public static function provideItemSetups(): array {
253 return [
254 [ true ],
255 [ false ],
256 ];
257 }
258 }