wiki.techinc.nl/tests/phpunit/includes/deferred/SiteStatsUpdateTest.php

77 lines
2.4 KiB
PHP
Raw Normal View History

<?php
use Wikimedia\TestingAccessWrapper;
/**
* @group Database
*/
class SiteStatsUpdateTest extends MediaWikiIntegrationTestCase {
/**
* @covers SiteStatsUpdate::factory
* @covers SiteStatsUpdate::merge
*/
public function testFactoryAndMerge() {
$update1 = SiteStatsUpdate::factory( [ 'pages' => 1, 'users' => 2 ] );
$update2 = SiteStatsUpdate::factory( [ 'users' => 1, 'images' => 1 ] );
$update1->merge( $update2 );
$wrapped = TestingAccessWrapper::newFromObject( $update1 );
$this->assertSame( 1, $wrapped->pages );
$this->assertEquals( 3, $wrapped->users );
$this->assertSame( 1, $wrapped->images );
$this->assertSame( 0, $wrapped->edits );
$this->assertSame( 0, $wrapped->articles );
}
/**
* @covers SiteStatsUpdate::doUpdate()
* @covers SiteStatsInit::refresh()
*/
public function testDoUpdate() {
$this->setMwGlobals( 'wgSiteStatsAsyncFactor', false );
$dbw = wfGetDB( DB_PRIMARY );
$statsInit = new SiteStatsInit( $dbw );
$statsInit->refresh();
$ei = SiteStats::edits(); // trigger load
$pi = SiteStats::pages();
$ui = SiteStats::users();
$fi = SiteStats::images();
$ai = SiteStats::articles();
$this->assertSame( 0, DeferredUpdates::pendingUpdatesCount() );
$dbw->begin( __METHOD__ ); // block opportunistic updates
DeferredUpdates::addUpdate(
SiteStatsUpdate::factory( [ 'pages' => 2, 'images' => 1, 'edits' => 2 ] )
);
$this->assertSame( 1, DeferredUpdates::pendingUpdatesCount() );
// Still the same
SiteStats::unload();
$this->assertEquals( $pi, SiteStats::pages(), 'page count' );
$this->assertEquals( $ei, SiteStats::edits(), 'edit count' );
$this->assertEquals( $ui, SiteStats::users(), 'user count' );
$this->assertEquals( $fi, SiteStats::images(), 'file count' );
$this->assertEquals( $ai, SiteStats::articles(), 'article count' );
$this->assertSame( 1, DeferredUpdates::pendingUpdatesCount() );
rdbms: Move setLBFactoryTriggers from doMaintenance to service wiring This logic is not needed to run on every PHP process and was making it difficult to run offline maintenance scripts without additional complexity based on Maintenance::getDbType and DB_NONE. Instead of skipping this only for DB_NONE, and establishing a pattern that may spread to other ad-hoc places throughout the codebase, instead remove this entirely from the eager set up code for all PHP processes and move it to the service wiring and dependency injection. That way, it naturally doesn't happen until and unless the DB service is actually called upon. Scripts and entry point that need to disable the DB service, can continue to use MediaWikiServices::disableStorageBackend. == Impact on SiteStatsUpdate == With wgCommandLineMode no longer being read at run-time from a global, but in service wiring, this means SiteStatsUpdateTest can't change the behaviour between CLI-like and Web-like, unless it e.g. resets the 'DBLoadBalancerFactory' service first. Unfortunately, while most any reset is supported, a reset of the 'DBLoadBalancerFactory' would be unsupported as that would lose the temporary db clone context and such, bringing us to either the developer's live db, or a broken set up altogether. If there is a strong need for toggling oppertunistic updates off and on at run-time, this could be supported in the DeferredUpdates class perhaps, but we already have numerous methods there (incl db begin/commit being a good proxy already), which this test already used, so for now I've just removed the extra assertion for this as it wasn't essential to that test. Bug: T228895 Bug: T238436 Change-Id: Icf29bc484c155f52b6d8f61e5902233a15ba0c6d
2021-04-15 23:08:44 +00:00
// This also notifies DeferredUpdates to do an opportunistic run
$dbw->commit( __METHOD__ );
$this->assertSame( 0, DeferredUpdates::pendingUpdatesCount() );
SiteStats::unload();
$this->assertEquals( $pi + 2, SiteStats::pages(), 'page count' );
$this->assertEquals( $ei + 2, SiteStats::edits(), 'edit count' );
$this->assertEquals( $ui, SiteStats::users(), 'user count' );
$this->assertEquals( $fi + 1, SiteStats::images(), 'file count' );
$this->assertEquals( $ai, SiteStats::articles(), 'article count' );
$statsInit = new SiteStatsInit();
$statsInit->refresh();
}
}