Add test for WikiPage post-edit stats update
Bug: T187585 Change-Id: I734b7f42799a9bfe77a1fb1269bb3d48b3852ffd
This commit is contained in:
parent
8be694c78f
commit
ab2bf12cf8
2 changed files with 83 additions and 0 deletions
|
|
@ -66,6 +66,12 @@ class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate {
|
|||
public static function factory( array $deltas ) {
|
||||
$update = new self( 0, 0, 0 );
|
||||
|
||||
foreach ( $deltas as $name => $unused ) {
|
||||
if ( !in_array( $name, self::$counters ) ) { // T187585
|
||||
throw new UnexpectedValueException( __METHOD__ . ": no field called '$name'" );
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( self::$counters as $field ) {
|
||||
if ( isset( $deltas[$field] ) && $deltas[$field] ) {
|
||||
$update->$field = $deltas[$field];
|
||||
|
|
|
|||
77
tests/phpunit/includes/deferred/SiteStatsUpdateTest.php
Normal file
77
tests/phpunit/includes/deferred/SiteStatsUpdateTest.php
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
use Wikimedia\TestingAccessWrapper;
|
||||
|
||||
/**
|
||||
* @group Database
|
||||
*/
|
||||
class SiteStatsUpdateTest extends MediaWikiTestCase {
|
||||
/**
|
||||
* @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->assertEquals( 1, $wrapped->pages );
|
||||
$this->assertEquals( 3, $wrapped->users );
|
||||
$this->assertEquals( 1, $wrapped->images );
|
||||
$this->assertEquals( 0, $wrapped->edits );
|
||||
$this->assertEquals( 0, $wrapped->articles );
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers SiteStatsUpdate::doUpdate()
|
||||
* @covers SiteStatsInit::refresh()
|
||||
*/
|
||||
public function testDoUpdate() {
|
||||
$this->setMwGlobals( 'wgSiteStatsAsyncFactor', false );
|
||||
$this->setMwGlobals( 'wgCommandLineMode', false ); // disable opportunistic updates
|
||||
|
||||
$dbw = wfGetDB( DB_MASTER );
|
||||
$statsInit = new SiteStatsInit( $dbw );
|
||||
$statsInit->refresh();
|
||||
|
||||
$ei = SiteStats::edits(); // trigger load
|
||||
$pi = SiteStats::pages();
|
||||
$ui = SiteStats::users();
|
||||
$fi = SiteStats::images();
|
||||
$ai = SiteStats::articles();
|
||||
|
||||
$dbw->begin( __METHOD__ ); // block opportunistic updates
|
||||
|
||||
$update = SiteStatsUpdate::factory( [ 'pages' => 2, 'images' => 1, 'edits' => 2 ] );
|
||||
$this->assertEquals( 0, DeferredUpdates::pendingUpdatesCount() );
|
||||
$update->doUpdate();
|
||||
$this->assertEquals( 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->assertEquals( 1, DeferredUpdates::pendingUpdatesCount() );
|
||||
|
||||
$dbw->commit( __METHOD__ );
|
||||
|
||||
$this->assertEquals( 1, DeferredUpdates::pendingUpdatesCount() );
|
||||
DeferredUpdates::doUpdates();
|
||||
$this->assertEquals( 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();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue