ad5041c9564e3eb15abadffebf6e2eb4acb959bd
[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 $this->assertEquals( $initialWatchers + 1, $store->countWatchers( $title ) );
43 $this->assertEquals(
44 $initialWatchers + 1,
45 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
46 );
47 $this->assertEquals(
48 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialWatchers + 1 ] ],
49 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers + 1 ] )
50 );
51 $this->assertEquals(
52 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
53 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers + 2 ] )
54 );
55 $this->assertEquals(
56 [ $title->getNamespace() => [ $title->getDBkey() => null ] ],
57 $store->getNotificationTimestampsBatch( $user, [ $title ] )
58 );
59
60 $store->removeWatch( $user, $title );
61 $this->assertFalse(
62 $store->isWatched( $user, $title ),
63 'Page should be unwatched'
64 );
65 $this->assertEquals( $initialUserWatchedItems, $store->countWatchedItems( $user ) );
66 $this->assertEquals( $initialWatchers, $store->countWatchers( $title ) );
67 $this->assertEquals(
68 $initialWatchers,
69 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
70 );
71 $this->assertEquals(
72 [ $title->getNamespace() => [ $title->getDBkey() => false ] ],
73 $store->getNotificationTimestampsBatch( $user, [ $title ] )
74 );
75 }
76
77 public function testUpdateAndResetNotificationTimestamp() {
78 $user = $this->getUser();
79 $otherUser = ( new TestUser( 'WatchedItemStoreIntegrationTestUser_otherUser' ) )->getUser();
80 $title = Title::newFromText( 'WatchedItemStoreIntegrationTestPage' );
81 $store = WatchedItemStore::getDefaultInstance();
82 $store->addWatch( $user, $title );
83 $this->assertNull( $store->loadWatchedItem( $user, $title )->getNotificationTimestamp() );
84 $initialVisitingWatchers = $store->countVisitingWatchers( $title, '20150202020202' );
85 $initialUnreadNotifications = $store->countUnreadNotifications( $user );
86
87 $store->updateNotificationTimestamp( $otherUser, $title, '20150202010101' );
88 $this->assertEquals(
89 '20150202010101',
90 $store->loadWatchedItem( $user, $title )->getNotificationTimestamp()
91 );
92 $this->assertEquals(
93 [ $title->getNamespace() => [ $title->getDBkey() => '20150202010101' ] ],
94 $store->getNotificationTimestampsBatch( $user, [ $title ] )
95 );
96 $this->assertEquals(
97 $initialVisitingWatchers - 1,
98 $store->countVisitingWatchers( $title, '20150202020202' )
99 );
100 $this->assertEquals(
101 $initialVisitingWatchers - 1,
102 $store->countVisitingWatchersMultiple(
103 [ [ $title, '20150202020202' ] ]
104 )[$title->getNamespace()][$title->getDBkey()]
105 );
106 $this->assertEquals(
107 $initialUnreadNotifications + 1,
108 $store->countUnreadNotifications( $user )
109 );
110 $this->assertSame(
111 true,
112 $store->countUnreadNotifications( $user, $initialUnreadNotifications + 1 )
113 );
114
115 $this->assertTrue( $store->resetNotificationTimestamp( $user, $title ) );
116 $this->assertNull( $store->getWatchedItem( $user, $title )->getNotificationTimestamp() );
117 $this->assertEquals(
118 [ $title->getNamespace() => [ $title->getDBkey() => null ] ],
119 $store->getNotificationTimestampsBatch( $user, [ $title ] )
120 );
121 $this->assertEquals(
122 $initialVisitingWatchers,
123 $store->countVisitingWatchers( $title, '20150202020202' )
124 );
125 $this->assertEquals(
126 $initialVisitingWatchers,
127 $store->countVisitingWatchersMultiple(
128 [ [ $title, '20150202020202' ] ]
129 )[$title->getNamespace()][$title->getDBkey()]
130 );
131 $this->assertEquals(
132 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialVisitingWatchers ] ],
133 $store->countVisitingWatchersMultiple(
134 [ [ $title, '20150202020202' ] ], $initialVisitingWatchers
135 )
136 );
137 $this->assertEquals(
138 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
139 $store->countVisitingWatchersMultiple(
140 [ [ $title, '20150202020202' ] ], $initialVisitingWatchers + 1
141 )
142 );
143 }
144
145 public function testDuplicateAllAssociatedEntries() {
146 $user = $this->getUser();
147 $titleOld = Title::newFromText( 'WatchedItemStoreIntegrationTestPageOld' );
148 $titleNew = Title::newFromText( 'WatchedItemStoreIntegrationTestPageNew' );
149 $store = WatchedItemStore::getDefaultInstance();
150 $store->addWatch( $user, $titleOld->getSubjectPage() );
151 $store->addWatch( $user, $titleOld->getTalkPage() );
152 // Cleanup after previous tests
153 $store->removeWatch( $user, $titleNew->getSubjectPage() );
154 $store->removeWatch( $user, $titleNew->getTalkPage() );
155
156 $store->duplicateAllAssociatedEntries( $titleOld, $titleNew );
157
158 $this->assertTrue( $store->isWatched( $user, $titleOld->getSubjectPage() ) );
159 $this->assertTrue( $store->isWatched( $user, $titleOld->getTalkPage() ) );
160 $this->assertTrue( $store->isWatched( $user, $titleNew->getSubjectPage() ) );
161 $this->assertTrue( $store->isWatched( $user, $titleNew->getTalkPage() ) );
162 }
163
164 }