Debian Codegraph @

tests/phpunit/includes/logging/RightsLogFormatterTest.php

raw symbols SCIP
1 <?php
2
3 use Wikimedia\Rdbms\IDatabase;
4 use Wikimedia\Rdbms\LBFactory;
5
6 /**
7 * @covers \RightsLogFormatter
8 */
9 class RightsLogFormatterTest extends LogFormatterTestCase {
10
11 protected function setUp(): void {
12 parent::setUp();
13
14 $db = $this->createNoOpMock( IDatabase::class, [ 'getInfinity' ] );
15 $db->method( 'getInfinity' )->willReturn( 'infinity' );
16 $lbFactory = $this->createMock( LBFactory::class );
17 $lbFactory->method( 'getReplicaDatabase' )->willReturn( $db );
18 $this->setService( 'DBLoadBalancerFactory', $lbFactory );
19 }
20
21 /**
22 * Provide different rows from the logging table to test
23 * for backward compatibility.
24 * Do not change the existing data, just add a new database row
25 */
26 public static function provideRightsLogDatabaseRows() {
27 return [
28 // Current format
29 [
30 [
31 'type' => 'rights',
32 'action' => 'rights',
33 'comment' => 'rights comment',
34 'user' => 0,
35 'user_text' => 'Sysop',
36 'namespace' => NS_USER,
37 'title' => 'User',
38 'params' => [
39 '4::oldgroups' => [],
40 '5::newgroups' => [ 'sysop', 'bureaucrat' ],
41 'oldmetadata' => [],
42 'newmetadata' => [
43 [ 'expiry' => null ],
44 [ 'expiry' => '20160101123456' ]
45 ],
46 ],
47 ],
48 [
49 'text' => 'Sysop changed group membership for User from (none) to '
50 . 'bureaucrat (temporary, until 12:34, 1 January 2016) and administrator',
51 'api' => [
52 'oldgroups' => [],
53 'newgroups' => [ 'sysop', 'bureaucrat' ],
54 'oldmetadata' => [],
55 'newmetadata' => [
56 [ 'group' => 'sysop', 'expiry' => 'infinity' ],
57 [ 'group' => 'bureaucrat', 'expiry' => '2016-01-01T12:34:56Z' ],
58 ],
59 ],
60 ],
61 ],
62
63 // Previous format (oldgroups and newgroups as arrays, no metadata)
64 [
65 [
66 'type' => 'rights',
67 'action' => 'rights',
68 'comment' => 'rights comment',
69 'user' => 0,
70 'user_text' => 'Sysop',
71 'namespace' => NS_USER,
72 'title' => 'User',
73 'params' => [
74 '4::oldgroups' => [],
75 '5::newgroups' => [ 'sysop', 'bureaucrat' ],
76 ],
77 ],
78 [
79 'text' => 'Sysop changed group membership for User from (none) to '
80 . 'administrator and bureaucrat',
81 'api' => [
82 'oldgroups' => [],
83 'newgroups' => [ 'sysop', 'bureaucrat' ],
84 'oldmetadata' => [],
85 'newmetadata' => [
86 [ 'group' => 'sysop', 'expiry' => 'infinity' ],
87 [ 'group' => 'bureaucrat', 'expiry' => 'infinity' ],
88 ],
89 ],
90 ],
91 ],
92
93 // Legacy format (oldgroups and newgroups as numeric-keyed strings)
94 [
95 [
96 'type' => 'rights',
97 'action' => 'rights',
98 'comment' => 'rights comment',
99 'user' => 0,
100 'user_text' => 'Sysop',
101 'namespace' => NS_USER,
102 'title' => 'User',
103 'params' => [
104 '',
105 'sysop, bureaucrat',
106 ],
107 ],
108 [
109 'legacy' => true,
110 'text' => 'Sysop changed group membership for User from (none) to '
111 . 'administrator and bureaucrat',
112 'api' => [
113 'oldgroups' => [],
114 'newgroups' => [ 'sysop', 'bureaucrat' ],
115 'oldmetadata' => [],
116 'newmetadata' => [
117 [ 'group' => 'sysop', 'expiry' => 'infinity' ],
118 [ 'group' => 'bureaucrat', 'expiry' => 'infinity' ],
119 ],
120 ],
121 ],
122 ],
123
124 // Really old entry
125 [
126 [
127 'type' => 'rights',
128 'action' => 'rights',
129 'comment' => 'rights comment',
130 'user' => 0,
131 'user_text' => 'Sysop',
132 'namespace' => NS_USER,
133 'title' => 'User',
134 'params' => [],
135 ],
136 [
137 'legacy' => true,
138 'text' => 'Sysop changed group membership for User',
139 'api' => [],
140 ],
141 ],
142 ];
143 }
144
145 /**
146 * @dataProvider provideRightsLogDatabaseRows
147 */
148 public function testRightsLogDatabaseRows( $row, $extra ) {
149 $this->doTestLogFormatter( $row, $extra );
150 }
151
152 /**
153 * Provide different rows from the logging table to test
154 * for backward compatibility.
155 * Do not change the existing data, just add a new database row
156 */
157 public static function provideAutopromoteLogDatabaseRows() {
158 return [
159 // Current format
160 [
161 [
162 'type' => 'rights',
163 'action' => 'autopromote',
164 'comment' => 'rights comment',
165 'user' => 0,
166 'user_text' => 'Sysop',
167 'namespace' => NS_USER,
168 'title' => 'Sysop',
169 'params' => [
170 '4::oldgroups' => [ 'sysop' ],
171 '5::newgroups' => [ 'sysop', 'bureaucrat' ],
172 ],
173 ],
174 [
175 'text' => 'Sysop was automatically promoted from administrator to '
176 . 'administrator and bureaucrat',
177 'api' => [
178 'oldgroups' => [ 'sysop' ],
179 'newgroups' => [ 'sysop', 'bureaucrat' ],
180 'oldmetadata' => [
181 [ 'group' => 'sysop', 'expiry' => 'infinity' ],
182 ],
183 'newmetadata' => [
184 [ 'group' => 'sysop', 'expiry' => 'infinity' ],
185 [ 'group' => 'bureaucrat', 'expiry' => 'infinity' ],
186 ],
187 ],
188 ],
189 ],
190
191 // Legacy format
192 [
193 [
194 'type' => 'rights',
195 'action' => 'autopromote',
196 'comment' => 'rights comment',
197 'user' => 0,
198 'user_text' => 'Sysop',
199 'namespace' => NS_USER,
200 'title' => 'Sysop',
201 'params' => [
202 'sysop',
203 'sysop, bureaucrat',
204 ],
205 ],
206 [
207 'legacy' => true,
208 'text' => 'Sysop was automatically promoted from administrator to '
209 . 'administrator and bureaucrat',
210 'api' => [
211 'oldgroups' => [ 'sysop' ],
212 'newgroups' => [ 'sysop', 'bureaucrat' ],
213 'oldmetadata' => [
214 [ 'group' => 'sysop', 'expiry' => 'infinity' ],
215 ],
216 'newmetadata' => [
217 [ 'group' => 'sysop', 'expiry' => 'infinity' ],
218 [ 'group' => 'bureaucrat', 'expiry' => 'infinity' ],
219 ],
220 ],
221 ],
222 ],
223 ];
224 }
225
226 /**
227 * @dataProvider provideAutopromoteLogDatabaseRows
228 */
229 public function testAutopromoteLogDatabaseRows( $row, $extra ) {
230 $this->doTestLogFormatter( $row, $extra );
231 }
232 }