Debian Codegraph @

extensions/AbuseFilter/tests/phpunit/integration/ProtectedVarsAccessLoggerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\AbuseFilter\Tests\Integration;
4
5 use Generator;
6 use MediaWiki\CheckUser\Logging\TemporaryAccountLogger;
7 use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
8 use MediaWiki\Extension\AbuseFilter\ProtectedVarsAccessLogger;
9 use MediaWiki\Registration\ExtensionRegistry;
10 use MediaWikiIntegrationTestCase;
11
12 /**
13 * @covers \MediaWiki\Extension\AbuseFilter\ProtectedVarsAccessLogger
14 * @group Database
15 */
16 class ProtectedVarsAccessLoggerTest extends MediaWikiIntegrationTestCase {
17 public function provideProtectedVarsLogTypes(): Generator {
18 yield 'enable access to protected vars values' => [
19 [
20 'logAction' => 'logAccessEnabled',
21 'params' => [],
22 ],
23 [
24 'expectedCULogType' => 'af-change-access-enable',
25 'expectedAFLogType' => 'change-access-enable',
26 ]
27 ];
28
29 yield 'disable access to protected vars values' => [
30 [
31 'logAction' => 'logAccessDisabled',
32 'params' => []
33 ],
34 [
35 'expectedCULogType' => 'af-change-access-disable',
36 'expectedAFLogType' => 'change-access-disable'
37 ]
38 ];
39 }
40
41 /**
42 * @dataProvider provideProtectedVarsLogTypes
43 */
44 public function testLogs_CUDisabled( $options, $expected ) {
45 $extensionRegistry = $this->createMock( ExtensionRegistry::class );
46 $extensionRegistry->method( 'isLoaded' )->with( 'CheckUser' )->willReturn( false );
47 $this->setService( 'ExtensionRegistry', $extensionRegistry );
48
49 $performer = $this->getTestSysop();
50 $logAction = $options['logAction'];
51 AbuseFilterServices::getAbuseLoggerFactory()
52 ->getProtectedVarsAccessLogger()
53 ->$logAction( $performer->getUserIdentity(), ...$options['params'] );
54
55 // Assert that the action wasn't inserted into CheckUsers' temp account logging table
56 $this->assertSame(
57 0,
58 (int)$this->getDb()->newSelectQueryBuilder()
59 ->select( 'COUNT(*)' )
60 ->from( 'logging' )
61 ->where( [
62 'log_action' => $expected['expectedCULogType'],
63 'log_type' => 'checkuser-temporary-account',
64 ] )
65 ->fetchField()
66 );
67 // and also that it was inserted into abusefilter's protected vars logging table
68 $this->assertSame(
69 1,
70 (int)$this->getDb()->newSelectQueryBuilder()
71 ->select( 'COUNT(*)' )
72 ->from( 'logging' )
73 ->where( [
74 'log_action' => $expected['expectedAFLogType'],
75 'log_type' => ProtectedVarsAccessLogger::LOG_TYPE,
76 ] )
77 ->fetchField()
78 );
79
80 $this->resetServices();
81 }
82
83 /**
84 * @dataProvider provideProtectedVarsLogTypes
85 */
86 public function testLogs_CUEnabled( $options, $expected ) {
87 $this->markTestSkippedIfExtensionNotLoaded( 'CheckUser' );
88
89 $performer = $this->getTestSysop();
90 $logAction = $options['logAction'];
91 AbuseFilterServices::getAbuseLoggerFactory()
92 ->getProtectedVarsAccessLogger()
93 ->$logAction( $performer->getUserIdentity(), ...$options['params'] );
94
95 // Assert that the action was inserted into CheckUsers' temp account logging table
96 $this->assertSame(
97 1,
98 (int)$this->getDb()->newSelectQueryBuilder()
99 ->select( 'COUNT(*)' )
100 ->from( 'logging' )
101 ->where( [
102 'log_action' => $expected['expectedCULogType'],
103 'log_type' => TemporaryAccountLogger::LOG_TYPE,
104 ] )
105 ->fetchField()
106 );
107 // and also that it wasn't inserted into abusefilter's protected vars logging table
108 $this->assertSame(
109 0,
110 (int)$this->getDb()->newSelectQueryBuilder()
111 ->select( 'COUNT(*)' )
112 ->from( 'logging' )
113 ->where( [
114 'log_action' => $expected['expectedAFLogType'],
115 'log_type' => ProtectedVarsAccessLogger::LOG_TYPE,
116 ] )
117 ->fetchField()
118 );
119 }
120
121 public function testDebouncedLogs_CUDisabled() {
122 $extensionRegistry = $this->createMock( ExtensionRegistry::class );
123 $extensionRegistry->method( 'isLoaded' )->with( 'CheckUser' )->willReturn( false );
124 $this->setService( 'ExtensionRegistry', $extensionRegistry );
125
126 // Run the same action twice
127 $performer = $this->getTestSysop();
128 AbuseFilterServices::getAbuseLoggerFactory()
129 ->getProtectedVarsAccessLogger()
130 ->logViewProtectedVariableValue( $performer->getUserIdentity(), '~2024-01', (int)wfTimestamp() );
131 AbuseFilterServices::getAbuseLoggerFactory()
132 ->getProtectedVarsAccessLogger()
133 ->logViewProtectedVariableValue( $performer->getUserIdentity(), '~2024-01', (int)wfTimestamp() );
134
135 // Assert that the action wasn't inserted into CheckUsers' temp account logging table
136 $this->assertSame(
137 0,
138 (int)$this->getDb()->newSelectQueryBuilder()
139 ->select( 'COUNT(*)' )
140 ->from( 'logging' )
141 ->where( [
142 'log_action' => 'af-view-protected-var-value',
143 'log_type' => 'checkuser-temporary-account',
144 ] )
145 ->fetchField()
146 );
147 // and also that it only inserted once into abusefilter's protected vars logging table
148 $this->assertSame(
149 1,
150 (int)$this->getDb()->newSelectQueryBuilder()
151 ->select( 'COUNT(*)' )
152 ->from( 'logging' )
153 ->where( [
154 'log_action' => 'view-protected-var-value',
155 'log_type' => ProtectedVarsAccessLogger::LOG_TYPE,
156 ] )
157 ->fetchField()
158 );
159
160 $this->resetServices();
161 }
162
163 public function testDebouncedLogs_CUEnabled() {
164 $this->markTestSkippedIfExtensionNotLoaded( 'CheckUser' );
165
166 // Run the same action twice
167 $performer = $this->getTestSysop();
168 AbuseFilterServices::getAbuseLoggerFactory()
169 ->getProtectedVarsAccessLogger()
170 ->logViewProtectedVariableValue( $performer->getUserIdentity(), '~2024-01', (int)wfTimestamp() );
171 AbuseFilterServices::getAbuseLoggerFactory()
172 ->getProtectedVarsAccessLogger()
173 ->logViewProtectedVariableValue( $performer->getUserIdentity(), '~2024-01', (int)wfTimestamp() );
174
175 // Assert that the action only inserted once into CheckUsers' temp account logging table
176 $this->assertSame(
177 1,
178 (int)$this->getDb()->newSelectQueryBuilder()
179 ->select( 'COUNT(*)' )
180 ->from( 'logging' )
181 ->where( [
182 'log_action' => 'af-view-protected-var-value',
183 'log_type' => TemporaryAccountLogger::LOG_TYPE,
184 ] )
185 ->fetchField()
186 );
187 // and also that it wasn't inserted into abusefilter's protected vars logging table
188 $this->assertSame(
189 0,
190 (int)$this->getDb()->newSelectQueryBuilder()
191 ->select( 'COUNT(*)' )
192 ->from( 'logging' )
193 ->where( [
194 'log_action' => 'view-protected-var-value',
195 'log_type' => ProtectedVarsAccessLogger::LOG_TYPE,
196 ] )
197 ->fetchField()
198 );
199 }
200 }