tests: Migrate more cases to SQB

Bug: T344971
Change-Id: Ia69d82d6a6e623b9032240dc910fb47ff5887661
This commit is contained in:
Amir Sarabadani 2023-09-21 18:37:37 +02:00 committed by DannyS712
parent b286f79f5d
commit abbb4281cb
28 changed files with 272 additions and 207 deletions

View file

@ -105,12 +105,12 @@ class CategoryTest extends MediaWikiIntegrationTestCase {
public function testNewFromRow_found() {
$dbw = wfGetDB( DB_PRIMARY );
$category = Category::newFromRow( $dbw->selectRow(
'category',
[ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
[ 'cat_id' => 1 ],
__METHOD__
) );
$category = Category::newFromRow( $dbw->newSelectQueryBuilder()
->select( [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ] )
->from( 'category' )
->where( [ 'cat_id' => 1 ] )
->caller( __METHOD__ )->fetchRow()
);
$this->assertSame( '1', $category->getID() );
}

View file

@ -174,12 +174,11 @@ class CommentStoreTest extends MediaWikiLangTestCase {
$rstore = $this->makeStore();
$fieldRow = $this->db->selectRow(
$table,
$rstore->getFields( $key ),
[ $pk => $id ],
__METHOD__
);
$fieldRow = $this->db->newSelectQueryBuilder()
->select( $rstore->getFields( $key ) )
->from( $table )
->where( [ $pk => $id ] )
->caller( __METHOD__ )->fetchRow();
$queryInfo = $rstore->getJoin( $key );
$joinRow = $this->db->selectRow(
@ -308,9 +307,11 @@ class CommentStoreTest extends MediaWikiLangTestCase {
$store = $this->makeStore();
$fields = $store->insert( $this->db, 'ipb_reason', $comment );
$stored = $this->db->selectField(
'comment', 'comment_text', [ 'comment_id' => $fields['ipb_reason_id'] ], __METHOD__
);
$stored = $this->db->newSelectQueryBuilder()
->select( 'comment_text' )
->from( 'comment' )
->where( [ 'comment_id' => $fields['ipb_reason_id'] ] )
->caller( __METHOD__ )->fetchField();
$this->assertSame( $truncated, $stored );
}

View file

@ -3015,7 +3015,10 @@ class RevisionStoreDbTest extends MediaWikiIntegrationTestCase {
// NOTE: must be done before checking MAX(rev_id)
$page = $this->getTestPage();
$maxRevId = $this->getDb()->selectField( 'revision', 'MAX(rev_id)' );
$maxRevId = $this->getDb()->newSelectQueryBuilder()
->select( 'MAX(rev_id)' )
->from( 'revision' )
->fetchField();
// Construct a slot row that will conflict with the insertion of the next revision ID,
// to emulate the failure mode described in T202032. Nothing will ever read this row,

View file

@ -1090,7 +1090,11 @@ class DerivedPageDataUpdaterTest extends MediaWikiIntegrationTestCase {
$rev = $this->createRevision( $page, 'first', $content );
$pageId = $page->getId();
$oldStats = $this->db->selectRow( 'site_stats', '*', '1=1' );
$oldStats = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'site_stats' )
->where( '1=1' )
->fetchRow();
$this->db->delete( 'pagelinks', '*' );
$pcache = $this->getServiceContainer()->getParserCache();
@ -1105,13 +1109,12 @@ class DerivedPageDataUpdaterTest extends MediaWikiIntegrationTestCase {
$updater->doUpdates();
// links table update
$pageLinks = $this->db->select(
'pagelinks',
'*',
[ 'pl_from' => $pageId ],
__METHOD__,
[ 'ORDER BY' => [ 'pl_namespace', 'pl_title' ] ]
);
$pageLinks = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'pagelinks' )
->where( [ 'pl_from' => $pageId ] )
->orderBy( [ 'pl_namespace', 'pl_title' ] )
->caller( __METHOD__ )->fetchResultSet();
$pageLinksRow = $pageLinks->fetchObject();
$this->assertIsObject( $pageLinksRow );
@ -1127,7 +1130,11 @@ class DerivedPageDataUpdaterTest extends MediaWikiIntegrationTestCase {
$this->assertEquals( $updater->getCanonicalParserOutput(), $cached );
// site stats
$stats = $this->db->selectRow( 'site_stats', '*', '1=1' );
$stats = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'site_stats' )
->where( '1=1' )
->fetchRow();
$this->assertSame( $oldStats->ss_total_pages + 1, (int)$stats->ss_total_pages );
$this->assertSame( $oldStats->ss_total_edits + 1, (int)$stats->ss_total_edits );
$this->assertSame( $oldStats->ss_good_articles + 1, (int)$stats->ss_good_articles );

View file

@ -92,7 +92,11 @@ class PageUpdaterTest extends MediaWikiIntegrationTestCase {
$page = $wikiPageFactory->newFromTitle( $title );
$updater = $page->newPageUpdater( $user );
$oldStats = $this->db->selectRow( 'site_stats', '*', '1=1' );
$oldStats = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'site_stats' )
->where( '1=1' )
->fetchRow();
$this->assertFalse( $updater->wasCommitted(), 'wasCommitted' );
@ -168,7 +172,11 @@ class PageUpdaterTest extends MediaWikiIntegrationTestCase {
$this->assertNotNull( $rc, 'RecentChange' );
// check site stats - this asserts that derived data updates where run.
$stats = $this->db->selectRow( 'site_stats', '*', '1=1' );
$stats = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'site_stats' )
->where( '1=1' )
->fetchRow();
$this->assertSame( $oldStats->ss_total_pages + 1, (int)$stats->ss_total_pages );
$this->assertSame( $oldStats->ss_total_edits + 1, (int)$stats->ss_total_edits );
@ -203,7 +211,11 @@ class PageUpdaterTest extends MediaWikiIntegrationTestCase {
$updater = $page->newPageUpdater( $user );
$oldStats = $this->db->selectRow( 'site_stats', '*', '1=1' );
$oldStats = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'site_stats' )
->where( '1=1' )
->fetchRow();
$updater->setOriginalRevisionId( 7 );
@ -300,7 +312,11 @@ class PageUpdaterTest extends MediaWikiIntegrationTestCase {
);
// check site stats - this asserts that derived data updates where run.
$stats = $this->db->selectRow( 'site_stats', '*', '1=1' );
$stats = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'site_stats' )
->where( '1=1' )
->fetchRow();
$this->assertNotNull( $stats, 'site_stats' );
$this->assertSame( $oldStats->ss_total_pages + 0, (int)$stats->ss_total_pages );
$this->assertSame( $oldStats->ss_total_edits + 2, (int)$stats->ss_total_edits );

View file

@ -123,17 +123,13 @@ class ApiBlockTest extends ApiTestCase {
$this->doBlock( [ 'tags' => 'custom tag' ] );
$dbw = wfGetDB( DB_PRIMARY );
$this->assertSame( 1, (int)$dbw->selectField(
[ 'change_tag', 'logging', 'change_tag_def' ],
'COUNT(*)',
[ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
__METHOD__,
[],
[
'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ],
]
) );
$this->assertSame( 1, (int)$dbw->newSelectQueryBuilder()
->select( 'COUNT(*)' )
->from( 'logging' )
->join( 'change_tag', null, 'ct_log_id = log_id' )
->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' )
->where( [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ] )
->caller( __METHOD__ )->fetchField() );
}
public function testBlockWithProhibitedTag() {
@ -155,12 +151,11 @@ class ApiBlockTest extends ApiTestCase {
new UltimateAuthority( $this->getTestSysop()->getUser() )
);
$this->assertSame( '1', $this->db->selectField(
'ipblocks',
'ipb_deleted',
[ 'ipb_id' => $res[0]['block']['id'] ],
__METHOD__
) );
$this->assertSame( '1', $this->db->newSelectQueryBuilder()
->select( 'ipb_deleted' )
->from( 'ipblocks' )
->where( [ 'ipb_id' => $res[0]['block']['id'] ] )
->caller( __METHOD__ )->fetchField() );
}
public function testBlockWithProhibitedHide() {
@ -182,12 +177,11 @@ class ApiBlockTest extends ApiTestCase {
$res = $this->doBlock( [ 'noemail' => '' ] );
$dbw = wfGetDB( DB_PRIMARY );
$this->assertSame( '1', $dbw->selectField(
'ipblocks',
'ipb_block_email',
[ 'ipb_id' => $res[0]['block']['id'] ],
__METHOD__
) );
$this->assertSame( '1', $dbw->newSelectQueryBuilder()
->select( 'ipb_block_email' )
->from( 'ipblocks' )
->where( [ 'ipb_id' => $res[0]['block']['id'] ] )
->caller( __METHOD__ )->fetchField() );
}
public function testBlockWithProhibitedEmailBlock() {

View file

@ -324,12 +324,11 @@ class ApiChangeContentModelTest extends ApiTestCase {
$dbw = wfGetDB( DB_PRIMARY );
$this->assertSame(
'4',
$dbw->selectField(
[ 'change_tag_def' ],
'ctd_count',
[ 'ctd_name' => 'api edit content model tag' ],
__METHOD__
),
$dbw->newSelectQueryBuilder()
->select( 'ctd_count' )
->from( 'change_tag_def' )
->where( [ 'ctd_name' => 'api edit content model tag' ] )
->caller( __METHOD__ )->fetchField(),
'There should be four uses of the `api edit content model tag` tag, '
. 'two for the two revisions and two for the two log entries'
);

View file

@ -149,20 +149,13 @@ class ApiDeleteTest extends ApiTestCase {
$this->assertFalse( $title->exists( Title::READ_LATEST ) );
$dbw = wfGetDB( DB_PRIMARY );
$this->assertSame( 'custom tag', $dbw->selectField(
[ 'change_tag', 'logging', 'change_tag_def' ],
'ctd_name',
[
'log_namespace' => $title->getNamespace(),
'log_title' => $title->getDBkey(),
],
__METHOD__,
[],
[
'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ]
]
) );
$this->assertSame( 'custom tag', $dbw->newSelectQueryBuilder()
->select( 'ctd_name' )
->from( 'logging' )
->join( 'change_tag', null, 'ct_log_id = log_id' )
->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' )
->where( [ 'log_namespace' => $title->getNamespace(), 'log_title' => $title->getDBkey(), ] )
->caller( __METHOD__ )->fetchField() );
}
public function testDeleteWithoutTagPermission() {

View file

@ -1536,14 +1536,12 @@ class ApiEditPageTest extends ApiTestCase {
'tags' => 'custom tag',
] )[0]['edit']['newrevid'];
$this->assertSame( 'custom tag', $this->getDb()->selectField(
[ 'change_tag', 'change_tag_def' ],
'ctd_name',
[ 'ct_rev_id' => $revId ],
__METHOD__,
[ 'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ] ]
)
);
$this->assertSame( 'custom tag', $this->getDb()->newSelectQueryBuilder()
->select( 'ctd_name' )
->from( 'change_tag' )
->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' )
->where( [ 'ct_rev_id' => $revId ] )
->caller( __METHOD__ )->fetchField() );
}
public function testEditWithoutTagPermission() {

View file

@ -123,17 +123,13 @@ class ApiUnblockTest extends ApiTestCase {
$this->doUnblock( [ 'tags' => 'custom tag' ] );
$dbw = wfGetDB( DB_PRIMARY );
$this->assertSame( 1, (int)$dbw->selectField(
[ 'change_tag', 'logging', 'change_tag_def' ],
'COUNT(*)',
[ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
__METHOD__,
[],
[
'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ],
]
) );
$this->assertSame( 1, (int)$dbw->newSelectQueryBuilder()
->select( 'COUNT(*)' )
->from( 'logging' )
->join( 'change_tag', null, 'ct_log_id = log_id' )
->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' )
->where( [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ] )
->caller( __METHOD__ )->fetchField() );
}
public function testUnblockWithProhibitedTag() {

View file

@ -218,18 +218,13 @@ class ApiUserrightsTest extends ApiTestCase {
$dbr = wfGetDB( DB_REPLICA );
$this->assertSame(
'custom tag',
$dbr->selectField(
[ 'change_tag', 'logging', 'change_tag_def' ],
'ctd_name',
[
'ct_log_id = log_id',
'log_namespace' => NS_USER,
'log_title' => strtr( $user->getName(), ' ', '_' )
],
__METHOD__,
[ 'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ] ]
)
);
$dbr->newSelectQueryBuilder()
->select( 'ctd_name' )
->from( 'logging' )
->join( 'change_tag', null, 'ct_log_id = log_id' )
->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' )
->where( [ 'log_namespace' => NS_USER, 'log_title' => strtr( $user->getName(), ' ', '_' ) ] )
->caller( __METHOD__ )->fetchField() );
}
public function testWithoutTagPermission() {

View file

@ -531,7 +531,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
} ),
/* $timeSinceAuth*/ $mutableSession
? $this->equalToWithDelta( 500, 2 )
: $this->equalTo( -1 )
: -1
)
->willReturnCallback( static function ( &$v ) use ( $hook ) {
$v = $hook;
@ -2285,7 +2285,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->callback( static function ( $user ) use ( $username ) {
return $user->getName() === $username;
} ),
$this->equalTo( false )
false
);
$expectLog[] = [ LogLevel::INFO, "Creating user {user} during account creation" ];
} else {
@ -2383,8 +2383,11 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->assertSame(
$maxLogId,
$dbw->selectField( 'logging', 'MAX(log_id)', [ 'log_type' => 'newusers' ] )
);
$dbw->newSelectQueryBuilder()
->select( 'MAX(log_id)' )
->from( 'logging' )
->where( [ 'log_type' => 'newusers' ] )
->fetchField() );
}
public function provideAccountCreation() {
@ -2636,7 +2639,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$mocks['pre']->expects( $this->exactly( 13 ) )->method( 'testUserForCreation' )
->with( $callback, $callback2 )
->will( $this->onConsecutiveCalls(
->willReturnOnConsecutiveCalls(
$ok, $ok, $ok, // For testing permissions
StatusValue::newFatal( 'fail-in-pre' ), $good, $good,
$good, // backoff test
@ -2644,7 +2647,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$good, // addToDatabase throws test
$good, // addToDatabase exists test
$good, $good, $good // success
) );
);
$mocks['primary']->method( 'accountCreationType' )
->willReturn( PrimaryAuthenticationProvider::TYPE_CREATE );
@ -2652,27 +2655,27 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
->willReturn( true );
$mocks['primary']->expects( $this->exactly( 9 ) )->method( 'testUserForCreation' )
->with( $callback, $callback2 )
->will( $this->onConsecutiveCalls(
->willReturnOnConsecutiveCalls(
StatusValue::newFatal( 'fail-in-primary' ), $good,
$good, // backoff test
$good, // addToDatabase fails test
$good, // addToDatabase throws test
$good, // addToDatabase exists test
$good, $good, $good
) );
);
$mocks['primary']->expects( $this->exactly( 3 ) )->method( 'autoCreatedAccount' )
->with( $callback, $callback2 );
$mocks['secondary']->expects( $this->exactly( 8 ) )->method( 'testUserForCreation' )
->with( $callback, $callback2 )
->will( $this->onConsecutiveCalls(
->willReturnOnConsecutiveCalls(
StatusValue::newFatal( 'fail-in-secondary' ),
$good, // backoff test
$good, // addToDatabase fails test
$good, // addToDatabase throws test
$good, // addToDatabase exists test
$good, $good, $good
) );
);
$mocks['secondary']->expects( $this->exactly( 3 ) )->method( 'autoCreatedAccount' )
->with( $callback, $callback2 );
@ -2967,7 +2970,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$user = $this->getMockBuilder( User::class )
->onlyMethods( [ 'addToDatabase' ] )->getMock();
$user->expects( $this->once() )->method( 'addToDatabase' )
->will( $this->throwException( new \Exception( 'Excepted' ) ) );
->willThrowException( new \Exception( 'Excepted' ) );
$user->setName( $username );
try {
$this->manager->autoCreateUser( $user, AuthManager::AUTOCREATE_SOURCE_SESSION, true, true );
@ -3053,8 +3056,11 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$logger->clearBuffer();
$this->assertSame(
$maxLogId,
$dbw->selectField( 'logging', 'MAX(log_id)', [ 'log_type' => 'newusers' ] )
);
$dbw->newSelectQueryBuilder()
->select( 'MAX(log_id)' )
->from( 'logging' )
->where( [ 'log_type' => 'newusers' ] )
->fetchField() );
$this->config->set( MainConfigNames::NewUserLog, true );
$session->clear();

View file

@ -600,7 +600,11 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiIntegrati
$expectExpiry,
wfTimestampOrNull(
TS_MW,
$dbw->selectField( 'user', 'user_password_expires', [ 'user_name' => $cuser ] )
$dbw->newSelectQueryBuilder()
->select( 'user_password_expires' )
->from( 'user' )
->where( [ 'user_name' => $cuser ] )
->fetchField()
)
);
}

View file

@ -565,7 +565,11 @@ class TemporaryPasswordPrimaryAuthenticationProviderTest extends \MediaWikiInteg
'new password should pass'
);
$this->assertNotNull(
$dbw->selectField( 'user', 'user_newpass_time', [ 'user_name' => $user ] )
$dbw->newSelectQueryBuilder()
->select( 'user_newpass_time' )
->from( 'user' )
->where( [ 'user_name' => $user ] )
->fetchField()
);
} else {
$this->assertEquals(
@ -579,7 +583,11 @@ class TemporaryPasswordPrimaryAuthenticationProviderTest extends \MediaWikiInteg
'new password should fail'
);
$this->assertNull(
$dbw->selectField( 'user', 'user_newpass_time', [ 'user_name' => $user ] )
$dbw->newSelectQueryBuilder()
->select( 'user_newpass_time' )
->from( 'user' )
->where( [ 'user_name' => $user ] )
->fetchField()
);
}
}

View file

@ -735,12 +735,11 @@ class DatabaseBlockTest extends MediaWikiLangTestCase {
$this->assertFalse( $result );
// Ensure that there are no restrictions where the blockId is 0.
$count = $this->db->selectRowCount(
'ipblocks_restrictions',
'*',
[ 'ir_ipb_id' => 0 ],
__METHOD__
);
$count = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'ipblocks_restrictions' )
->where( [ 'ir_ipb_id' => 0 ] )
->caller( __METHOD__ )->fetchRowCount();
$this->assertSame( 0, $count );
$blockStore->deleteBlock( $block );

View file

@ -73,7 +73,11 @@ class LinksDeletionUpdateTest extends MediaWikiLangTestCase {
'templatelinks' => 'tl_from',
];
foreach ( $tables as $table => $fromField ) {
$res = $this->db->select( $table, [ 1 ], [ $fromField => $id ], __METHOD__ );
$res = $this->db->newSelectQueryBuilder()
->select( [ 1 ] )
->from( $table )
->where( [ $fromField => $id ] )
->caller( __METHOD__ )->fetchResultSet();
$this->assertSame( 1, $res->numRows(), "Number of rows in table $table" );
}
@ -84,7 +88,11 @@ class LinksDeletionUpdateTest extends MediaWikiLangTestCase {
$linksDeletionUpdate->doUpdate();
foreach ( $tables as $table => $fromField ) {
$res = $this->db->select( $table, [ 1 ], [ $fromField => $id ], __METHOD__ );
$res = $this->db->newSelectQueryBuilder()
->select( [ 1 ] )
->from( $table )
->where( [ $fromField => $id ] )
->caller( __METHOD__ )->fetchResultSet();
$this->assertSame( 0, $res->numRows(), "Number of rows in table $table" );
}
}

View file

@ -962,8 +962,12 @@ class LocalFileTest extends MediaWikiIntegrationTestCase {
$file->load();
$file->maybeUpgradeRow();
$metadata = $dbw->decodeBlob( $dbw->selectField( 'image', 'img_metadata',
[ 'img_name' => 'Test.pdf' ], __METHOD__ ) );
$metadata = $dbw->decodeBlob( $dbw->newSelectQueryBuilder()
->select( 'img_metadata' )
->from( 'image' )
->where( [ 'img_name' => 'Test.pdf' ] )
->caller( __METHOD__ )->fetchField()
);
$this->assertStringMatchesFormat( $expected, $metadata );
}
@ -1016,8 +1020,12 @@ class LocalFileTest extends MediaWikiIntegrationTestCase {
$file = new LocalFile( $title, $repo );
$file->load();
$file->maybeUpgradeRow();
$metadata = $dbw->decodeBlob( $dbw->selectField( 'image', 'img_metadata',
[ 'img_name' => 'Png-native-test.png' ] ) );
$metadata = $dbw->decodeBlob( $dbw->newSelectQueryBuilder()
->select( 'img_metadata' )
->from( 'image' )
->where( [ 'img_name' => 'Png-native-test.png' ] )
->fetchField()
);
// Just confirm that it looks like JSON with real metadata
$this->assertStringStartsWith( '{"data":{"frameCount":0,', $metadata );

View file

@ -73,12 +73,11 @@ class ClearUserWatchlistJobTest extends MediaWikiIntegrationTestCase {
$watchedItemStore->addWatch( $user, new TitleValue( 0, __METHOD__ . 'has expiry' ), '1 week' );
// Get the IDs of these items.
$itemIds = $this->db->selectFieldValues(
[ 'watchlist' ],
'wl_id',
[ 'wl_user' => $user->getId() ],
__METHOD__
);
$itemIds = $this->db->newSelectQueryBuilder()
->select( 'wl_id' )
->from( 'watchlist' )
->where( [ 'wl_user' => $user->getId() ] )
->caller( __METHOD__ )->fetchFieldValues();
// Clear the watchlist by running the job.
$job = new ClearUserWatchlistJob( [
@ -89,12 +88,11 @@ class ClearUserWatchlistJobTest extends MediaWikiIntegrationTestCase {
$this->runJobs( [ 'complete' => false ], [ 'maxJobs' => 1 ] );
// Confirm that there are now no expiry records.
$watchedCount = $this->db->selectRowCount(
'watchlist_expiry',
'*',
[ 'we_item' => $itemIds ],
__METHOD__
);
$watchedCount = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'watchlist_expiry' )
->where( [ 'we_item' => $itemIds ] )
->caller( __METHOD__ )->fetchRowCount();
$this->assertSame( 0, $watchedCount );
}
}

View file

@ -651,11 +651,11 @@ class PageStoreTest extends MediaWikiIntegrationTestCase {
$existingPage = $this->getExistingTestPage();
$pageStore = $this->getPageStore();
$row = $this->db->selectRow(
'page',
$pageStore->getSelectFields(),
[ 'page_id' => $existingPage->getId() ]
);
$row = $this->db->newSelectQueryBuilder()
->select( $pageStore->getSelectFields() )
->from( 'page' )
->where( [ 'page_id' => $existingPage->getId() ] )
->fetchRow();
$rec = $pageStore->newPageRecordFromRow( $row );
$this->assertSamePage( $existingPage, $rec );

View file

@ -435,14 +435,11 @@ class WikiPageDbTest extends MediaWikiLangTestCase {
// as long as no garbage is written to the database.
}
$row = $this->db->selectRow(
'page',
'*',
[
'page_namespace' => $title->getNamespace(),
'page_title' => $title->getDBkey()
]
);
$row = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'page' )
->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey() ] )
->fetchRow();
$this->assertFalse( $row );
}
@ -1547,11 +1544,19 @@ more stuff
] ] );
// Check the page_random field has been filled
$pageRandom = $this->db->selectField( 'page', 'page_random', $condition );
$pageRandom = $this->db->newSelectQueryBuilder()
->select( 'page_random' )
->from( 'page' )
->where( $condition )
->fetchField();
$this->assertTrue( (float)$pageRandom < 1 && (float)$pageRandom > 0 );
// Assert the touched timestamp in the DB is roughly when we inserted the page
$pageTouched = $this->db->selectField( 'page', 'page_touched', $condition );
$pageTouched = $this->db->newSelectQueryBuilder()
->select( 'page_touched' )
->from( 'page' )
->where( $condition )
->fetchField();
$this->assertTrue(
wfTimestamp( TS_UNIX, $startTimeStamp )
<= wfTimestamp( TS_UNIX, $pageTouched )
@ -2037,7 +2042,11 @@ more stuff
public function testGetTouched() {
$page = $this->createPage( __METHOD__, 'whatever' );
$touched = $this->db->selectField( 'page', 'page_touched', [ 'page_id' => $page->getId() ] );
$touched = $this->db->newSelectQueryBuilder()
->select( 'page_touched' )
->from( 'page' )
->where( [ 'page_id' => $page->getId() ] )
->fetchField();
$touched = MWTimestamp::convert( TS_MW, $touched );
// Internal cache of the touched time was set after the page was created

View file

@ -8,7 +8,7 @@ use MediaWikiIntegrationTestCase;
/**
* @group Session
* @group Database
* @covers MediaWiki\Session\UserInfo
* @covers \MediaWiki\Session\UserInfo
*/
class UserInfoTest extends MediaWikiIntegrationTestCase {
@ -26,7 +26,10 @@ class UserInfoTest extends MediaWikiIntegrationTestCase {
}
public function testNewFromId() {
$id = wfGetDB( DB_PRIMARY )->selectField( 'user', 'MAX(user_id)' ) + 1;
$id = $this->getDb()->newSelectQueryBuilder()
->select( 'MAX(user_id)' )
->from( 'user' )
->fetchField() + 1;
try {
UserInfo::newFromId( $id );
$this->fail( 'Expected exception not thrown' );

View file

@ -389,12 +389,11 @@ class SpecialBlockTest extends SpecialPageTestBase {
$this->assertSame( [], $block->getRestrictions() );
// Ensure that there are no restrictions where the blockId is 0.
$count = $this->db->selectRowCount(
'ipblocks_restrictions',
'*',
[ 'ir_ipb_id' => 0 ],
__METHOD__
);
$count = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'ipblocks_restrictions' )
->where( [ 'ir_ipb_id' => 0 ] )
->caller( __METHOD__ )->fetchRowCount();
$this->assertSame( 0, $count );
}

View file

@ -761,12 +761,10 @@ class TitleTest extends MediaWikiIntegrationTestCase {
*/
public function testNewFromMissingId() {
// Testing return of null for an id that does not exist
$maxPageId = (int)$this->db->selectField(
'page',
'max(page_id)',
'',
__METHOD__
);
$maxPageId = (int)$this->db->newSelectQueryBuilder()
->select( 'max(page_id)' )
->from( 'page' )
->caller( __METHOD__ )->fetchField();
$res = Title::newFromID( $maxPageId + 1 );
$this->assertNull( $res, 'newFromID returns null for missing ids' );
}

View file

@ -770,9 +770,11 @@ class UserTest extends MediaWikiIntegrationTestCase {
$user->saveSettings();
$this->assertSame(
$user->getName(),
$this->db->selectField(
'actor', 'actor_name', [ 'actor_id' => $user->getActorId() ], __METHOD__
),
$this->db->newSelectQueryBuilder()
->select( 'actor_name' )
->from( 'actor' )
->where( [ 'actor_id' => $user->getActorId() ] )
->caller( __METHOD__ )->fetchField(),
'User::saveSettings updates actor table for name change'
);

View file

@ -422,20 +422,29 @@ class WatchedItemStoreIntegrationTest extends MediaWikiIntegrationTestCase {
[ 'we_item' => '100001', 'we_expiry' => $this->db->timestamp( '30300101000000' ) ],
];
$this->db->insert( 'watchlist_expiry', $orphanRows, __METHOD__ );
$initialRowCount = $this->db->selectRowCount( 'watchlist_expiry', '*', [], __METHOD__ );
$initialRowCount = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'watchlist_expiry' )
->caller( __METHOD__ )->fetchRowCount();
// Make sure the orphans aren't removed if it's not requested.
$store->removeExpired( 10, false );
$this->assertSame(
$initialRowCount,
$this->db->selectRowCount( 'watchlist_expiry', '*', [], __METHOD__ )
$this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'watchlist_expiry' )
->caller( __METHOD__ )->fetchRowCount()
);
// Make sure they are removed when requested.
$store->removeExpired( 10, true );
$this->assertSame(
$initialRowCount - 2,
$this->db->selectRowCount( 'watchlist_expiry', '*', [], __METHOD__ )
$this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'watchlist_expiry' )
->caller( __METHOD__ )->fetchRowCount()
);
}
}

View file

@ -376,16 +376,16 @@ class DatabaseBlockStoreTest extends MediaWikiIntegrationTestCase {
* @param bool $expected Whether to expect to find any rows
*/
private function assertPurgeWorked( int $blockId, bool $expected ): void {
$blockRows = (bool)$this->db->select(
'ipblocks',
'ipb_id',
[ 'ipb_id' => $blockId ]
)->numRows();
$blockRestrictionsRows = (bool)$this->db->select(
'ipblocks_restrictions',
'ir_ipb_id',
[ 'ir_ipb_id' => $blockId ]
)->numRows();
$blockRows = (bool)$this->db->newSelectQueryBuilder()
->select( 'ipb_id' )
->from( 'ipblocks' )
->where( [ 'ipb_id' => $blockId ] )
->fetchResultSet()->numRows();
$blockRestrictionsRows = (bool)$this->db->newSelectQueryBuilder()
->select( 'ir_ipb_id' )
->from( 'ipblocks_restrictions' )
->where( [ 'ir_ipb_id' => $blockId ] )
->fetchResultSet()->numRows();
$this->assertSame( $expected, $blockRows );
$this->assertSame( $expected, $blockRestrictionsRows );

View file

@ -68,7 +68,11 @@ class ResultWrapperTest extends MediaWikiIntegrationTestCase {
7 => (object)[ 'col_a' => '8', 'col_b' => 'h' ]
];
$res = $this->db->select( 'ResultWrapperTest', [ 'col_a', 'col_b' ], '1 = 1', __METHOD__ );
$res = $this->db->newSelectQueryBuilder()
->select( [ 'col_a', 'col_b' ] )
->from( 'ResultWrapperTest' )
->where( '1 = 1' )
->caller( __METHOD__ )->fetchResultSet();
$this->assertSame( 8, $res->numRows() );
$this->assertTrue( $res->valid() );
@ -107,26 +111,29 @@ class ResultWrapperTest extends MediaWikiIntegrationTestCase {
}
public function testCurrentNoResults() {
$res = $this->db->select( 'ResultWrapperTest',
[ 'col_a', 'col_b' ],
'1 = 0',
__METHOD__ );
$res = $this->db->newSelectQueryBuilder()
->select( [ 'col_a', 'col_b' ] )
->from( 'ResultWrapperTest' )
->where( '1 = 0' )
->caller( __METHOD__ )->fetchResultSet();
$this->assertFalse( $res->current() );
}
public function testValidNoResults() {
$res = $this->db->select( 'ResultWrapperTest',
[ 'col_a', 'col_b' ],
'1 = 0',
__METHOD__ );
$res = $this->db->newSelectQueryBuilder()
->select( [ 'col_a', 'col_b' ] )
->from( 'ResultWrapperTest' )
->where( '1 = 0' )
->caller( __METHOD__ )->fetchResultSet();
$this->assertFalse( $res->valid() );
}
public function testSeekNoResults() {
$res = $this->db->select( 'ResultWrapperTest',
[ 'col_a', 'col_b' ],
'1 = 0',
__METHOD__ );
$res = $this->db->newSelectQueryBuilder()
->select( [ 'col_a', 'col_b' ] )
->from( 'ResultWrapperTest' )
->where( '1 = 0' )
->caller( __METHOD__ )->fetchResultSet();
$res->seek( 0 );
$this->assertTrue( true ); // no error
}
@ -142,10 +149,11 @@ class ResultWrapperTest extends MediaWikiIntegrationTestCase {
[ [ 'col_a' => $i, 'col_b' => $i ] ],
__METHOD__ );
}
$res = $this->db->select( 'ResultWrapperTest',
[ 'col_a', 'col_b' ],
'1 = 0',
__METHOD__ );
$res = $this->db->newSelectQueryBuilder()
->select( [ 'col_a', 'col_b' ] )
->from( 'ResultWrapperTest' )
->where( '1 = 0' )
->caller( __METHOD__ )->fetchResultSet();
$this->expectException( OutOfBoundsException::class );
$res->seek( $seekPos );
}

View file

@ -18,7 +18,11 @@ class DatabaseIntegrationTest extends MediaWikiIntegrationTestCase {
}
public function testUnknownTableCorruptsResults() {
$res = $this->db->select( 'page', '*', [ 'page_id' => 1 ] );
$res = $this->db->newSelectQueryBuilder()
->select( '*' )
->from( 'page' )
->where( [ 'page_id' => 1 ] )
->fetchResultSet();
$this->assertFalse( $this->db->tableExists( 'foobarbaz' ) );
$this->assertIsInt( $res->numRows() );
}