2 use MediaWiki\Linker\LinkTarget;
9 class WatchedItemUnitTest extends MediaWikiTestCase {
11 public function setUp() {
14 $this->hideDeprecated( 'WatchedItem::fromUserTitle' );
15 $this->hideDeprecated( 'WatchedItem::addWatch' );
16 $this->hideDeprecated( 'WatchedItem::removeWatch' );
17 $this->hideDeprecated( 'WatchedItem::isWatched' );
18 $this->hideDeprecated( 'WatchedItem::duplicateEntries' );
19 $this->hideDeprecated( 'WatchedItem::batchAddWatch' );
25 * @return PHPUnit_Framework_MockObject_MockObject|User
27 private function getMockUser( $id ) {
28 $user = $this->createMock( User::class );
29 $user->expects( $this->any() )
31 ->will( $this->returnValue( $id ) );
32 $user->expects( $this->any() )
33 ->method( 'isAllowed' )
34 ->will( $this->returnValue( true ) );
38 public function provideUserTitleTimestamp() {
39 $user = $this->getMockUser( 111 );
41 [ $user, Title::newFromText( 'SomeTitle' ), null ],
42 [ $user, Title::newFromText( 'SomeTitle' ), '20150101010101' ],
43 [ $user, new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
48 * @return PHPUnit_Framework_MockObject_MockObject|WatchedItemStore
50 private function getMockWatchedItemStore() {
51 return $this->getMockBuilder( WatchedItemStore::class )
52 ->disableOriginalConstructor()
57 * @dataProvider provideUserTitleTimestamp
59 public function testConstruction( $user, LinkTarget $linkTarget, $notifTimestamp ) {
60 $item = new WatchedItem( $user, $linkTarget, $notifTimestamp );
62 $this->assertSame( $user, $item->getUser() );
63 $this->assertSame( $linkTarget, $item->getLinkTarget() );
64 $this->assertSame( $notifTimestamp, $item->getNotificationTimestamp() );
66 // The below tests the internal WatchedItem::getTitle method
67 $this->assertInstanceOf( 'Title', $item->getTitle() );
68 $this->assertSame( $linkTarget->getDBkey(), $item->getTitle()->getDBkey() );
69 $this->assertSame( $linkTarget->getFragment(), $item->getTitle()->getFragment() );
70 $this->assertSame( $linkTarget->getNamespace(), $item->getTitle()->getNamespace() );
71 $this->assertSame( $linkTarget->getText(), $item->getTitle()->getText() );
75 * @dataProvider provideUserTitleTimestamp
77 public function testFromUserTitle( $user, $linkTarget, $timestamp ) {
78 $store = $this->getMockWatchedItemStore();
79 $store->expects( $this->once() )
80 ->method( 'loadWatchedItem' )
81 ->with( $user, $linkTarget )
82 ->will( $this->returnValue( new WatchedItem( $user, $linkTarget, $timestamp ) ) );
83 $this->setService( 'WatchedItemStore', $store );
85 $item = WatchedItem::fromUserTitle( $user, $linkTarget, User::IGNORE_USER_RIGHTS );
87 $this->assertEquals( $user, $item->getUser() );
88 $this->assertEquals( $linkTarget, $item->getLinkTarget() );
89 $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
92 public function testAddWatch() {
93 $title = Title::newFromText( 'SomeTitle' );
97 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
98 $user = $this->createMock( 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->createMock( 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->createMock( 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 $this->setService( 'WatchedItemStore', $store );
158 WatchedItem::duplicateEntries( $oldTitle, $newTitle );