File: PopulateUserIsTempTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (68 lines) | stat: -rw-r--r-- 2,114 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

namespace MediaWiki\Tests\Maintenance;

use MediaWiki\Request\FauxRequest;
use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
use PopulateUserIsTemp;

/**
 * @covers \PopulateUserIsTemp
 * @group Database
 */
class PopulateUserIsTempTest extends MaintenanceBaseTestCase {
	use TempUserTestTrait;

	protected function getMaintenanceClass() {
		return PopulateUserIsTemp::class;
	}

	public function testDoDBUpdates() {
		$this->enableAutoCreateTempUser( [
			'matchPattern' => [ '*$1', '~$1' ],
		] );
		$this->maintenance->setOption( 'batch-size', 2 );
		$this->assertSame(
			2,
			(int)$this->getDb()->newSelectQueryBuilder()
				->select( 'COUNT(*)' )
				->from( 'user' )
				->where( [ 'user_is_temp' => 1 ] )
				->fetchField(),
			'The database should have 2 users with user_is_temp set to 1 before the execute method is called.'
		);
		$this->assertTrue(
			$this->maintenance->execute(),
			'The execute method did not return true as expected.'
		);
		$this->assertSame(
			5,
			(int)$this->getDb()->newSelectQueryBuilder()
				->select( 'COUNT(*)' )
				->from( 'user' )
				->where( [ 'user_is_temp' => 1 ] )
				->fetchField(),
			'The number of users with user_is_temp set to 1 is not as expected.'
		);
	}

	public function addDBData() {
		parent::addDBData();

		// Create some temporary users and then set the user table to have user_is_temp as 0 for some of them.
		$this->enableAutoCreateTempUser( [
			'matchPattern' => [ '*$1', '~$1' ],
		] );
		$tempUserCreator = $this->getServiceContainer()->getTempUserCreator();
		$tempUserCreator->create( '*Unregistered 1', new FauxRequest() );
		$tempUserCreator->create( '*Unregistered 2', new FauxRequest() );
		$tempUserCreator->create( '~Unregistered 3', new FauxRequest() );
		$tempUserCreator->create( '~Unregistered 4567', new FauxRequest() );
		$tempUserCreator->create( '~Unregistered 456789', new FauxRequest() );
		$this->getDb()->newUpdateQueryBuilder()
			->update( 'user' )
			->set( [ 'user_is_temp' => 0 ] )
			->where( [ 'user_name' => [ '*Unregistered 2', '~Unregistered 3', '~Unregistered 456789' ] ] )
			->execute();
	}
}