Move countVisitingWatchers 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
75 $store->updateNotificationTimestamp( $otherUser, $title, '20150202010101' );
76 $this->assertEquals(
77 '20150202010101',
78 $store->loadWatchedItem( $user, $title )->getNotificationTimestamp()
79 );
80 $this->assertEquals(
81 $initialVisitingWatchers - 1,
82 $store->countVisitingWatchers( $title, '20150202020202' )
83 );
84
85 $this->assertTrue( $store->resetNotificationTimestamp( $user, $title ) );
86 $this->assertNull( $store->getWatchedItem( $user, $title )->getNotificationTimestamp() );
87 $this->assertEquals(
88 $initialVisitingWatchers,
89 $store->countVisitingWatchers( $title, '20150202020202' )
90 );
91 }
92
93 public function testDuplicateAllAssociatedEntries() {
94 $user = $this->getUser();
95 $titleOld = Title::newFromText( 'WatchedItemStoreIntegrationTestPageOld' );
96 $titleNew = Title::newFromText( 'WatchedItemStoreIntegrationTestPageNew' );
97 $store = WatchedItemStore::getDefaultInstance();
98 $store->addWatch( $user, $titleOld->getSubjectPage() );
99 $store->addWatch( $user, $titleOld->getTalkPage() );
100 // Cleanup after previous tests
101 $store->removeWatch( $user, $titleNew->getSubjectPage() );
102 $store->removeWatch( $user, $titleNew->getTalkPage() );
103
104 $store->duplicateAllAssociatedEntries( $titleOld, $titleNew );
105
106 $this->assertTrue( $store->isWatched( $user, $titleOld->getSubjectPage() ) );
107 $this->assertTrue( $store->isWatched( $user, $titleOld->getTalkPage() ) );
108 $this->assertTrue( $store->isWatched( $user, $titleNew->getSubjectPage() ) );
109 $this->assertTrue( $store->isWatched( $user, $titleNew->getTalkPage() ) );
110 }
111
112 }