Add WatchedItemStore::getWatchedItemsForUser
[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
71 $store->removeWatch( $user, $title );
72 $this->assertFalse(
73 $store->isWatched( $user, $title ),
74 'Page should be unwatched'
75 );
76 $this->assertEquals( $initialUserWatchedItems, $store->countWatchedItems( $user ) );
77 $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
78 $this->assertCount( $initialUserWatchedItems, $watchedItemsForUser );
79 $watchedItemsForUserHasExpectedItem = false;
80 foreach ( $watchedItemsForUser as $watchedItem ) {
81 if (
82 $watchedItem->getUser()->equals( $user ) &&
83 $watchedItem->getLinkTarget() == $title->getTitleValue()
84 ) {
85 $watchedItemsForUserHasExpectedItem = true;
86 }
87 }
88 $this->assertFalse(
89 $watchedItemsForUserHasExpectedItem,
90 'getWatchedItemsForUser should not contain the page'
91 );
92 $this->assertEquals( $initialWatchers, $store->countWatchers( $title ) );
93 $this->assertEquals(
94 $initialWatchers,
95 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
96 );
97 }
98
99 public function testUpdateAndResetNotificationTimestamp() {
100 $user = $this->getUser();
101 $otherUser = ( new TestUser( 'WatchedItemStoreIntegrationTestUser_otherUser' ) )->getUser();
102 $title = Title::newFromText( 'WatchedItemStoreIntegrationTestPage' );
103 $store = WatchedItemStore::getDefaultInstance();
104 $store->addWatch( $user, $title );
105 $this->assertNull( $store->loadWatchedItem( $user, $title )->getNotificationTimestamp() );
106 $initialVisitingWatchers = $store->countVisitingWatchers( $title, '20150202020202' );
107 $initialUnreadNotifications = $store->countUnreadNotifications( $user );
108
109 $store->updateNotificationTimestamp( $otherUser, $title, '20150202010101' );
110 $this->assertEquals(
111 '20150202010101',
112 $store->loadWatchedItem( $user, $title )->getNotificationTimestamp()
113 );
114 $this->assertEquals(
115 $initialVisitingWatchers - 1,
116 $store->countVisitingWatchers( $title, '20150202020202' )
117 );
118 $this->assertEquals(
119 $initialUnreadNotifications + 1,
120 $store->countUnreadNotifications( $user )
121 );
122 $this->assertSame(
123 true,
124 $store->countUnreadNotifications( $user, $initialUnreadNotifications + 1 )
125 );
126
127 $this->assertTrue( $store->resetNotificationTimestamp( $user, $title ) );
128 $this->assertNull( $store->getWatchedItem( $user, $title )->getNotificationTimestamp() );
129 $this->assertEquals(
130 $initialVisitingWatchers,
131 $store->countVisitingWatchers( $title, '20150202020202' )
132 );
133 }
134
135 public function testDuplicateAllAssociatedEntries() {
136 $user = $this->getUser();
137 $titleOld = Title::newFromText( 'WatchedItemStoreIntegrationTestPageOld' );
138 $titleNew = Title::newFromText( 'WatchedItemStoreIntegrationTestPageNew' );
139 $store = WatchedItemStore::getDefaultInstance();
140 $store->addWatch( $user, $titleOld->getSubjectPage() );
141 $store->addWatch( $user, $titleOld->getTalkPage() );
142 // Cleanup after previous tests
143 $store->removeWatch( $user, $titleNew->getSubjectPage() );
144 $store->removeWatch( $user, $titleNew->getTalkPage() );
145
146 $store->duplicateAllAssociatedEntries( $titleOld, $titleNew );
147
148 $this->assertTrue( $store->isWatched( $user, $titleOld->getSubjectPage() ) );
149 $this->assertTrue( $store->isWatched( $user, $titleOld->getTalkPage() ) );
150 $this->assertTrue( $store->isWatched( $user, $titleNew->getSubjectPage() ) );
151 $this->assertTrue( $store->isWatched( $user, $titleNew->getTalkPage() ) );
152 }
153
154 }