Debian Codegraph @

tests/phpunit/integration/includes/user/TempUser/TempUserCreatorTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Integration\User\TempUser;
4
5 use MediaWiki\Auth\AuthManager;
6 use MediaWiki\Auth\Throttler;
7 use MediaWiki\MainConfigNames;
8 use MediaWiki\Registration\ExtensionRegistry;
9 use MediaWiki\Request\FauxRequest;
10 use MediaWiki\Session\Session;
11 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
12 use MediaWiki\User\CentralId\CentralIdLookup;
13 use MediaWiki\User\TempUser\RealTempUserConfig;
14 use MediaWiki\User\TempUser\SerialMapping;
15 use MediaWiki\User\TempUser\SerialProvider;
16 use MediaWiki\User\TempUser\TempUserCreator;
17 use MediaWiki\User\UserFactory;
18 use Wikimedia\TestingAccessWrapper;
19 use Wikimedia\Timestamp\ConvertibleTimestamp;
20
21 /**
22 * @group Database
23 * @covers \MediaWiki\User\TempUser\DBSerialProvider
24 * @covers \MediaWiki\User\TempUser\LocalSerialProvider
25 * @covers \MediaWiki\User\TempUser\TempUserCreator
26 * @covers \MediaWiki\User\TempUser\CreateStatus
27 */
28 class TempUserCreatorTest extends \MediaWikiIntegrationTestCase {
29
30 use TempUserTestTrait;
31
32 public function testCreate() {
33 $this->enableAutoCreateTempUser( [
34 'serialProvider' => [ 'type' => 'local', 'useYear' => false ],
35 'matchPattern' => '~$1',
36 ] );
37 $tuc = $this->getServiceContainer()->getTempUserCreator();
38 $this->assertTrue( $tuc->isAutoCreateAction( 'edit' ) );
39 $this->assertTrue( $tuc->isTempName( '~1' ) );
40
41 // Create a temporary account
42 $status = $tuc->create( null, new FauxRequest() );
43 $this->assertSame( '~1', $status->getUser()->getName() );
44 $this->assertSame(
45 1,
46 $this->getDb()->newSelectQueryBuilder()
47 ->from( 'logging' )
48 ->join( 'actor', null, 'log_actor=actor_id' )
49 ->where( [ 'actor_name' => '~1', 'log_action' => 'autocreate' ] )
50 ->fetchRowCount(),
51 'A logging entry indicating the autocreation of ~1 was expected.'
52 );
53
54 // Repeat the test to verify that the serial number increments
55 $status = $tuc->create( null, new FauxRequest() );
56 $this->assertSame( '~2', $status->getUser()->getName() );
57 $this->assertSame(
58 1,
59 $this->getDb()->newSelectQueryBuilder()
60 ->from( 'logging' )
61 ->join( 'actor', null, 'log_actor=actor_id' )
62 ->where( [ 'actor_name' => '~2', 'log_action' => 'autocreate' ] )
63 ->fetchRowCount(),
64 'A logging entry indicating the autocreation of ~2 was expected.'
65 );
66 }
67
68 private function getTempUserCreatorUnit() {
69 $scope1 = ExtensionRegistry::getInstance()->setAttributeForTest(
70 'TempUserSerialProviders',
71 [
72 'test' => [
73 'factory' => static function () {
74 return new class implements SerialProvider {
75 public function acquireIndex( int $year = 0 ): int {
76 return 1;
77 }
78 };
79 }
80 ],
81 ]
82 );
83 $scope2 = ExtensionRegistry::getInstance()->setAttributeForTest(
84 'TempUserSerialMappings',
85 [
86 'test' => [
87 'factory' => static function () {
88 return new class implements SerialMapping {
89 public function getSerialIdForIndex( int $index ): string {
90 $index--;
91 $adjective = (int)( $index / 2 );
92 $animal = $index % 2;
93 return [ 'active' ][$adjective] . ' ' . [ 'aardvark' ][$animal];
94 }
95 };
96 }
97 ]
98 ]
99 );
100 $config = new RealTempUserConfig( [
101 'enabled' => true,
102 'expireAfterDays' => null,
103 'actions' => [ 'edit' ],
104 'genPattern' => '*Unregistered $1',
105 'matchPattern' => '*$1',
106 'serialProvider' => [ 'type' => 'test' ],
107 'serialMapping' => [ 'type' => 'test' ],
108 ] );
109 $creator = new TempUserCreator(
110 $config,
111 $this->createSimpleObjectFactory(),
112 $this->createMock( UserFactory::class ),
113 $this->createMock( AuthManager::class ),
114 $this->createMock( CentralIdLookup::class ),
115 $this->createMock( Throttler::class ),
116 $this->createMock( Throttler::class )
117 );
118 return [ $creator, [ $scope1, $scope2 ] ];
119 }
120
121 public function testAcquireName_unit() {
122 [ $creator, $scope ] = $this->getTempUserCreatorUnit();
123 /** @var TempUserCreator $creator */
124 $creator = TestingAccessWrapper::newFromObject( $creator );
125 $this->assertSame(
126 '*Unregistered active aardvark',
127 $creator->acquireName( '127.0.0.1' )
128 );
129 }
130
131 public function testAcquireName_db() {
132 $this->enableAutoCreateTempUser( [
133 'serialProvider' => [ 'type' => 'local', 'useYear' => false ],
134 'matchPattern' => '~$1',
135 ] );
136 $tuc = TestingAccessWrapper::newFromObject(
137 $this->getServiceContainer()->getTempUserCreator()
138 );
139 $this->assertSame( '~1', $tuc->acquireName( '127.0.0.1' ) );
140 $this->assertSame( '~2', $tuc->acquireName( '127.0.0.1' ) );
141 }
142
143 public function testAcquireName_dbWithYear() {
144 $this->enableAutoCreateTempUser( [ 'serialProvider' => [ 'type' => 'local', 'useYear' => true ] ] );
145
146 ConvertibleTimestamp::setFakeTime( '20000101000000' );
147 $tuc = TestingAccessWrapper::newFromObject(
148 $this->getServiceContainer()->getTempUserCreator()
149 );
150 $this->assertSame( '~2000-1', $tuc->acquireName( '127.0.0.1' ) );
151 $this->assertSame( '~2000-2', $tuc->acquireName( '127.0.0.1' ) );
152
153 ConvertibleTimestamp::setFakeTime( '20010101000000' );
154 $this->assertSame( '~2001-1', $tuc->acquireName( '127.0.0.1' ) );
155 }
156
157 public function testAcquireNameOnDuplicate_db() {
158 ConvertibleTimestamp::setFakeTime( '20000101000000' );
159 $this->enableAutoCreateTempUser();
160 $tuc = TestingAccessWrapper::newFromObject(
161 $this->getServiceContainer()->getTempUserCreator()
162 );
163 // Create a temporary account
164 $this->assertSame( '~2000-1', $tuc->create( null, new FauxRequest() )->value->getName() );
165 // Reset the user_autocreate_serial table
166 $this->truncateTable( 'user_autocreate_serial' );
167 // Because user_autocreate_serial was truncated, the ::acquireName method should
168 // return null as the code attempts to return a temporary account that already exists.
169 $this->assertSame( null, $tuc->acquireName( '127.0.0.1' ) );
170 }
171
172 public function testCreateOnDuplicate_db() {
173 ConvertibleTimestamp::setFakeTime( '20000101000000' );
174 $this->enableAutoCreateTempUser();
175 $tuc = $this->getServiceContainer()->getTempUserCreator();
176 // Create a temporary account
177 $this->assertSame( '~2000-1', $tuc->create( null, new FauxRequest() )->value->getName() );
178 // Create a temporary account with an existing temporary account username.
179 $secondCreateStatus = $tuc->create( '~2000-1', new FauxRequest() );
180 $this->assertStatusError( 'temp-user-unable-to-acquire', $secondCreateStatus );
181 // Assert that only one log entry for autocreation exists for ~2000-1, as the second call should have not
182 // created a new log entry.
183 $this->assertSame(
184 1,
185 $this->getDb()->newSelectQueryBuilder()
186 ->from( 'logging' )
187 ->join( 'actor', null, 'log_actor=actor_id' )
188 ->where( [ 'actor_name' => '~2000-1', 'log_action' => 'autocreate' ] )
189 ->fetchRowCount(),
190 'Only one logging entry indicating the autocreation of ~2000-1 was expected.'
191 );
192 }
193
194 public function testCreateOnInvalidUsername() {
195 $this->enableAutoCreateTempUser();
196 $tuc = $this->getServiceContainer()->getTempUserCreator();
197 // Attempt to create the temporary account with an invalid username.
198 $secondCreateStatus = $tuc->create( 'Template:InvalidUsername#test', new FauxRequest() );
199 $this->assertStatusError( 'internalerror_info', $secondCreateStatus );
200 }
201
202 public function testAcquireNameThrottled() {
203 ConvertibleTimestamp::setFakeTime( '20000101000000' );
204 $this->enableAutoCreateTempUser();
205 $this->overrideConfigValue(
206 MainConfigNames::TempAccountNameAcquisitionThrottle,
207 [
208 'count' => 1,
209 'seconds' => 30 * 86400,
210 ]
211 );
212 $tuc = TestingAccessWrapper::newFromObject(
213 $this->getServiceContainer()->getTempUserCreator()
214 );
215 // Create a temporary account
216 $this->assertSame( '~2000-1', $tuc->create( null, new FauxRequest() )->value->getName() );
217 // Attempt again; name acquisition should be limited
218 $this->assertStatusError( 'temp-user-unable-to-acquire', $tuc->create( null, new FauxRequest() ) );
219 }
220
221 public function testAcquireAndStashName() {
222 /** @var TempUserCreator $creator */
223 [ $creator, $scope ] = $this->getTempUserCreatorUnit();
224
225 $session = new class extends Session {
226 private $data = [];
227
228 public function __construct() {
229 }
230
231 public function __destruct() {
232 }
233
234 public function get( $key, $default = null ) {
235 return array_key_exists( $key, $this->data ) ? $this->data[$key] : $default;
236 }
237
238 public function set( $key, $value ) {
239 $this->data[$key] = $value;
240 }
241
242 public function save() {
243 }
244
245 public function getRequest() {
246 return new FauxRequest();
247 }
248 };
249
250 $name = $creator->acquireAndStashName( $session );
251 $this->assertSame( '*Unregistered active aardvark', $name );
252 $name = $creator->acquireAndStashName( $session );
253 $this->assertSame( '*Unregistered active aardvark', $name );
254 }
255
256 public function testRateLimit() {
257 $this->enableAutoCreateTempUser( [
258 'serialProvider' => [ 'type' => 'local', 'useYear' => false ],
259 'matchPattern' => '~$1',
260 ] );
261 $this->overrideConfigValues( [
262 MainConfigNames::AccountCreationThrottle => [
263 'count' => 10,
264 'seconds' => 86400
265 ],
266 MainConfigNames::TempAccountCreationThrottle => [
267 'count' => 1,
268 'seconds' => 86400
269 ],
270 ] );
271 $tuc = $this->getServiceContainer()->getTempUserCreator();
272 $status = $tuc->create( null, new FauxRequest() );
273 $this->assertSame( '~1', $status->getUser()->getName() );
274
275 // Repeat creating a temporary account, and verify that this fails due to the rate limit.
276 $status = $tuc->create( null, new FauxRequest() );
277 // TODO: Use new message key (T357777, T357802)
278 $this->assertStatusError( 'acct_creation_throttle_hit', $status );
279 // If the temporary account creation failed due to the rate limit, then no log entry should have been created.
280 $this->assertSame(
281 0,
282 $this->getDb()->newSelectQueryBuilder()
283 ->from( 'logging' )
284 ->join( 'actor', null, 'log_actor=actor_id' )
285 ->where( [ 'actor_name' => '~2', 'log_action' => 'autocreate' ] )
286 ->fetchRowCount(),
287 'A logging entry indicating the autocreation of ~2 was not expected.'
288 );
289 }
290 }