Chinese Conversion Table Update 2017-6
[lhc/web/wiklou.git] / tests / phpunit / includes / watcheditem / WatchedItemUnitTest.php
1 <?php
2 use MediaWiki\Linker\LinkTarget;
3
4 /**
5 * @author Addshore
6 *
7 * @covers WatchedItem
8 */
9 class WatchedItemUnitTest extends MediaWikiTestCase {
10
11 /**
12 * @param int $id
13 *
14 * @return PHPUnit_Framework_MockObject_MockObject|User
15 */
16 private function getMockUser( $id ) {
17 $user = $this->createMock( 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;
25 }
26
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' ],
33 ];
34 }
35
36 /**
37 * @return PHPUnit_Framework_MockObject_MockObject|WatchedItemStore
38 */
39 private function getMockWatchedItemStore() {
40 return $this->getMockBuilder( WatchedItemStore::class )
41 ->disableOriginalConstructor()
42 ->getMock();
43 }
44
45 /**
46 * @dataProvider provideUserTitleTimestamp
47 */
48 public function testConstruction( $user, LinkTarget $linkTarget, $notifTimestamp ) {
49 $item = new WatchedItem( $user, $linkTarget, $notifTimestamp );
50
51 $this->assertSame( $user, $item->getUser() );
52 $this->assertSame( $linkTarget, $item->getLinkTarget() );
53 $this->assertSame( $notifTimestamp, $item->getNotificationTimestamp() );
54
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() );
61 }
62
63 /**
64 * @dataProvider provideUserTitleTimestamp
65 */
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 );
73
74 $item = WatchedItem::fromUserTitle( $user, $linkTarget, User::IGNORE_USER_RIGHTS );
75
76 $this->assertEquals( $user, $item->getUser() );
77 $this->assertEquals( $linkTarget, $item->getLinkTarget() );
78 $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
79 }
80
81 public function testAddWatch() {
82 $title = Title::newFromText( 'SomeTitle' );
83 $timestamp = null;
84 $checkRights = 0;
85
86 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
87 $user = $this->createMock( User::class );
88 $user->expects( $this->once() )
89 ->method( 'addWatch' )
90 ->with( $title, $checkRights );
91
92 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
93 $this->assertTrue( $item->addWatch() );
94 }
95
96 public function testRemoveWatch() {
97 $title = Title::newFromText( 'SomeTitle' );
98 $timestamp = null;
99 $checkRights = 0;
100
101 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
102 $user = $this->createMock( User::class );
103 $user->expects( $this->once() )
104 ->method( 'removeWatch' )
105 ->with( $title, $checkRights );
106
107 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
108 $this->assertTrue( $item->removeWatch() );
109 }
110
111 public function provideBooleans() {
112 return [
113 [ true ],
114 [ false ],
115 ];
116 }
117
118 /**
119 * @dataProvider provideBooleans
120 */
121 public function testIsWatched( $returnValue ) {
122 $title = Title::newFromText( 'SomeTitle' );
123 $timestamp = null;
124 $checkRights = 0;
125
126 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
127 $user = $this->createMock( User::class );
128 $user->expects( $this->once() )
129 ->method( 'isWatched' )
130 ->with( $title, $checkRights )
131 ->will( $this->returnValue( $returnValue ) );
132
133 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
134 $this->assertEquals( $returnValue, $item->isWatched() );
135 }
136
137 public function testDuplicateEntries() {
138 $oldTitle = Title::newFromText( 'OldTitle' );
139 $newTitle = Title::newFromText( 'NewTitle' );
140
141 $store = $this->getMockWatchedItemStore();
142 $store->expects( $this->once() )
143 ->method( 'duplicateAllAssociatedEntries' )
144 ->with( $oldTitle, $newTitle );
145 $this->setService( 'WatchedItemStore', $store );
146
147 WatchedItem::duplicateEntries( $oldTitle, $newTitle );
148 }
149
150 }