Debian Codegraph @

extensions/DiscussionTools/tests/phpunit/CommentUtilsTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\DiscussionTools\Tests;
4
5 use MediaWiki\Config\HashConfig;
6 use MediaWiki\Extension\DiscussionTools\CommentUtils;
7 use MediaWiki\MainConfigNames;
8
9 /**
10 * @group DiscussionTools
11 * @covers \MediaWiki\Extension\DiscussionTools\CommentUtils
12 */
13 class CommentUtilsTest extends IntegrationTestCase {
14 /**
15 * @dataProvider provideIsSingleCommentSignedBy
16 */
17 public function testIsSingleCommentSignedBy(
18 string $msg, string $title, string $username, string $html, bool $expected
19 ) {
20 $doc = static::createDocument( $html );
21 $container = static::getThreadContainer( $doc );
22
23 $config = static::getJson( "../data/enwiki-config.json" );
24 $data = static::getJson( "../data/enwiki-data.json" );
25 $title = $this->createTitleParser( $config )->parseTitle( $title );
26 $parser = $this->createParser( $config, $data );
27
28 $threadItemSet = $parser->parse( $container, $title );
29 $isSigned = CommentUtils::isSingleCommentSignedBy( $threadItemSet, $username, $container );
30 static::assertEquals( $expected, $isSigned, $msg );
31 }
32
33 public static function provideIsSingleCommentSignedBy(): array {
34 return static::getJson( '../cases/isSingleCommentSignedBy.json' );
35 }
36
37 /**
38 * @covers \MediaWiki\Extension\DiscussionTools\CommentUtils::getTitleFromUrl
39 * @dataProvider provideGetTitleFromUrl_ShortUrl
40 * @dataProvider provideGetTitleFromUrl_Decoding
41 * @dataProvider provideGetTitleFromUrl_ConfusingShortUrl
42 * @dataProvider provideGetTitleFromUrl_NoShortUrl
43 */
44 public function testGetTitleFromUrl( $expected, $input, $config ) {
45 static::assertEquals(
46 $expected,
47 CommentUtils::getTitleFromUrl( $input, $config )
48 );
49 }
50
51 public static function provideGetTitleFromUrl_Decoding() {
52 // Standard short URL configuration like on Wikimedia wikis
53 $config = new HashConfig( [ MainConfigNames::ArticlePath => '/wiki/$1' ] );
54
55 // In URL paths, non-percent-encoded `+` represents itself
56 yield [ 'A+B', '/wiki/A+B', $config ];
57 yield [ 'A B', '/wiki/A B', $config ];
58 yield [ 'A+B', '/wiki/A%2BB', $config ];
59 yield [ 'A B', '/wiki/A%20B', $config ];
60
61 // In URL query parameters, non-percent-encoded `+` represents ` `
62 yield [ 'A B', '/w/index.php?title=A+B', $config ];
63 yield [ 'A B', '/w/index.php?title=A B', $config ];
64 yield [ 'A+B', '/w/index.php?title=A%2BB', $config ];
65 yield [ 'A B', '/w/index.php?title=A%20B', $config ];
66 }
67
68 public static function provideGetTitleFromUrl_ShortUrl() {
69 // Standard short URL configuration like on Wikimedia wikis
70 $config = new HashConfig( [ MainConfigNames::ArticlePath => '/wiki/$1' ] );
71
72 // These should never occur in documents generated by either wikitext parser
73 yield 'ShortUrl-null-string' => [ null, 'Foo', $config ];
74 yield 'ShortUrl-null-path' => [ null, 'path/Foo', $config ];
75 yield 'ShortUrl-null-wiki-path' => [ null, 'wiki/Foo', $config ];
76
77 // Legacy wikitext parser
78 yield 'ShortUrl-simple-path' => [ 'Foo', '/wiki/Foo', $config ];
79 yield 'ShortUrl-simple-cgi' => [ 'Foo', '/w/index.php?title=Foo', $config ];
80 yield 'ShortUrl-viewing-path' => [ 'Foo', '/wiki/Foo?action=view', $config ];
81 yield 'ShortUrl-viewing-cgi' => [ 'Foo', '/w/index.php?title=Foo&action=view', $config ];
82 yield 'ShortUrl-editing-path' => [ 'Foo', '/wiki/Foo?action=edit', $config ];
83 yield 'ShortUrl-editing-cgi' => [ 'Foo', '/w/index.php?title=Foo&action=edit', $config ];
84 yield 'ShortUrl-repeated question-mark' => [ 'Foo', '/wiki/Foo?Gosh?This?Path?Is?Bad', $config ];
85
86 // Parsoid parser
87 yield 'ShortUrl-parsoid-simple-path' => [ 'Foo', './Foo', $config ];
88 yield 'ShortUrl-parsoid-viewing-path' => [ 'Foo', './Foo?action=view', $config ];
89 yield 'ShortUrl-parsoid-editing-path' => [ 'Foo', './Foo?action=edit', $config ];
90
91 // External link (matches regardless of domain - this may be unexpected)
92 yield 'ShortUrl-external-path1' => [ 'Foo', 'http://example.com/wiki/Foo', $config ];
93 yield 'ShortUrl-external-path2' => [ 'Foo', 'http://example.org/wiki/Foo', $config ];
94 yield 'ShortUrl-external-cgi1' => [ 'Foo', 'http://example.com/w/index.php?title=Foo', $config ];
95 yield 'ShortUrl-external-cgi2' => [ 'Foo', 'http://example.org/w/index.php?title=Foo', $config ];
96 yield 'ShortUrl-external-null' => [ null, 'http://example.net/Foo', $config ];
97 }
98
99 public static function provideGetTitleFromUrl_ConfusingShortUrl() {
100 // Super short URL that is confusing for the software but people use it anyway
101 $config = new HashConfig( [ MainConfigNames::ArticlePath => '/$1' ] );
102
103 // These should never occur in documents generated by either wikitext parser
104 yield 'ConfusingShortUrl-null-string' => [ null, 'Foo', $config ];
105 yield 'ConfusingShortUrl-null-path' => [ null, 'path/Foo', $config ];
106 yield 'ConfusingShortUrl-null-wiki-path' => [ null, 'wiki/Foo', $config ];
107
108 // Legacy wikitext parser
109 yield 'ConfusingShortUrl-simple-path' => [ 'Foo', '/Foo', $config ];
110 yield 'ConfusingShortUrl-simple-cgi' => [ 'Foo', '/index.php?title=Foo', $config ];
111 yield 'ConfusingShortUrl-viewing-path' => [ 'Foo', '/Foo?action=view', $config ];
112 yield 'ConfusingShortUrl-viewing-cgi' => [ 'Foo', '/index.php?title=Foo&action=view', $config ];
113 yield 'ConfusingShortUrl-editing-path' => [ 'Foo', '/Foo?action=edit', $config ];
114 yield 'ConfusingShortUrl-editing-cgi' => [ 'Foo', '/index.php?title=Foo&action=edit', $config ];
115 yield 'ConfusingShortUrl-repeated question-mark' => [ 'Foo', '/Foo?Gosh?This?Path?Is?Bad', $config ];
116
117 // Parsoid parser
118 yield 'ConfusingShortUrl-parsoid-simple-path' => [ 'Foo', './Foo', $config ];
119 yield 'ConfusingShortUrl-parsoid-viewing-path' => [ 'Foo', './Foo?action=view', $config ];
120 yield 'ConfusingShortUrl-parsoid-editing-path' => [ 'Foo', './Foo?action=edit', $config ];
121
122 // External link (matches regardless of domain - this may be unexpected)
123 yield 'ShortUrl-external-path1' => [ 'Foo', 'http://example.com/Foo', $config ];
124 yield 'ShortUrl-external-path2' => [ 'Foo', 'http://example.org/Foo', $config ];
125 yield 'ShortUrl-external-cgi1' => [ 'Foo', 'http://example.com/index.php?title=Foo', $config ];
126 yield 'ShortUrl-external-cgi2' => [ 'Foo', 'http://example.org/index.php?title=Foo', $config ];
127 }
128
129 public static function provideGetTitleFromUrl_NoShortUrl() {
130 // No short URL configuration
131 $config = new HashConfig( [ MainConfigNames::ArticlePath => '/wiki/index.php?title=$1' ] );
132
133 // These should never occur in documents generated by either wikitext parser
134 yield 'NoShortUrl-null-string' => [ null, 'Foo', $config ];
135 yield 'NoShortUrl-null-path' => [ null, 'path/Foo', $config ];
136 yield 'NoShortUrl-null-wiki-path' => [ null, 'wiki/Foo', $config ];
137
138 // Legacy wikitext parser
139 yield 'NoShortUrl-simple-path' => [ 'Foo', '/wiki/index.php?title=Foo', $config ];
140 yield 'NoShortUrl-viewing-path' => [ 'Foo', '/wiki/index.php?title=Foo&action=view', $config ];
141 yield 'NoShortUrl-editing-path' => [ 'Foo', '/wiki/index.php?title=Foo&action=edit', $config ];
142
143 // Parsoid parser
144 yield 'NoShortUrl-parsoid-simple-path' => [ 'Foo', './index.php?title=Foo', $config ];
145 yield 'NoShortUrl-parsoid-viewing-path' => [ 'Foo', './index.php?title=Foo&action=view', $config ];
146 yield 'NoShortUrl-parsoid-editing-path' => [ 'Foo', './index.php?title=Foo&action=edit', $config ];
147
148 // External link (matches regardless of domain - this may be unexpected)
149 yield 'ShortUrl-external-cgi1' => [ 'Foo', 'http://example.com/wiki/index.php?title=Foo', $config ];
150 yield 'ShortUrl-external-cgi2' => [ 'Foo', 'http://example.org/wiki/index.php?title=Foo', $config ];
151 yield 'ShortUrl-external-null' => [ null, 'http://example.net/Foo', $config ];
152 }
153 }