8 class WatchedItemUnitTest
extends MediaWikiTestCase
{
13 * @return PHPUnit_Framework_MockObject_MockObject|User
15 private function getMockUser( $id ) {
16 $user = $this->getMock( User
::class );
17 $user->expects( $this->any() )
19 ->will( $this->returnValue( $id ) );
20 $user->expects( $this->any() )
21 ->method( 'isAllowed' )
22 ->will( $this->returnValue( true ) );
26 public function provideUserTitleTimestamp() {
27 $user = $this->getMockUser( 111 );
29 [ $user, Title
::newFromText( 'SomeTitle' ), null ],
30 [ $user, Title
::newFromText( 'SomeTitle' ), '20150101010101' ],
31 [ $user, new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
36 * @return PHPUnit_Framework_MockObject_MockObject|WatchedItemStore
38 private function getMockWatchedItemStore() {
39 return $this->getMockBuilder( WatchedItemStore
::class )
40 ->disableOriginalConstructor()
45 * @dataProvider provideUserTitleTimestamp
47 public function testConstruction( $user, LinkTarget
$linkTarget, $notifTimestamp ) {
48 $item = new WatchedItem( $user, $linkTarget, $notifTimestamp );
50 $this->assertSame( $user, $item->getUser() );
51 $this->assertSame( $linkTarget, $item->getLinkTarget() );
52 $this->assertSame( $notifTimestamp, $item->getNotificationTimestamp() );
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() );
63 * @dataProvider provideUserTitleTimestamp
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 );
73 $item = WatchedItem
::fromUserTitle( $user, $linkTarget, User
::IGNORE_USER_RIGHTS
);
75 $this->assertEquals( $user, $item->getUser() );
76 $this->assertEquals( $linkTarget, $item->getLinkTarget() );
77 $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
81 * @dataProvider provideUserTitleTimestamp
83 public function testResetNotificationTimestamp( $user, $linkTarget, $timestamp ) {
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() );
103 $this->setService( 'WatchedItemStore', $store );
105 $item = new WatchedItem( $user, $linkTarget, $timestamp );
106 $item->resetNotificationTimestamp( $force, $oldid );
109 public function testAddWatch() {
110 $title = Title
::newFromText( 'SomeTitle' );
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 );
120 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
121 $this->assertTrue( $item->addWatch() );
124 public function testRemoveWatch() {
125 $title = Title
::newFromText( 'SomeTitle' );
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 );
135 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
136 $this->assertTrue( $item->removeWatch() );
139 public function provideBooleans() {
147 * @dataProvider provideBooleans
149 public function testIsWatched( $returnValue ) {
150 $title = Title
::newFromText( 'SomeTitle' );
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 ) );
161 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
162 $this->assertEquals( $returnValue, $item->isWatched() );
165 public function testDuplicateEntries() {
166 $oldTitle = Title
::newFromText( 'OldTitle' );
167 $newTitle = Title
::newFromText( 'NewTitle' );
169 $store = $this->getMockWatchedItemStore();
170 $store->expects( $this->once() )
171 ->method( 'duplicateAllAssociatedEntries' )
172 ->with( $oldTitle, $newTitle );
173 $this->setService( 'WatchedItemStore', $store );
175 WatchedItem
::duplicateEntries( $oldTitle, $newTitle );
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' ),
186 $store = $this->getMockWatchedItemStore();
187 $store->expects( $this->exactly( 2 ) )
188 ->method( 'addWatchBatchForUser' );
189 $store->expects( $this->at( 0 ) )
190 ->method( 'addWatchBatchForUser' )
194 $itemOne->getTitle()->getSubjectPage(),
195 $itemOne->getTitle()->getTalkPage(),
198 $store->expects( $this->at( 1 ) )
199 ->method( 'addWatchBatchForUser' )
203 $itemTwo->getTitle()->getSubjectPage(),
204 $itemTwo->getTitle()->getTalkPage(),
207 $this->setService( 'WatchedItemStore', $store );
209 WatchedItem
::batchAddWatch( [ $itemOne, $itemTwo ] );