mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-12-02 19:36:48 +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
113 lines
3.1 KiB
PHP
113 lines
3.1 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
|
use MediaWiki\Tests\Api\ApiTestCase;
|
|
|
|
/**
|
|
* @group Echo
|
|
* @group Database
|
|
* @group medium
|
|
*/
|
|
class TalkPageFunctionalTest extends ApiTestCase {
|
|
|
|
protected function tearDown(): void {
|
|
Event::$alwaysInsert = false;
|
|
parent::tearDown();
|
|
}
|
|
|
|
/**
|
|
* Creates and updates a user talk page a few times to ensure proper events are
|
|
* created.
|
|
* @covers \MediaWiki\Extension\Notifications\DiscussionParser
|
|
*/
|
|
public function testAddCommentsToTalkPage() {
|
|
Event::$alwaysInsert = true;
|
|
|
|
$editor = $this->getTestSysop()->getUser();
|
|
$talkTitle = $this->getTestSysop()->getUser()->getTalkPage();
|
|
$talkPage = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $talkTitle );
|
|
|
|
$expectedMessageCount = 0;
|
|
$this->assertCount( $expectedMessageCount, $this->fetchAllEvents() );
|
|
|
|
// Start a talkpage
|
|
$expectedMessageCount++;
|
|
$content = "== Section 8 ==\n\nblah blah ~~~~\n";
|
|
$this->editPage(
|
|
$talkPage,
|
|
$content,
|
|
'',
|
|
NS_USER_TALK,
|
|
$editor
|
|
);
|
|
|
|
// Ensure the proper event was created
|
|
$events = $this->fetchAllEvents();
|
|
$this->assertCount( $expectedMessageCount, $events, 'After initial edit a single event must exist.' );
|
|
$row = array_pop( $events );
|
|
$this->assertEquals( 'edit-user-talk', $row->event_type );
|
|
$this->assertEventSectionTitle( 'Section 8', $row );
|
|
|
|
// Add another message to the talk page
|
|
$expectedMessageCount++;
|
|
$content .= "More content ~~~~\n";
|
|
$this->editPage(
|
|
$talkPage,
|
|
$content,
|
|
'',
|
|
NS_USER_TALK,
|
|
$editor
|
|
);
|
|
|
|
// Ensure another event was created
|
|
$events = $this->fetchAllEvents();
|
|
$this->assertCount( $expectedMessageCount, $events );
|
|
$row = array_pop( $events );
|
|
$this->assertEquals( 'edit-user-talk', $row->event_type );
|
|
$this->assertEventSectionTitle( 'Section 8', $row );
|
|
|
|
// Add a new section and a message within it
|
|
$expectedMessageCount++;
|
|
$content .= "\n\n== EE ==\n\nhere we go with a new section ~~~~\n";
|
|
$this->editPage(
|
|
$talkPage,
|
|
$content,
|
|
'',
|
|
NS_USER_TALK,
|
|
$editor
|
|
);
|
|
|
|
// Ensure this event has the new section title
|
|
$events = $this->fetchAllEvents();
|
|
$this->assertCount( $expectedMessageCount, $events );
|
|
$row = array_pop( $events );
|
|
$this->assertEquals( 'edit-user-talk', $row->event_type );
|
|
$this->assertEventSectionTitle( 'EE', $row );
|
|
}
|
|
|
|
protected function assertEventSectionTitle( $sectionTitle, $row ) {
|
|
$this->assertNotNull( $row->event_extra, 'Event must contain extra data.' );
|
|
$extra = unserialize( $row->event_extra );
|
|
$this->assertArrayHasKey( 'section-title', $extra, 'Extra data must include a section-title key.' );
|
|
$this->assertEquals( $sectionTitle, $extra['section-title'], 'Detected section title must match' );
|
|
}
|
|
|
|
/**
|
|
* @return \stdClass[] All talk page edit events in db sorted from oldest to newest
|
|
*/
|
|
protected function fetchAllEvents() {
|
|
$res = $this->getDb()->newSelectQueryBuilder()
|
|
->select( Event::selectFields() )
|
|
->from( 'echo_event' )
|
|
->where( [
|
|
'event_type' => 'edit-user-talk',
|
|
] )
|
|
->orderBy( 'event_id' )
|
|
->caller( __METHOD__ )
|
|
->fetchResultSet();
|
|
|
|
return iterator_to_array( $res );
|
|
}
|
|
|
|
}
|