mediawiki.widgets: Remove use of bind() for lexical 'this' binding
[lhc/web/wiklou.git] / tests / phpunit / includes / WatchedItemUnitTest.php
1 <?php
2
3 /**
4 * @author Addshore
5 *
6 * @covers WatchedItem
7 */
8 class WatchedItemUnitTest extends PHPUnit_Framework_TestCase {
9
10 public function provideUserTitleTimestamp() {
11 return [
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' ],
15 ];
16 }
17
18 /**
19 * @return PHPUnit_Framework_MockObject_MockObject|WatchedItemStore
20 */
21 private function getMockWatchedItemStore() {
22 return $this->getMockBuilder( WatchedItemStore::class )
23 ->disableOriginalConstructor()
24 ->getMock();
25 }
26
27 /**
28 * @dataProvider provideUserTitleTimestamp
29 */
30 public function testConstruction( $user, LinkTarget $linkTarget, $notifTimestamp ) {
31 $item = new WatchedItem( $user, $linkTarget, $notifTimestamp );
32
33 $this->assertSame( $user, $item->getUser() );
34 $this->assertSame( $linkTarget, $item->getLinkTarget() );
35 $this->assertSame( $notifTimestamp, $item->getNotificationTimestamp() );
36
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() );
43 }
44
45 /**
46 * @dataProvider provideUserTitleTimestamp
47 */
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 $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
55
56 $item = WatchedItem::fromUserTitle( $user, $linkTarget, User::IGNORE_USER_RIGHTS );
57
58 $this->assertEquals( $user, $item->getUser() );
59 $this->assertEquals( $linkTarget, $item->getLinkTarget() );
60 $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
61
62 ScopedCallback::consume( $scopedOverride );
63 }
64
65 /**
66 * @dataProvider provideUserTitleTimestamp
67 */
68 public function testResetNotificationTimestamp( $user, $linkTarget, $timestamp ) {
69 $force = 'XXX';
70 $oldid = 999;
71
72 $store = $this->getMockWatchedItemStore();
73 $store->expects( $this->once() )
74 ->method( 'resetNotificationTimestamp' )
75 ->with( $user, $this->isInstanceOf( Title::class ), $force, $oldid )
76 ->will( $this->returnCallback(
77 function ( $user, Title $title, $force, $oldid ) use ( $linkTarget ) {
78 /** @var LinkTarget $linkTarget */
79 $this->assertInstanceOf( 'Title', $title );
80 $this->assertSame( $linkTarget->getDBkey(), $title->getDBkey() );
81 $this->assertSame( $linkTarget->getFragment(), $title->getFragment() );
82 $this->assertSame( $linkTarget->getNamespace(), $title->getNamespace() );
83 $this->assertSame( $linkTarget->getText(), $title->getText() );
84
85 return true;
86 }
87 ) );
88 $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
89
90 $item = new WatchedItem( $user, $linkTarget, $timestamp );
91 $item->resetNotificationTimestamp( $force, $oldid );
92
93 ScopedCallback::consume( $scopedOverride );
94 }
95
96 public function testAddWatch() {
97 $title = Title::newFromText( 'SomeTitle' );
98 $timestamp = null;
99 $checkRights = 0;
100
101 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
102 $user = $this->getMock( User::class );
103 $user->expects( $this->once() )
104 ->method( 'addWatch' )
105 ->with( $title, $checkRights );
106
107 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
108 $this->assertTrue( $item->addWatch() );
109 }
110
111 public function testRemoveWatch() {
112 $title = Title::newFromText( 'SomeTitle' );
113 $timestamp = null;
114 $checkRights = 0;
115
116 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
117 $user = $this->getMock( User::class );
118 $user->expects( $this->once() )
119 ->method( 'removeWatch' )
120 ->with( $title, $checkRights );
121
122 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
123 $this->assertTrue( $item->removeWatch() );
124 }
125
126 public function provideBooleans() {
127 return [
128 [ true ],
129 [ false ],
130 ];
131 }
132
133 /**
134 * @dataProvider provideBooleans
135 */
136 public function testIsWatched( $returnValue ) {
137 $title = Title::newFromText( 'SomeTitle' );
138 $timestamp = null;
139 $checkRights = 0;
140
141 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
142 $user = $this->getMock( User::class );
143 $user->expects( $this->once() )
144 ->method( 'isWatched' )
145 ->with( $title, $checkRights )
146 ->will( $this->returnValue( $returnValue ) );
147
148 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
149 $this->assertEquals( $returnValue, $item->isWatched() );
150 }
151
152 public function testDuplicateEntries() {
153 $oldTitle = Title::newFromText( 'OldTitle' );
154 $newTitle = Title::newFromText( 'NewTitle' );
155
156 $store = $this->getMockWatchedItemStore();
157 $store->expects( $this->once() )
158 ->method( 'duplicateAllAssociatedEntries' )
159 ->with( $oldTitle, $newTitle );
160 $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
161
162 WatchedItem::duplicateEntries( $oldTitle, $newTitle );
163
164 ScopedCallback::consume( $scopedOverride );
165 }
166
167 public function testBatchAddWatch() {
168 /** @var WatchedItem[] $items */
169 $items = [
170 new WatchedItem( User::newFromId( 1 ), new TitleValue( 0, 'Title1' ), null ),
171 new WatchedItem( User::newFromId( 3 ), Title::newFromText( 'Title2' ), '20150101010101' ),
172 ];
173
174 $userTargetCombinations = [];
175 foreach ( $items as $item ) {
176 $userTargetCombinations[] = [ $item->getUser(), $item->getTitle()->getSubjectPage() ];
177 $userTargetCombinations[] = [ $item->getUser(), $item->getTitle()->getTalkPage() ];
178 }
179
180 $store = $this->getMockWatchedItemStore();
181 $store->expects( $this->once() )
182 ->method( 'addWatchBatch' )
183 ->with( $userTargetCombinations );
184 $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
185
186 WatchedItem::batchAddWatch( $items );
187
188 ScopedCallback::consume( $scopedOverride );
189 }
190
191 }