Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / WatchedItemUnitTest.php
blob7e1ff3d7a9381617d892a888d263e5019d10aeca
1 <?php
2 use MediaWiki\Linker\LinkTarget;
4 /**
5 * @author Addshore
7 * @covers WatchedItem
8 */
9 class WatchedItemUnitTest extends MediaWikiTestCase {
11 /**
12 * @param int $id
14 * @return PHPUnit_Framework_MockObject_MockObject|User
16 private function getMockUser( $id ) {
17 $user = $this->getMock( User::class );
18 $user->expects( $this->any() )
19 ->method( 'getId' )
20 ->will( $this->returnValue( $id ) );
21 $user->expects( $this->any() )
22 ->method( 'isAllowed' )
23 ->will( $this->returnValue( true ) );
24 return $user;
27 public function provideUserTitleTimestamp() {
28 $user = $this->getMockUser( 111 );
29 return [
30 [ $user, Title::newFromText( 'SomeTitle' ), null ],
31 [ $user, Title::newFromText( 'SomeTitle' ), '20150101010101' ],
32 [ $user, new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
36 /**
37 * @return PHPUnit_Framework_MockObject_MockObject|WatchedItemStore
39 private function getMockWatchedItemStore() {
40 return $this->getMockBuilder( WatchedItemStore::class )
41 ->disableOriginalConstructor()
42 ->getMock();
45 /**
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() );
63 /**
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' );
83 $timestamp = null;
84 $checkRights = 0;
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' );
98 $timestamp = null;
99 $checkRights = 0;
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() {
112 return [
113 [ true ],
114 [ false ],
119 * @dataProvider provideBooleans
121 public function testIsWatched( $returnValue ) {
122 $title = Title::newFromText( 'SomeTitle' );
123 $timestamp = null;
124 $checkRights = 0;
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 );