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 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() );
64 * @dataProvider provideUserTitleTimestamp
66 public function testResetNotificationTimestamp( $user, $linkTarget, $timestamp ) {
70 $store = $this->getMockWatchedItemStore();
71 $store->expects( $this->once() )
72 ->method( 'resetNotificationTimestamp' )
73 ->with( $user, $this->isInstanceOf( Title
::class ), $force, $oldid )
74 ->will( $this->returnCallback(
75 function ( $user, Title
$title, $force, $oldid ) use ( $linkTarget ) {
76 /** @var LinkTarget $linkTarget */
77 $this->assertInstanceOf( 'Title', $title );
78 $this->assertSame( $linkTarget->getDBkey(), $title->getDBkey() );
79 $this->assertSame( $linkTarget->getFragment(), $title->getFragment() );
80 $this->assertSame( $linkTarget->getNamespace(), $title->getNamespace() );
81 $this->assertSame( $linkTarget->getText(), $title->getText() );
86 WatchedItemStore
::overrideDefaultInstance( $store );
88 $item = new WatchedItem( $user, $linkTarget, $timestamp );
89 $item->resetNotificationTimestamp( $force, $oldid );
92 public function testAddWatch() {
93 $title = Title
::newFromText( 'SomeTitle' );
97 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
98 $user = $this->getMock( User
::class );
99 $user->expects( $this->once() )
100 ->method( 'addWatch' )
101 ->with( $title, $checkRights );
103 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
104 $this->assertTrue( $item->addWatch() );
107 public function testRemoveWatch() {
108 $title = Title
::newFromText( 'SomeTitle' );
112 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
113 $user = $this->getMock( User
::class );
114 $user->expects( $this->once() )
115 ->method( 'removeWatch' )
116 ->with( $title, $checkRights );
118 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
119 $this->assertTrue( $item->removeWatch() );
122 public function provideBooleans() {
130 * @dataProvider provideBooleans
132 public function testIsWatched( $returnValue ) {
133 $title = Title
::newFromText( 'SomeTitle' );
137 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
138 $user = $this->getMock( User
::class );
139 $user->expects( $this->once() )
140 ->method( 'isWatched' )
141 ->with( $title, $checkRights )
142 ->will( $this->returnValue( $returnValue ) );
144 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
145 $this->assertEquals( $returnValue, $item->isWatched() );
148 public function testDuplicateEntries() {
149 $oldTitle = Title
::newFromText( 'OldTitle' );
150 $newTitle = Title
::newFromText( 'NewTitle' );
152 $store = $this->getMockWatchedItemStore();
153 $store->expects( $this->once() )
154 ->method( 'duplicateAllAssociatedEntries' )
155 ->with( $oldTitle, $newTitle );
156 WatchedItemStore
::overrideDefaultInstance( $store );
158 WatchedItem
::duplicateEntries( $oldTitle, $newTitle );
161 public function testBatchAddWatch() {
162 /** @var WatchedItem[] $items */
164 new WatchedItem( User
::newFromId( 1 ), new TitleValue( 0, 'Title1' ), null ),
165 new WatchedItem( User
::newFromId( 3 ), Title
::newFromText( 'Title2' ), '20150101010101' ),
168 $userTargetCombinations = [];
169 foreach ( $items as $item ) {
170 $userTargetCombinations[] = [ $item->getUser(), $item->getTitle()->getSubjectPage() ];
171 $userTargetCombinations[] = [ $item->getUser(), $item->getTitle()->getTalkPage() ];
174 $store = $this->getMockWatchedItemStore();
175 $store->expects( $this->once() )
176 ->method( 'addWatchBatch' )
177 ->with( $userTargetCombinations );
178 WatchedItemStore
::overrideDefaultInstance( $store );
180 WatchedItem
::batchAddWatch( $items );