Merge "Hygiene: Add wrapHTML method"
[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 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
63 /**
64 * @dataProvider provideUserTitleTimestamp
65 */
66 public function testResetNotificationTimestamp( $user, $linkTarget, $timestamp ) {
67 $force = 'XXX';
68 $oldid = 999;
69
70 $store = $this->getMockWatchedItemStore();
71 $store->expects( $this->once() )
72 ->method( 'resetNotificationTimestamp' )
73 ->with( $user, $this->isInstanceOf( Title::class ), $force, $oldid )
74 ->will( $this->returnCallback(
75 function ( $user, Title $title, $force, $oldid ) use ( $linkTarget ) {
76 /** @var LinkTarget $linkTarget */
77 $this->assertInstanceOf( 'Title', $title );
78 $this->assertSame( $linkTarget->getDBkey(), $title->getDBkey() );
79 $this->assertSame( $linkTarget->getFragment(), $title->getFragment() );
80 $this->assertSame( $linkTarget->getNamespace(), $title->getNamespace() );
81 $this->assertSame( $linkTarget->getText(), $title->getText() );
82
83 return true;
84 }
85 ) );
86 WatchedItemStore::overrideDefaultInstance( $store );
87
88 $item = new WatchedItem( $user, $linkTarget, $timestamp );
89 $item->resetNotificationTimestamp( $force, $oldid );
90 }
91
92 public function testAddWatch() {
93 $title = Title::newFromText( 'SomeTitle' );
94 $timestamp = null;
95 $checkRights = 0;
96
97 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
98 $user = $this->getMock( User::class );
99 $user->expects( $this->once() )
100 ->method( 'addWatch' )
101 ->with( $title, $checkRights );
102
103 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
104 $this->assertTrue( $item->addWatch() );
105 }
106
107 public function testRemoveWatch() {
108 $title = Title::newFromText( 'SomeTitle' );
109 $timestamp = null;
110 $checkRights = 0;
111
112 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
113 $user = $this->getMock( User::class );
114 $user->expects( $this->once() )
115 ->method( 'removeWatch' )
116 ->with( $title, $checkRights );
117
118 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
119 $this->assertTrue( $item->removeWatch() );
120 }
121
122 public function provideBooleans() {
123 return [
124 [ true ],
125 [ false ],
126 ];
127 }
128
129 /**
130 * @dataProvider provideBooleans
131 */
132 public function testIsWatched( $returnValue ) {
133 $title = Title::newFromText( 'SomeTitle' );
134 $timestamp = null;
135 $checkRights = 0;
136
137 /** @var User|PHPUnit_Framework_MockObject_MockObject $user */
138 $user = $this->getMock( User::class );
139 $user->expects( $this->once() )
140 ->method( 'isWatched' )
141 ->with( $title, $checkRights )
142 ->will( $this->returnValue( $returnValue ) );
143
144 $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
145 $this->assertEquals( $returnValue, $item->isWatched() );
146 }
147
148 public function testDuplicateEntries() {
149 $oldTitle = Title::newFromText( 'OldTitle' );
150 $newTitle = Title::newFromText( 'NewTitle' );
151
152 $store = $this->getMockWatchedItemStore();
153 $store->expects( $this->once() )
154 ->method( 'duplicateAllAssociatedEntries' )
155 ->with( $oldTitle, $newTitle );
156 WatchedItemStore::overrideDefaultInstance( $store );
157
158 WatchedItem::duplicateEntries( $oldTitle, $newTitle );
159 }
160
161 public function testBatchAddWatch() {
162 /** @var WatchedItem[] $items */
163 $items = [
164 new WatchedItem( User::newFromId( 1 ), new TitleValue( 0, 'Title1' ), null ),
165 new WatchedItem( User::newFromId( 3 ), Title::newFromText( 'Title2' ), '20150101010101' ),
166 ];
167
168 $userTargetCombinations = [];
169 foreach ( $items as $item ) {
170 $userTargetCombinations[] = [ $item->getUser(), $item->getTitle()->getSubjectPage() ];
171 $userTargetCombinations[] = [ $item->getUser(), $item->getTitle()->getTalkPage() ];
172 }
173
174 $store = $this->getMockWatchedItemStore();
175 $store->expects( $this->once() )
176 ->method( 'addWatchBatch' )
177 ->with( $userTargetCombinations );
178 WatchedItemStore::overrideDefaultInstance( $store );
179
180 WatchedItem::batchAddWatch( $items );
181 }
182
183 }