diff options
Diffstat (limited to 'tests/phpunit')
3 files changed, 108 insertions, 3 deletions
diff --git a/tests/phpunit/includes/api/query/ApiQueryBlockInfoTraitTest.php b/tests/phpunit/includes/api/query/ApiQueryBlockInfoTraitTest.php index 915da66575d0..badcaa2bb5bb 100644 --- a/tests/phpunit/includes/api/query/ApiQueryBlockInfoTraitTest.php +++ b/tests/phpunit/includes/api/query/ApiQueryBlockInfoTraitTest.php @@ -61,7 +61,8 @@ class ApiQueryBlockInfoTraitTest extends MediaWikiIntegrationTestCase { 'fields' => [ 'hu_deleted' => '1=0' ], 'conds' => [ '(SELECT 1 FROM "block_target" "hu_block_target" ' . 'JOIN "block" "hu_block" ON ((hu_block.bl_target=hu_block_target.bt_id)) ' . - 'WHERE (hu_block_target.bt_user=user_id) AND hu_block.bl_deleted = 1 ) IS NULL' ], + 'WHERE (hu_block_target.bt_user=user_id) AND hu_block.bl_deleted = 1 ' . + 'LIMIT 1 ) IS NULL' ], 'options' => [], 'join_conds' => [], ], @@ -72,7 +73,8 @@ class ApiQueryBlockInfoTraitTest extends MediaWikiIntegrationTestCase { 'tables' => [ 'table' ], 'fields' => [ 'hu_deleted' => '(SELECT 1 FROM "block_target" "hu_block_target" ' . 'JOIN "block" "hu_block" ON ((hu_block.bl_target=hu_block_target.bt_id)) ' . - 'WHERE (hu_block_target.bt_user=user_id) AND hu_block.bl_deleted = 1 ) IS NOT NULL' ], + 'WHERE (hu_block_target.bt_user=user_id) AND hu_block.bl_deleted = 1 ' . + 'LIMIT 1 ) IS NOT NULL' ], 'conds' => [], 'options' => [], 'join_conds' => [] diff --git a/tests/phpunit/integration/includes/block/HideUserUtilsTest.php b/tests/phpunit/integration/includes/block/HideUserUtilsTest.php new file mode 100644 index 000000000000..0bad42f69554 --- /dev/null +++ b/tests/phpunit/integration/includes/block/HideUserUtilsTest.php @@ -0,0 +1,102 @@ +<?php + +use MediaWiki\Block\DatabaseBlock; +use MediaWiki\Block\HideUserUtils; + +/** + * @group Database + * @covers \MediaWiki\Block\HideUserUtils + */ +class HideUserUtilsTest extends MediaWikiIntegrationTestCase { + /** @var HideUserUtils */ + private $hideUserUtils; + + public function setUp(): void { + $this->hideUserUtils = $this->getServiceContainer()->getHideUserUtils(); + } + + public function addDBDataOnce() { + $admin = User::createNew( 'Admin' ); + User::createNew( 'Normal' ); + User::createNew( 'Blocked' ); + User::createNew( 'Deleted' ); + User::createNew( 'Deleted twice' ); + + $blocks = [ + [ + 'address' => 'Blocked', + 'by' => $admin, + ], + [ + 'address' => 'Deleted', + 'by' => $admin, + 'hideName' => true, + ], + [ + 'address' => 'Deleted twice', + 'by' => $admin, + 'hideName' => true + ], + [ + 'address' => 'Deleted twice', + 'by' => $admin, + 'hideName' => true + ], + ]; + + $dbs = $this->getServiceContainer()->getDatabaseBlockStore(); + foreach ( $blocks as $params ) { + $dbs->insertBlock( new DatabaseBlock( $params ), null ); + } + } + + public static function provideGetExpression() { + return [ + [ 'Nonexistent', HideUserUtils::SHOWN_USERS, false ], + [ 'Nonexistent', HideUserUtils::HIDDEN_USERS, false ], + [ 'Normal', HideUserUtils::SHOWN_USERS, true ], + [ 'Normal', HideUserUtils::HIDDEN_USERS, false ], + [ 'Deleted', HideUserUtils::SHOWN_USERS, false ], + [ 'Deleted', HideUserUtils::HIDDEN_USERS, true ], + [ 'Deleted twice', HideUserUtils::SHOWN_USERS, false ], + [ 'Deleted twice', HideUserUtils::HIDDEN_USERS, true ], + ]; + } + + /** + * @dataProvider provideGetExpression + * @param string $name + * @param int $status + * @param bool $expected + */ + public function testGetExpression( $name, $status, $expected ) { + $qb = $this->newSelectQueryBuilder() + ->select( 'user_name' ) + ->from( 'user' ) + ->where( [ + 'user_name' => $name, + $this->hideUserUtils->getExpression( $this->getDb(), 'user_id', $status ) + ] ) + ->caller( __METHOD__ ); + if ( $expected ) { + $qb->assertFieldValue( $name ); + } else { + $qb->assertEmptyResult(); + } + } + + public function testAddFieldToBuilder() { + $qb = $this->newSelectQueryBuilder() + ->select( [ 'user_id', 'user_name' ] ) + ->from( 'user' ) + ->caller( __METHOD__ ); + $this->hideUserUtils->addFieldToBuilder( $qb ); + $qb->assertResultSet( [ + [ 1, 'Admin', 0 ], + [ 2, 'Normal', 0 ], + [ 3, 'Blocked', 0 ], + [ 4, 'Deleted', 1 ], + [ 5, 'Deleted twice', 1 ], + ] ); + } +} diff --git a/tests/phpunit/unit/includes/user/UserNamePrefixSearchTest.php b/tests/phpunit/unit/includes/user/UserNamePrefixSearchTest.php index 00145f97d2fe..eae39985bbae 100644 --- a/tests/phpunit/unit/includes/user/UserNamePrefixSearchTest.php +++ b/tests/phpunit/unit/includes/user/UserNamePrefixSearchTest.php @@ -85,7 +85,8 @@ class UserNamePrefixSearchTest extends MediaWikiUnitTestCase { if ( $excludeHidden ) { $conds[] = '(SELECT 1 FROM block_target hu_block_target ' . 'JOIN block hu_block ON ((hu_block.bl_target=hu_block_target.bt_id)) ' . - 'WHERE (hu_block_target.bt_user=user_id) AND hu_block.bl_deleted = 1 ) IS NULL'; + 'WHERE (hu_block_target.bt_user=user_id) AND hu_block.bl_deleted = 1 ' . + 'LIMIT 1 ) IS NULL'; } $options = [ 'LIMIT' => $limit, |