Merge "Add templateOverridesBySection to multi LBFactory"
[lhc/web/wiklou.git] / tests / phpunit / includes / WatchedItemUnitTest.php
1 <?php
2
3 /**
4 * @author Addshore
5 *
6 * @covers WatchedItem
7 */
8 class WatchedItemUnitTest extends MediaWikiTestCase {
9
10 /**
11 * @param int $id
12 *
13 * @return PHPUnit_Framework_MockObject_MockObject|User
14 */
15 private function getMockUser( $id ) {
16 $user = $this->getMock( User::class );
17 $user->expects( $this->any() )
18 ->method( 'getId' )
19 ->will( $this->returnValue( $id ) );
20 $user->expects( $this->any() )
21 ->method( 'isAllowed' )
22 ->will( $this->returnValue( true ) );
23 return $user;
24 }
25
26 public function provideUserTitleTimestamp() {
27 $user = $this->getMockUser( 111 );
28 return [
29 [ $user, Title::newFromText( 'SomeTitle' ), null ],
30 [ $user, Title::newFromText( 'SomeTitle' ), '20150101010101' ],
31 [ $user, new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
32 ];
33 }
34
35 /**
36 * @return PHPUnit_Framework_MockObject_MockObject|WatchedItemStore
37 */
38 private function getMockWatchedItemStore() {
39 return $this->getMockBuilder( WatchedItemStore::class )
40 ->disableOriginalConstructor()
41 ->getMock();
42 }
43
44 /**
45 * @dataProvider provideUserTitleTimestamp
46 */
47 public function testConstruction( $user, LinkTarget $linkTarget, $notifTimestamp ) {
48 $item = new WatchedItem( $user, $linkTarget, $notifTimestamp );
49
50 $this->assertSame( $user, $item->getUser() );
51 $this->assertSame( $linkTarget, $item->getLinkTarget() );
52 $this->assertSame( $notifTimestamp, $item->getNotificationTimestamp() );
53
54 // The below tests the internal WatchedItem::getTitle method
55 $this->assertInstanceOf( 'Title', $item->getTitle() );
56 $this->assertSame( $linkTarget->getDBkey(), $item->getTitle()->getDBkey() );
57 $this->assertSame( $linkTarget->getFragment(), $item->getTitle()->getFragment() );
58 $this->assertSame( $linkTarget->getNamespace(), $item->getTitle()->getNamespace() );
59 $this->assertSame( $linkTarget->getText(), $item->getTitle()->getText() );
60 }
61
62 /**
63 * @dataProvider provideUserTitleTimestamp
64 */
65 public function testFromUserTitle( $user, $linkTarget, $timestamp ) {
66 $store = $this->getMockWatchedItemStore();
67 $store->expects( $this->once() )
68 ->method( 'loadWatchedItem' )
69 ->with( $user, $linkTarget )
70 ->will( $this->returnValue( new WatchedItem( $user, $linkTarget, $timestamp ) ) );
71 $this->setService( 'WatchedItemStore', $store );
72
73 $item = WatchedItem::fromUserTitle( $user, $linkTarget, User::IGNORE_USER_RIGHTS );
74
75 $this->assertEquals( $user, $item->getUser() );
76 $this->assertEquals( $linkTarget, $item->getLinkTarget() );
77 $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
78 }
79
80 /**
81 * @dataProvider provideUserTitleTimestamp
82 */
83 public function testResetNotificationTimestamp( $user, $linkTarget, $timestamp ) {
84 $force = 'XXX';
85 $oldid = 999;
86
87 $store = $this->getMockWatchedItemStore();
88 $store->expects( $this->once() )
89 ->method( 'resetNotificationTimestamp' )
90 ->with( $user, $this->isInstanceOf( Title::class ), $force, $oldid )
91 ->will( $this->returnCallback(
92 function ( $user, Title $title, $force, $oldid ) use ( $linkTarget ) {
93 /** @var LinkTarget $linkTarget */
94 $this->assertInstanceOf( 'Title', $title );
95 $this->assertSame( $linkTarget->getDBkey(), $title->getDBkey() );
96 $this->assertSame( $linkTarget->getFragment(), $title->getFragment() );
97 $this->assertSame( $linkTarget->getNamespace(), $title->getNamespace() );
98 $this->assertSame( $linkTarget->getText(), $title->getText() );
99
100 return true;
101 }
102 ) );
103 $this->setService( 'WatchedItemStore', $store );
104
105 $item = new WatchedItem( $user, $linkTarget, $timestamp );
106 $item->resetNotificationTimestamp( $force, $oldid );
107 }
108
109 public function testAddWatch() {
110 $title = Title::newFromText( 'SomeTitle' );
111 $timestamp = null;
112 $checkRights = 0;
113
114 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
115 $user = $this->getMock( User::class );
116 $user->expects( $this->once() )
117 ->method( 'addWatch' )
118 ->with( $title, $checkRights );
119
120 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
121 $this->assertTrue( $item->addWatch() );
122 }
123
124 public function testRemoveWatch() {
125 $title = Title::newFromText( 'SomeTitle' );
126 $timestamp = null;
127 $checkRights = 0;
128
129 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
130 $user = $this->getMock( User::class );
131 $user->expects( $this->once() )
132 ->method( 'removeWatch' )
133 ->with( $title, $checkRights );
134
135 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
136 $this->assertTrue( $item->removeWatch() );
137 }
138
139 public function provideBooleans() {
140 return [
141 [ true ],
142 [ false ],
143 ];
144 }
145
146 /**
147 * @dataProvider provideBooleans
148 */
149 public function testIsWatched( $returnValue ) {
150 $title = Title::newFromText( 'SomeTitle' );
151 $timestamp = null;
152 $checkRights = 0;
153
154 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
155 $user = $this->getMock( User::class );
156 $user->expects( $this->once() )
157 ->method( 'isWatched' )
158 ->with( $title, $checkRights )
159 ->will( $this->returnValue( $returnValue ) );
160
161 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
162 $this->assertEquals( $returnValue, $item->isWatched() );
163 }
164
165 public function testDuplicateEntries() {
166 $oldTitle = Title::newFromText( 'OldTitle' );
167 $newTitle = Title::newFromText( 'NewTitle' );
168
169 $store = $this->getMockWatchedItemStore();
170 $store->expects( $this->once() )
171 ->method( 'duplicateAllAssociatedEntries' )
172 ->with( $oldTitle, $newTitle );
173 $this->setService( 'WatchedItemStore', $store );
174
175 WatchedItem::duplicateEntries( $oldTitle, $newTitle );
176 }
177
178 public function testBatchAddWatch() {
179 $itemOne = new WatchedItem( $this->getMockUser( 1 ), new TitleValue( 0, 'Title1' ), null );
180 $itemTwo = new WatchedItem(
181 $this->getMockUser( 3 ),
182 Title::newFromText( 'Title2' ),
183 '20150101010101'
184 );
185
186 $store = $this->getMockWatchedItemStore();
187 $store->expects( $this->exactly( 2 ) )
188 ->method( 'addWatchBatchForUser' );
189 $store->expects( $this->at( 0 ) )
190 ->method( 'addWatchBatchForUser' )
191 ->with(
192 $itemOne->getUser(),
193 [
194 $itemOne->getTitle()->getSubjectPage(),
195 $itemOne->getTitle()->getTalkPage(),
196 ]
197 );
198 $store->expects( $this->at( 1 ) )
199 ->method( 'addWatchBatchForUser' )
200 ->with(
201 $itemTwo->getUser(),
202 [
203 $itemTwo->getTitle()->getSubjectPage(),
204 $itemTwo->getTitle()->getTalkPage(),
205 ]
206 );
207 $this->setService( 'WatchedItemStore', $store );
208
209 WatchedItem::batchAddWatch( [ $itemOne, $itemTwo ] );
210 }
211
212 }