Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / pager / HistoryPagerTest.php
blob709e1c5029dbc54eb05a82e3efb93529a37464b9
1 <?php
3 use MediaWiki\Context\RequestContext;
4 use MediaWiki\Output\OutputPage;
5 use MediaWiki\Pager\HistoryPager;
6 use MediaWiki\Request\FauxRequest;
7 use MediaWiki\Revision\MutableRevisionRecord;
8 use MediaWiki\Title\Title;
9 use Wikimedia\Rdbms\FakeResultWrapper;
10 use Wikimedia\TestingAccessWrapper;
12 /**
13 * Test class for HistoryPager methods.
15 * @group Pager
16 * @group Database
18 class HistoryPagerTest extends MediaWikiIntegrationTestCase {
19 /**
20 * @param array $results for passing to FakeResultWrapper and deriving
21 * RevisionRecords and formatted comments.
22 * @return HistoryPager
24 private function getHistoryPager( array $results ) {
25 $wikiPageMock = $this->createMock( WikiPage::class );
26 $contextMock = $this->getMockBuilder( RequestContext::class )
27 ->disableOriginalConstructor()
28 ->onlyMethods( [ 'getRequest', 'getWikiPage', 'getTitle' ] )
29 ->getMock();
30 $contextMock->method( 'getRequest' )->willReturn(
31 new FauxRequest( [] )
33 $title = Title::makeTitle( NS_MAIN, 'HistoryPagerTest' );
34 $contextMock->method( 'getTitle' )->willReturn( $title );
36 $contextMock->method( 'getWikiPage' )->willReturn( $wikiPageMock );
37 $articleMock = $this->getMockBuilder( Article::class )
38 ->disableOriginalConstructor()
39 ->onlyMethods( [ 'getContext' ] )
40 ->getMock();
41 $articleMock->method( 'getContext' )->willReturn( $contextMock );
43 $actionMock = $this->getMockBuilder( HistoryAction::class )
44 ->setConstructorArgs( [
45 $articleMock,
46 $contextMock
47 ] )
48 ->getMock();
49 $actionMock->method( 'getArticle' )->willReturn( $articleMock );
50 $actionMock->message = [
51 'cur' => 'cur',
52 'last' => 'last',
53 'tooltip-cur' => '',
54 'tooltip-last' => '',
57 $outputMock = $this->getMockBuilder( OutputPage::class )
58 ->disableOriginalConstructor()
59 ->onlyMethods( [ 'wrapWikiMsg' ] )
60 ->getMock();
62 $pager = $this->getMockBuilder( HistoryPager::class )
63 ->onlyMethods( [ 'reallyDoQuery', 'doBatchLookups',
64 'getOutput' ] )
65 ->setConstructorArgs( [
66 $actionMock
67 ] )
68 ->getMock();
70 $pager->method( 'getOutput' )->willReturn( $outputMock );
71 $pager->method( 'reallyDoQuery' )->willReturn(
72 new FakeResultWrapper( $results )
75 // make all the methods in our mock public
76 $pager = TestingAccessWrapper::newFromObject( $pager );
77 // and update the private properties...
78 $pager->formattedComments = array_map( static function ( $result ) {
79 return 'dummy comment';
80 }, $results );
82 $pager->revisions = array_map( static function ( $result ) {
83 $title = Title::makeTitle( NS_MAIN, 'HistoryPagerTest' );
84 $r = new MutableRevisionRecord( $title );
85 $r->setId( $result[ 'rev_id' ] );
86 return $r;
87 }, $results );
89 return $pager;
92 /**
93 * @covers \MediaWiki\Pager\HistoryPager::getBody
95 public function testGetBodyEmpty() {
96 $pager = $this->getHistoryPager( [] );
97 $html = $pager->getBody();
98 $this->assertStringContainsString( 'No matching revisions were found.', $html );
99 $this->assertStringNotContainsString( '<h4', $html );
103 * @covers \MediaWiki\Pager\HistoryPager::getBody
105 public function testGetBodyOneHeading() {
106 $pager = $this->getHistoryPager(
109 'rev_page' => 'title',
110 'ts_tags' => '',
111 'rev_deleted' => false,
112 'rev_minor_edit' => false,
113 'rev_parent_id' => 1,
114 'user_name' => 'Jdlrobson',
115 'rev_id' => 2,
116 'rev_comment_data' => '{}',
117 'rev_comment_cid' => '1',
118 'rev_comment_text' => 'Created page',
119 'rev_timestamp' => '20220101001122',
123 $html = $pager->getBody();
124 $this->assertStringContainsString( '<h4', $html );
128 * @covers \MediaWiki\Pager\HistoryPager::getBody
130 public function testGetBodyTwoHeading() {
131 $pagerData = [
132 'rev_page' => 'title',
133 'rev_deleted' => false,
134 'rev_minor_edit' => false,
135 'ts_tags' => '',
136 'rev_parent_id' => 1,
137 'user_name' => 'Jdlrobson',
138 'rev_comment_data' => '{}',
139 'rev_comment_cid' => '1',
140 'rev_comment_text' => 'Fixed typo',
142 $pager = $this->getHistoryPager(
144 $pagerData + [
145 'rev_id' => 3,
146 'rev_timestamp' => '20220301001122',
148 $pagerData + [
149 'rev_id' => 2,
150 'rev_timestamp' => '20220101001122',
154 $html = preg_replace( "/\n+/", '', $pager->getBody() );
155 $firstHeader = '<h4 class="mw-index-pager-list-header-first mw-index-pager-list-header">1 March 2022</h4>'
156 . '<ul class="mw-contributions-list">'
157 . '<li data-mw-revid="3">';
158 $secondHeader = '<h4 class="mw-index-pager-list-header">1 January 2022</h4>'
159 . '<ul class="mw-contributions-list">'
160 . '<li data-mw-revid="2">';
162 // Check that the undo links are correct in the topmost displayed row (for rev_id=3).
163 // This is tricky because the other rev number (in this example, '2') is magically
164 // pulled from the next row, before we've processed that row.
165 $this->assertStringContainsString( '&amp;undoafter=2&amp;undo=3', $html );
167 $this->assertStringContainsString( $firstHeader, $html );
168 $this->assertStringContainsString( $secondHeader, $html );
169 $this->assertStringContainsString( '<section id="pagehistory"', $html );
173 * @covers \MediaWiki\Pager\HistoryPager::getBody
175 public function testGetBodyLastItem() {
176 $pagerData = [
177 'rev_page' => 'title',
178 'rev_deleted' => false,
179 'rev_minor_edit' => false,
180 'ts_tags' => '',
181 'rev_parent_id' => 1,
182 'user_name' => 'Jdlrobson',
183 'rev_comment_data' => '{}',
184 'rev_comment_cid' => '1',
185 'rev_comment_text' => 'Fixed typo',
187 $pager = $this->getHistoryPager(
189 $pagerData + [
190 'rev_id' => 2,
191 'rev_timestamp' => '20220301001111',
193 $pagerData + [
194 'rev_id' => 3,
195 'rev_timestamp' => '20220301001122',
199 $html = preg_replace( "/\n+/", '', $pager->getBody() );
200 $this->assertSame( 1, substr_count( $html, '<h4' ),
201 'There is exactly one header if the date is the same for all edits' );
202 $this->assertSame( 1, substr_count( $html, '<ul' ),
203 'There is exactly one list if the date is the same for all edits' );