Merge "Add countUnreadNotifications to WatchedItemStore"
[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
30 $this->assertFalse(
31 $store->isWatched( $user, $title ),
32 'Page should not initially be watched'
33 );
34
35 $store->addWatch( $user, $title );
36 $this->assertTrue(
37 $store->isWatched( $user, $title ),
38 'Page should be watched'
39 );
40 $this->assertEquals( $initialWatchers + 1, $store->countWatchers( $title ) );
41 $this->assertEquals(
42 $initialWatchers + 1,
43 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
44 );
45 $this->assertEquals(
46 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialWatchers + 1 ] ],
47 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers + 1 ] )
48 );
49 $this->assertEquals(
50 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
51 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers + 2 ] )
52 );
53
54 $store->removeWatch( $user, $title );
55 $this->assertFalse(
56 $store->isWatched( $user, $title ),
57 'Page should be unwatched'
58 );
59 $this->assertEquals( $initialWatchers, $store->countWatchers( $title ) );
60 $this->assertEquals(
61 $initialWatchers,
62 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
63 );
64 }
65
66 public function testUpdateAndResetNotificationTimestamp() {
67 $user = $this->getUser();
68 $otherUser = ( new TestUser( 'WatchedItemStoreIntegrationTestUser_otherUser' ) )->getUser();
69 $title = Title::newFromText( 'WatchedItemStoreIntegrationTestPage' );
70 $store = WatchedItemStore::getDefaultInstance();
71 $store->addWatch( $user, $title );
72 $this->assertNull( $store->loadWatchedItem( $user, $title )->getNotificationTimestamp() );
73 $initialVisitingWatchers = $store->countVisitingWatchers( $title, '20150202020202' );
74 $initialUnreadNotifications = $store->countUnreadNotifications( $user );
75
76 $store->updateNotificationTimestamp( $otherUser, $title, '20150202010101' );
77 $this->assertEquals(
78 '20150202010101',
79 $store->loadWatchedItem( $user, $title )->getNotificationTimestamp()
80 );
81 $this->assertEquals(
82 $initialVisitingWatchers - 1,
83 $store->countVisitingWatchers( $title, '20150202020202' )
84 );
85 $this->assertEquals(
86 $initialUnreadNotifications + 1,
87 $store->countUnreadNotifications( $user )
88 );
89 $this->assertSame(
90 true,
91 $store->countUnreadNotifications( $user, $initialUnreadNotifications + 1 )
92 );
93
94 $this->assertTrue( $store->resetNotificationTimestamp( $user, $title ) );
95 $this->assertNull( $store->getWatchedItem( $user, $title )->getNotificationTimestamp() );
96 $this->assertEquals(
97 $initialVisitingWatchers,
98 $store->countVisitingWatchers( $title, '20150202020202' )
99 );
100 }
101
102 public function testDuplicateAllAssociatedEntries() {
103 $user = $this->getUser();
104 $titleOld = Title::newFromText( 'WatchedItemStoreIntegrationTestPageOld' );
105 $titleNew = Title::newFromText( 'WatchedItemStoreIntegrationTestPageNew' );
106 $store = WatchedItemStore::getDefaultInstance();
107 $store->addWatch( $user, $titleOld->getSubjectPage() );
108 $store->addWatch( $user, $titleOld->getTalkPage() );
109 // Cleanup after previous tests
110 $store->removeWatch( $user, $titleNew->getSubjectPage() );
111 $store->removeWatch( $user, $titleNew->getTalkPage() );
112
113 $store->duplicateAllAssociatedEntries( $titleOld, $titleNew );
114
115 $this->assertTrue( $store->isWatched( $user, $titleOld->getSubjectPage() ) );
116 $this->assertTrue( $store->isWatched( $user, $titleOld->getTalkPage() ) );
117 $this->assertTrue( $store->isWatched( $user, $titleNew->getSubjectPage() ) );
118 $this->assertTrue( $store->isWatched( $user, $titleNew->getTalkPage() ) );
119 }
120
121 }