3 use MediaWiki\Deferred\DeferredUpdates
;
4 use MediaWiki\Deferred\SiteStatsUpdate
;
5 use MediaWiki\SiteStats\SiteStats
;
6 use MediaWiki\SiteStats\SiteStatsInit
;
7 use Wikimedia\TestingAccessWrapper
;
11 * @covers \MediaWiki\Deferred\SiteStatsUpdate
12 * @covers \MediaWiki\SiteStats\SiteStats
13 * @covers \MediaWiki\SiteStats\SiteStatsInit
15 class SiteStatsUpdateTest
extends MediaWikiIntegrationTestCase
{
16 public function testFactoryAndMerge() {
17 $update1 = SiteStatsUpdate
::factory( [ 'pages' => 1, 'users' => 2 ] );
18 $update2 = SiteStatsUpdate
::factory( [ 'users' => 1, 'images' => 1 ] );
20 $update1->merge( $update2 );
21 $wrapped = TestingAccessWrapper
::newFromObject( $update1 );
23 $this->assertSame( 1, $wrapped->pages
);
24 $this->assertEquals( 3, $wrapped->users
);
25 $this->assertSame( 1, $wrapped->images
);
26 $this->assertSame( 0, $wrapped->edits
);
27 $this->assertSame( 0, $wrapped->articles
);
30 public function testDoUpdate() {
31 $dbw = $this->getDb();
32 $statsInit = new SiteStatsInit( $dbw );
33 $statsInit->refresh();
35 $ei = SiteStats
::edits(); // trigger load
36 $pi = SiteStats
::pages();
37 $ui = SiteStats
::users();
38 $fi = SiteStats
::images();
39 $ai = SiteStats
::articles();
41 $this->assertSame( 0, DeferredUpdates
::pendingUpdatesCount() );
43 $dbw->begin( __METHOD__
); // block opportunistic updates
45 DeferredUpdates
::addUpdate(
46 SiteStatsUpdate
::factory( [ 'pages' => 2, 'images' => 1, 'edits' => 2 ] )
48 $this->assertSame( 1, DeferredUpdates
::pendingUpdatesCount() );
52 $this->assertEquals( $pi, SiteStats
::pages(), 'page count' );
53 $this->assertEquals( $ei, SiteStats
::edits(), 'edit count' );
54 $this->assertEquals( $ui, SiteStats
::users(), 'user count' );
55 $this->assertEquals( $fi, SiteStats
::images(), 'file count' );
56 $this->assertEquals( $ai, SiteStats
::articles(), 'article count' );
57 $this->assertSame( 1, DeferredUpdates
::pendingUpdatesCount() );
59 // This also notifies DeferredUpdates to do an opportunistic run
60 $dbw->commit( __METHOD__
);
61 $this->runDeferredUpdates();
62 $this->assertSame( 0, DeferredUpdates
::pendingUpdatesCount() );
65 $this->assertEquals( $pi +
2, SiteStats
::pages(), 'page count' );
66 $this->assertEquals( $ei +
2, SiteStats
::edits(), 'edit count' );
67 $this->assertEquals( $ui, SiteStats
::users(), 'user count' );
68 $this->assertEquals( $fi +
1, SiteStats
::images(), 'file count' );
69 $this->assertEquals( $ai, SiteStats
::articles(), 'article count' );
71 $statsInit = new SiteStatsInit();
72 $statsInit->refresh();