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() );
81 public function testAddWatch() {
82 $title = Title
::newFromText( 'SomeTitle' );
86 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
87 $user = $this->getMock( User
::class );
88 $user->expects( $this->once() )
89 ->method( 'addWatch' )
90 ->with( $title, $checkRights );
92 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
93 $this->assertTrue( $item->addWatch() );
96 public function testRemoveWatch() {
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( 'removeWatch' )
105 ->with( $title, $checkRights );
107 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
108 $this->assertTrue( $item->removeWatch() );
111 public function provideBooleans() {
119 * @dataProvider provideBooleans
121 public function testIsWatched( $returnValue ) {
122 $title = Title
::newFromText( 'SomeTitle' );
126 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
127 $user = $this->getMock( User
::class );
128 $user->expects( $this->once() )
129 ->method( 'isWatched' )
130 ->with( $title, $checkRights )
131 ->will( $this->returnValue( $returnValue ) );
133 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
134 $this->assertEquals( $returnValue, $item->isWatched() );
137 public function testDuplicateEntries() {
138 $oldTitle = Title
::newFromText( 'OldTitle' );
139 $newTitle = Title
::newFromText( 'NewTitle' );
141 $store = $this->getMockWatchedItemStore();
142 $store->expects( $this->once() )
143 ->method( 'duplicateAllAssociatedEntries' )
144 ->with( $oldTitle, $newTitle );
145 $this->setService( 'WatchedItemStore', $store );
147 WatchedItem
::duplicateEntries( $oldTitle, $newTitle );