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