Debian Codegraph @

extensions/Cite/tests/phpunit/integration/CiteTest.php

raw symbols SCIP
1 <?php
2
3 namespace Cite\Tests\Integration;
4
5 use Cite\Cite;
6 use Cite\ErrorReporter;
7 use Cite\FootnoteMarkFormatter;
8 use Cite\ReferenceListFormatter;
9 use Cite\ReferenceStack;
10 use Cite\Tests\TestUtils;
11 use LogicException;
12 use MediaWiki\Language\Language;
13 use MediaWiki\Parser\Parser;
14 use MediaWiki\Parser\ParserOptions;
15 use MediaWiki\Parser\StripState;
16 use Wikimedia\TestingAccessWrapper;
17
18 /**
19 * @coversDefaultClass \Cite\Cite
20 * @license GPL-2.0-or-later
21 */
22 class CiteTest extends \MediaWikiIntegrationTestCase {
23
24 /**
25 * @covers ::parseArguments
26 * @dataProvider provideParseArguments
27 */
28 public function testParseArguments(
29 array $attributes,
30 array $expectedValue,
31 ?string $expectedError = null
32 ) {
33 /** @var Cite $cite */
34 $cite = TestingAccessWrapper::newFromObject( $this->newCite() );
35 $status = $cite->parseArguments(
36 $attributes,
37 [ 'dir', 'extends', 'follow', 'group', 'name' ]
38 );
39 $this->assertSame( $expectedValue, array_values( $status->getValue() ) );
40 if ( $expectedError ) {
41 $this->assertStatusError( $expectedError, $status );
42 } else {
43 $this->assertStatusGood( $status );
44 }
45 }
46
47 public static function provideParseArguments() {
48 // Note: Values are guaranteed to be trimmed by the parser, see
49 // Sanitizer::decodeTagAttributes()
50 return [
51 [
52 'attributes' => [],
53 'expectedValue' => [ null, null, null, null, null ],
54 ],
55
56 // One attribute only
57 [
58 'attributes' => [ 'dir' => 'invalid' ],
59 'expectedValue' => [ 'invalid', null, null, null, null ] ],
60 [
61 'attributes' => [ 'dir' => 'RTL' ],
62 'expectedValue' => [ 'rtl', null, null, null, null ] ],
63 [
64 'attributes' => [ 'follow' => 'f' ],
65 'expectedValue' => [ null, null, 'f', null, null ] ],
66 [
67 'attributes' => [ 'group' => 'g' ],
68 'expectedValue' => [ null, null, null, 'g', null ] ],
69 [
70 'attributes' => [ 'invalid' => 'i' ],
71 'expectedValue' => [ null, null, null, null, null ],
72 'expectedError' => 'cite_error_ref_too_many_keys'
73 ],
74 [
75 'attributes' => [ 'invalid' => null ],
76 'expectedValue' => [ null, null, null, null, null ],
77 'expectedError' => 'cite_error_ref_too_many_keys'
78 ],
79 [
80 'attributes' => [ 'name' => 'n' ],
81 'expectedValue' => [ null, null, null, null, 'n' ]
82 ],
83 [
84 'attributes' => [ 'name' => null ],
85 'expectedValue' => [ null, null, null, null, null ]
86 ],
87 [
88 'attributes' => [ 'extends' => 'e' ],
89 'expectedValue' => [ null, 'e', null, null, null ]
90 ],
91
92 // Pairs
93 [
94 'attributes' => [ 'follow' => 'f', 'name' => 'n' ],
95 'expectedValue' => [ null, null, 'f', null, 'n' ]
96 ],
97 [
98 'attributes' => [ 'follow' => null, 'name' => null ],
99 'expectedValue' => [ null, null, null, null, null ]
100 ],
101 [
102 'attributes' => [ 'follow' => 'f', 'extends' => 'e' ],
103 'expectedValue' => [ null, 'e', 'f', null, null ]
104 ],
105 [
106 'attributes' => [ 'group' => 'g', 'name' => 'n' ],
107 'expectedValue' => [ null, null, null, 'g', 'n' ]
108 ],
109
110 // Combinations of 3 or more attributes
111 [
112 'attributes' => [ 'group' => 'g', 'name' => 'n', 'extends' => 'e', 'dir' => 'rtl' ],
113 'expectedValue' => [ 'rtl', 'e', null, 'g', 'n' ]
114 ],
115 ];
116 }
117
118 /**
119 * @covers ::references
120 * @dataProvider provideGuardedReferences
121 */
122 public function testGuardedReferences(
123 ?string $text,
124 array $argv,
125 int $expectedRollbackCount,
126 string $expectedInReferencesGroup,
127 bool $expectedResponsive,
128 string $expectedOutput
129 ) {
130 $this->overrideConfigValue( 'CiteResponsiveReferences', false );
131
132 $parser = $this->createNoOpMock( Parser::class, [ 'recursiveTagParse' ] );
133
134 $cite = $this->newCite();
135 /** @var Cite $spy */
136 $spy = TestingAccessWrapper::newFromObject( $cite );
137 $spy->errorReporter = $this->createPartialMock( ErrorReporter::class, [ 'halfParsed' ] );
138 $spy->errorReporter->method( 'halfParsed' )->willReturnArgument( 1 );
139 $spy->referenceListFormatter = $this->createMock( ReferenceListFormatter::class );
140 $spy->referenceListFormatter->method( 'formatReferences' )
141 ->with( $parser, [], $expectedResponsive, false )
142 ->willReturn( 'references!' );
143 $spy->isSectionPreview = false;
144 $spy->referenceStack = $this->createMock( ReferenceStack::class );
145 $spy->referenceStack->method( 'popGroup' )
146 ->with( $expectedInReferencesGroup )->willReturn( [] );
147 $spy->referenceStack->expects( $expectedRollbackCount ? $this->once() : $this->never() )
148 ->method( 'rollbackRefs' )
149 ->with( $expectedRollbackCount )
150 ->willReturn( [ [ 't', [] ] ] );
151
152 $output = $cite->references( $parser, $text, $argv );
153 $this->assertSame( $expectedOutput, $output );
154 }
155
156 public static function provideGuardedReferences() {
157 return [
158 'Bare references tag' => [
159 'text' => null,
160 'argv' => [],
161 'expectedRollbackCount' => 0,
162 'expectedInReferencesGroup' => '',
163 'expectedResponsive' => false,
164 'expectedOutput' => 'references!'
165 ],
166 'References with group' => [
167 'text' => null,
168 'argv' => [ 'group' => 'g' ],
169 'expectedRollbackCount' => 0,
170 'expectedInReferencesGroup' => 'g',
171 'expectedResponsive' => false,
172 'expectedOutput' => 'references!'
173 ],
174 'Empty references tag' => [
175 'text' => '',
176 'argv' => [],
177 'expectedRollbackCount' => 0,
178 'expectedInReferencesGroup' => '',
179 'expectedResponsive' => false,
180 'expectedOutput' => 'references!'
181 ],
182 'Set responsive' => [
183 'text' => '',
184 'argv' => [ 'responsive' => '1' ],
185 'expectedRollbackCount' => 0,
186 'expectedInReferencesGroup' => '',
187 'expectedResponsive' => true,
188 'expectedOutput' => 'references!'
189 ],
190 'Unknown attribute' => [
191 'text' => '',
192 'argv' => [ 'blargh' => '0' ],
193 'expectedRollbackCount' => 0,
194 'expectedInReferencesGroup' => '',
195 'expectedResponsive' => false,
196 'expectedOutput' => 'cite_error_references_invalid_parameters',
197 ],
198 'Contains refs (which are broken)' => [
199 'text' => Parser::MARKER_PREFIX . '-ref- and ' . Parser::MARKER_PREFIX . '-notref-',
200 'argv' => [],
201 'expectedRollbackCount' => 1,
202 'expectedInReferencesGroup' => '',
203 'expectedResponsive' => false,
204 'expectedOutput' => "references!\ncite_error_references_no_key"
205 ],
206 ];
207 }
208
209 /**
210 * @covers ::guardedRef
211 * @dataProvider provideGuardedRef
212 */
213 public function testGuardedRef(
214 string $text,
215 array $argv,
216 ?string $inReferencesGroup,
217 array $initialRefs,
218 string $expectOutput,
219 ?string $expectedError,
220 array $expectedRefs,
221 bool $isSectionPreview = false
222 ) {
223 $mockParser = $this->createNoOpMock( Parser::class, [ 'getStripState' ] );
224 $mockParser->method( 'getStripState' )
225 ->willReturn( $this->createMock( StripState::class ) );
226
227 $errorReporter = $this->createPartialMock( ErrorReporter::class, [ 'halfParsed' ] );
228 $errorReporter->method( 'halfParsed' )->willReturnArgument( 1 );
229
230 $referenceStack = new ReferenceStack();
231 /** @var ReferenceStack $stackSpy */
232 $stackSpy = TestingAccessWrapper::newFromObject( $referenceStack );
233 $stackSpy->refs = TestUtils::refGroupsFromArray( $initialRefs );
234
235 $mockFootnoteMarkFormatter = $this->createMock( FootnoteMarkFormatter::class );
236 $mockFootnoteMarkFormatter->method( 'linkRef' )->willReturn( '<foot />' );
237
238 $cite = $this->newCite( $isSectionPreview );
239 /** @var Cite $spy */
240 $spy = TestingAccessWrapper::newFromObject( $cite );
241 $spy->errorReporter = $errorReporter;
242 $spy->footnoteMarkFormatter = $mockFootnoteMarkFormatter;
243 $spy->inReferencesGroup = $inReferencesGroup;
244 $spy->referenceStack = $referenceStack;
245
246 $result = $spy->guardedRef( $mockParser, $text, $argv );
247 $this->assertSame( $expectOutput, $result );
248 if ( $expectedError ) {
249 $this->assertStatusError( $expectedError, $spy->mReferencesErrors );
250 } else {
251 $this->assertStatusGood( $spy->mReferencesErrors );
252 }
253 $expectedRefs = TestUtils::refGroupsFromArray( $expectedRefs );
254 $this->assertEquals( $expectedRefs, $stackSpy->refs );
255 }
256
257 public static function provideGuardedRef() {
258 return [
259 'Whitespace text' => [
260 'text' => ' ',
261 'argv' => [ 'name' => 'a' ],
262 'inReferencesGroup' => null,
263 'initialRefs' => [],
264 'expectedOutput' => '<foot />',
265 'expectedError' => null,
266 'expectedRefs' => [
267 '' => [
268 'a' => [
269 'count' => 1,
270 'dir' => null,
271 'key' => 1,
272 'group' => '',
273 'name' => 'a',
274 'text' => null,
275 'number' => 1,
276 ],
277 ],
278 ]
279 ],
280 'Empty in default references' => [
281 'text' => '',
282 'argv' => [],
283 'inReferencesGroup' => '',
284 'initialRefs' => [ '' => [] ],
285 'expectedOutput' => '',
286 'expectedError' => 'cite_error_references_no_key',
287 'expectedRefs' => [ '' => [] ]
288 ],
289 'Fallback to references group' => [
290 'text' => 'text',
291 'argv' => [ 'name' => 'a' ],
292 'inReferencesGroup' => 'foo',
293 'initialRefs' => [
294 'foo' => [ 'a' => [] ],
295 ],
296 'expectedOutput' => '',
297 'expectedError' => null,
298 'expectedRefs' => [
299 'foo' => [
300 'a' => [
301 'text' => 'text',
302 'count' => 0,
303 ],
304 ],
305 ]
306 ],
307 'Successful ref' => [
308 'text' => 'text',
309 'argv' => [ 'name' => 'a' ],
310 'inReferencesGroup' => null,
311 'initialRefs' => [],
312 'expectedOutput' => '<foot />',
313 'expectedError' => null,
314 'expectedRefs' => [
315 '' => [
316 'a' => [
317 'count' => 1,
318 'dir' => null,
319 'key' => 1,
320 'group' => '',
321 'name' => 'a',
322 'text' => 'text',
323 'number' => 1,
324 ],
325 ],
326 ]
327 ],
328 'Invalid ref' => [
329 'text' => 'text',
330 'argv' => [
331 'name' => 'a',
332 'badkey' => 'b',
333 ],
334 'inReferencesGroup' => null,
335 'initialRefs' => [],
336 'expectedOutput' => 'cite_error_ref_too_many_keys',
337 'expectedError' => null,
338 'expectedRefs' => []
339 ],
340 'Successful references ref' => [
341 'text' => 'text',
342 'argv' => [ 'name' => 'a' ],
343 'inReferencesGroup' => '',
344 'initialRefs' => [
345 '' => [
346 'a' => []
347 ]
348 ],
349 'expectedOutput' => '',
350 'expectedError' => null,
351 'expectedRefs' => [
352 '' => [
353 'a' => [
354 'text' => 'text',
355 'count' => 0,
356 ],
357 ],
358 ]
359 ],
360 'T245376: Preview a list-defined ref that was never used' => [
361 'text' => 'T245376',
362 'argv' => [ 'name' => 'a' ],
363 'inReferencesGroup' => '',
364 'initialRefs' => [],
365 'expectOutput' => '',
366 'expectedError' => null,
367 'expectedRefs' => [
368 '' => [
369 'a' => [
370 'text' => 'T245376',
371 'count' => 0,
372 ],
373 ],
374 ],
375 'isSectionPreview' => true,
376 ],
377 'Mismatched text in references' => [
378 'text' => 'text-2',
379 'argv' => [ 'name' => 'a' ],
380 'inReferencesGroup' => '',
381 'initialRefs' => [
382 '' => [
383 'a' => [
384 'text' => 'text-1',
385 'count' => 1,
386 ],
387 ]
388 ],
389 'expectedOutput' => '',
390 'expectedError' => null,
391 'expectedRefs' => [
392 '' => [
393 'a' => [
394 'text' => 'text-1',
395 'count' => 1,
396 'warnings' => [ [ 'cite_error_references_duplicate_key', 'a' ] ],
397 ],
398 ],
399 ]
400 ],
401 ];
402 }
403
404 /**
405 * @covers ::guardedRef
406 */
407 public function testGuardedRef_extendsUsageTracking() {
408 $this->overrideConfigValue( 'CiteBookReferencing', false );
409
410 $mockParser = $this->createNoOpMock( Parser::class, [ 'addTrackingCategory' ] );
411 // This will be our most important assertion.
412 $mockParser->expects( $this->once() )
413 ->method( 'addTrackingCategory' )
414 ->with( Cite::EXTENDS_TRACKING_CATEGORY );
415
416 $cite = $this->newCite();
417 /** @var Cite $spy */
418 $spy = TestingAccessWrapper::newFromObject( $cite );
419 $spy->errorReporter = $this->createMock( ErrorReporter::class );
420
421 $spy->guardedRef( $mockParser, 'text', [ Cite::SUBREF_ATTRIBUTE => 'a' ] );
422 }
423
424 /**
425 * @coversNothing
426 */
427 public function testReferencesSectionPreview() {
428 $language = $this->createNoOpMock( Language::class );
429
430 $parserOptions = $this->createMock( ParserOptions::class );
431 $parserOptions->method( 'getIsSectionPreview' )->willReturn( true );
432
433 $parser = $this->createNoOpMock( Parser::class, [ 'getOptions', 'getContentLanguage' ] );
434 $parser->method( 'getOptions' )->willReturn( $parserOptions );
435 $parser->method( 'getContentLanguage' )->willReturn( $language );
436
437 $config = $this->getServiceContainer()->getMainConfig();
438 /** @var Cite $cite */
439 $cite = TestingAccessWrapper::newFromObject( new Cite( $parser, $config ) );
440 // Assume the currently parsed <ref> is wrapped in <references>
441 $cite->inReferencesGroup = '';
442
443 $html = $cite->guardedRef( $parser, 'a', [ 'name' => 'a' ] );
444 $this->assertSame( '', $html );
445 }
446
447 /**
448 * @covers ::__clone
449 * @covers ::__construct
450 */
451 public function testClone() {
452 $cite = $this->newCite();
453
454 $this->expectException( LogicException::class );
455 clone $cite;
456 }
457
458 private function newCite( bool $isSectionPreview = false ): Cite {
459 $language = $this->createNoOpMock( Language::class, [ '__debugInfo' ] );
460
461 $mockOptions = $this->createMock( ParserOptions::class );
462 $mockOptions->method( 'getIsSectionPreview' )->willReturn( $isSectionPreview );
463
464 $mockParser = $this->createNoOpMock( Parser::class, [ 'getOptions', 'getContentLanguage' ] );
465 $mockParser->method( 'getOptions' )->willReturn( $mockOptions );
466 $mockParser->method( 'getContentLanguage' )->willReturn( $language );
467 $config = $this->getServiceContainer()->getMainConfig();
468 return new Cite( $mockParser, $config );
469 }
470
471 }