Merge "SpecialUploadStash: Add links to view a thumb of each uploaded file"
[lhc/web/wiklou.git] / tests / phpunit / includes / WatchedItemStoreIntegrationTest.php
1 <?php
2
3 /**
4 * @author Addshore
5 *
6 * @group Database
7 *
8 * @covers WatchedItemStore
9 */
10 class WatchedItemStoreIntegrationTest extends MediaWikiTestCase {
11
12 public function setUp() {
13 parent::setUp();
14 self::$users['WatchedItemStoreIntegrationTestUser']
15 = new TestUser( 'WatchedItemStoreIntegrationTestUser' );
16 }
17
18 private function getUser() {
19 return self::$users['WatchedItemStoreIntegrationTestUser']->getUser();
20 }
21
22 public function testWatchAndUnWatchItem() {
23 $user = $this->getUser();
24 $title = Title::newFromText( 'WatchedItemStoreIntegrationTestPage' );
25 $store = WatchedItemStore::getDefaultInstance();
26 // Cleanup after previous tests
27 $store->removeWatch( $user, $title );
28 $initialWatchers = $store->countWatchers( $title );
29 $initialUserWatchedItems = $store->countWatchedItems( $user );
30
31 $this->assertFalse(
32 $store->isWatched( $user, $title ),
33 'Page should not initially be watched'
34 );
35
36 $store->addWatch( $user, $title );
37 $this->assertTrue(
38 $store->isWatched( $user, $title ),
39 'Page should be watched'
40 );
41 $this->assertEquals( $initialUserWatchedItems + 1, $store->countWatchedItems( $user ) );
42 $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
43 $this->assertCount( $initialUserWatchedItems + 1, $watchedItemsForUser );
44 $watchedItemsForUserHasExpectedItem = false;
45 foreach ( $watchedItemsForUser as $watchedItem ) {
46 if (
47 $watchedItem->getUser()->equals( $user ) &&
48 $watchedItem->getLinkTarget() == $title->getTitleValue()
49 ) {
50 $watchedItemsForUserHasExpectedItem = true;
51 }
52 }
53 $this->assertTrue(
54 $watchedItemsForUserHasExpectedItem,
55 'getWatchedItemsForUser should contain the page'
56 );
57 $this->assertEquals( $initialWatchers + 1, $store->countWatchers( $title ) );
58 $this->assertEquals(
59 $initialWatchers + 1,
60 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
61 );
62 $this->assertEquals(
63 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialWatchers + 1 ] ],
64 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers + 1 ] )
65 );
66 $this->assertEquals(
67 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
68 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers + 2 ] )
69 );
70 $this->assertEquals(
71 [ $title->getNamespace() => [ $title->getDBkey() => null ] ],
72 $store->getNotificationTimestampsBatch( $user, [ $title ] )
73 );
74
75 $store->removeWatch( $user, $title );
76 $this->assertFalse(
77 $store->isWatched( $user, $title ),
78 'Page should be unwatched'
79 );
80 $this->assertEquals( $initialUserWatchedItems, $store->countWatchedItems( $user ) );
81 $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
82 $this->assertCount( $initialUserWatchedItems, $watchedItemsForUser );
83 $watchedItemsForUserHasExpectedItem = false;
84 foreach ( $watchedItemsForUser as $watchedItem ) {
85 if (
86 $watchedItem->getUser()->equals( $user ) &&
87 $watchedItem->getLinkTarget() == $title->getTitleValue()
88 ) {
89 $watchedItemsForUserHasExpectedItem = true;
90 }
91 }
92 $this->assertFalse(
93 $watchedItemsForUserHasExpectedItem,
94 'getWatchedItemsForUser should not contain the page'
95 );
96 $this->assertEquals( $initialWatchers, $store->countWatchers( $title ) );
97 $this->assertEquals(
98 $initialWatchers,
99 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
100 );
101 $this->assertEquals(
102 [ $title->getNamespace() => [ $title->getDBkey() => false ] ],
103 $store->getNotificationTimestampsBatch( $user, [ $title ] )
104 );
105 }
106
107 public function testUpdateAndResetNotificationTimestamp() {
108 $user = $this->getUser();
109 $otherUser = ( new TestUser( 'WatchedItemStoreIntegrationTestUser_otherUser' ) )->getUser();
110 $title = Title::newFromText( 'WatchedItemStoreIntegrationTestPage' );
111 $store = WatchedItemStore::getDefaultInstance();
112 $store->addWatch( $user, $title );
113 $this->assertNull( $store->loadWatchedItem( $user, $title )->getNotificationTimestamp() );
114 $initialVisitingWatchers = $store->countVisitingWatchers( $title, '20150202020202' );
115 $initialUnreadNotifications = $store->countUnreadNotifications( $user );
116
117 $store->updateNotificationTimestamp( $otherUser, $title, '20150202010101' );
118 $this->assertEquals(
119 '20150202010101',
120 $store->loadWatchedItem( $user, $title )->getNotificationTimestamp()
121 );
122 $this->assertEquals(
123 [ $title->getNamespace() => [ $title->getDBkey() => '20150202010101' ] ],
124 $store->getNotificationTimestampsBatch( $user, [ $title ] )
125 );
126 $this->assertEquals(
127 $initialVisitingWatchers - 1,
128 $store->countVisitingWatchers( $title, '20150202020202' )
129 );
130 $this->assertEquals(
131 $initialVisitingWatchers - 1,
132 $store->countVisitingWatchersMultiple(
133 [ [ $title, '20150202020202' ] ]
134 )[$title->getNamespace()][$title->getDBkey()]
135 );
136 $this->assertEquals(
137 $initialUnreadNotifications + 1,
138 $store->countUnreadNotifications( $user )
139 );
140 $this->assertSame(
141 true,
142 $store->countUnreadNotifications( $user, $initialUnreadNotifications + 1 )
143 );
144
145 $this->assertTrue( $store->resetNotificationTimestamp( $user, $title ) );
146 $this->assertNull( $store->getWatchedItem( $user, $title )->getNotificationTimestamp() );
147 $this->assertEquals(
148 [ $title->getNamespace() => [ $title->getDBkey() => null ] ],
149 $store->getNotificationTimestampsBatch( $user, [ $title ] )
150 );
151 $this->assertEquals(
152 $initialVisitingWatchers,
153 $store->countVisitingWatchers( $title, '20150202020202' )
154 );
155 $this->assertEquals(
156 $initialVisitingWatchers,
157 $store->countVisitingWatchersMultiple(
158 [ [ $title, '20150202020202' ] ]
159 )[$title->getNamespace()][$title->getDBkey()]
160 );
161 $this->assertEquals(
162 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialVisitingWatchers ] ],
163 $store->countVisitingWatchersMultiple(
164 [ [ $title, '20150202020202' ] ], $initialVisitingWatchers
165 )
166 );
167 $this->assertEquals(
168 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
169 $store->countVisitingWatchersMultiple(
170 [ [ $title, '20150202020202' ] ], $initialVisitingWatchers + 1
171 )
172 );
173 }
174
175 public function testDuplicateAllAssociatedEntries() {
176 $user = $this->getUser();
177 $titleOld = Title::newFromText( 'WatchedItemStoreIntegrationTestPageOld' );
178 $titleNew = Title::newFromText( 'WatchedItemStoreIntegrationTestPageNew' );
179 $store = WatchedItemStore::getDefaultInstance();
180 $store->addWatch( $user, $titleOld->getSubjectPage() );
181 $store->addWatch( $user, $titleOld->getTalkPage() );
182 // Cleanup after previous tests
183 $store->removeWatch( $user, $titleNew->getSubjectPage() );
184 $store->removeWatch( $user, $titleNew->getTalkPage() );
185
186 $store->duplicateAllAssociatedEntries( $titleOld, $titleNew );
187
188 $this->assertTrue( $store->isWatched( $user, $titleOld->getSubjectPage() ) );
189 $this->assertTrue( $store->isWatched( $user, $titleOld->getTalkPage() ) );
190 $this->assertTrue( $store->isWatched( $user, $titleNew->getSubjectPage() ) );
191 $this->assertTrue( $store->isWatched( $user, $titleNew->getTalkPage() ) );
192 }
193
194 }