Merge "Call IDatabase::timestamp before inserting echo_unread_wikis"

This commit is contained in:
jenkins-bot 2022-04-11 23:28:32 +00:00 committed by Gerrit Code Review
commit 7d54187ce0
3 changed files with 90 additions and 8 deletions

View file

@ -211,6 +211,11 @@ class EchoHooks implements RecentChange_saveHook {
$updater->modifyExtensionField( 'echo_unread_wikis', 'euw_wiki', $updater->modifyExtensionField( 'echo_unread_wikis', 'euw_wiki',
"$dir/db_patches/patch-increase-varchar-echo_unread_wikis-euw_wiki.sql" ); "$dir/db_patches/patch-increase-varchar-echo_unread_wikis-euw_wiki.sql" );
global $wgWikimediaJenkinsCI;
if ( !empty( $wgWikimediaJenkinsCI ) ) {
$updater->addExtensionTable( 'echo_unread_wikis', "$dir/db_patches/echo_unread_wikis.sql" );
}
} }
/** /**

View file

@ -9,6 +9,7 @@ use MediaWiki\User\UserIdentity;
class EchoUnreadWikis { class EchoUnreadWikis {
private const DEFAULT_TS = '00000000000000'; private const DEFAULT_TS = '00000000000000';
private const DEFAULT_TS_DB = '00010101010101';
/** /**
* @var int * @var int
@ -82,11 +83,13 @@ class EchoUnreadWikis {
$wikis[$row->euw_wiki] = [ $wikis[$row->euw_wiki] = [
EchoAttributeManager::ALERT => [ EchoAttributeManager::ALERT => [
'count' => $row->euw_alerts, 'count' => $row->euw_alerts,
'ts' => $row->euw_alerts_ts, 'ts' => $row->euw_alerts_ts === static::DEFAULT_TS_DB ?
static::DEFAULT_TS : wfTimestamp( TS_MW, $row->euw_alerts_ts ),
], ],
EchoAttributeManager::MESSAGE => [ EchoAttributeManager::MESSAGE => [
'count' => $row->euw_messages, 'count' => $row->euw_messages,
'ts' => $row->euw_messages_ts, 'ts' => $row->euw_messages_ts === static::DEFAULT_TS_DB ?
static::DEFAULT_TS : wfTimestamp( TS_MW, $row->euw_messages_ts ),
], ],
]; ];
} }
@ -117,13 +120,17 @@ class EchoUnreadWikis {
if ( $alertCount || $msgCount ) { if ( $alertCount || $msgCount ) {
$values = [ $values = [
'euw_alerts' => $alertCount, 'euw_alerts' => $alertCount,
'euw_alerts_ts' => $alertTime 'euw_alerts_ts' => $dbw->timestamp(
? $alertTime->getTimestamp( TS_MW ) $alertTime
: static::DEFAULT_TS, ? $alertTime->getTimestamp( TS_MW )
: static::DEFAULT_TS_DB
),
'euw_messages' => $msgCount, 'euw_messages' => $msgCount,
'euw_messages_ts' => $msgTime 'euw_messages_ts' => $dbw->timestamp(
? $msgTime->getTimestamp( TS_MW ) $msgTime
: static::DEFAULT_TS, ? $msgTime->getTimestamp( TS_MW )
: static::DEFAULT_TS_DB
),
]; ];
// when there is unread alert(s) and/or message(s), upsert the row // when there is unread alert(s) and/or message(s), upsert the row

View file

@ -0,0 +1,70 @@
<?php
use Wikimedia\TestingAccessWrapper;
/**
* Tests for unread wiki database access
*
* @group Database
* @covers \EchoUnreadWikis
*/
class UnreadWikisTest extends MediaWikiIntegrationTestCase {
public function testUpdateCount() {
$unread = TestingAccessWrapper::newFromObject( new EchoUnreadWikis( 1 ) );
$unread->dbFactory = $this->mockMWEchoDbFactory( $this->db );
$unread->updateCount(
'foobar',
2,
new MWTimestamp( '20220322222222' ),
3,
new MWTimestamp( '20220322222223' )
);
$this->assertSame(
[
'foobar' => [
'alert' => [ 'count' => '2', 'ts' => '20220322222222' ],
'message' => [ 'count' => '3', 'ts' => '20220322222223' ],
]
],
$unread->getUnreadCounts()
);
}
public function testUpdateCountFalse() {
$unread = TestingAccessWrapper::newFromObject( new EchoUnreadWikis( 1 ) );
$unread->dbFactory = $this->mockMWEchoDbFactory( $this->db );
$unread->updateCount(
'foobar',
3,
false,
4,
false
);
$this->assertSame(
[
'foobar' => [
'alert' => [ 'count' => '3', 'ts' => '00000000000000' ],
'message' => [ 'count' => '4', 'ts' => '00000000000000' ],
]
],
$unread->getUnreadCounts()
);
}
/**
* Mock object of MWEchoDbFactory
* @param \Wikimedia\Rdbms\IDatabase $db
* @return MWEchoDbFactory
*/
protected function mockMWEchoDbFactory( $db ) {
$dbFactory = $this->getMockBuilder( MWEchoDbFactory::class )
->disableOriginalConstructor()
->getMock();
$dbFactory->expects( $this->any() )
->method( 'getSharedDb' )
->will( $this->returnValue( $db ) );
return $dbFactory;
}
}