Debian Codegraph @

extensions/Echo/tests/phpunit/DiscussionParserTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Test;
4
5 // phpcs:disable Generic.Files.LineLength -- Long html test examples
6
7 use MediaWiki\CommentStore\CommentStoreComment;
8 use MediaWiki\Content\WikitextContent;
9 use MediaWiki\Extension\Notifications\DiscussionParser;
10 use MediaWiki\Extension\Notifications\Model\Event;
11 use MediaWiki\MainConfigNames;
12 use MediaWiki\Registration\ExtensionRegistry;
13 use MediaWiki\Revision\MutableRevisionRecord;
14 use MediaWiki\Revision\SlotRecord;
15 use MediaWiki\Title\Title;
16 use MediaWiki\User\User;
17 use MediaWikiIntegrationTestCase;
18 use ReflectionObject;
19 use Wikimedia\TestingAccessWrapper;
20
21 /**
22 * @covers \MediaWiki\Extension\Notifications\DiscussionParser
23 * @group Echo
24 * @group Database
25 *
26 * TODO test cases for:
27 * - stripHeader
28 * - stripSignature
29 */
30 class DiscussionParserTest extends MediaWikiIntegrationTestCase {
31
32 /**
33 * @var string
34 */
35 private const EXEMPLAR_TIMESTAMP = '20:47, 2 November 2022 (UTC)';
36
37 /**
38 * Convenience users for use in these tests.
39 * Can be setup one by one using the setupTestUser() method
40 * Or all at once using the setupAllTestUsers() method
41 *
42 * @var array[] [username => [user preference => preference value]]
43 */
44 protected $testUsers = [
45 'Werdna' => [
46 'nickname' => '',
47 'fancysig' => '0',
48 ],
49 'Werdna2' => [
50 'nickname' => '[[User:Werdna2|Andrew]]',
51 'fancysig' => '1',
52 ],
53 'Werdna3' => [
54 'nickname' => '[[User talk:Werdna3|Andrew]]',
55 'fancysig' => '1',
56 ],
57 'Werdna4' => [
58 'nickname' => '[[User:Werdna4|wer]dna]]',
59 'fancysig' => '1',
60 ],
61 'We buried our secrets in the garden' => [
62 'nickname' => '[[User talk:We buried our secrets in the garden#top|wbositg]]',
63 'fancysig' => '1',
64 ],
65 'I Heart Spaces' => [
66 'nickname' => '[[User:I_Heart_Spaces]] ([[User_talk:I_Heart_Spaces]])',
67 'fancysig' => '1',
68 ],
69 'Jam' => [
70 'nickname' => '[[User:Jam]]',
71 'fancysig' => '1',
72 ],
73 'Reverta-me' => [
74 'nickname' => "[[User:Reverta-me|<span style=\"font-size:13px; color:blue;font-family:Lucida Handwriting;text-shadow:aqua 5px 3px 12px;\">Aaaaa Bbbbbbb</span>]]'' <sup>[[User Talk:Reverta-me|<font color=\"gold\" face=\"Lucida Calligraphy\">Discussão</font>]]</sup>''",
75 'fancysig' => '1',
76 ],
77 'Jorm' => [
78 'nickname' => '',
79 'fancysig' => '0',
80 ],
81 'Jdforrester' => [
82 'nickname' => '',
83 'fancysig' => '0',
84 ],
85 'DarTar' => [
86 'nickname' => '',
87 'fancysig' => '0',
88 ],
89 'Bsitu' => [
90 'nickname' => '',
91 'fancysig' => '0',
92 ],
93 'JarJar' => [
94 'nickname' => '',
95 'fancysig' => '0',
96 ],
97 'Schnark' => [
98 'nickname' => '[[Benutzer:Schnark]] ([[Benutzer:Schnark/js|js]])',
99 'fancysig' => '1',
100 ],
101 'Cwobeel' => [
102 'nickname' => '[[User:Cwobeel|<span style="color:#339966">Cwobeel</span>]] [[User_talk:Cwobeel|<span style="font-size:80%">(talk)</span>]]',
103 'fancysig' => '1',
104 ],
105 'Bob K31416' => [
106 'nickname' => '',
107 'fancysig' => '0',
108 ],
109 'X" onclick="alert(\'XSS\');" title="y' => [
110 'nickname' => '',
111 'fancysig' => '0',
112 ],
113 'He7d3r' => [
114 'nickname' => '',
115 'fancysig' => '0',
116 ],
117 'PauloEduardo' => [
118 'nickname' => "[[User:PauloEduardo|<span style=\"font-size:13px; color:blue;font-family:Lucida Handwriting;text-shadow:aqua 5px 3px 12px;\">Paulo Eduardo</span>]]'' <sup>[[User Talk:PauloEduardo|<font color=\"gold\" face=\"Lucida Calligraphy\">Discussão</font>]]</sup>''",
119 'fancysig' => '1',
120 ],
121 'PatHadley' => [
122 'nickname' => '',
123 'fancysig' => '0',
124 ],
125 'Samwalton9' => [
126 'nickname' => '',
127 'fancysig' => '0',
128 ],
129 'Kudpung' => [
130 'nickname' => '[[User:Kudpung|Kudpung กุดผึ้ง]] ([[User talk:Kudpung#top|talk]])',
131 'fancysig' => '1',
132 ],
133 'Jim Carter' => [
134 'nickname' => '',
135 'fancysig' => '0',
136 ],
137 'Buster7' => [
138 'nickname' => '',
139 'fancysig' => '0',
140 ],
141 'Admin' => [
142 'nickname' => '[[:User:Admin|Admin]]',
143 'fancysig' => '1',
144 ],
145 'Test11' => [
146 'nickname' => '',
147 'fancysig' => '0',
148 ],
149 ];
150
151 private function setupAllTestUsers() {
152 foreach ( $this->testUsers as $username => $_ ) {
153 $this->setupTestUser( $username );
154 }
155 }
156
157 private function setupTestUser( $username ) {
158 // Skip user creation requests that are not in the list (such as IPs)
159 if ( !array_key_exists( $username, $this->testUsers ) ) {
160 return;
161 }
162
163 $preferences = $this->testUsers[$username];
164 $user = User::createNew( $username );
165
166 // Set preferences
167 if ( $user ) {
168 $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
169 foreach ( $preferences as $option => $value ) {
170 $userOptionsManager->setOption( $user, $option, $value );
171 }
172 $userOptionsManager->saveOptions( $user );
173 }
174 }
175
176 public static function provideHeaderExtractions() {
177 return [
178 [ '', false ],
179 [ '== Grand jury no bill reception ==', 'Grand jury no bill reception' ],
180 [ '=== Echo-Test ===', 'Echo-Test' ],
181 [ '==== Notificações ====', 'Notificações' ],
182 [ '=====Me?=====', 'Me?' ],
183 ];
184 }
185
186 /**
187 * @dataProvider provideHeaderExtractions
188 */
189 public function testExtractHeader( $text, $expected ) {
190 $this->assertEquals( $expected, DiscussionParser::extractHeader( $text ) );
191 }
192
193 public static function generateEventsForRevisionDataProvider() {
194 return [
195 [
196 'new' => 637638133,
197 'old' => 637637213,
198 'username' => 'Cwobeel',
199 'lang' => 'en',
200 'pages' => [
201 // pages expected to exist (e.g. templates to be expanded)
202 'Template:u' => '[[User:{{{1}}}|{{<includeonly>safesubst:</includeonly>#if:{{{2|}}}|{{{2}}}|{{{1}}}}}]]<noinclude>{{documentation}}</noinclude>',
203 ],
204 // can't remember, not important here
205 'title' => 'UTPage',
206 'expected' => [
207 // events expected to be fired going from old revision to new
208 [
209 'type' => 'mention',
210 'agent' => 'Cwobeel',
211 'section-title' => 'Grand jury no bill reception',
212 /*
213 * I wish I could also compare Event::$extra data to
214 * compare user ids of mentioned users. However, due to
215 * How PHPUnit works, setUp won't be run by the time
216 * this dataset is generated, so we don't yet know the
217 * user ids of the folks we're about to insert...
218 * I'll skip that part for now.
219 */
220 ],
221 ],
222 ],
223 [
224 'new' => 138275105,
225 'old' => 138274875,
226 'username' => 'Schnark',
227 'lang' => 'de',
228 'pages' => [],
229 // can't remember, not important here
230 'title' => 'UTPage',
231 'expected' => [
232 [
233 'type' => 'mention',
234 'agent' => 'Schnark',
235 'section-title' => 'Echo-Test',
236 ],
237 ],
238 ],
239 [
240 'new' => 40610292,
241 'old' => 40608353,
242 'username' => 'PauloEduardo',
243 'lang' => 'pt',
244 'pages' => [
245 'Predefinição:U' => '[[User:{{{1|<noinclude>Exemplo</noinclude>}}}|{{{{{|safesubst:}}}#if:{{{2|}}}|{{{2}}}|{{{1|<noinclude>Exemplo</noinclude>}}}}}]]<noinclude>{{Atalho|Predefinição:U}}{{Documentação|Predefinição:Usuário/doc}}</noinclude>',
246 ],
247 // can't remember, not important here
248 'title' => 'UTPage',
249 'expected' => [
250 [
251 'type' => 'mention',
252 'agent' => 'PauloEduardo',
253 'section-title' => 'Notificações',
254 ],
255 ],
256 ],
257 [
258 'new' => 646792804,
259 'old' => 646790570,
260 'username' => 'PatHadley',
261 'lang' => 'en',
262 'pages' => [
263 'Template:ping' => '{{SAFESUBST:<noinclude />#if:{{{1|<noinclude>$</noinclude>}}}
264 |<span class="template-ping">@[[:User:{{SAFESUBST:<noinclude />BASEPAGENAME:{{{1|Example}}}}}|{{SAFESUBST:<noinclude />BASEPAGENAME:{{{label1|{{{1|Example}}}}}}}}]]{{SAFESUBST:<noinclude />#if:{{{2|}}}
265 |, [[:User:{{SAFESUBST:<noinclude />BASEPAGENAME:{{{2|Example}}}}}|{{SAFESUBST:<noinclude />BASEPAGENAME:{{{label2|{{{2|Example}}}}}}}}]]{{SAFESUBST:<noinclude />#if:{{{3|}}}
266 |, [[:User:{{SAFESUBST:<noinclude />BASEPAGENAME:{{{3|Example}}}}}|{{SAFESUBST:<noinclude />BASEPAGENAME:{{{label3|{{{3|Example}}}}}}}}]]{{SAFESUBST:<noinclude />#if:{{{4|}}}
267 |, [[:User:{{SAFESUBST:<noinclude />BASEPAGENAME:{{{4|Example}}}}}|{{SAFESUBST:<noinclude />BASEPAGENAME:{{{label4|{{{4|Example}}}}}}}}]]{{SAFESUBST:<noinclude />#if:{{{5|}}}
268 |, [[:User:{{SAFESUBST:<noinclude />BASEPAGENAME:{{{5|Example}}}}}|{{SAFESUBST:<noinclude />BASEPAGENAME:{{{label5|{{{5|Example}}}}}}}}]]{{SAFESUBST:<noinclude />#if:{{{6|}}}
269 |, [[:User:{{SAFESUBST:<noinclude />BASEPAGENAME:{{{6|Example}}}}}|{{SAFESUBST:<noinclude />BASEPAGENAME:{{{label6|{{{6|Example}}}}}}}}]]{{SAFESUBST:<noinclude />#if:{{{7|}}}
270 |, [[:User:{{SAFESUBST:<noinclude />BASEPAGENAME:{{{7|Example}}}}}|{{SAFESUBST:<noinclude />BASEPAGENAME:{{{label7|{{{7|Example}}}}}}}}]]
271 }}
272 }}
273 }}
274 }}
275 }}
276 }}{{{p|:}}}</span>
277 |{{SAFESUBST:<noinclude />Error|Error in [[Template:Replyto]]: Username not given.}}
278 }}<noinclude>
279
280 {{documentation}}
281 </noinclude>',
282 'MediaWiki:Signature' => '[[User:$1|$2]] {{#ifeq:{{FULLPAGENAME}}|User talk:$1|([[User talk:$1#top|talk]])|([[User talk:$1|talk]])}}',
283 ],
284 'title' => 'User_talk:PatHadley',
285 'expected' => [
286 [
287 'type' => 'mention',
288 'agent' => 'PatHadley',
289 'section-title' => 'Wizardry required',
290 ],
291 [
292 'type' => 'edit-user-talk',
293 'agent' => 'PatHadley',
294 'section-title' => 'Wizardry required',
295 ],
296 ],
297 'precondition' => 'isParserFunctionsInstalled',
298 ],
299 [
300 'new' => 647260329,
301 'old' => 647258025,
302 'username' => 'Kudpung',
303 'lang' => 'en',
304 'pages' => [
305 'Template:U' => '[[User:{{{1}}}|{{<includeonly>safesubst:</includeonly>#if:{{{2|}}}|{{{2}}}|{{{1}}}}}]]<noinclude>{{documentation}}</noinclude>',
306 ],
307 'title' => 'User_talk:Kudpung',
308 'expected' => [
309 [
310 'type' => 'mention',
311 'agent' => 'Kudpung',
312 'section-title' => 'Me?',
313 ],
314 [
315 'type' => 'edit-user-talk',
316 'agent' => 'Kudpung',
317 'section-title' => 'Me?',
318 ],
319 ],
320 ],
321 // T68512, leading colon in user page link in signature
322 [
323 'new' => 612485855,
324 'old' => 612485595,
325 'username' => 'Admin',
326 'lang' => 'en',
327 'pages' => [],
328 'title' => 'User_talk:Admin',
329 'expected' => [
330 [
331 'type' => 'mention',
332 'agent' => 'Admin',
333 'section-title' => 'Hi',
334 ],
335 [
336 'type' => 'edit-user-talk',
337 'agent' => 'Admin',
338 'section-title' => 'Hi',
339 ],
340 ],
341 'precondition' => 'isParserFunctionsInstalled',
342 ],
343 // T154406 unintended mentions when changing content
344 [
345 'new' => 987667999,
346 'old' => 987667998,
347 'username' => 'Admin',
348 'lang' => 'en',
349 'pages' => [],
350 'title' => 'MultipleSignatureMentions',
351 'expected' => [],
352 ],
353 [
354 'new' => 1234,
355 'old' => 123,
356 'username' => 'Admin',
357 'lang' => 'en',
358 'pages' => [],
359 'title' => 'Pings in summary',
360 'expected' => [
361 [
362 'type' => 'mention-summary',
363 'agent' => 'Admin',
364 'section-title' => null,
365 ]
366 ],
367 'precondition' => '',
368 'summary' => 'Hey [[User:Werdna|Werdna]] and [[User:Jorm]], [[User:Admin]] here',
369 ],
370 ];
371 }
372
373 /**
374 * @dataProvider generateEventsForRevisionDataProvider
375 */
376 public function testGenerateEventsForRevision(
377 $newId, $oldId, $username, $lang, $pages, $title, $expected, $precondition = '',
378 $summary = ''
379 ) {
380 if ( $precondition !== '' ) {
381 $result = $this->$precondition();
382 if ( $result !== true ) {
383 $this->markTestSkipped( $result );
384
385 return;
386 }
387 }
388
389 $this->setupAllTestUsers();
390
391 $revision = $this->setupTestRevisionsForEventGeneration(
392 $newId, $oldId, $username, $lang, $pages, $title, $summary
393 );
394 $events = [];
395 $this->setupEventCallbackForEventGeneration(
396 static function ( Event $event ) use ( &$events ) {
397 $events[] = [
398 'type' => $event->getType(),
399 'agent' => $event->getAgent()->getName(),
400 'section-title' => $event->getExtraParam( 'section-title' ),
401 ];
402 return false;
403 }
404 );
405
406 $this->overrideConfigValues( [
407 // disable mention failure and success notifications
408 'EchoMentionStatusNotifications' => false,
409 // enable pings from summary
410 'EchoMaxMentionsInEditSummary' => 5,
411 ] );
412 $this->clearHook( 'EchoGetEventsForRevision' );
413
414 DiscussionParser::generateEventsForRevision( $revision, false );
415
416 $this->assertEquals( $expected, $events );
417 }
418
419 public static function provider_generateEventsForRevision_mentionStatus() {
420 return [
421 [
422 'new' => 747747748,
423 'old' => 747747747,
424 'username' => 'Admin',
425 'lang' => 'en',
426 'pages' => [],
427 'title' => 'UTPage',
428 'expected' => [
429 [
430 'type' => 'mention-failure',
431 'agent' => 'Admin',
432 'section-title' => 'Hello Users',
433 'subject-name' => 'Ping',
434 ],
435 [
436 'type' => 'mention-failure',
437 'agent' => 'Admin',
438 'section-title' => 'Hello Users',
439 'subject-name' => 'Po?ng',
440 ],
441 ],
442 ],
443 [
444 'new' => 747747750,
445 'old' => 747747747,
446 'username' => 'Admin',
447 'lang' => 'en',
448 'pages' => [],
449 'title' => 'UTPage',
450 'expected' => [
451 [
452 'type' => 'mention',
453 'agent' => 'Admin',
454 'section-title' => 'Hello Users',
455 'subject-name' => null,
456 ],
457 [
458 'type' => 'mention-success',
459 'agent' => 'Admin',
460 'section-title' => 'Hello Users',
461 'subject-name' => 'Test11',
462 ],
463 ],
464 ],
465 [
466 'new' => 747798766,
467 'old' => 747798765,
468 'username' => 'Admin',
469 'lang' => 'en',
470 'pages' => [],
471 'title' => 'UTPage',
472 'expected' => [
473 [
474 'type' => 'mention-failure',
475 'agent' => 'Admin',
476 'section-title' => 'Section 2',
477 'subject-name' => 'NoUser',
478 ],
479 ],
480 ],
481 [
482 'new' => 747798767,
483 'old' => 747798765,
484 'username' => 'Admin',
485 'lang' => 'en',
486 'pages' => [],
487 'title' => 'UTPage',
488 'expected' => [
489 [
490 'type' => 'mention-failure',
491 'agent' => 'Admin',
492 'section-title' => 'Section 2',
493 'subject-name' => 'NoUser',
494 ],
495 ],
496 ],
497 [
498 'new' => 747798768,
499 'old' => 747798765,
500 'username' => 'Admin',
501 'lang' => 'en',
502 'pages' => [],
503 'title' => 'UTPage',
504 'expected' => [],
505 ],
506 [
507 'new' => 747798770,
508 'old' => 747798765,
509 'username' => 'Admin',
510 'lang' => 'en',
511 'pages' => [],
512 'title' => 'UTPage',
513 'expected' => [
514 [
515 'type' => 'mention',
516 'agent' => 'Admin',
517 'section-title' => 'Section 1.5',
518 'subject-name' => null,
519 ],
520 [
521 'type' => 'mention-success',
522 'agent' => 'Admin',
523 'section-title' => 'Section 1.5',
524 'subject-name' => 'Test11',
525 ],
526 ],
527 ],
528 [
529 'new' => 747798771,
530 'old' => 747798765,
531 'username' => 'Admin',
532 'lang' => 'en',
533 'pages' => [],
534 'title' => 'UTPage',
535 'expected' => [
536 [
537 'type' => 'mention-failure',
538 'agent' => 'Admin',
539 'section-title' => 'Section 1.5',
540 'subject-name' => 'NoUser1.5',
541 ],
542 [
543 'type' => 'mention-failure',
544 'agent' => 'Admin',
545 'section-title' => 'Section 2',
546 'subject-name' => 'NoUser2',
547 ],
548 ],
549 ],
550 [
551 'new' => 747798772,
552 'old' => 747798765,
553 'username' => 'Admin',
554 'lang' => 'en',
555 'pages' => [],
556 'title' => 'UTPage',
557 'expected' => [
558 [
559 'type' => 'mention-failure',
560 'agent' => 'Admin',
561 'section-title' => 'Section 1',
562 'subject-name' => 'NoUser1',
563 ],
564 [
565 'type' => 'mention-failure',
566 'agent' => 'Admin',
567 'section-title' => 'Section 1.75',
568 'subject-name' => 'NoUser1.75',
569 ],
570 [
571 'type' => 'mention-failure',
572 'agent' => 'Admin',
573 'section-title' => 'Section 2',
574 'subject-name' => 'NoUser2',
575 ],
576 ],
577 [
578 'new' => 987654322,
579 'old' => 987654321,
580 'username' => 'Admin',
581 'lang' => 'en',
582 'pages' => [],
583 'title' => 'User_talk:Admin',
584 'expected' => [ [
585 'type' => 'edit-user-talk',
586 'agent' => 'Admin',
587 'section-title' => false,
588 'subject-name' => null,
589 ] ],
590 ],
591 [
592 'new' => 987654323,
593 'old' => 987654321,
594 'username' => 'Admin',
595 'lang' => 'en',
596 'pages' => [],
597 'title' => 'User_talk:Admin',
598 'expected' => [
599 [
600 'type' => 'mention',
601 'agent' => 'Admin',
602 'section-title' => 'Section 1',
603 'subject-name' => null,
604 ],
605 [
606 'type' => 'mention-success',
607 'agent' => 'Admin',
608 'section-title' => 'Section 1',
609 'subject-name' => 'Test11',
610 ],
611 [
612 'type' => 'edit-user-talk',
613 'agent' => 'Admin',
614 'section-title' => 'Section 1',
615 'subject-name' => null,
616 ],
617 ],
618 ],
619 ],
620 [
621 'new' => 987654324,
622 'old' => 987654321,
623 'username' => 'Admin',
624 'lang' => 'en',
625 'pages' => [],
626 'title' => 'User_talk:Admin',
627 'expected' => [
628 [
629 'type' => 'mention',
630 'agent' => 'Admin',
631 'section-title' => 'Section 1',
632 'subject-name' => null,
633 ],
634 [
635 'type' => 'mention-success',
636 'agent' => 'Admin',
637 'section-title' => 'Section 1',
638 'subject-name' => 'Test11',
639 ],
640 [
641 'type' => 'edit-user-talk',
642 'agent' => 'Admin',
643 'section-title' => false,
644 'subject-name' => null,
645 ],
646 ],
647 ],
648 [
649 'new' => 987654325,
650 'old' => 987654321,
651 'username' => 'Admin',
652 'lang' => 'en',
653 'pages' => [],
654 'title' => 'User_talk:Admin',
655 'expected' => [
656 [
657 'type' => 'mention',
658 'agent' => 'Admin',
659 'section-title' => 'Section 2',
660 'subject-name' => null,
661 ],
662 [
663 'type' => 'mention-success',
664 'agent' => 'Admin',
665 'section-title' => 'Section 2',
666 'subject-name' => 'Test11',
667 ],
668 [
669 'type' => 'edit-user-talk',
670 'agent' => 'Admin',
671 'section-title' => 'Section 2',
672 'subject-name' => null,
673 ],
674 ],
675 ],
676 [
677 'new' => 987654401,
678 'old' => 987654400,
679 'username' => 'Admin',
680 'lang' => 'en',
681 'pages' => [],
682 'title' => 'UTPage2',
683 'expected' => [
684 [
685 'type' => 'mention',
686 'agent' => 'Admin',
687 'section-title' => false,
688 'subject-name' => null,
689 ],
690 [
691 'type' => 'mention-success',
692 'agent' => 'Admin',
693 'section-title' => false,
694 'subject-name' => 'Test11',
695 ],
696 ],
697 ],
698 ];
699 }
700
701 /**
702 * @dataProvider provider_generateEventsForRevision_mentionStatus
703 */
704 public function testGenerateEventsForRevision_mentionStatus(
705 $newId, $oldId, $username, $lang, $pages, $title, $expected
706 ) {
707 $this->setupAllTestUsers();
708
709 $revision = $this->setupTestRevisionsForEventGeneration(
710 $newId, $oldId, $username, $lang, $pages, $title
711 );
712 $events = [];
713 $this->setupEventCallbackForEventGeneration(
714 static function ( Event $event ) use ( &$events ) {
715 $events[] = [
716 'type' => $event->getType(),
717 'agent' => $event->getAgent()->getName(),
718 'section-title' => $event->getExtraParam( 'section-title' ),
719 'subject-name' => $event->getExtraParam( 'subject-name' ),
720 ];
721 return false;
722 }
723 );
724
725 $this->overrideConfigValues( [
726 // enable mention failure and success notifications
727 'EchoMentionStatusNotifications' => true,
728 // enable multiple sections mentions
729 'EchoMentionsOnMultipleSectionEdits' => true,
730 ] );
731 $this->clearHook( 'EchoGetEventsForRevision' );
732
733 DiscussionParser::generateEventsForRevision( $revision, false );
734
735 $this->assertEquals( $expected, $events );
736 }
737
738 public static function provider_extractSections() {
739 return [
740 [
741 'content' => 'Just Text.',
742 'result' => [
743 [
744 'header' => false,
745 'content' => 'Just Text.',
746 ],
747 ],
748 ],
749 [
750 'content' =>
751 <<<TEXT
752 Text and a
753 == Headline ==
754 with text
755 TEXT
756 ,
757 'result' => [
758 [
759 'header' => false,
760 'content' =>
761 <<<TEXT
762 Text and a
763 TEXT
764 ,
765 ],
766 [
767 'header' => 'Headline',
768 'content' =>
769 <<<TEXT
770 == Headline ==
771 with text
772 TEXT
773 ,
774 ],
775 ],
776 ],
777 [
778 'content' =>
779 <<<TEXT
780 == Headline ==
781 Text and a [[User:Test]]
782 TEXT
783 ,
784 'result' => [
785 [
786 'header' => 'Headline',
787 'content' =>
788 <<<TEXT
789 == Headline ==
790 Text and a [[User:Test]]
791 TEXT
792 ,
793 ],
794 ],
795 ],
796 [
797 'content' =>
798 <<<TEXT
799 Content 0
800 == Headline 1 ==
801 Content 1
802 === Headline 2 ===
803 Content 2
804 TEXT
805 ,
806 'result' => [
807 [
808 'header' => false,
809 'content' => 'Content 0',
810 ],
811 [
812 'header' => 'Headline 1',
813 'content' =>
814 <<<TEXT
815 == Headline 1 ==
816 Content 1
817 TEXT
818 ,
819 ],
820 [
821 'header' => 'Headline 2',
822 'content' =>
823 <<<TEXT
824 === Headline 2 ===
825 Content 2
826 TEXT
827 ,
828 ],
829 ],
830 ],
831 [
832 'content' =>
833 <<<TEXT
834 == Headline 1 ==
835 مرحبا كيف حالك
836 === Headline 2 ===
837 انا بخير شكرا
838 TEXT
839 ,
840 'result' => [
841 [
842 'header' => 'Headline 1',
843 'content' =>
844 <<<TEXT
845 == Headline 1 ==
846 مرحبا كيف حالك
847 TEXT
848 ,
849 ],
850 [
851 'header' => 'Headline 2',
852 'content' =>
853 <<<TEXT
854 === Headline 2 ===
855 انا بخير شكرا
856 TEXT
857 ,
858 ],
859 ],
860 ],
861 [
862 'content' =>
863 <<<TEXT
864 مرحبا كيف حالك
865 === Headline 1 ===
866 انا بخير شكرا
867 TEXT
868 ,
869 'result' => [
870 [
871 'header' => false,
872 'content' =>
873 <<<TEXT
874 مرحبا كيف حالك
875 TEXT
876 ,
877 ],
878 [
879 'header' => 'Headline 1',
880 'content' =>
881 <<<TEXT
882 === Headline 1 ===
883 انا بخير شكرا
884 TEXT
885 ,
886 ],
887 ],
888 ],
889 ];
890 }
891
892 /**
893 * @dataProvider provider_extractSections
894 */
895 public function testExtractSections( $content, $result ) {
896 $discussionParser = TestingAccessWrapper::newFromClass( DiscussionParser::class );
897 $sections = $discussionParser->extractSections( $content );
898
899 $this->assertEquals( $result, $sections );
900 }
901
902 public function testGenerateEventsForRevision_tooManyMentionsFailure() {
903 $expected = [
904 [
905 'type' => 'mention-failure-too-many',
906 'agent' => 'Admin',
907 'section-title' => 'Hello Users',
908 'max-mentions' => 5,
909 ],
910 ];
911
912 $this->setupTestUser( 'Admin' );
913 $revision = $this->setupTestRevisionsForEventGeneration( 747747749, 747747747, 'Admin', 'en', [], 'UTPage' );
914
915 $events = [];
916 $this->setupEventCallbackForEventGeneration(
917 static function ( Event $event ) use ( &$events ) {
918 $events[] = [
919 'type' => $event->getType(),
920 'agent' => $event->getAgent()->getName(),
921 'section-title' => $event->getExtraParam( 'section-title' ),
922 'max-mentions' => $event->getExtraParam( 'max-mentions' ),
923 ];
924 return false;
925 }
926 );
927
928 $this->overrideConfigValues( [
929 // enable mention failure and success notifications
930 'EchoMentionStatusNotifications' => true,
931 // lower limit for the mention-failure-too-many notification
932 'EchoMaxMentionsCount' => 5
933 ] );
934 $this->clearHook( 'EchoGetEventsForRevision' );
935
936 DiscussionParser::generateEventsForRevision( $revision, false );
937
938 $this->assertEquals( $expected, $events );
939 }
940
941 private function setupTestRevisionsForEventGeneration( $newId, $oldId, $username, $lang, $pages,
942 $title, $summary = ''
943 ) {
944 $services = $this->getServiceContainer();
945 $store = $services->getRevisionStore();
946 // Content language is used by the code that interprets the namespace part of titles
947 // (Title::getTitleParser), so should be the fake language ;)
948 $this->setContentLang( $lang );
949 $this->overrideConfigValues( [
950 // this one allows MediaWiki:xyz pages to be set as messages
951 MainConfigNames::UseDatabaseMessages => true
952 ] );
953
954 // pages to be created: templates may be used to ping users (e.g.
955 // {{u|...}}) but if we don't have that template, it just won't work!
956 $pages += [ $title => '' ];
957
958 $user = $this->getTestUser()->getUser();
959 $wikiPageFactory = $services->getWikiPageFactory();
960 foreach ( $pages as $pageTitle => $pageText ) {
961 $template = $wikiPageFactory->newFromTitle( Title::newFromText( $pageTitle ) );
962 $template->doUserEditContent( new WikitextContent( $pageText ), $user, '' );
963 }
964
965 // force i18n messages to be reloaded from MessageCache (from DB, where a new message
966 // might have been created as page)
967 $services->resetServiceForTesting( 'MessageCache' );
968
969 // grab revision excerpts (didn't include them in this src file because
970 // they can be pretty long)
971 $oldText = file_get_contents( __DIR__ . '/revision_txt/' . $oldId . '.txt' );
972 $newText = file_get_contents( __DIR__ . '/revision_txt/' . $newId . '.txt' );
973
974 // revision texts can be in different languages, where links etc are
975 // different (e.g. User: becomes Benutzer: in German), so let's pretend
976 // the page they belong to is from that language
977 $title = Title::newFromText( $title );
978 $object = new ReflectionObject( $title );
979 $property = $object->getProperty( 'mDbPageLanguage' );
980 $property->setValue( $title, $lang );
981
982 // create stub MutableRevisionRecord object
983 $revision = new MutableRevisionRecord( $title );
984
985 $content = new WikitextContent( $newText );
986
987 $revision->setId( $newId );
988 $revision->setUser( User::newFromName( $username ) );
989
990 $revision->setParentId( $oldId );
991 $comment = CommentStoreComment::newUnsavedComment(
992 $summary,
993 null
994 );
995 $revision->setComment( $comment );
996 $revision->setContent( SlotRecord::MAIN, $content );
997
998 $userName = $revision->getUser()->getName();
999
1000 // generate diff between 2 revisions
1001 $changes = DiscussionParser::getMachineReadableDiff( $oldText, $newText );
1002 $output = DiscussionParser::interpretDiff( $changes, $userName, $title );
1003
1004 // store diff in some local cache var, to circumvent
1005 // DiscussionParser::getChangeInterpretationForRevision's attempt to
1006 // retrieve parent revision from DB
1007 $cacheKey = $revision->getId() . '|' . $revision->getPage()->getNamespace() . '|' . $revision->getPage()->getDBkey();
1008 TestingAccessWrapper::newFromClass( DiscussionParser::class )->revisionInterpretationCache = [ $cacheKey => $output ];
1009 return $revision;
1010 }
1011
1012 private function setupEventCallbackForEventGeneration( callable $callback ) {
1013 $this->setTemporaryHook( 'BeforeEchoEventInsert', $callback );
1014 }
1015
1016 public function testTimestampRegex() {
1017 $exemplarTimestamp = self::EXEMPLAR_TIMESTAMP;
1018 $timestampRegex = DiscussionParser::getTimestampRegex();
1019
1020 $match = preg_match( '/' . $timestampRegex . '/u', $exemplarTimestamp );
1021 $this->assertSame( 1, $match );
1022 }
1023
1024 public function testTimestampRegex_T264922() {
1025 $this->overrideConfigValue( MainConfigNames::LanguageCode, 'skr' );
1026 $this->assertIsString( DiscussionParser::getTimestampRegex(), 'does not fail' );
1027 }
1028
1029 public function testGetTimestampPosition() {
1030 $line = 'Hello World. ' . self::EXEMPLAR_TIMESTAMP;
1031 $pos = DiscussionParser::getTimestampPosition( $line );
1032 $this->assertSame( 13, $pos );
1033 }
1034
1035 /**
1036 * @dataProvider signingDetectionDataProvider
1037 * FIXME some of the app logic is in the test...
1038 */
1039 public function testSigningDetection( $line, $expectedUser, $contentLangCode = 'en' ) {
1040 $this->overrideConfigValue( MainConfigNames::LanguageCode, $contentLangCode );
1041
1042 if ( is_array( $expectedUser ) ) {
1043 $this->setupTestUser( $expectedUser[1] );
1044 }
1045
1046 if ( !DiscussionParser::isSignedComment( $line ) ) {
1047 $this->assertFalse( $expectedUser );
1048 return;
1049 }
1050
1051 $output = DiscussionParser::getUserFromLine( $line );
1052
1053 if ( $output === false ) {
1054 $this->assertFalse( $expectedUser );
1055 } elseif ( is_array( $expectedUser ) ) {
1056 // Sometimes testing for correct user detection,
1057 // sometimes testing for offset detection
1058 $this->assertEquals( $expectedUser, $output );
1059 } else {
1060 $this->assertEquals( $expectedUser, $output[1] );
1061 }
1062 }
1063
1064 public static function signingDetectionDataProvider() {
1065 $ts = self::EXEMPLAR_TIMESTAMP;
1066
1067 return [
1068 // Basic
1069 [
1070 "I like this. [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts",
1071 [
1072 13,
1073 'Werdna'
1074 ],
1075 ],
1076 // Confounding
1077 [
1078 "[[User:Jorm]] is a meanie. --[[User:Werdna2|Andrew]] $ts",
1079 [
1080 29,
1081 "Werdna2"
1082 ],
1083 ],
1084 // Talk page link only
1085 [
1086 "[[User:Swalling|Steve]] is the best person I have ever met. --[[User talk:Werdna3|Andrew]] $ts",
1087 [
1088 62,
1089 'Werdna3'
1090 ],
1091 ],
1092 // Anonymous user
1093 [
1094 "I am anonymous because I like my IP address. --[[Special:Contributions/127.0.0.1|127.0.0.1]] $ts",
1095 [
1096 47,
1097 '127.0.0.1'
1098 ],
1099 ],
1100 // No signature
1101 [
1102 "Well, \nI do think that [[User:Newyorkbrad]] is pretty cool, but what do I know?",
1103 false
1104 ],
1105 // Hash symbols in usernames
1106 [
1107 "What do you think? [[User talk:We buried our secrets in the garden#top|wbositg]] $ts",
1108 [
1109 19,
1110 'We buried our secrets in the garden'
1111 ],
1112 ],
1113 // Title that gets normalized different than it is provided in the wikitext
1114 [
1115 "Beep boop [[User:I_Heart_Spaces]] ([[User_talk:I_Heart_Spaces]]) $ts",
1116 [
1117 strlen( "Beep boop " ),
1118 'I Heart Spaces'
1119 ],
1120 ],
1121 // Accepts ] in the pipe
1122 [
1123 "Shake n Bake --[[User:Werdna4|wer]dna]] $ts",
1124 [
1125 strlen( "Shake n Bake --" ),
1126 'Werdna4',
1127 ],
1128 ],
1129
1130 [
1131 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxã? [[User:Jam]] $ts",
1132 [
1133 strlen( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxã? " ),
1134 "Jam"
1135 ],
1136 ],
1137 // extra long signature
1138 [
1139 "{{U|He7d3r}}, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxã? [[User:Reverta-me|<span style=\"font-size:13px; color:blue;font-family:Lucida Handwriting;text-shadow:aqua 5px 3px 12px;\">Aaaaa Bbbbbbb</span>]]'' <sup>[[User Talk:Reverta-me|<font color=\"gold\" face=\"Lucida Calligraphy\">Discussão</font>]]</sup>''",
1140 [
1141 strlen( "{{U|He7d3r}}, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxã? " ),
1142 'Reverta-me',
1143 ],
1144 ],
1145 // Bug: T87852
1146 [
1147 "Test --[[Benutzer:Schnark]] ([[Benutzer:Schnark/js|js]])",
1148 [
1149 strlen( "Test --" ),
1150 'Schnark',
1151 ],
1152 'de'
1153 ],
1154 // when adding additional tests, make sure to add the non-anon users
1155 // to DiscussionParserTest::$testUsers - the DiscussionParser
1156 // needs the users to exist, because it'll generate a comparison
1157 // signature, which is different when the user is considered anon
1158 ];
1159 }
1160
1161 /** @dataProvider diffDataProvider */
1162 public function testDiff( $oldText, $newText, $expected ) {
1163 $actual = DiscussionParser::getMachineReadableDiff( $oldText, $newText );
1164 unset( $actual['_info'] );
1165 unset( $expected['_info'] );
1166
1167 $this->assertEquals( $expected, $actual );
1168 }
1169
1170 public static function diffDataProvider() {
1171 return [
1172 [
1173 <<<TEXT
1174 line 1
1175 line 2
1176 line 3
1177 line 4
1178 TEXT
1179 , <<<TEXT
1180 line 1
1181 line 3
1182 line 4
1183 TEXT
1184 ,
1185 [ [
1186 'action' => 'subtract',
1187 'content' => 'line 2',
1188 'left-pos' => 2,
1189 'right-pos' => 2,
1190 ] ]
1191 ],
1192 [
1193 <<<TEXT
1194 line 1
1195 line 2
1196 line 3
1197 line 4
1198 TEXT
1199 , <<<TEXT
1200 line 1
1201 line 2
1202 line 2.5
1203 line 3
1204 line 4
1205 TEXT
1206 ,
1207 [ [
1208 'action' => 'add',
1209 'content' => 'line 2.5',
1210 'left-pos' => 3,
1211 'right-pos' => 3,
1212 ] ]
1213 ],
1214 [
1215 <<<TEXT
1216 line 1
1217 line 2
1218 line 3
1219 line 4
1220 TEXT
1221 , <<<TEXT
1222 line 1
1223 line b
1224 line 3
1225 line 4
1226 TEXT
1227 ,
1228 [ [
1229 'action' => 'change',
1230 'old_content' => 'line 2',
1231 'new_content' => 'line b',
1232 'left-pos' => 2,
1233 'right-pos' => 2,
1234 ] ]
1235 ],
1236 [
1237 <<<TEXT
1238 line 1
1239 line 2
1240 line 3
1241 line 4
1242 TEXT
1243 , <<<TEXT
1244 line 1
1245 line b
1246 line c
1247 line d
1248 line 3
1249 line 4
1250 TEXT
1251 ,
1252 [
1253 [
1254 'action' => 'change',
1255 'old_content' => 'line 2',
1256 'new_content' => 'line b',
1257 'left-pos' => 2,
1258 'right-pos' => 2,
1259 ],
1260 [
1261 'action' => 'add',
1262 'content' => 'line c
1263 line d',
1264 'left-pos' => 3,
1265 'right-pos' => 3,
1266 ],
1267 ],
1268 ],
1269 ];
1270 }
1271
1272 /** @dataProvider annotationDataProvider */
1273 public function testAnnotation( $message, $diff, $user, $expectedAnnotation ) {
1274 $this->setupTestUser( $user );
1275 $actual = DiscussionParser::interpretDiff( $diff, $user );
1276 $this->assertEquals( $expectedAnnotation, $actual, $message );
1277 }
1278
1279 public static function annotationDataProvider() {
1280 $ts = self::EXEMPLAR_TIMESTAMP;
1281
1282 return [
1283
1284 [
1285 'Must detect added comments',
1286 // Diff
1287 [
1288 // Action
1289 [
1290 'action' => 'add',
1291 'content' => ":What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts",
1292 'left-pos' => 3,
1293 'right-pos' => 3,
1294 ],
1295 '_info' => [
1296 'lhs' => [
1297 '== Section 1 ==',
1298 "I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts",
1299 ],
1300 'rhs' => [
1301 '== Section 1 ==',
1302 "I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts",
1303 ":What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts",
1304 ],
1305 ],
1306 ],
1307 // User
1308 'Werdna',
1309 // Expected annotation
1310 [
1311 [
1312 'type' => 'add-comment',
1313 'content' => ":What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts",
1314 'full-section' => <<<TEXT
1315 == Section 1 ==
1316 I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts
1317 :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts
1318 TEXT
1319 ],
1320 ],
1321 ],
1322
1323 [
1324 'Full Section must not include the following pre-existing section',
1325 // Diff
1326 [
1327 // Action
1328 [
1329 'action' => 'add',
1330 'content' => ":What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts",
1331 'left-pos' => 3,
1332 'right-pos' => 3,
1333 ],
1334 '_info' => [
1335 'lhs' => [
1336 '== Section 1 ==',
1337 "I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts",
1338 '== Section 2 ==',
1339 "Well well well. [[User:DarTar|DarTar]] ([[User talk:DarTar|talk]]) $ts",
1340 ],
1341 'rhs' => [
1342 '== Section 1 ==',
1343 "I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts",
1344 ":What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts",
1345 '== Section 2 ==',
1346 "Well well well. [[User:DarTar|DarTar]] ([[User talk:DarTar|talk]]) $ts",
1347 ],
1348 ],
1349 ],
1350 // User
1351 'Werdna',
1352 // Expected annotation
1353 [
1354 [
1355 'type' => 'add-comment',
1356 'content' => ":What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts",
1357 'full-section' => <<<TEXT
1358 == Section 1 ==
1359 I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts
1360 :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts
1361 TEXT
1362 ],
1363 ],
1364 ],
1365
1366 [
1367 'Must detect new-section-with-comment when a new section is added',
1368 // Diff
1369 [
1370 // Action
1371 [
1372 'action' => 'add',
1373 'content' => <<<TEXT
1374 == Section 1a ==
1375 Hmmm? [[User:Jdforrester|Jdforrester]] ([[User talk:Jdforrester|talk]]) $ts
1376 TEXT
1377 ,
1378 'left-pos' => 4,
1379 'right-pos' => 4,
1380 ],
1381 '_info' => [
1382 'lhs' => [
1383 '== Section 1 ==',
1384 "I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts",
1385 ":What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts",
1386 '== Section 2 ==',
1387 "Well well well. [[User:DarTar|DarTar]] ([[User talk:DarTar|talk]]) $ts",
1388 ],
1389 'rhs' => [
1390 '== Section 1 ==',
1391 "I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts",
1392 ":What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts",
1393 '== Section 1a ==',
1394 'Hmmm? [[User:Jdforrester|Jdforrester]] ([[User talk:Jdforrested|talk]]) $ts',
1395 '== Section 2 ==',
1396 "Well well well. [[User:DarTar|DarTar]] ([[User talk:DarTar|talk]]) $ts",
1397 ],
1398 ],
1399 ],
1400 // User
1401 'Jdforrester',
1402 // Expected annotation
1403 [
1404 [
1405 'type' => 'new-section-with-comment',
1406 'content' => <<<TEXT
1407 == Section 1a ==
1408 Hmmm? [[User:Jdforrester|Jdforrester]] ([[User talk:Jdforrester|talk]]) $ts
1409 TEXT
1410 ,
1411 ],
1412 ],
1413 ],
1414
1415 [
1416 'Must detect multiple added comments when multiple sections are edited',
1417 DiscussionParser::getMachineReadableDiff(
1418 <<<TEXT
1419 == Section 1 ==
1420 I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts
1421 :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts
1422 == Section 2 ==
1423 Well well well. [[User:DarTar|DarTar]] ([[User talk:DarTar|talk]]) $ts
1424 == Section 3 ==
1425 Hai [[User:Bsitu|Bsitu]] ([[User talk:Bsitu|talk]]) $ts
1426 TEXT
1427 ,
1428 <<<TEXT
1429 == Section 1 ==
1430 I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts
1431 :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts
1432 :New Comment [[User:JarJar|JarJar]] ([[User talk:JarJar|talk]]) $ts
1433 == Section 2 ==
1434 Well well well. [[User:DarTar|DarTar]] ([[User talk:DarTar|talk]]) $ts
1435 == Section 3 ==
1436 Hai [[User:Bsitu|Bsitu]] ([[User talk:Bsitu|talk]]) $ts
1437 :Other New Comment [[User:JarJar|JarJar]] ([[User talk:JarJar|talk]]) $ts
1438 TEXT
1439 ),
1440 // User
1441 'JarJar',
1442 // Expected annotation
1443 [
1444 [
1445 'type' => 'add-comment',
1446 'content' => ":New Comment [[User:JarJar|JarJar]] ([[User talk:JarJar|talk]]) $ts",
1447 'full-section' => <<<TEXT
1448 == Section 1 ==
1449 I do not like you. [[User:Jorm|Jorm]] ([[User talk:Jorm|talk]]) $ts
1450 :What do you think? [[User:Werdna|Werdna]] ([[User talk:Werdna|talk]]) $ts
1451 :New Comment [[User:JarJar|JarJar]] ([[User talk:JarJar|talk]]) $ts
1452 TEXT
1453 ],
1454 [
1455 'type' => 'add-comment',
1456 'content' => ":Other New Comment [[User:JarJar|JarJar]] ([[User talk:JarJar|talk]]) $ts",
1457 'full-section' => <<<TEXT
1458 == Section 3 ==
1459 Hai [[User:Bsitu|Bsitu]] ([[User talk:Bsitu|talk]]) $ts
1460 :Other New Comment [[User:JarJar|JarJar]] ([[User talk:JarJar|talk]]) $ts
1461 TEXT
1462 ],
1463 ],
1464 ],
1465
1466 [
1467 'Bug T78424',
1468 DiscussionParser::getMachineReadableDiff(
1469 <<<TEXT
1470 == Washington Post Reception Source ==
1471
1472 ''The Boston Post'' source that was used in the reception section has a couple of problems. First, it's actually a repost of ''The Washington Post'', but ''The Washington Post'' doesn't allow the Internet Archive to preserve it. Should it still be sourced to Boston or to Washington? Second, it seems to be a lot of analysis that can't be summed up easily without trimming it out, and doesn't really fit with the reception section and should probably moved next to Wilson's testimony. Any suggestions? --[[User:RAN1|RAN1]] ([[User talk:RAN1|talk]]) 01:44, 11 December 2014 (UTC)
1473 TEXT
1474 ,
1475 <<<TEXT
1476 == Washington Post Reception Source ==
1477
1478 ''The Boston Post'' source that was used in the reception section has a couple of problems. First, it's actually a repost of ''The Washington Post'', but ''The Washington Post'' doesn't allow the Internet Archive to preserve it. Should it still be sourced to Boston or to Washington? Second, it seems to be a lot of analysis that can't be summed up easily without trimming it out, and doesn't really fit with the reception section and should probably moved next to Wilson's testimony. Any suggestions? --[[User:RAN1|RAN1]] ([[User talk:RAN1|talk]]) 01:44, 11 December 2014 (UTC)
1479
1480 == Grand jury no bill reception ==
1481
1482 {{u|Bob K31416}} has started a process of summarizing that section, in a manner that I believe it to be counter productive. We have expert opinions from legal, law enforcement, politicians, and media outlets all of which are notable and informative. [[WP:NOTPAPER|Wikipedia is not paper]] – If the section is too long, the correct process to avoid losing good content that is well sources, is to create a sub-article with all the detail, and summarize here per [[WP:SUMMARY]]. But deleting useful and well sourced material, is not acceptable. We are here to build an encyclopedia. - [[User:Cwobeel|<span style="color:#339966">Cwobeel</span>]] [[User_talk:Cwobeel|<span style="font-size:80%">(talk)</span>]] 16:02, 11 December 2014 (UTC)
1483 TEXT
1484 ),
1485 // User
1486 'Cwobeel',
1487 // Expected annotation
1488 [
1489 [
1490 'type' => 'new-section-with-comment',
1491 'content' => '== Grand jury no bill reception ==
1492
1493 {{u|Bob K31416}} has started a process of summarizing that section, in a manner that I believe it to be counter productive. We have expert opinions from legal, law enforcement, politicians, and media outlets all of which are notable and informative. [[WP:NOTPAPER|Wikipedia is not paper]] – If the section is too long, the correct process to avoid losing good content that is well sources, is to create a sub-article with all the detail, and summarize here per [[WP:SUMMARY]]. But deleting useful and well sourced material, is not acceptable. We are here to build an encyclopedia. - [[User:Cwobeel|<span style="color:#339966">Cwobeel</span>]] [[User_talk:Cwobeel|<span style="font-size:80%">(talk)</span>]] 16:02, 11 December 2014 (UTC)',
1494 ],
1495 ],
1496 ],
1497 // when adding additional tests, make sure to add the non-anon users
1498 // to DiscussionParserTest::$testusers - the DiscussionParser
1499 // needs the users to exist, because it'll generate a comparison
1500 // signature, which is different when the user is considered anon
1501 ];
1502 }
1503
1504 public static function provider_detectSectionTitleAndText() {
1505 // See DiscussionParserTest::$testUsers
1506 $name = 'Werdna';
1507 $comment = self::signedMessage( $name );
1508
1509 return [
1510 [
1511 'Must detect first sub heading when inserting in the middle of two sub headings',
1512 // expected header content
1513 'Sub Heading 1',
1514 // test content format
1515 "
1516 == Heading ==
1517 $comment
1518
1519 == Sub Heading 1 ==
1520 $comment
1521 %s
1522
1523 == Sub Heading 2 ==
1524 $comment
1525 ",
1526 // user signing new comment
1527 $name
1528 ],
1529
1530 [
1531 'Must detect second sub heading when inserting in the end of two sub headings',
1532 // expected header content
1533 'Sub Heading 2',
1534 // test content format
1535 "
1536 == Heading ==
1537 $comment
1538
1539 == Sub Heading 1 ==
1540 $comment
1541
1542 == Sub Heading 2 ==
1543 $comment
1544 %s
1545 ",
1546 // user signing new comment
1547 $name
1548 ],
1549
1550 [
1551 'Commenting in multiple sub-headings must result in no section link',
1552 // expected header content
1553 '',
1554 // test content format
1555 "
1556 == Heading ==
1557 $comment
1558
1559 == Sub Heading 1 ==
1560 $comment
1561 %s
1562
1563 == Sub Heading 2 ==
1564 $comment
1565 %s
1566
1567 ",
1568 // user signing new comment
1569 $name
1570 ],
1571
1572 [
1573 'Must accept headings without a space between the = and the section name',
1574 // expected header content
1575 'Heading',
1576 // test content format
1577 "
1578 ==Heading==
1579 $comment
1580 %s
1581 ",
1582 // user signing new comment
1583 $name
1584 ],
1585
1586 [
1587 'Must not accept invalid headings split with a return',
1588 // expected header content
1589 '',
1590 // test content format
1591 "
1592 ==Some
1593 Heading==
1594 $comment
1595 %s
1596 ",
1597 // user signing new comment
1598 $name
1599 ],
1600 ];
1601 }
1602
1603 /**
1604 * @dataProvider provider_detectSectionTitleAndText
1605 */
1606 public function testDetectSectionTitleAndText( $message, $expect, $format, $name ) {
1607 $this->setupTestUser( $name );
1608
1609 // str_replace because we want to replace multiple instances of '%s' with the same value
1610 $before = str_replace( '%s', '', $format );
1611 $after = str_replace( '%s', self::signedMessage( $name ), $format );
1612
1613 $diff = DiscussionParser::getMachineReadableDiff( $before, $after );
1614 $interp = DiscussionParser::interpretDiff( $diff, $name );
1615
1616 // There should be a section-text only if there is section-title
1617 $expectText = $expect ? self::message( $name ) : '';
1618 $this->assertEquals(
1619 [ 'section-title' => $expect, 'section-text' => $expectText ],
1620 DiscussionParser::detectSectionTitleAndText( $interp ),
1621 $message
1622 );
1623 }
1624
1625 protected static function signedMessage( $name ) {
1626 return ": " . self::message() . " [[User:$name|$name]] ([[User talk:$name|talk]]) 00:17, 7 May 2013 (UTC)";
1627 }
1628
1629 protected static function message() {
1630 return 'foo';
1631 }
1632
1633 public static function provider_getFullSection() {
1634 $tests = [
1635 [
1636 'Extracts full section',
1637 // Full document content
1638 <<<TEXT
1639 ==Header 1==
1640 foo
1641 ===Header 2===
1642 bar
1643 ==Header 3==
1644 baz
1645 TEXT
1646 ,
1647 // Map of Line numbers to expanded section content
1648 [
1649 1 => "==Header 1==\nfoo",
1650 2 => "==Header 1==\nfoo",
1651 3 => "===Header 2===\nbar",
1652 4 => "===Header 2===\nbar",
1653 5 => "==Header 3==\nbaz",
1654 6 => "==Header 3==\nbaz",
1655 ],
1656 ],
1657 ];
1658
1659 // Allow for setting an array of line numbers to expand from rather than
1660 // just a single line number
1661 $retval = [];
1662 foreach ( $tests as $test ) {
1663 foreach ( $test[2] as $lineNum => $expected ) {
1664 $retval[] = [
1665 $test[0],
1666 $expected,
1667 $test[1],
1668 $lineNum,
1669 ];
1670 }
1671 }
1672
1673 return $retval;
1674 }
1675
1676 /**
1677 * @dataProvider provider_getFullSection
1678 */
1679 public function testGetFullSection( $message, $expect, $lines, $startLineNum ) {
1680 $section = DiscussionParser::getFullSection( explode( "\n", $lines ), $startLineNum );
1681 $this->assertEquals( $expect, $section, $message );
1682 }
1683
1684 public function testGetSectionCount() {
1685 $one = "==Zomg==\nfoobar\n";
1686 $two = "===SubZomg===\nHi there\n";
1687 $three = "==Header==\nOh Hai!\n";
1688
1689 $this->assertSame( 1, DiscussionParser::getSectionCount( $one ) );
1690 $this->assertSame( 2, DiscussionParser::getSectionCount( $one . $two ) );
1691 $this->assertSame( 2, DiscussionParser::getSectionCount( $one . $three ) );
1692 $this->assertSame( 3, DiscussionParser::getSectionCount( $one . $two . $three ) );
1693 $this->assertSame( 30, DiscussionParser::getSectionCount(
1694 file_get_contents( __DIR__ . '/revision_txt/637638133.txt' )
1695 ) );
1696 }
1697
1698 public function testGetOverallUserMentionsCount() {
1699 $userMentions = [
1700 'validMentions' => [ 1 => 1 ],
1701 'unknownUsers' => [ 'NotKnown1', 'NotKnown2' ],
1702 'anonymousUsers' => [ '127.0.0.1' ],
1703 ];
1704
1705 $discussionParser = TestingAccessWrapper::newFromClass( DiscussionParser::class );
1706 $this->assertSame( 4, $discussionParser->getOverallUserMentionsCount( $userMentions ) );
1707 }
1708
1709 public static function provider_getUserMentions() {
1710 return [
1711 [
1712 [ 'NotKnown1' => 0 ],
1713 [
1714 'validMentions' => [],
1715 'unknownUsers' => [ 'NotKnown1' ],
1716 'anonymousUsers' => [],
1717 ],
1718 1
1719 ],
1720 [
1721 [ '127.0.0.1' => 0 ],
1722 [
1723 'validMentions' => [],
1724 'unknownUsers' => [],
1725 'anonymousUsers' => [ '127.0.0.1' ],
1726 ],
1727 1
1728 ],
1729 ];
1730 }
1731
1732 /**
1733 * @dataProvider provider_getUserMentions
1734 */
1735 public function testGetUserMentions( $userLinks, $expectedUserMentions, $agent ) {
1736 $title = Title::newFromText( 'Test' );
1737 $discussionParser = TestingAccessWrapper::newFromClass( DiscussionParser::class );
1738 $this->assertEquals( $expectedUserMentions, $discussionParser->getUserMentions( $title, $agent, $userLinks ) );
1739 }
1740
1741 public function testGetUserMentions_validMention() {
1742 $userName = 'Admin';
1743 $this->setupTestUser( $userName );
1744 $userId = User::newFromName( $userName )->getId();
1745 $otherUserId = $userId + 1;
1746 $expectedUserMentions = [
1747 'validMentions' => [ $userId => $userId ],
1748 'unknownUsers' => [],
1749 'anonymousUsers' => [],
1750 ];
1751 $userLinks = [ $userName => $userId ];
1752 $this->testGetUserMentions( $userLinks, $expectedUserMentions, $otherUserId );
1753 }
1754
1755 public function testGetUserMentions_ownMention() {
1756 $userName = 'Admin';
1757 $this->setupTestUser( $userName );
1758 $userId = User::newFromName( 'Admin' )->getId();
1759 $expectedUserMentions = [
1760 'validMentions' => [],
1761 'unknownUsers' => [],
1762 'anonymousUsers' => [],
1763 ];
1764 $userLinks = [ $userName => $userId ];
1765 $this->testGetUserMentions( $userLinks, $expectedUserMentions, $userId );
1766 }
1767
1768 public function testGetUserMentions_tooManyMentions() {
1769 $userLinks = [
1770 'NotKnown1' => 0,
1771 'NotKnown2' => 0,
1772 'NotKnown3' => 0,
1773 '127.0.0.1' => 0,
1774 '127.0.0.2' => 0,
1775 ];
1776
1777 $this->overrideConfigValues( [
1778 // lower limit for the mention-too-many notification
1779 'EchoMaxMentionsCount' => 3
1780 ] );
1781
1782 $title = Title::newFromText( 'Test' );
1783 $discussionParser = TestingAccessWrapper::newFromClass( DiscussionParser::class );
1784 $this->assertSame( 4, $discussionParser->getOverallUserMentionsCount( $discussionParser->getUserMentions( $title, 1, $userLinks ) ) );
1785 }
1786
1787 protected function isParserFunctionsInstalled() {
1788 if ( ExtensionRegistry::getInstance()->isLoaded( 'ParserFunctions' ) ) {
1789 return true;
1790 } else {
1791 return "ParserFunctions not enabled";
1792 }
1793 }
1794
1795 public function testGetTextSnippet() {
1796 $lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' );
1797 $this->assertEquals(
1798 'Page001',
1799 DiscussionParser::getTextSnippet(
1800 '[[:{{BASEPAGENAME}}]]',
1801 $lang,
1802 DiscussionParser::DEFAULT_SNIPPET_LENGTH,
1803 Title::newFromText( 'Page001' )
1804 )
1805 );
1806 $this->assertEquals(
1807 'Hello',
1808 DiscussionParser::getTextSnippet(
1809 '* Hello',
1810 $lang,
1811 DiscussionParser::DEFAULT_SNIPPET_LENGTH,
1812 null,
1813 true
1814 )
1815 );
1816 $this->assertEquals(
1817 '* Hello',
1818 DiscussionParser::getTextSnippet(
1819 '* Hello',
1820 $lang,
1821 DiscussionParser::DEFAULT_SNIPPET_LENGTH,
1822 null,
1823 false
1824 )
1825 );
1826 }
1827 }