Debian Codegraph @

extensions/Echo/tests/phpunit/maintenance/SuppressionMaintenanceTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Test\Maintenance;
4
5 use MediaWiki\Extension\Notifications\SuppressionRowUpdateGenerator;
6 use MediaWiki\Title\Title;
7 use MediaWikiIntegrationTestCase;
8 use PHPUnit\Framework\TestCase;
9
10 /**
11 * @group Echo
12 * @covers \MediaWiki\Extension\Notifications\SuppressionRowUpdateGenerator
13 * @group Database
14 */
15 class SuppressionMaintenanceTest extends MediaWikiIntegrationTestCase {
16
17 public static function provider_updateRow() {
18 $input = [
19 'event_id' => 2,
20 'event_type' => 'mention',
21 'event_variant' => null,
22 'event_agent_id' => 3,
23 'event_agent_ip' => null,
24 'event_page_title' => '',
25 'event_page_namespace' => 0,
26 'event_page_extra' => null,
27 'event_extra' => null,
28 'event_page_id' => null,
29 ];
30
31 return [
32 [ 'Unrelated row must result in no update', [], $input ],
33
34 [
35 'Page title and namespace for non-existant page must move into event_extra',
36 [
37 // expected update
38 'event_extra' => serialize( [
39 'page_title' => 'Yabba Dabba Do',
40 'page_namespace' => NS_MAIN
41 ] ),
42 ],
43 [
44 // input row
45 'event_page_title' => 'Yabba Dabba Do',
46 'event_page_namespace' => NS_MAIN,
47 ] + $input,
48 ],
49
50 [
51 'Page title and namespace for existing page must be result in update to event_page_id',
52 [
53 // expected update
54 'event_page_id' => 42,
55 ],
56 [
57 // input row
58 'event_page_title' => 'Mount Rushmore',
59 'event_page_namespace' => NS_MAIN,
60 ] + $input,
61 self::attachTitleFor( 42, 'Mount Rushmore', NS_MAIN )
62 ],
63
64 [
65 'When updating non-existent page must keep old extra data',
66 [
67 // expected update
68 'event_extra' => serialize( [
69 'foo' => 'bar',
70 'page_title' => 'Yabba Dabba Do',
71 'page_namespace' => NS_MAIN
72 ] ),
73 ],
74 [
75 // input row
76 'event_page_title' => 'Yabba Dabba Do',
77 'event_page_namespace' => NS_MAIN,
78 'event_extra' => serialize( [ 'foo' => 'bar' ] ),
79 ] + $input,
80 ],
81
82 [
83 'Must update link-from-title/namespace to link-from-page-id for page-linked events',
84 [
85 // expected update
86 'event_extra' => serialize( [ 'link-from-page-id' => 99 ] ),
87 ],
88 [
89 // input row
90 'event_type' => 'page-linked',
91 'event_extra' => serialize( [
92 'link-from-title' => 'Horse',
93 'link-from-namespace' => NS_USER_TALK
94 ] ),
95 ] + $input,
96 self::attachTitleFor( 99, 'Horse', NS_USER_TALK )
97 ],
98
99 [
100 'Must perform both generic update and page-linked update at same time',
101 [
102 // expected update
103 'event_extra' => serialize( [ 'link-from-page-id' => 8675309 ] ),
104 'event_page_id' => 8675309,
105 ],
106 [
107 // input row
108 'event_type' => 'page-linked',
109 'event_extra' => serialize( [
110 'link-from-title' => 'Jenny',
111 'link-from-namespace' => NS_MAIN,
112 ] ),
113 'event_page_title' => 'Jenny',
114 'event_page_namespace' => NS_MAIN,
115 ] + $input,
116 self::attachTitleFor( 8675309, 'Jenny', NS_MAIN ),
117 ],
118 ];
119 }
120
121 protected static function attachTitleFor( $id, $providedText, $providedNamespace ) {
122 return static function (
123 TestCase $test,
124 SuppressionRowUpdateGenerator $gen
125 ) use ( $id, $providedText, $providedNamespace ) {
126 $title = $test->createMock( Title::class );
127 $title->expects( $test->any() )
128 ->method( 'getArticleId' )
129 ->willReturn( $id );
130
131 $titles = [ $providedNamespace => [ $providedText => $title ] ];
132
133 $gen->setNewTitleFromNsAndText( static function ( $namespace, $text ) use ( $titles ) {
134 return $titles[$namespace][$text] ?? Title::makeTitleSafe( $namespace, $text );
135 } );
136 };
137 }
138
139 /**
140 * @dataProvider provider_updateRow
141 */
142 public function testUpdateRow( $message, array $expected, array $input, ?callable $callable = null ) {
143 $gen = new SuppressionRowUpdateGenerator;
144 if ( $callable ) {
145 call_user_func( $callable, $this, $gen );
146 }
147 $update = $gen->update( (object)$input );
148 $this->assertEquals( $expected, $update, $message );
149 }
150 }