8 class WatchedItemUnitTest
extends PHPUnit_Framework_TestCase
{
10 public function provideUserTitleTimestamp() {
12 [ User
::newFromId( 111 ), Title
::newFromText( 'SomeTitle' ), null ],
13 [ User
::newFromId( 111 ), Title
::newFromText( 'SomeTitle' ), '20150101010101' ],
14 [ User
::newFromId( 111 ), new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
19 * @return PHPUnit_Framework_MockObject_MockObject|WatchedItemStore
21 private function getMockWatchedItemStore() {
22 return $this->getMockBuilder( WatchedItemStore
::class )
23 ->disableOriginalConstructor()
28 * @dataProvider provideUserTitleTimestamp
30 public function testConstruction( $user, LinkTarget
$linkTarget, $notifTimestamp ) {
31 $item = new WatchedItem( $user, $linkTarget, $notifTimestamp );
33 $this->assertSame( $user, $item->getUser() );
34 $this->assertSame( $linkTarget, $item->getLinkTarget() );
35 $this->assertSame( $notifTimestamp, $item->getNotificationTimestamp() );
37 // The below tests the internal WatchedItem::getTitle method
38 $this->assertInstanceOf( 'Title', $item->getTitle() );
39 $this->assertSame( $linkTarget->getDBkey(), $item->getTitle()->getDBkey() );
40 $this->assertSame( $linkTarget->getFragment(), $item->getTitle()->getFragment() );
41 $this->assertSame( $linkTarget->getNamespace(), $item->getTitle()->getNamespace() );
42 $this->assertSame( $linkTarget->getText(), $item->getTitle()->getText() );
46 * @dataProvider provideUserTitleTimestamp
48 public function testFromUserTitle( $user, $linkTarget, $timestamp ) {
49 $store = $this->getMockWatchedItemStore();
50 $store->expects( $this->once() )
51 ->method( 'loadWatchedItem' )
52 ->with( $user, $linkTarget )
53 ->will( $this->returnValue( new WatchedItem( $user, $linkTarget, $timestamp ) ) );
54 $scopedOverride = WatchedItemStore
::overrideDefaultInstance( $store );
56 $item = WatchedItem
::fromUserTitle( $user, $linkTarget, User
::IGNORE_USER_RIGHTS
);
58 $this->assertEquals( $user, $item->getUser() );
59 $this->assertEquals( $linkTarget, $item->getLinkTarget() );
60 $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
62 ScopedCallback
::consume( $scopedOverride );
66 * @dataProvider provideUserTitleTimestamp
68 public function testResetNotificationTimestamp( $user, $linkTarget, $timestamp ) {
72 $store = $this->getMockWatchedItemStore();
73 $store->expects( $this->once() )
74 ->method( 'resetNotificationTimestamp' )
75 ->with( $user, $this->isInstanceOf( Title
::class ), $force, $oldid )
76 ->will( $this->returnCallback(
77 function ( $user, Title
$title, $force, $oldid ) use ( $linkTarget ) {
78 /** @var LinkTarget $linkTarget */
79 $this->assertInstanceOf( 'Title', $title );
80 $this->assertSame( $linkTarget->getDBkey(), $title->getDBkey() );
81 $this->assertSame( $linkTarget->getFragment(), $title->getFragment() );
82 $this->assertSame( $linkTarget->getNamespace(), $title->getNamespace() );
83 $this->assertSame( $linkTarget->getText(), $title->getText() );
88 $scopedOverride = WatchedItemStore
::overrideDefaultInstance( $store );
90 $item = new WatchedItem( $user, $linkTarget, $timestamp );
91 $item->resetNotificationTimestamp( $force, $oldid );
93 ScopedCallback
::consume( $scopedOverride );
96 public function testAddWatch() {
97 $title = Title
::newFromText( 'SomeTitle' );
101 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
102 $user = $this->getMock( User
::class );
103 $user->expects( $this->once() )
104 ->method( 'addWatch' )
105 ->with( $title, $checkRights );
107 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
108 $this->assertTrue( $item->addWatch() );
111 public function testRemoveWatch() {
112 $title = Title
::newFromText( 'SomeTitle' );
116 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
117 $user = $this->getMock( User
::class );
118 $user->expects( $this->once() )
119 ->method( 'removeWatch' )
120 ->with( $title, $checkRights );
122 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
123 $this->assertTrue( $item->removeWatch() );
126 public function provideBooleans() {
134 * @dataProvider provideBooleans
136 public function testIsWatched( $returnValue ) {
137 $title = Title
::newFromText( 'SomeTitle' );
141 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
142 $user = $this->getMock( User
::class );
143 $user->expects( $this->once() )
144 ->method( 'isWatched' )
145 ->with( $title, $checkRights )
146 ->will( $this->returnValue( $returnValue ) );
148 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
149 $this->assertEquals( $returnValue, $item->isWatched() );
152 public function testDuplicateEntries() {
153 $oldTitle = Title
::newFromText( 'OldTitle' );
154 $newTitle = Title
::newFromText( 'NewTitle' );
156 $store = $this->getMockWatchedItemStore();
157 $store->expects( $this->once() )
158 ->method( 'duplicateAllAssociatedEntries' )
159 ->with( $oldTitle, $newTitle );
160 $scopedOverride = WatchedItemStore
::overrideDefaultInstance( $store );
162 WatchedItem
::duplicateEntries( $oldTitle, $newTitle );
164 ScopedCallback
::consume( $scopedOverride );
167 public function testBatchAddWatch() {
168 $itemOne = new WatchedItem( User
::newFromId( 1 ), new TitleValue( 0, 'Title1' ), null );
169 $itemTwo = new WatchedItem(
170 User
::newFromId( 3 ),
171 Title
::newFromText( 'Title2' ),
175 $store = $this->getMockWatchedItemStore();
176 $store->expects( $this->exactly( 2 ) )
177 ->method( 'addWatchBatchForUser' );
178 $store->expects( $this->at( 0 ) )
179 ->method( 'addWatchBatchForUser' )
183 $itemOne->getTitle()->getSubjectPage(),
184 $itemOne->getTitle()->getTalkPage(),
187 $store->expects( $this->at( 1 ) )
188 ->method( 'addWatchBatchForUser' )
192 $itemTwo->getTitle()->getSubjectPage(),
193 $itemTwo->getTitle()->getTalkPage(),
196 $scopedOverride = WatchedItemStore
::overrideDefaultInstance( $store );
198 WatchedItem
::batchAddWatch( [ $itemOne, $itemTwo ] );
200 ScopedCallback
::consume( $scopedOverride );