2021-02-10 21:56:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\User;
|
|
|
|
|
|
|
|
|
|
use MediaWiki\User\UserIdentityValue;
|
|
|
|
|
use MediaWiki\User\UserSelectQueryBuilder;
|
|
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
* @covers \MediaWiki\User\UserSelectQueryBuilder
|
|
|
|
|
* @coversDefaultClass \MediaWiki\User\UserSelectQueryBuilder
|
|
|
|
|
* @package MediaWiki\Tests\User
|
|
|
|
|
*/
|
|
|
|
|
class UserSelectQueryBuilderTest extends ActorStoreTestBase {
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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()
|
|
|
|
|
->newSelectQueryBuilder()
|
|
|
|
|
->limit( 100 )
|
|
|
|
|
->whereUserIds( [
|
|
|
|
|
$hiddenUser->getId(),
|
|
|
|
|
$normalUser->getId()
|
|
|
|
|
] )
|
|
|
|
|
->hidden( true )
|
|
|
|
|
->fetchUserIdentities()
|
|
|
|
|
);
|
|
|
|
|
$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
|
|
|
}
|