CommentStore: Hard-deprecate newKey()
[lhc/web/wiklou.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 if ( $timestamp === true ) {
32 // Although this timestamp is after the latest timestamp of both pages,
33 // MergeHistory should select the latest source timestamp up to this which should
34 // still work for the merge.
35 $timestamp = time() + ( 24 * 3600 );
36 }
37 $mh = new MergeHistory(
38 Title::newFromText( $source ),
39 Title::newFromText( $dest ),
40 $timestamp
41 );
42 $status = $mh->isValidMerge();
43 if ( $error === true ) {
44 $this->assertTrue( $status->isGood() );
45 } else {
46 $this->assertTrue( $status->hasMessage( $error ) );
47 }
48 }
49
50 public static function provideIsValidMerge() {
51 return [
52 // for MergeHistory::isValidMerge
53 [ 'Test', 'Test2', false, true ],
54 // Timestamp of `true` is a placeholder for "in the future""
55 [ 'Test', 'Test2', true, true ],
56 [ 'Test', 'Test', false, 'mergehistory-fail-self-merge' ],
57 [ 'Nonexistant', 'Test2', false, 'mergehistory-fail-invalid-source' ],
58 [ 'Test', 'Nonexistant', false, 'mergehistory-fail-invalid-dest' ],
59 [
60 'Test',
61 'Test2',
62 'This is obviously an invalid timestamp',
63 'mergehistory-fail-bad-timestamp'
64 ],
65 ];
66 }
67
68 /**
69 * Test merge revision limit checking
70 * @covers MergeHistory::isValidMerge
71 */
72 public function testIsValidMergeRevisionLimit() {
73 $limit = MergeHistory::REVISION_LIMIT;
74
75 $mh = $this->getMockBuilder( MergeHistory::class )
76 ->setMethods( [ 'getRevisionCount' ] )
77 ->setConstructorArgs( [
78 Title::newFromText( 'Test' ),
79 Title::newFromText( 'Test2' ),
80 ] )
81 ->getMock();
82 $mh->expects( $this->once() )
83 ->method( 'getRevisionCount' )
84 ->will( $this->returnValue( $limit + 1 ) );
85
86 $status = $mh->isValidMerge();
87 $this->assertTrue( $status->hasMessage( 'mergehistory-fail-toobig' ) );
88 $errors = $status->getErrorsByType( 'error' );
89 $params = $errors[0]['params'];
90 $this->assertEquals( $params[0], Message::numParam( $limit ) );
91 }
92
93 /**
94 * Test user permission checking
95 * @covers MergeHistory::checkPermissions
96 */
97 public function testCheckPermissions() {
98 $mh = new MergeHistory(
99 Title::newFromText( 'Test' ),
100 Title::newFromText( 'Test2' )
101 );
102
103 // Sysop with mergehistory permission
104 $sysop = static::getTestSysop()->getUser();
105 $status = $mh->checkPermissions( $sysop, '' );
106 $this->assertTrue( $status->isOK() );
107
108 // Normal user
109 $notSysop = static::getTestUser()->getUser();
110 $status = $mh->checkPermissions( $notSysop, '' );
111 $this->assertTrue( $status->hasMessage( 'mergehistory-fail-permission' ) );
112 }
113
114 /**
115 * Test merged revision count
116 * @covers MergeHistory::getMergedRevisionCount
117 */
118 public function testGetMergedRevisionCount() {
119 $mh = new MergeHistory(
120 Title::newFromText( 'Merge1' ),
121 Title::newFromText( 'Merge2' )
122 );
123
124 $sysop = static::getTestSysop()->getUser();
125 $mh->merge( $sysop );
126 $this->assertEquals( $mh->getMergedRevisionCount(), 1 );
127 }
128 }