mediawiki-extensions-Echo/tests/phpunit/TalkPageFunctionalTest.php
Ostrzyciel a585ad635a Explicitly provide sysop user in unit tests that need it
Currently Echo assumes the default user used when ->editPage() is
called to be a sysop user… which is kind of the case? It passed null
to WikiPage::doEditContent() which in turn falls back to the default
user (127.0.0.1). In a test that relies on the user to be a sysop, it's
better to provide the user explicitly to avoid confusion.

This is to clean up the confusion introduced by ->editPage()'s doc
block, see: I9f77474f40e0f6901aa2c6f846e471b822636aa5

Change-Id: I7a79e0eaa1617e4d87a8d615a5391723c0e30b6a
2020-09-13 08:16:45 +00:00

100 lines
2.8 KiB
PHP

<?php
/**
* @group Echo
* @group Database
* @group medium
*/
class EchoTalkPageFunctionalTest extends ApiTestCase {
protected function setUp() : void {
parent::setUp();
$this->db->delete( 'echo_event', '*' );
}
/**
* Creates and updates a user talk page a few times to ensure proper events are
* created. The user performing the edits is self::$users['sysop'].
* @covers \EchoDiscussionParser
*/
public function testAddCommentsToTalkPage() {
$talkPage = self::$users['uploader']->getUser()->getName();
$messageCount = 0;
$this->assertCount( $messageCount, $this->fetchAllEvents() );
// Start a talkpage
$content = "== Section 8 ==\n\nblah blah ~~~~\n";
$this->editPage(
$talkPage,
$content,
'',
NS_USER_TALK,
self::$users['sysop']->getUser()
);
// Ensure the proper event was created
$events = $this->fetchAllEvents();
// +1 is due to 0 index
$this->assertCount( 1 + $messageCount, $events, 'After initial edit a single event must exist.' );
$row = array_shift( $events );
$this->assertEquals( 'edit-user-talk', $row->event_type );
$this->assertEventSectionTitle( 'Section 8', $row );
// Add another message to the talk page
$messageCount++;
$content .= "More content ~~~~\n";
$this->editPage(
$talkPage,
$content,
'',
NS_USER_TALK,
self::$users['sysop']->getUser()
);
// Ensure another event was created
$events = $this->fetchAllEvents();
$this->assertCount( 1 + $messageCount, $events );
$row = array_shift( $events );
$this->assertEquals( 'edit-user-talk', $row->event_type );
$this->assertEventSectionTitle( 'Section 8', $row );
// Add a new section and a message within it
$messageCount++;
$content .= "\n\n== EE ==\n\nhere we go with a new section ~~~~\n";
$this->editPage(
$talkPage,
$content,
'',
NS_USER_TALK,
self::$users['sysop']->getUser()
);
// Ensure this event has the new section title
$events = $this->fetchAllEvents();
$this->assertCount( 1 + $messageCount, $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 non-watchlist events in db sorted from oldest to newest
*/
protected function fetchAllEvents() {
$res = $this->db->select( 'echo_event', EchoEvent::selectFields(), [
'event_type != "watchlist-change"'
], __METHOD__, [ 'ORDER BY' => 'event_id ASC' ] );
return iterator_to_array( $res );
}
}