Merge "Add WatchedItemStore::countWatchedItems"
[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
56 $store->removeWatch( $user, $title );
57 $this->assertFalse(
58 $store->isWatched( $user, $title ),
59 'Page should be unwatched'
60 );
61 $this->assertEquals( $initialUserWatchedItems, $store->countWatchedItems( $user ) );
62 $this->assertEquals( $initialWatchers, $store->countWatchers( $title ) );
63 $this->assertEquals(
64 $initialWatchers,
65 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
66 );
67 }
68
69 public function testUpdateAndResetNotificationTimestamp() {
70 $user = $this->getUser();
71 $otherUser = ( new TestUser( 'WatchedItemStoreIntegrationTestUser_otherUser' ) )->getUser();
72 $title = Title::newFromText( 'WatchedItemStoreIntegrationTestPage' );
73 $store = WatchedItemStore::getDefaultInstance();
74 $store->addWatch( $user, $title );
75 $this->assertNull( $store->loadWatchedItem( $user, $title )->getNotificationTimestamp() );
76 $initialVisitingWatchers = $store->countVisitingWatchers( $title, '20150202020202' );
77 $initialUnreadNotifications = $store->countUnreadNotifications( $user );
78
79 $store->updateNotificationTimestamp( $otherUser, $title, '20150202010101' );
80 $this->assertEquals(
81 '20150202010101',
82 $store->loadWatchedItem( $user, $title )->getNotificationTimestamp()
83 );
84 $this->assertEquals(
85 $initialVisitingWatchers - 1,
86 $store->countVisitingWatchers( $title, '20150202020202' )
87 );
88 $this->assertEquals(
89 $initialUnreadNotifications + 1,
90 $store->countUnreadNotifications( $user )
91 );
92 $this->assertSame(
93 true,
94 $store->countUnreadNotifications( $user, $initialUnreadNotifications + 1 )
95 );
96
97 $this->assertTrue( $store->resetNotificationTimestamp( $user, $title ) );
98 $this->assertNull( $store->getWatchedItem( $user, $title )->getNotificationTimestamp() );
99 $this->assertEquals(
100 $initialVisitingWatchers,
101 $store->countVisitingWatchers( $title, '20150202020202' )
102 );
103 }
104
105 public function testDuplicateAllAssociatedEntries() {
106 $user = $this->getUser();
107 $titleOld = Title::newFromText( 'WatchedItemStoreIntegrationTestPageOld' );
108 $titleNew = Title::newFromText( 'WatchedItemStoreIntegrationTestPageNew' );
109 $store = WatchedItemStore::getDefaultInstance();
110 $store->addWatch( $user, $titleOld->getSubjectPage() );
111 $store->addWatch( $user, $titleOld->getTalkPage() );
112 // Cleanup after previous tests
113 $store->removeWatch( $user, $titleNew->getSubjectPage() );
114 $store->removeWatch( $user, $titleNew->getTalkPage() );
115
116 $store->duplicateAllAssociatedEntries( $titleOld, $titleNew );
117
118 $this->assertTrue( $store->isWatched( $user, $titleOld->getSubjectPage() ) );
119 $this->assertTrue( $store->isWatched( $user, $titleOld->getTalkPage() ) );
120 $this->assertTrue( $store->isWatched( $user, $titleNew->getSubjectPage() ) );
121 $this->assertTrue( $store->isWatched( $user, $titleNew->getTalkPage() ) );
122 }
123
124 }