]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/WatchedItemUnitTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / tests / phpunit / includes / WatchedItemUnitTest.php
1 <?php
2 use MediaWiki\Linker\LinkTarget;
3
4 /**
5  * @author Addshore
6  *
7  * @covers WatchedItem
8  */
9 class WatchedItemUnitTest extends MediaWikiTestCase {
10
11         public function setUp() {
12                 parent::setUp();
13
14                 $this->hideDeprecated( 'WatchedItem::fromUserTitle' );
15                 $this->hideDeprecated( 'WatchedItem::addWatch' );
16                 $this->hideDeprecated( 'WatchedItem::removeWatch' );
17                 $this->hideDeprecated( 'WatchedItem::isWatched' );
18                 $this->hideDeprecated( 'WatchedItem::duplicateEntries' );
19                 $this->hideDeprecated( 'WatchedItem::batchAddWatch' );
20         }
21
22         /**
23          * @param int $id
24          *
25          * @return PHPUnit_Framework_MockObject_MockObject|User
26          */
27         private function getMockUser( $id ) {
28                 $user = $this->createMock( User::class );
29                 $user->expects( $this->any() )
30                         ->method( 'getId' )
31                         ->will( $this->returnValue( $id ) );
32                 $user->expects( $this->any() )
33                         ->method( 'isAllowed' )
34                         ->will( $this->returnValue( true ) );
35                 return $user;
36         }
37
38         public function provideUserTitleTimestamp() {
39                 $user = $this->getMockUser( 111 );
40                 return [
41                         [ $user, Title::newFromText( 'SomeTitle' ), null ],
42                         [ $user, Title::newFromText( 'SomeTitle' ), '20150101010101' ],
43                         [ $user, new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
44                 ];
45         }
46
47         /**
48          * @return PHPUnit_Framework_MockObject_MockObject|WatchedItemStore
49          */
50         private function getMockWatchedItemStore() {
51                 return $this->getMockBuilder( WatchedItemStore::class )
52                         ->disableOriginalConstructor()
53                         ->getMock();
54         }
55
56         /**
57          * @dataProvider provideUserTitleTimestamp
58          */
59         public function testConstruction( $user, LinkTarget $linkTarget, $notifTimestamp ) {
60                 $item = new WatchedItem( $user, $linkTarget, $notifTimestamp );
61
62                 $this->assertSame( $user, $item->getUser() );
63                 $this->assertSame( $linkTarget, $item->getLinkTarget() );
64                 $this->assertSame( $notifTimestamp, $item->getNotificationTimestamp() );
65
66                 // The below tests the internal WatchedItem::getTitle method
67                 $this->assertInstanceOf( 'Title', $item->getTitle() );
68                 $this->assertSame( $linkTarget->getDBkey(), $item->getTitle()->getDBkey() );
69                 $this->assertSame( $linkTarget->getFragment(), $item->getTitle()->getFragment() );
70                 $this->assertSame( $linkTarget->getNamespace(), $item->getTitle()->getNamespace() );
71                 $this->assertSame( $linkTarget->getText(), $item->getTitle()->getText() );
72         }
73
74         /**
75          * @dataProvider provideUserTitleTimestamp
76          */
77         public function testFromUserTitle( $user, $linkTarget, $timestamp ) {
78                 $store = $this->getMockWatchedItemStore();
79                 $store->expects( $this->once() )
80                         ->method( 'loadWatchedItem' )
81                         ->with( $user, $linkTarget )
82                         ->will( $this->returnValue( new WatchedItem( $user, $linkTarget, $timestamp ) ) );
83                 $this->setService( 'WatchedItemStore', $store );
84
85                 $item = WatchedItem::fromUserTitle( $user, $linkTarget, User::IGNORE_USER_RIGHTS );
86
87                 $this->assertEquals( $user, $item->getUser() );
88                 $this->assertEquals( $linkTarget, $item->getLinkTarget() );
89                 $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
90         }
91
92         public function testAddWatch() {
93                 $title = Title::newFromText( 'SomeTitle' );
94                 $timestamp = null;
95                 $checkRights = 0;
96
97                 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
98                 $user = $this->createMock( User::class );
99                 $user->expects( $this->once() )
100                         ->method( 'addWatch' )
101                         ->with( $title, $checkRights );
102
103                 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
104                 $this->assertTrue( $item->addWatch() );
105         }
106
107         public function testRemoveWatch() {
108                 $title = Title::newFromText( 'SomeTitle' );
109                 $timestamp = null;
110                 $checkRights = 0;
111
112                 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
113                 $user = $this->createMock( User::class );
114                 $user->expects( $this->once() )
115                         ->method( 'removeWatch' )
116                         ->with( $title, $checkRights );
117
118                 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
119                 $this->assertTrue( $item->removeWatch() );
120         }
121
122         public function provideBooleans() {
123                 return [
124                         [ true ],
125                         [ false ],
126                 ];
127         }
128
129         /**
130          * @dataProvider provideBooleans
131          */
132         public function testIsWatched( $returnValue ) {
133                 $title = Title::newFromText( 'SomeTitle' );
134                 $timestamp = null;
135                 $checkRights = 0;
136
137                 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
138                 $user = $this->createMock( User::class );
139                 $user->expects( $this->once() )
140                         ->method( 'isWatched' )
141                         ->with( $title, $checkRights )
142                         ->will( $this->returnValue( $returnValue ) );
143
144                 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
145                 $this->assertEquals( $returnValue, $item->isWatched() );
146         }
147
148         public function testDuplicateEntries() {
149                 $oldTitle = Title::newFromText( 'OldTitle' );
150                 $newTitle = Title::newFromText( 'NewTitle' );
151
152                 $store = $this->getMockWatchedItemStore();
153                 $store->expects( $this->once() )
154                         ->method( 'duplicateAllAssociatedEntries' )
155                         ->with( $oldTitle, $newTitle );
156                 $this->setService( 'WatchedItemStore', $store );
157
158                 WatchedItem::duplicateEntries( $oldTitle, $newTitle );
159         }
160
161 }