mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-12-20 19:53:09 +00:00
4ae63d1b4d
Why: * On wikis with lots of bot activity like Wikidata, there is a large volume of edits which can potentially create an article-linked notification. These notifications are now actually rarely sent because they are disabled for bots (T318523). However, the event record is always inserted into the database, with no reference to it, bloating the database. What: * Do not unconditionally insert an event into the database when Event::create is called. Pass it to downstream calls and have it inserted when it's clear it will actually be needed (i.e., a notification is definitely going to be created). * Pass the event's payload to the job queue instead of requiring its ID. Introduce Event::newFromArray, which unlike ::loadFromRow handles ::toDbArray values that haven't been inserted into the database yet. * Introduce Event::acquireId which ensures the event has been inserted prior to returning its ID as well as it does not get re-inserted. Bug: T221258 Change-Id: I8b9a99a197d6af2845d85d9e35c6703640f70b91
88 lines
2.5 KiB
PHP
88 lines
2.5 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Extension\Notifications\Mapper\EventMapper;
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
|
use MediaWiki\User\UserIdentityValue;
|
|
|
|
/**
|
|
* @covers \MediaWiki\Extension\Notifications\Model\Event
|
|
* @covers \MediaWiki\Extension\Notifications\Controller\NotificationController
|
|
* @covers \MediaWiki\Extension\Notifications\Jobs\NotificationJob
|
|
* @group Database
|
|
*/
|
|
class EventIntegrationTest extends MediaWikiIntegrationTestCase {
|
|
|
|
public function testEventInsertionImmediate() {
|
|
$this->clearHook( 'BeforeEchoEventInsert' );
|
|
$this->overrideConfigValue( 'EchoUseJobQueue', false );
|
|
|
|
$user = $this->getTestUser()->getUser();
|
|
$event = Event::create( [
|
|
'type' => 'welcome',
|
|
'agent' => $user,
|
|
'extra' => [ 'key' => 'value' ]
|
|
] );
|
|
$eventId = $event->getId();
|
|
$this->assertNotFalse( $eventId );
|
|
$this->assertSame( $eventId, $event->acquireId() );
|
|
$this->assertSelect(
|
|
'echo_event',
|
|
[ 'count' => 'COUNT(*)' ],
|
|
[ 'event_type' => 'welcome' ],
|
|
[ [ '1' ] ]
|
|
);
|
|
}
|
|
|
|
public function testEventNotInserted() {
|
|
$this->clearHook( 'BeforeEchoEventInsert' );
|
|
$this->overrideConfigValue( 'EchoUseJobQueue', false );
|
|
|
|
$event = Event::create( [
|
|
'type' => 'welcome',
|
|
// anons cannot be notified
|
|
'agent' => UserIdentityValue::newAnonymous( '1.2.3.4' ),
|
|
'extra' => [ 'key' => 'value' ]
|
|
] );
|
|
$this->assertFalse( $event->getId() );
|
|
$this->assertSelect(
|
|
'echo_event',
|
|
[ 'count' => 'COUNT(*)' ],
|
|
[ 'event_type' => 'welcome' ],
|
|
[ [ '0' ] ]
|
|
);
|
|
}
|
|
|
|
public function testEventInsertionDeferred() {
|
|
$this->clearHook( 'BeforeEchoEventInsert' );
|
|
$this->clearHook( 'PageSaveComplete' );
|
|
$this->overrideConfigValue( 'EchoUseJobQueue', true );
|
|
|
|
$user = $this->getTestUser()->getUser();
|
|
$title = $this->getExistingTestPage()->getTitle();
|
|
$this->runJobs();
|
|
|
|
$event = Event::create( [
|
|
'type' => 'welcome',
|
|
'agent' => $user,
|
|
'title' => $title,
|
|
'extra' => [ 'key' => 'value' ]
|
|
] );
|
|
$this->assertFalse( $event->getId() );
|
|
unset( $event );
|
|
|
|
$jobQueueGroup = $this->getServiceContainer()->getJobQueueGroup();
|
|
$queues = $jobQueueGroup->getQueuesWithJobs();
|
|
$this->assertSame( [ 'EchoNotificationJob' ], $queues );
|
|
$job = $jobQueueGroup->pop( 'EchoNotificationJob' );
|
|
$job->run();
|
|
|
|
$eventMapper = new EventMapper();
|
|
$events = $eventMapper->fetchByPage( $title->getArticleID() );
|
|
$this->assertCount( 1, $events );
|
|
[ $event ] = $events;
|
|
$this->assertSame( 'welcome', $event->getType() );
|
|
$this->assertTrue( $user->equals( $event->getAgent() ) );
|
|
}
|
|
|
|
}
|