]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - tests/phpunit/includes/MergeHistoryTest.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / tests / phpunit / includes / MergeHistoryTest.php
1 <?php
2
3 /**
4  * @group Database
5  */
6 class MergeHistoryTest extends MediaWikiTestCase {
7
8         /**
9          * Make some pages to work with
10          */
11         public function addDBDataOnce() {
12                 // Pages that won't actually be merged
13                 $this->insertPage( 'Test' );
14                 $this->insertPage( 'Test2' );
15
16                 // Pages that will be merged
17                 $this->insertPage( 'Merge1' );
18                 $this->insertPage( 'Merge2' );
19         }
20
21         /**
22          * @dataProvider provideIsValidMerge
23          * @covers MergeHistory::isValidMerge
24          * @param string $source Source page
25          * @param string $dest Destination page
26          * @param string|bool $timestamp Timestamp up to which revisions are merged (or false for all)
27          * @param string|bool $error Expected error for test (or true for no error)
28          */
29         public function testIsValidMerge( $source, $dest, $timestamp, $error ) {
30                 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
31                 $mh = new MergeHistory(
32                         Title::newFromText( $source ),
33                         Title::newFromText( $dest ),
34                         $timestamp
35                 );
36                 $status = $mh->isValidMerge();
37                 if ( $error === true ) {
38                         $this->assertTrue( $status->isGood() );
39                 } else {
40                         $this->assertTrue( $status->hasMessage( $error ) );
41                 }
42         }
43
44         public static function provideIsValidMerge() {
45                 return [
46                         // for MergeHistory::isValidMerge
47                         [ 'Test', 'Test2', false, true ],
48                         // Although this timestamp is after the latest timestamp of both pages,
49                         // MergeHistory should select the latest source timestamp up to this which should
50                         // still work for the merge.
51                         [ 'Test', 'Test2', strtotime( 'tomorrow' ), true ],
52                         [ 'Test', 'Test', false, 'mergehistory-fail-self-merge' ],
53                         [ 'Nonexistant', 'Test2', false, 'mergehistory-fail-invalid-source' ],
54                         [ 'Test', 'Nonexistant', false, 'mergehistory-fail-invalid-dest' ],
55                         [
56                                 'Test',
57                                 'Test2',
58                                 'This is obviously an invalid timestamp',
59                                 'mergehistory-fail-bad-timestamp'
60                         ],
61                 ];
62         }
63
64         /**
65          * Test merge revision limit checking
66          * @covers MergeHistory::isValidMerge
67          */
68         public function testIsValidMergeRevisionLimit() {
69                 $limit = MergeHistory::REVISION_LIMIT;
70
71                 $mh = $this->getMockBuilder( 'MergeHistory' )
72                         ->setMethods( [ 'getRevisionCount' ] )
73                         ->setConstructorArgs( [
74                                 Title::newFromText( 'Test' ),
75                                 Title::newFromText( 'Test2' ),
76                         ] )
77                         ->getMock();
78                 $mh->expects( $this->once() )
79                         ->method( 'getRevisionCount' )
80                         ->will( $this->returnValue( $limit + 1 ) );
81
82                 $status = $mh->isValidMerge();
83                 $this->assertTrue( $status->hasMessage( 'mergehistory-fail-toobig' ) );
84                 $errors = $status->getErrorsByType( 'error' );
85                 $params = $errors[0]['params'];
86                 $this->assertEquals( $params[0], Message::numParam( $limit ) );
87         }
88
89         /**
90          * Test user permission checking
91          * @covers MergeHistory::checkPermissions
92          */
93         public function testCheckPermissions() {
94                 $mh = new MergeHistory(
95                         Title::newFromText( 'Test' ),
96                         Title::newFromText( 'Test2' )
97                 );
98
99                 // Sysop with mergehistory permission
100                 $sysop = static::getTestSysop()->getUser();
101                 $status = $mh->checkPermissions( $sysop, '' );
102                 $this->assertTrue( $status->isOK() );
103
104                 // Normal user
105                 $notSysop = static::getTestUser()->getUser();
106                 $status = $mh->checkPermissions( $notSysop, '' );
107                 $this->assertTrue( $status->hasMessage( 'mergehistory-fail-permission' ) );
108         }
109
110         /**
111          * Test merged revision count
112          * @covers MergeHistory::getMergedRevisionCount
113          */
114         public function testGetMergedRevisionCount() {
115                 $mh = new MergeHistory(
116                         Title::newFromText( 'Merge1' ),
117                         Title::newFromText( 'Merge2' )
118                 );
119
120                 $sysop = static::getTestSysop()->getUser();
121                 $mh->merge( $sysop );
122                 $this->assertEquals( $mh->getMergedRevisionCount(), 1 );
123         }
124 }