Debian Codegraph @

extensions/DiscussionTools/tests/phpunit/EventDispatcherTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\DiscussionTools\Tests;
4
5 use DateTimeImmutable;
6 use MediaWiki\Language\RawMessage;
7 use MediaWiki\Page\PageIdentityValue;
8 use MediaWiki\Revision\MutableRevisionRecord;
9 use MediaWiki\User\UserIdentityValue;
10
11 /**
12 * @group DiscussionTools
13 * @group Database
14 * @covers \MediaWiki\Extension\DiscussionTools\Notifications\EventDispatcher
15 */
16 class EventDispatcherTest extends IntegrationTestCase {
17
18 /**
19 * @dataProvider provideGenerateCases
20 */
21 public function testGenerateEventsFromParsers(
22 string $rev1, string $rev2, string $authorUsername, ?string $other, string $expected
23 ): void {
24 $wikitext1 = static::getText( $rev1 );
25 $wikitext2 = static::getText( $rev2 );
26 $expectedEvents = static::getJson( $expected, false );
27 $config = static::getJson( "../data/enwiki-config.json" );
28 $data = static::getJson( "../data/enwiki-data.json" );
29
30 $dom1 = ( new RawMessage( $wikitext1 ) )->parse();
31 $doc1 = static::createDocument( $dom1 );
32 $container1 = static::getThreadContainer( $doc1 );
33
34 $dom2 = ( new RawMessage( $wikitext2 ) )->parse();
35 $doc2 = static::createDocument( $dom2 );
36 $container2 = static::getThreadContainer( $doc2 );
37
38 $dummyTitle = $this->createTitleParser( $config )->parseTitle( 'Dummy' );
39 $parser = $this->createParser( $config, $data );
40 $itemSet1 = $parser->parse( $container1, $dummyTitle );
41 $itemSet2 = $parser->parse( $container2, $dummyTitle );
42
43 $events = $other ? static::getJson( $other, true ) : [];
44
45 $fakeUser = new UserIdentityValue( 0, $authorUsername );
46 $fakeTitle = new PageIdentityValue( 0, NS_TALK, __CLASS__, PageIdentityValue::LOCAL );
47 $fakeRevRecord = new MutableRevisionRecord( $fakeTitle );
48 // All mock comments are posted between 00:00 and 00:10 on 2020-01-01
49 $fakeRevRecord->setTimestamp( ( new DateTimeImmutable( '2020-01-01T00:10Z' ) )->format( 'c' ) );
50 MockEventDispatcher::generateEventsFromItemSets(
51 $events, $itemSet1, $itemSet2, $fakeRevRecord, $fakeTitle, $fakeUser
52 );
53
54 foreach ( $events as &$event ) {
55 $event = json_decode( json_encode( $event ), false );
56 }
57
58 // Optionally write updated content to the JSON files
59 if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
60 static::overwriteJsonFile( $expected, $events );
61 }
62
63 static::assertEquals( $expectedEvents, $events );
64
65 // Assert that no "new comment" events are generated for comments saved >10 minutes after their timestamps
66 $events = $other ? static::getJson( $other, true ) : [];
67 $fakeRevRecord->setTimestamp( ( new DateTimeImmutable( '2020-01-01T00:20Z' ) )->format( 'c' ) );
68 MockEventDispatcher::generateEventsFromItemSets(
69 $events, $itemSet1, $itemSet2, $fakeRevRecord, $fakeTitle, $fakeUser
70 );
71
72 foreach ( $events as &$event ) {
73 $event = json_decode( json_encode( $event ), false );
74 }
75
76 $events = array_filter( $events, static function ( $event ) {
77 return $event->type === 'dt-subscribed-new-comment';
78 } );
79
80 static::assertEquals( [], $events );
81 }
82
83 public static function provideGenerateCases(): array {
84 return [
85 // Several simple edits adding replies by different users.
86 [
87 'cases/EventDispatcher/simple/rev1.txt',
88 'cases/EventDispatcher/simple/rev2.txt',
89 'Z',
90 null,
91 '../cases/EventDispatcher/simple/rev2.json',
92 ],
93 [
94 'cases/EventDispatcher/simple/rev2.txt',
95 'cases/EventDispatcher/simple/rev3.txt',
96 'Z',
97 null,
98 '../cases/EventDispatcher/simple/rev3.json',
99 ],
100 [
101 'cases/EventDispatcher/simple/rev3.txt',
102 'cases/EventDispatcher/simple/rev4.txt',
103 'Y',
104 null,
105 '../cases/EventDispatcher/simple/rev4.json',
106 ],
107 [
108 'cases/EventDispatcher/simple/rev4.txt',
109 'cases/EventDispatcher/simple/rev5.txt',
110 'X',
111 null,
112 '../cases/EventDispatcher/simple/rev5.json',
113 ],
114 // Adding a new section with heading and a top-level comment.
115 [
116 'cases/EventDispatcher/newsection/rev1.txt',
117 'cases/EventDispatcher/newsection/rev2.txt',
118 'Z',
119 null,
120 '../cases/EventDispatcher/newsection/rev2.json',
121 ],
122 // Adding multiple replies in one edit.
123 [
124 'cases/EventDispatcher/multiple/rev1.txt',
125 'cases/EventDispatcher/multiple/rev2.txt',
126 'Z',
127 null,
128 '../cases/EventDispatcher/multiple/rev2.json',
129 ],
130 // Adding comments in section 0 (before first heading).
131 // These do not generate notifications, because the interface doesn't allow subscribing to it.
132 [
133 'cases/EventDispatcher/section0/rev1.txt',
134 'cases/EventDispatcher/section0/rev2.txt',
135 'X',
136 null,
137 '../cases/EventDispatcher/section0/rev2.json',
138 ],
139 [
140 'cases/EventDispatcher/section0/rev2.txt',
141 'cases/EventDispatcher/section0/rev3.txt',
142 'Y',
143 null,
144 '../cases/EventDispatcher/section0/rev3.json',
145 ],
146 // Adding comments in section starting with a heading with level 1.
147 // These do not generate notifications, because the interface doesn't allow subscribing to it.
148 [
149 'cases/EventDispatcher/sectionlevel1/rev1.txt',
150 'cases/EventDispatcher/sectionlevel1/rev2.txt',
151 'Y',
152 null,
153 '../cases/EventDispatcher/sectionlevel1/rev2.json',
154 ],
155 [
156 'cases/EventDispatcher/sectionlevel1/rev2.txt',
157 'cases/EventDispatcher/sectionlevel1/rev3.txt',
158 'Z',
159 null,
160 '../cases/EventDispatcher/sectionlevel1/rev3.json',
161 ],
162 [
163 'cases/EventDispatcher/sectionlevel1/rev3.txt',
164 'cases/EventDispatcher/sectionlevel1/rev4.txt',
165 'Z',
166 null,
167 '../cases/EventDispatcher/sectionlevel1/rev4.json',
168 ],
169 // Adding comments in section starting with a heading with level 3 (not following level 2 headings).
170 // These do not generate notifications, because the interface doesn't allow subscribing to it.
171 [
172 'cases/EventDispatcher/sectionlevel3/rev1.txt',
173 'cases/EventDispatcher/sectionlevel3/rev2.txt',
174 'Y',
175 null,
176 '../cases/EventDispatcher/sectionlevel3/rev2.json',
177 ],
178 // Adding a comment in a previously empty section.
179 [
180 'cases/EventDispatcher/emptysection/rev1.txt',
181 'cases/EventDispatcher/emptysection/rev2.txt',
182 'Y',
183 null,
184 '../cases/EventDispatcher/emptysection/rev2.json',
185 ],
186 // Adding comments in sub-sections, where the parent section has no comments (except in
187 // sub-sections). They generate notifications now (since T298617), previously they didn't.
188 [
189 'cases/EventDispatcher/subsection-empty/rev1.txt',
190 'cases/EventDispatcher/subsection-empty/rev2.txt',
191 'Z',
192 null,
193 '../cases/EventDispatcher/subsection-empty/rev2.json',
194 ],
195 [
196 'cases/EventDispatcher/subsection-empty/rev2.txt',
197 'cases/EventDispatcher/subsection-empty/rev3.txt',
198 'Z',
199 null,
200 '../cases/EventDispatcher/subsection-empty/rev3.json',
201 ],
202 // Adding comments in sub-sections, where the parent section also has comments.
203 [
204 'cases/EventDispatcher/subsection/rev1.txt',
205 'cases/EventDispatcher/subsection/rev2.txt',
206 'Z',
207 null,
208 '../cases/EventDispatcher/subsection/rev2.json',
209 ],
210 [
211 'cases/EventDispatcher/subsection/rev2.txt',
212 'cases/EventDispatcher/subsection/rev3.txt',
213 'Z',
214 null,
215 '../cases/EventDispatcher/subsection/rev3.json',
216 ],
217 [
218 'cases/EventDispatcher/subsection/rev3.txt',
219 'cases/EventDispatcher/subsection/rev4.txt',
220 'Z',
221 null,
222 '../cases/EventDispatcher/subsection/rev4.json',
223 ],
224 // Edits that do not add comments, and do not generate notifications.
225 [
226 // Copying a discussion from another page (note the author of revision)
227 'cases/EventDispatcher/notcomments/rev1.txt',
228 'cases/EventDispatcher/notcomments/rev2.txt',
229 'Z',
230 null,
231 '../cases/EventDispatcher/notcomments/rev2.json',
232 ],
233 [
234 // Editing a comment
235 'cases/EventDispatcher/notcomments/rev2.txt',
236 'cases/EventDispatcher/notcomments/rev3.txt',
237 'X',
238 null,
239 '../cases/EventDispatcher/notcomments/rev3.json',
240 ],
241 [
242 // Editing page intro section
243 'cases/EventDispatcher/notcomments/rev3.txt',
244 'cases/EventDispatcher/notcomments/rev4.txt',
245 'X',
246 null,
247 '../cases/EventDispatcher/notcomments/rev4.json',
248 ],
249 // Multiple edits within a minute adding comments by the same user.
250 // See T285528#7177220 for more detail about each case.
251 [
252 'cases/EventDispatcher/sametime/rev1.txt',
253 'cases/EventDispatcher/sametime/rev2.txt',
254 'Z',
255 null,
256 '../cases/EventDispatcher/sametime/rev2.json',
257 ],
258 [
259 'cases/EventDispatcher/sametime/rev2.txt',
260 'cases/EventDispatcher/sametime/rev3-case1.txt',
261 'Z',
262 null,
263 '../cases/EventDispatcher/sametime/rev3-case1.json',
264 ],
265 [
266 'cases/EventDispatcher/sametime/rev2.txt',
267 'cases/EventDispatcher/sametime/rev3-case2.txt',
268 'Z',
269 null,
270 '../cases/EventDispatcher/sametime/rev3-case2.json',
271 ],
272 [
273 'cases/EventDispatcher/sametime/rev2.txt',
274 'cases/EventDispatcher/sametime/rev3-case3.txt',
275 'Z',
276 null,
277 '../cases/EventDispatcher/sametime/rev3-case3.json',
278 ],
279 [
280 'cases/EventDispatcher/sametime/rev2.txt',
281 'cases/EventDispatcher/sametime/rev3-case4.txt',
282 'Z',
283 null,
284 '../cases/EventDispatcher/sametime/rev3-case4.json',
285 ],
286 [
287 'cases/EventDispatcher/sametime/rev2.txt',
288 'cases/EventDispatcher/sametime/rev3-case5.txt',
289 'Z',
290 null,
291 '../cases/EventDispatcher/sametime/rev3-case5.json',
292 ],
293 [
294 'cases/EventDispatcher/sametime/rev1b.txt',
295 'cases/EventDispatcher/sametime/rev2b.txt',
296 'Z',
297 null,
298 '../cases/EventDispatcher/sametime/rev2b.json',
299 ],
300 [
301 'cases/EventDispatcher/sametime/rev2b.txt',
302 'cases/EventDispatcher/sametime/rev3b-case6.txt',
303 'Z',
304 null,
305 '../cases/EventDispatcher/sametime/rev3b-case6.json',
306 ],
307 [
308 'cases/EventDispatcher/sametime/rev2b.txt',
309 'cases/EventDispatcher/sametime/rev3b-case7.txt',
310 'Z',
311 null,
312 '../cases/EventDispatcher/sametime/rev3b-case7.json',
313 ],
314 // Removing topics
315 [
316 // Warning if section is deleted
317 'cases/EventDispatcher/removing-topics/rev1.txt',
318 'cases/EventDispatcher/removing-topics/rev2a.txt',
319 'Z',
320 null,
321 '../cases/EventDispatcher/removing-topics/rev2a.json',
322 ],
323 [
324 // No warning if section is moved
325 'cases/EventDispatcher/removing-topics/rev1.txt',
326 'cases/EventDispatcher/removing-topics/rev2b.txt',
327 'Z',
328 null,
329 '../cases/EventDispatcher/removing-topics/rev2b.json',
330 ],
331 [
332 // Warning if section becomes a subsection
333 'cases/EventDispatcher/removing-topics/rev1.txt',
334 'cases/EventDispatcher/removing-topics/rev2c.txt',
335 'Z',
336 null,
337 '../cases/EventDispatcher/removing-topics/rev2c.json',
338 ],
339 [
340 // Correct warning if section is deleted in case of duplicates (A)
341 'cases/EventDispatcher/removing-topics-same/rev1.txt',
342 'cases/EventDispatcher/removing-topics-same/rev2a.txt',
343 'Z',
344 null,
345 '../cases/EventDispatcher/removing-topics-same/rev2a.json',
346 ],
347 [
348 // Correct warning if section is deleted in case of duplicates (B)
349 'cases/EventDispatcher/removing-topics-same/rev1.txt',
350 'cases/EventDispatcher/removing-topics-same/rev2b.txt',
351 'Z',
352 null,
353 '../cases/EventDispatcher/removing-topics-same/rev2b.json',
354 ],
355 ];
356 }
357
358 }