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