2 use MediaWiki\Linker\LinkTarget
;
9 class WatchedItemUnitTest
extends PHPUnit_Framework_TestCase
{
11 public function provideUserTitleTimestamp() {
13 [ User
::newFromId( 111 ), Title
::newFromText( 'SomeTitle' ), null ],
14 [ User
::newFromId( 111 ), Title
::newFromText( 'SomeTitle' ), '20150101010101' ],
15 [ User
::newFromId( 111 ), new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
20 * @return PHPUnit_Framework_MockObject_MockObject|WatchedItemStore
22 private function getMockWatchedItemStore() {
23 return $this->getMockBuilder( WatchedItemStore
::class )
24 ->disableOriginalConstructor()
29 * @dataProvider provideUserTitleTimestamp
31 public function testConstruction( $user, LinkTarget
$linkTarget, $notifTimestamp ) {
32 $item = new WatchedItem( $user, $linkTarget, $notifTimestamp );
34 $this->assertSame( $user, $item->getUser() );
35 $this->assertSame( $linkTarget, $item->getLinkTarget() );
36 $this->assertSame( $notifTimestamp, $item->getNotificationTimestamp() );
38 // The below tests the internal WatchedItem::getTitle method
39 $this->assertInstanceOf( 'Title', $item->getTitle() );
40 $this->assertSame( $linkTarget->getDBkey(), $item->getTitle()->getDBkey() );
41 $this->assertSame( $linkTarget->getFragment(), $item->getTitle()->getFragment() );
42 $this->assertSame( $linkTarget->getNamespace(), $item->getTitle()->getNamespace() );
43 $this->assertSame( $linkTarget->getText(), $item->getTitle()->getText() );
47 * @dataProvider provideUserTitleTimestamp
49 public function testFromUserTitle( $user, $linkTarget, $timestamp ) {
50 $store = $this->getMockWatchedItemStore();
51 $store->expects( $this->once() )
52 ->method( 'loadWatchedItem' )
53 ->with( $user, $linkTarget )
54 ->will( $this->returnValue( new WatchedItem( $user, $linkTarget, $timestamp ) ) );
55 $scopedOverride = WatchedItemStore
::overrideDefaultInstance( $store );
57 $item = WatchedItem
::fromUserTitle( $user, $linkTarget, User
::IGNORE_USER_RIGHTS
);
59 $this->assertEquals( $user, $item->getUser() );
60 $this->assertEquals( $linkTarget, $item->getLinkTarget() );
61 $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
63 ScopedCallback
::consume( $scopedOverride );
67 * @dataProvider provideUserTitleTimestamp
69 public function testResetNotificationTimestamp( $user, $linkTarget, $timestamp ) {
73 $store = $this->getMockWatchedItemStore();
74 $store->expects( $this->once() )
75 ->method( 'resetNotificationTimestamp' )
76 ->with( $user, $this->isInstanceOf( Title
::class ), $force, $oldid )
77 ->will( $this->returnCallback(
78 function ( $user, Title
$title, $force, $oldid ) use ( $linkTarget ) {
79 /** @var LinkTarget $linkTarget */
80 $this->assertInstanceOf( 'Title', $title );
81 $this->assertSame( $linkTarget->getDBkey(), $title->getDBkey() );
82 $this->assertSame( $linkTarget->getFragment(), $title->getFragment() );
83 $this->assertSame( $linkTarget->getNamespace(), $title->getNamespace() );
84 $this->assertSame( $linkTarget->getText(), $title->getText() );
89 $scopedOverride = WatchedItemStore
::overrideDefaultInstance( $store );
91 $item = new WatchedItem( $user, $linkTarget, $timestamp );
92 $item->resetNotificationTimestamp( $force, $oldid );
94 ScopedCallback
::consume( $scopedOverride );
97 public function testAddWatch() {
98 $title = Title
::newFromText( 'SomeTitle' );
102 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
103 $user = $this->getMock( User
::class );
104 $user->expects( $this->once() )
105 ->method( 'addWatch' )
106 ->with( $title, $checkRights );
108 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
109 $this->assertTrue( $item->addWatch() );
112 public function testRemoveWatch() {
113 $title = Title
::newFromText( 'SomeTitle' );
117 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
118 $user = $this->getMock( User
::class );
119 $user->expects( $this->once() )
120 ->method( 'removeWatch' )
121 ->with( $title, $checkRights );
123 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
124 $this->assertTrue( $item->removeWatch() );
127 public function provideBooleans() {
135 * @dataProvider provideBooleans
137 public function testIsWatched( $returnValue ) {
138 $title = Title
::newFromText( 'SomeTitle' );
142 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
143 $user = $this->getMock( User
::class );
144 $user->expects( $this->once() )
145 ->method( 'isWatched' )
146 ->with( $title, $checkRights )
147 ->will( $this->returnValue( $returnValue ) );
149 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
150 $this->assertEquals( $returnValue, $item->isWatched() );
153 public function testDuplicateEntries() {
154 $oldTitle = Title
::newFromText( 'OldTitle' );
155 $newTitle = Title
::newFromText( 'NewTitle' );
157 $store = $this->getMockWatchedItemStore();
158 $store->expects( $this->once() )
159 ->method( 'duplicateAllAssociatedEntries' )
160 ->with( $oldTitle, $newTitle );
161 $scopedOverride = WatchedItemStore
::overrideDefaultInstance( $store );
163 WatchedItem
::duplicateEntries( $oldTitle, $newTitle );
165 ScopedCallback
::consume( $scopedOverride );
168 public function testBatchAddWatch() {
169 $itemOne = new WatchedItem( User
::newFromId( 1 ), new TitleValue( 0, 'Title1' ), null );
170 $itemTwo = new WatchedItem(
171 User
::newFromId( 3 ),
172 Title
::newFromText( 'Title2' ),
176 $store = $this->getMockWatchedItemStore();
177 $store->expects( $this->exactly( 2 ) )
178 ->method( 'addWatchBatchForUser' );
179 $store->expects( $this->at( 0 ) )
180 ->method( 'addWatchBatchForUser' )
184 $itemOne->getTitle()->getSubjectPage(),
185 $itemOne->getTitle()->getTalkPage(),
188 $store->expects( $this->at( 1 ) )
189 ->method( 'addWatchBatchForUser' )
193 $itemTwo->getTitle()->getSubjectPage(),
194 $itemTwo->getTitle()->getTalkPage(),
197 $scopedOverride = WatchedItemStore
::overrideDefaultInstance( $store );
199 WatchedItem
::batchAddWatch( [ $itemOne, $itemTwo ] );
201 ScopedCallback
::consume( $scopedOverride );