2021-02-10 21:56:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\User;
|
|
|
|
|
|
2024-04-08 09:56:01 +00:00
|
|
|
use MediaWiki\Request\FauxRequest;
|
2024-01-10 23:40:27 +00:00
|
|
|
use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
|
2021-02-10 21:56:38 +00:00
|
|
|
use MediaWiki\User\UserIdentityValue;
|
|
|
|
|
use MediaWiki\User\UserSelectQueryBuilder;
|
|
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
* @covers \MediaWiki\User\UserSelectQueryBuilder
|
|
|
|
|
* @coversDefaultClass \MediaWiki\User\UserSelectQueryBuilder
|
|
|
|
|
*/
|
|
|
|
|
class UserSelectQueryBuilderTest extends ActorStoreTestBase {
|
2024-01-10 23:40:27 +00:00
|
|
|
|
|
|
|
|
use TempUserTestTrait;
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideFetchUserIdentitiesByNamePrefix() {
|
2021-02-10 21:56:38 +00:00
|
|
|
yield 'nothing found' => [
|
|
|
|
|
'z_z_Z_Z_z_Z_z_z', // $prefix
|
|
|
|
|
[ 'limit' => 100 ], // $options
|
|
|
|
|
[], // $expected
|
|
|
|
|
];
|
|
|
|
|
yield 'default parameters' => [
|
|
|
|
|
'Test', // $prefix
|
|
|
|
|
[ 'limit' => 100 ], // $options
|
|
|
|
|
[
|
2021-02-15 18:58:09 +00:00
|
|
|
new UserIdentityValue( 24, 'TestUser' ),
|
|
|
|
|
new UserIdentityValue( 25, 'TestUser1' ),
|
2021-02-10 21:56:38 +00:00
|
|
|
], // $expected
|
|
|
|
|
];
|
|
|
|
|
yield 'limited' => [
|
|
|
|
|
'Test', // $prefix
|
|
|
|
|
[ 'limit' => 1 ], // $options
|
|
|
|
|
[
|
2021-02-15 18:58:09 +00:00
|
|
|
new UserIdentityValue( 24, 'TestUser' ),
|
2021-02-10 21:56:38 +00:00
|
|
|
], // $expected
|
|
|
|
|
];
|
|
|
|
|
yield 'sorted' => [
|
|
|
|
|
'Test', // $prefix
|
|
|
|
|
[
|
|
|
|
|
'sort' => UserSelectQueryBuilder::SORT_DESC,
|
|
|
|
|
'limit' => 100,
|
|
|
|
|
], // $options
|
|
|
|
|
[
|
2021-02-15 18:58:09 +00:00
|
|
|
new UserIdentityValue( 25, 'TestUser1' ),
|
|
|
|
|
new UserIdentityValue( 24, 'TestUser' ),
|
2021-02-10 21:56:38 +00:00
|
|
|
], // $expected
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideFetchUserIdentitiesByNamePrefix
|
|
|
|
|
*/
|
|
|
|
|
public function testFetchUserIdentitiesByNamePrefix( string $prefix, array $options, array $expected ) {
|
|
|
|
|
$queryBuilder = $this->getStore()
|
|
|
|
|
->newSelectQueryBuilder()
|
|
|
|
|
->limit( $options['limit'] )
|
2021-08-03 17:43:16 +00:00
|
|
|
->whereUserNamePrefix( $prefix )
|
2021-02-10 21:56:38 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->orderByName( $options['sort'] ?? SelectQueryBuilder::SORT_ASC );
|
|
|
|
|
$actors = iterator_to_array( $queryBuilder->fetchUserIdentities() );
|
2023-09-27 13:22:44 +00:00
|
|
|
$this->assertSameSize( $expected, $actors );
|
2021-02-10 21:56:38 +00:00
|
|
|
foreach ( $expected as $idx => $expectedActor ) {
|
|
|
|
|
$this->assertSameActors( $expectedActor, $actors[$idx] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideFetchUserIdentitiesByUserIds() {
|
2021-02-10 21:56:38 +00:00
|
|
|
yield 'default parameters' => [
|
|
|
|
|
[ 24, 25 ], // ids
|
|
|
|
|
[], // $options
|
|
|
|
|
[
|
2021-02-15 18:58:09 +00:00
|
|
|
new UserIdentityValue( 24, 'TestUser' ),
|
|
|
|
|
new UserIdentityValue( 25, 'TestUser1' ),
|
2021-02-10 21:56:38 +00:00
|
|
|
], // $expected
|
|
|
|
|
];
|
|
|
|
|
yield 'sorted' => [
|
|
|
|
|
[ 24, 25 ], // ids
|
|
|
|
|
[ 'sort' => UserSelectQueryBuilder::SORT_DESC ], // $options
|
|
|
|
|
[
|
2021-02-15 18:58:09 +00:00
|
|
|
new UserIdentityValue( 25, 'TestUser1' ),
|
|
|
|
|
new UserIdentityValue( 24, 'TestUser' ),
|
2021-02-10 21:56:38 +00:00
|
|
|
], // $expected
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideFetchUserIdentitiesByUserIds
|
|
|
|
|
*/
|
|
|
|
|
public function testFetchUserIdentitiesByUserIds( array $ids, array $options, array $expected ) {
|
|
|
|
|
$actors = iterator_to_array(
|
|
|
|
|
$this->getStore()
|
|
|
|
|
->newSelectQueryBuilder()
|
2021-08-03 17:43:16 +00:00
|
|
|
->whereUserIds( $ids )
|
2021-02-10 21:56:38 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->orderByUserId( $options['sort'] ?? SelectQueryBuilder::SORT_ASC )
|
|
|
|
|
->fetchUserIdentities()
|
|
|
|
|
);
|
2023-09-27 13:22:44 +00:00
|
|
|
$this->assertSameSize( $expected, $actors );
|
2021-02-10 21:56:38 +00:00
|
|
|
foreach ( $expected as $idx => $expectedActor ) {
|
|
|
|
|
$this->assertSameActors( $expectedActor, $actors[$idx] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideFetchUserIdentitiesByNames() {
|
2021-02-10 21:56:38 +00:00
|
|
|
yield 'default parameters' => [
|
|
|
|
|
[ 'TestUser', 'TestUser1' ], // $names
|
|
|
|
|
[], // $options
|
|
|
|
|
[
|
2021-02-15 18:58:09 +00:00
|
|
|
new UserIdentityValue( 24, 'TestUser' ),
|
|
|
|
|
new UserIdentityValue( 25, 'TestUser1' ),
|
2021-02-10 21:56:38 +00:00
|
|
|
], // $expected
|
|
|
|
|
];
|
|
|
|
|
yield 'sorted' => [
|
|
|
|
|
[ 'TestUser', 'TestUser1' ], // $names
|
|
|
|
|
[ 'sort' => UserSelectQueryBuilder::SORT_DESC ], // $options
|
|
|
|
|
[
|
2021-02-15 18:58:09 +00:00
|
|
|
new UserIdentityValue( 25, 'TestUser1' ),
|
|
|
|
|
new UserIdentityValue( 24, 'TestUser' ),
|
2021-02-10 21:56:38 +00:00
|
|
|
], // $expected
|
|
|
|
|
];
|
|
|
|
|
yield 'with IPs' => [
|
|
|
|
|
[ self::IP ], // $names
|
|
|
|
|
[], // $options
|
|
|
|
|
[
|
2021-02-15 18:58:09 +00:00
|
|
|
new UserIdentityValue( 0, self::IP ),
|
2021-02-10 21:56:38 +00:00
|
|
|
], // $expected
|
|
|
|
|
];
|
|
|
|
|
yield 'with IPs, normalization' => [
|
|
|
|
|
[ strtolower( self::IP ), self::IP ], // $names
|
|
|
|
|
[], // $options
|
|
|
|
|
[
|
2021-02-15 18:58:09 +00:00
|
|
|
new UserIdentityValue( 0, self::IP ),
|
2021-02-10 21:56:38 +00:00
|
|
|
], // $expected
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideFetchUserIdentitiesByNames
|
|
|
|
|
*/
|
|
|
|
|
public function testFetchUserIdentitiesByNames( array $names, array $options, array $expected ) {
|
|
|
|
|
$actors = iterator_to_array(
|
|
|
|
|
$this->getStore()
|
|
|
|
|
->newSelectQueryBuilder()
|
2021-08-03 17:43:16 +00:00
|
|
|
->whereUserNames( $names )
|
2021-02-10 21:56:38 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->orderByUserId( $options['sort'] ?? SelectQueryBuilder::SORT_ASC )
|
|
|
|
|
->fetchUserIdentities()
|
|
|
|
|
);
|
2023-09-27 13:22:44 +00:00
|
|
|
$this->assertSameSize( $expected, $actors );
|
2021-02-10 21:56:38 +00:00
|
|
|
foreach ( $expected as $idx => $expectedActor ) {
|
|
|
|
|
$this->assertSameActors( $expectedActor, $actors[$idx] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::fetchUserIdentity
|
|
|
|
|
*/
|
|
|
|
|
public function testFetchUserIdentity() {
|
|
|
|
|
$this->assertSameActors(
|
2021-02-15 18:58:09 +00:00
|
|
|
new UserIdentityValue( 24, 'TestUser' ),
|
2021-02-10 21:56:38 +00:00
|
|
|
$this->getStore()
|
|
|
|
|
->newSelectQueryBuilder()
|
2021-08-03 17:43:16 +00:00
|
|
|
->whereUserIds( 24 )
|
2021-02-10 21:56:38 +00:00
|
|
|
->fetchUserIdentity()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::fetchUserNames
|
|
|
|
|
*/
|
|
|
|
|
public function testFetchUserNames() {
|
|
|
|
|
$this->assertArrayEquals(
|
|
|
|
|
[ 'TestUser', 'TestUser1' ],
|
|
|
|
|
$this->getStore()
|
|
|
|
|
->newSelectQueryBuilder()
|
|
|
|
|
->conds( [ 'actor_id' => [ 42, 44 ] ] )
|
|
|
|
|
->fetchUserNames()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::registered
|
|
|
|
|
*/
|
|
|
|
|
public function testRegistered() {
|
|
|
|
|
$actors = iterator_to_array(
|
|
|
|
|
$this->getStore()
|
|
|
|
|
->newSelectQueryBuilder()
|
|
|
|
|
->conds( [ 'actor_id' => [ 42, 43 ] ] )
|
|
|
|
|
->registered()
|
|
|
|
|
->fetchUserIdentities()
|
|
|
|
|
);
|
|
|
|
|
$this->assertCount( 1, $actors );
|
|
|
|
|
$this->assertSameActors(
|
2021-02-15 18:58:09 +00:00
|
|
|
new UserIdentityValue( 24, 'TestUser' ),
|
2021-02-10 21:56:38 +00:00
|
|
|
$actors[0]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::anon
|
|
|
|
|
*/
|
|
|
|
|
public function testAnon() {
|
|
|
|
|
$actors = iterator_to_array(
|
|
|
|
|
$this->getStore()
|
|
|
|
|
->newSelectQueryBuilder()
|
|
|
|
|
->limit( 100 )
|
2021-08-03 17:43:16 +00:00
|
|
|
->whereUserNamePrefix( '' )
|
2021-02-10 21:56:38 +00:00
|
|
|
->anon()
|
|
|
|
|
->fetchUserIdentities()
|
|
|
|
|
);
|
2021-02-23 12:12:30 +00:00
|
|
|
$this->assertCount( 2, $actors );
|
2021-02-10 21:56:38 +00:00
|
|
|
$this->assertSameActors(
|
2021-02-15 18:58:09 +00:00
|
|
|
new UserIdentityValue( 0, self::IP ),
|
2021-02-10 21:56:38 +00:00
|
|
|
$actors[0]
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-12-19 11:58:04 +00:00
|
|
|
|
2024-01-10 23:40:27 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ::anon
|
|
|
|
|
* @covers ::named
|
|
|
|
|
* @dataProvider provideNamedAndTempMethodNames
|
|
|
|
|
*/
|
|
|
|
|
public function testNamedAndTempWhenTempUserAutoCreateDisabled( $methodName ) {
|
2024-10-06 16:03:07 +00:00
|
|
|
$this->enableAutoCreateTempUser();
|
|
|
|
|
// Add a temporary accounts for the test
|
|
|
|
|
$tempUserCreateStatus = $this->getServiceContainer()->getTempUserCreator()
|
|
|
|
|
->create( null, new FauxRequest() );
|
|
|
|
|
$this->assertStatusOK( $tempUserCreateStatus );
|
|
|
|
|
$tempUser = $tempUserCreateStatus->getUser();
|
|
|
|
|
|
|
|
|
|
$this->resetServices();
|
2024-01-10 23:40:27 +00:00
|
|
|
$this->disableAutoCreateTempUser();
|
2024-10-06 16:03:07 +00:00
|
|
|
|
|
|
|
|
$actors = iterator_to_array(
|
|
|
|
|
$this->getStore()
|
|
|
|
|
->newSelectQueryBuilder()
|
|
|
|
|
->limit( 100 )
|
|
|
|
|
->whereUserNamePrefix( '' )
|
|
|
|
|
->$methodName()
|
|
|
|
|
->fetchUserIdentities()
|
2024-01-10 23:40:27 +00:00
|
|
|
);
|
2024-10-06 16:03:07 +00:00
|
|
|
if ( $methodName === 'temp' ) {
|
|
|
|
|
$this->assertCount( 0, $actors );
|
|
|
|
|
} else {
|
|
|
|
|
// With temp users disabled, the temp user is treated as a normal user and included.
|
|
|
|
|
$this->assertArrayEquals(
|
|
|
|
|
array_merge(
|
|
|
|
|
array_map( fn ( $userRow ) => $userRow['actor_user'], self::TEST_USERS ),
|
|
|
|
|
[ $tempUser->getId() ]
|
|
|
|
|
),
|
|
|
|
|
array_map( fn ( $actor ) => $actor->getId(), $actors )
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-01-10 23:40:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideNamedAndTempMethodNames() {
|
|
|
|
|
return [
|
|
|
|
|
'::temp' => [ 'temp' ],
|
|
|
|
|
'::named' => [ 'named' ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::named
|
|
|
|
|
*/
|
|
|
|
|
public function testNamed() {
|
|
|
|
|
$this->enableAutoCreateTempUser();
|
|
|
|
|
// Add a temporary accounts for the test
|
2024-04-08 09:56:01 +00:00
|
|
|
$tempUserCreateStatus = $this->getServiceContainer()->getTempUserCreator()
|
|
|
|
|
->create( null, new FauxRequest() );
|
2024-01-10 23:40:27 +00:00
|
|
|
$this->assertStatusOK( $tempUserCreateStatus );
|
|
|
|
|
$tempUser = $tempUserCreateStatus->getUser();
|
|
|
|
|
$actors = iterator_to_array(
|
|
|
|
|
$this->getStore()
|
|
|
|
|
->newSelectQueryBuilder()
|
|
|
|
|
->limit( 100 )
|
|
|
|
|
->whereUserNamePrefix( '' )
|
|
|
|
|
->named()
|
|
|
|
|
->fetchUserIdentities()
|
|
|
|
|
);
|
|
|
|
|
// The temp user should not appear in the results list.
|
|
|
|
|
$this->assertCount( 5, $actors );
|
|
|
|
|
foreach ( $actors as $actor ) {
|
|
|
|
|
$this->assertNotSame( $tempUser->getId(), $actor->getId() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::temp
|
|
|
|
|
*/
|
|
|
|
|
public function testTemp() {
|
|
|
|
|
$this->enableAutoCreateTempUser();
|
|
|
|
|
// Add a temporary accounts for the test
|
2024-04-08 09:56:01 +00:00
|
|
|
$tempUserCreateStatus = $this->getServiceContainer()->getTempUserCreator()
|
|
|
|
|
->create( null, new FauxRequest() );
|
2024-01-10 23:40:27 +00:00
|
|
|
$this->assertStatusOK( $tempUserCreateStatus );
|
|
|
|
|
$tempUser = $tempUserCreateStatus->getUser();
|
|
|
|
|
$actors = iterator_to_array(
|
|
|
|
|
$this->getStore()
|
|
|
|
|
->newSelectQueryBuilder()
|
|
|
|
|
->limit( 100 )
|
|
|
|
|
->whereUserNamePrefix( '' )
|
|
|
|
|
->temp()
|
|
|
|
|
->fetchUserIdentities()
|
|
|
|
|
);
|
|
|
|
|
// The temp user should be the only user identity returned.
|
|
|
|
|
$this->assertCount( 1, $actors );
|
|
|
|
|
$this->assertSameActors( $tempUser, $actors[0] );
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 11:58:04 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ::hidden
|
|
|
|
|
*/
|
|
|
|
|
public function testHidden() {
|
|
|
|
|
$hiddenUser = $this->getMutableTestUser()->getUserIdentity();
|
|
|
|
|
$normalUser = $this->getMutableTestUser()->getUserIdentity();
|
|
|
|
|
$this->getServiceContainer()->getBlockUserFactory()->newBlockUser(
|
|
|
|
|
$hiddenUser,
|
|
|
|
|
$this->getTestSysop()->getUser(),
|
|
|
|
|
'infinity',
|
|
|
|
|
'Test',
|
|
|
|
|
[
|
|
|
|
|
'isHideUser' => true
|
|
|
|
|
]
|
|
|
|
|
)->placeBlockUnsafe( true );
|
|
|
|
|
|
|
|
|
|
// hidden set to true
|
|
|
|
|
$actors = iterator_to_array(
|
|
|
|
|
$this->getStore()
|
2024-04-19 22:25:15 +00:00
|
|
|
->newSelectQueryBuilder()
|
|
|
|
|
->limit( 100 )
|
|
|
|
|
->whereUserIds( [
|
|
|
|
|
$hiddenUser->getId(),
|
|
|
|
|
$normalUser->getId()
|
|
|
|
|
] )
|
|
|
|
|
->hidden( true )
|
|
|
|
|
->fetchUserIdentities()
|
2021-12-19 11:58:04 +00:00
|
|
|
);
|
|
|
|
|
$this->assertCount( 1, $actors );
|
|
|
|
|
$this->assertSameActors(
|
|
|
|
|
$hiddenUser,
|
|
|
|
|
$actors[0]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// hidden set to false
|
|
|
|
|
$actors = iterator_to_array(
|
|
|
|
|
$this->getStore()
|
|
|
|
|
->newSelectQueryBuilder()
|
|
|
|
|
->limit( 100 )
|
|
|
|
|
->whereUserIds( [
|
|
|
|
|
$hiddenUser->getId(),
|
|
|
|
|
$normalUser->getId()
|
|
|
|
|
] )
|
|
|
|
|
->hidden( false )
|
|
|
|
|
->fetchUserIdentities()
|
|
|
|
|
);
|
|
|
|
|
$this->assertCount( 1, $actors );
|
|
|
|
|
$this->assertSameActors(
|
|
|
|
|
$normalUser,
|
|
|
|
|
$actors[0]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// hidden not set
|
|
|
|
|
$usernames = $this->getStore()
|
|
|
|
|
->newSelectQueryBuilder()
|
|
|
|
|
->limit( 100 )
|
|
|
|
|
->whereUserIds( [
|
|
|
|
|
$hiddenUser->getId(),
|
|
|
|
|
$normalUser->getId()
|
|
|
|
|
] )
|
|
|
|
|
->fetchUserNames();
|
|
|
|
|
$this->assertCount( 2, $usernames );
|
|
|
|
|
$this->assertArrayEquals(
|
|
|
|
|
[ $normalUser->getName(), $hiddenUser->getName() ],
|
|
|
|
|
$usernames
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-02-10 21:56:38 +00:00
|
|
|
}
|