2021-10-13 16:22:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Revision\MutableRevisionRecord;
|
2022-05-27 20:38:32 +00:00
|
|
|
use Wikimedia\Rdbms\FakeResultWrapper;
|
2021-10-13 16:22:07 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test class for HistoryPager methods.
|
|
|
|
|
*
|
|
|
|
|
* @group Pager
|
|
|
|
|
*/
|
|
|
|
|
class HistoryPagerTest extends MediaWikiLangTestCase {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $results for passing to FakeResultWrapper and deriving
|
|
|
|
|
* RevisionRecords and formatted comments.
|
|
|
|
|
* @return HistoryPager
|
|
|
|
|
*/
|
|
|
|
|
private function getHistoryPager( array $results ) {
|
|
|
|
|
$wikiPageMock = $this->getMockBuilder( WikiPage::class )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
$contextMock = $this->getMockBuilder( RequestContext::class )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->onlyMethods( [ 'getRequest', 'getWikiPage', 'getTitle' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$contextMock->method( 'getRequest' )->willReturn(
|
|
|
|
|
new FauxRequest( [] )
|
|
|
|
|
);
|
|
|
|
|
$title = Title::newFromText( 'HistoryPagerTest' );
|
|
|
|
|
$contextMock->method( 'getTitle' )->willReturn( $title );
|
|
|
|
|
|
|
|
|
|
$contextMock->method( 'getWikiPage' )->willReturn( $wikiPageMock );
|
|
|
|
|
$articleMock = $this->getMockBuilder( Article::class )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->onlyMethods( [ 'getContext' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$articleMock->method( 'getContext' )->willReturn( $contextMock );
|
|
|
|
|
|
|
|
|
|
$actionMock = $this->getMockBuilder( HistoryAction::class )
|
|
|
|
|
->setConstructorArgs( [
|
|
|
|
|
$articleMock,
|
|
|
|
|
$contextMock
|
|
|
|
|
] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$actionMock->method( 'getArticle' )->willReturn( $articleMock );
|
|
|
|
|
$actionMock->message = [
|
|
|
|
|
'cur' => 'cur',
|
|
|
|
|
'last' => 'last',
|
|
|
|
|
'tooltip-cur' => '',
|
|
|
|
|
'tooltip-last' => '',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$outputMock = $this->getMockBuilder( OutputPage::class )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->onlyMethods( [ 'wrapWikiMsg' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$pager = $this->getMockBuilder( HistoryPager::class )
|
|
|
|
|
->onlyMethods( [ 'reallyDoQuery', 'doBatchLookups',
|
|
|
|
|
'getOutput' ] )
|
|
|
|
|
->setConstructorArgs( [
|
|
|
|
|
$actionMock
|
|
|
|
|
] )
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$pager->method( 'getOutput' )->willReturn( $outputMock );
|
|
|
|
|
$pager->method( 'reallyDoQuery' )->willReturn(
|
|
|
|
|
new FakeResultWrapper( $results )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// make all the methods in our mock public
|
|
|
|
|
$pager = TestingAccessWrapper::newFromObject( $pager );
|
|
|
|
|
// and update the private properties...
|
|
|
|
|
$pager->formattedComments = array_map( static function ( $result ) {
|
|
|
|
|
return 'dummy comment';
|
|
|
|
|
}, $results );
|
|
|
|
|
|
|
|
|
|
$pager->revisions = array_map( static function ( $result ) {
|
|
|
|
|
$title = Title::newFromText( 'HistoryPagerTest' );
|
|
|
|
|
$r = new MutableRevisionRecord( $title );
|
|
|
|
|
$r->setId( $result[ 'rev_id' ] );
|
|
|
|
|
return $r;
|
|
|
|
|
}, $results );
|
|
|
|
|
|
|
|
|
|
return $pager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers HistoryPager::getBody
|
|
|
|
|
*/
|
|
|
|
|
public function testGetBodyEmpty() {
|
|
|
|
|
$pager = $this->getHistoryPager( [] );
|
|
|
|
|
$html = $pager->getBody();
|
|
|
|
|
$this->assertStringContainsString( 'No matching revisions were found.', $html );
|
|
|
|
|
$this->assertStringNotContainsString( '<h4', $html );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers HistoryPager::getBody
|
|
|
|
|
*/
|
|
|
|
|
public function testGetBodyOneHeading() {
|
|
|
|
|
$pager = $this->getHistoryPager(
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
'rev_page' => 'title',
|
2021-12-04 10:45:06 +00:00
|
|
|
'ts_tags' => '',
|
2021-10-13 16:22:07 +00:00
|
|
|
'rev_deleted' => false,
|
|
|
|
|
'rev_minor_edit' => false,
|
|
|
|
|
'rev_parent_id' => 1,
|
|
|
|
|
'user_name' => 'Jdlrobson',
|
|
|
|
|
'rev_id' => 2,
|
|
|
|
|
'rev_comment_data' => '{}',
|
|
|
|
|
'rev_comment_cid' => '1',
|
|
|
|
|
'rev_comment_text' => 'Created page',
|
|
|
|
|
'rev_timestamp' => '20220101001122',
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$html = $pager->getBody();
|
|
|
|
|
$this->assertStringContainsString( '<h4', $html );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers HistoryPager::getBody
|
|
|
|
|
*/
|
|
|
|
|
public function testGetBodyTwoHeading() {
|
|
|
|
|
$pagerData = [
|
|
|
|
|
'rev_page' => 'title',
|
|
|
|
|
'rev_deleted' => false,
|
|
|
|
|
'rev_minor_edit' => false,
|
2021-12-04 10:45:06 +00:00
|
|
|
'ts_tags' => '',
|
2021-10-13 16:22:07 +00:00
|
|
|
'rev_parent_id' => 1,
|
|
|
|
|
'user_name' => 'Jdlrobson',
|
|
|
|
|
'rev_comment_data' => '{}',
|
|
|
|
|
'rev_comment_cid' => '1',
|
|
|
|
|
'rev_comment_text' => 'Fixed typo',
|
|
|
|
|
];
|
|
|
|
|
$pager = $this->getHistoryPager(
|
|
|
|
|
[
|
|
|
|
|
$pagerData + [
|
|
|
|
|
'rev_id' => 3,
|
|
|
|
|
'rev_timestamp' => '20220301001122',
|
|
|
|
|
],
|
|
|
|
|
$pagerData + [
|
|
|
|
|
'rev_id' => 2,
|
|
|
|
|
'rev_timestamp' => '20220101001122',
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$html = preg_replace( "/\n+/", '', $pager->getBody() );
|
|
|
|
|
$firstHeader = '<h4 class="mw-index-pager-list-header-first mw-index-pager-list-header">1 March 2022</h4>'
|
|
|
|
|
. '<ul class="mw-contributions-list">'
|
|
|
|
|
. '<li data-mw-revid="3">';
|
|
|
|
|
$secondHeader = '<h4 class="mw-index-pager-list-header">1 January 2022</h4>'
|
|
|
|
|
. '<ul class="mw-contributions-list">'
|
|
|
|
|
. '<li data-mw-revid="2">';
|
|
|
|
|
|
|
|
|
|
// Check that the undo links are correct in the topmost displayed row (for rev_id=3).
|
|
|
|
|
// This is tricky because the other rev number (in this example, '2') is magically
|
|
|
|
|
// pulled from the next row, before we've processed that row.
|
|
|
|
|
$this->assertStringContainsString( '&undoafter=2&undo=3', $html );
|
|
|
|
|
|
|
|
|
|
$this->assertStringContainsString( $firstHeader, $html );
|
|
|
|
|
$this->assertStringContainsString( $secondHeader, $html );
|
2022-02-25 13:01:08 +00:00
|
|
|
$this->assertStringContainsString( '<section id="pagehistory"', $html );
|
2021-10-13 16:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers HistoryPager::getBody
|
|
|
|
|
*/
|
|
|
|
|
public function testGetBodyLastItem() {
|
|
|
|
|
$pagerData = [
|
|
|
|
|
'rev_page' => 'title',
|
|
|
|
|
'rev_deleted' => false,
|
|
|
|
|
'rev_minor_edit' => false,
|
2021-12-04 10:45:06 +00:00
|
|
|
'ts_tags' => '',
|
2021-10-13 16:22:07 +00:00
|
|
|
'rev_parent_id' => 1,
|
|
|
|
|
'user_name' => 'Jdlrobson',
|
|
|
|
|
'rev_comment_data' => '{}',
|
|
|
|
|
'rev_comment_cid' => '1',
|
|
|
|
|
'rev_comment_text' => 'Fixed typo',
|
|
|
|
|
];
|
|
|
|
|
$pager = $this->getHistoryPager(
|
|
|
|
|
[
|
|
|
|
|
$pagerData + [
|
|
|
|
|
'rev_id' => 2,
|
|
|
|
|
'rev_timestamp' => '20220301001111',
|
|
|
|
|
],
|
|
|
|
|
$pagerData + [
|
|
|
|
|
'rev_id' => 3,
|
|
|
|
|
'rev_timestamp' => '20220301001122',
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$html = preg_replace( "/\n+/", '', $pager->getBody() );
|
2022-02-25 15:52:03 +00:00
|
|
|
$this->assertSame( 1, substr_count( $html, '<h4' ),
|
|
|
|
|
'There is exactly one header if the date is the same for all edits' );
|
|
|
|
|
$this->assertSame( 1, substr_count( $html, '<ul' ),
|
|
|
|
|
'There is exactly one list if the date is the same for all edits' );
|
2021-10-13 16:22:07 +00:00
|
|
|
}
|
|
|
|
|
}
|