2 use MediaWiki\Linker\LinkTarget
;
9 class WatchedItemUnitTest
extends MediaWikiTestCase
{
14 * @return PHPUnit_Framework_MockObject_MockObject|User
16 private function getMockUser( $id ) {
17 $user = $this->getMock( User
::class );
18 $user->expects( $this->any() )
20 ->will( $this->returnValue( $id ) );
21 $user->expects( $this->any() )
22 ->method( 'isAllowed' )
23 ->will( $this->returnValue( true ) );
27 public function provideUserTitleTimestamp() {
28 $user = $this->getMockUser( 111 );
30 [ $user, Title
::newFromText( 'SomeTitle' ), null ],
31 [ $user, Title
::newFromText( 'SomeTitle' ), '20150101010101' ],
32 [ $user, new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
37 * @return PHPUnit_Framework_MockObject_MockObject|WatchedItemStore
39 private function getMockWatchedItemStore() {
40 return $this->getMockBuilder( WatchedItemStore
::class )
41 ->disableOriginalConstructor()
46 * @dataProvider provideUserTitleTimestamp
48 public function testConstruction( $user, LinkTarget
$linkTarget, $notifTimestamp ) {
49 $item = new WatchedItem( $user, $linkTarget, $notifTimestamp );
51 $this->assertSame( $user, $item->getUser() );
52 $this->assertSame( $linkTarget, $item->getLinkTarget() );
53 $this->assertSame( $notifTimestamp, $item->getNotificationTimestamp() );
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() );
64 * @dataProvider provideUserTitleTimestamp
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 );
74 $item = WatchedItem
::fromUserTitle( $user, $linkTarget, User
::IGNORE_USER_RIGHTS
);
76 $this->assertEquals( $user, $item->getUser() );
77 $this->assertEquals( $linkTarget, $item->getLinkTarget() );
78 $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
82 * @dataProvider provideUserTitleTimestamp
84 public function testResetNotificationTimestamp( $user, $linkTarget, $timestamp ) {
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() );
104 $this->setService( 'WatchedItemStore', $store );
106 $item = new WatchedItem( $user, $linkTarget, $timestamp );
107 $item->resetNotificationTimestamp( $force, $oldid );
110 public function testAddWatch() {
111 $title = Title
::newFromText( 'SomeTitle' );
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 );
121 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
122 $this->assertTrue( $item->addWatch() );
125 public function testRemoveWatch() {
126 $title = Title
::newFromText( 'SomeTitle' );
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 );
136 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
137 $this->assertTrue( $item->removeWatch() );
140 public function provideBooleans() {
148 * @dataProvider provideBooleans
150 public function testIsWatched( $returnValue ) {
151 $title = Title
::newFromText( 'SomeTitle' );
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 ) );
162 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
163 $this->assertEquals( $returnValue, $item->isWatched() );
166 public function testDuplicateEntries() {
167 $oldTitle = Title
::newFromText( 'OldTitle' );
168 $newTitle = Title
::newFromText( 'NewTitle' );
170 $store = $this->getMockWatchedItemStore();
171 $store->expects( $this->once() )
172 ->method( 'duplicateAllAssociatedEntries' )
173 ->with( $oldTitle, $newTitle );
174 $this->setService( 'WatchedItemStore', $store );
176 WatchedItem
::duplicateEntries( $oldTitle, $newTitle );
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' ),
187 $store = $this->getMockWatchedItemStore();
188 $store->expects( $this->exactly( 2 ) )
189 ->method( 'addWatchBatchForUser' );
190 $store->expects( $this->at( 0 ) )
191 ->method( 'addWatchBatchForUser' )
195 $itemOne->getTitle()->getSubjectPage(),
196 $itemOne->getTitle()->getTalkPage(),
199 $store->expects( $this->at( 1 ) )
200 ->method( 'addWatchBatchForUser' )
204 $itemTwo->getTitle()->getSubjectPage(),
205 $itemTwo->getTitle()->getTalkPage(),
208 $this->setService( 'WatchedItemStore', $store );
210 WatchedItem
::batchAddWatch( [ $itemOne, $itemTwo ] );