MediaWiki  1.27.2
WatchedItemUnitTest.php
Go to the documentation of this file.
1 <?php
3 
9 class WatchedItemUnitTest extends PHPUnit_Framework_TestCase {
10 
11  public function provideUserTitleTimestamp() {
12  return [
13  [ User::newFromId( 111 ), Title::newFromText( 'SomeTitle' ), null ],
14  [ User::newFromId( 111 ), Title::newFromText( 'SomeTitle' ), '20150101010101' ],
15  [ User::newFromId( 111 ), new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
16  ];
17  }
18 
22  private function getMockWatchedItemStore() {
23  return $this->getMockBuilder( WatchedItemStore::class )
24  ->disableOriginalConstructor()
25  ->getMock();
26  }
27 
31  public function testConstruction( $user, LinkTarget $linkTarget, $notifTimestamp ) {
32  $item = new WatchedItem( $user, $linkTarget, $notifTimestamp );
33 
34  $this->assertSame( $user, $item->getUser() );
35  $this->assertSame( $linkTarget, $item->getLinkTarget() );
36  $this->assertSame( $notifTimestamp, $item->getNotificationTimestamp() );
37 
38  // The below tests the internal WatchedItem::getTitle method
39  $this->assertInstanceOf( 'Title', $item->getTitle() );
40  $this->assertSame( $linkTarget->getDBkey(), $item->getTitle()->getDBkey() );
41  $this->assertSame( $linkTarget->getFragment(), $item->getTitle()->getFragment() );
42  $this->assertSame( $linkTarget->getNamespace(), $item->getTitle()->getNamespace() );
43  $this->assertSame( $linkTarget->getText(), $item->getTitle()->getText() );
44  }
45 
49  public function testFromUserTitle( $user, $linkTarget, $timestamp ) {
50  $store = $this->getMockWatchedItemStore();
51  $store->expects( $this->once() )
52  ->method( 'loadWatchedItem' )
53  ->with( $user, $linkTarget )
54  ->will( $this->returnValue( new WatchedItem( $user, $linkTarget, $timestamp ) ) );
55  $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
56 
58 
59  $this->assertEquals( $user, $item->getUser() );
60  $this->assertEquals( $linkTarget, $item->getLinkTarget() );
61  $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
62 
63  ScopedCallback::consume( $scopedOverride );
64  }
65 
69  public function testResetNotificationTimestamp( $user, $linkTarget, $timestamp ) {
70  $force = 'XXX';
71  $oldid = 999;
72 
73  $store = $this->getMockWatchedItemStore();
74  $store->expects( $this->once() )
75  ->method( 'resetNotificationTimestamp' )
76  ->with( $user, $this->isInstanceOf( Title::class ), $force, $oldid )
77  ->will( $this->returnCallback(
78  function ( $user, Title $title, $force, $oldid ) use ( $linkTarget ) {
80  $this->assertInstanceOf( 'Title', $title );
81  $this->assertSame( $linkTarget->getDBkey(), $title->getDBkey() );
82  $this->assertSame( $linkTarget->getFragment(), $title->getFragment() );
83  $this->assertSame( $linkTarget->getNamespace(), $title->getNamespace() );
84  $this->assertSame( $linkTarget->getText(), $title->getText() );
85 
86  return true;
87  }
88  ) );
89  $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
90 
91  $item = new WatchedItem( $user, $linkTarget, $timestamp );
92  $item->resetNotificationTimestamp( $force, $oldid );
93 
94  ScopedCallback::consume( $scopedOverride );
95  }
96 
97  public function testAddWatch() {
98  $title = Title::newFromText( 'SomeTitle' );
99  $timestamp = null;
100  $checkRights = 0;
101 
103  $user = $this->getMock( User::class );
104  $user->expects( $this->once() )
105  ->method( 'addWatch' )
106  ->with( $title, $checkRights );
107 
108  $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
109  $this->assertTrue( $item->addWatch() );
110  }
111 
112  public function testRemoveWatch() {
113  $title = Title::newFromText( 'SomeTitle' );
114  $timestamp = null;
115  $checkRights = 0;
116 
118  $user = $this->getMock( User::class );
119  $user->expects( $this->once() )
120  ->method( 'removeWatch' )
121  ->with( $title, $checkRights );
122 
123  $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
124  $this->assertTrue( $item->removeWatch() );
125  }
126 
127  public function provideBooleans() {
128  return [
129  [ true ],
130  [ false ],
131  ];
132  }
133 
137  public function testIsWatched( $returnValue ) {
138  $title = Title::newFromText( 'SomeTitle' );
139  $timestamp = null;
140  $checkRights = 0;
141 
143  $user = $this->getMock( User::class );
144  $user->expects( $this->once() )
145  ->method( 'isWatched' )
146  ->with( $title, $checkRights )
147  ->will( $this->returnValue( $returnValue ) );
148 
149  $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
150  $this->assertEquals( $returnValue, $item->isWatched() );
151  }
152 
153  public function testDuplicateEntries() {
154  $oldTitle = Title::newFromText( 'OldTitle' );
155  $newTitle = Title::newFromText( 'NewTitle' );
156 
157  $store = $this->getMockWatchedItemStore();
158  $store->expects( $this->once() )
159  ->method( 'duplicateAllAssociatedEntries' )
160  ->with( $oldTitle, $newTitle );
161  $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
162 
164 
165  ScopedCallback::consume( $scopedOverride );
166  }
167 
168  public function testBatchAddWatch() {
169  $itemOne = new WatchedItem( User::newFromId( 1 ), new TitleValue( 0, 'Title1' ), null );
170  $itemTwo = new WatchedItem(
171  User::newFromId( 3 ),
172  Title::newFromText( 'Title2' ),
173  '20150101010101'
174  );
175 
176  $store = $this->getMockWatchedItemStore();
177  $store->expects( $this->exactly( 2 ) )
178  ->method( 'addWatchBatchForUser' );
179  $store->expects( $this->at( 0 ) )
180  ->method( 'addWatchBatchForUser' )
181  ->with(
182  $itemOne->getUser(),
183  [
184  $itemOne->getTitle()->getSubjectPage(),
185  $itemOne->getTitle()->getTalkPage(),
186  ]
187  );
188  $store->expects( $this->at( 1 ) )
189  ->method( 'addWatchBatchForUser' )
190  ->with(
191  $itemTwo->getUser(),
192  [
193  $itemTwo->getTitle()->getSubjectPage(),
194  $itemTwo->getTitle()->getTalkPage(),
195  ]
196  );
197  $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
198 
199  WatchedItem::batchAddWatch( [ $itemOne, $itemTwo ] );
200 
201  ScopedCallback::consume( $scopedOverride );
202  }
203 
204 }
static batchAddWatch(array $items)
getFragment()
Get the Title fragment (i.e.
Definition: Title.php:1353
getText()
Get the text form (spaces not underscores) of the main part.
Definition: Title.php:893
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36
static newFromId($id)
Static factory method for creation from a given user ID.
Definition: User.php:591
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:277
Represents a title within MediaWiki.
Definition: Title.php:34
getNamespace()
Get the namespace index.
getFragment()
Get the link fragment (i.e.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1798
testResetNotificationTimestamp($user, $linkTarget, $timestamp)
provideUserTitleTimestamp
getDBkey()
Get the main part with underscores.
Definition: Title.php:911
if($limit) $timestamp
static duplicateEntries(Title $oldTitle, Title $newTitle)
testConstruction($user, LinkTarget $linkTarget, $notifTimestamp)
provideUserTitleTimestamp
static overrideDefaultInstance(WatchedItemStore $store=null)
Overrides the default instance of this class This is intended for use while testing and will fail if ...
getDBkey()
Get the main part with underscores.
const IGNORE_USER_RIGHTS
Definition: User.php:84
Representation of a pair of user and title for watchlist entries.
Definition: WatchedItem.php:31
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:934
testIsWatched($returnValue)
provideBooleans
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
testFromUserTitle($user, $linkTarget, $timestamp)
provideUserTitleTimestamp
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
versus $oldTitle
Definition: globals.txt:16
getText()
Returns the link in text form, without namespace prefix or fragment.
static consume(ScopedCallback &$sc=null)
Trigger a scoped callback and destroy it.
static fromUserTitle($user, $title, $checkRights=User::CHECK_USER_RIGHTS)