MediaWiki  1.28.0
WatchedItemUnitTest.php
Go to the documentation of this file.
1 <?php
3 
10 
16  private function getMockUser( $id ) {
17  $user = $this->getMock( User::class );
18  $user->expects( $this->any() )
19  ->method( 'getId' )
20  ->will( $this->returnValue( $id ) );
21  $user->expects( $this->any() )
22  ->method( 'isAllowed' )
23  ->will( $this->returnValue( true ) );
24  return $user;
25  }
26 
27  public function provideUserTitleTimestamp() {
28  $user = $this->getMockUser( 111 );
29  return [
30  [ $user, Title::newFromText( 'SomeTitle' ), null ],
31  [ $user, Title::newFromText( 'SomeTitle' ), '20150101010101' ],
32  [ $user, new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
33  ];
34  }
35 
39  private function getMockWatchedItemStore() {
40  return $this->getMockBuilder( WatchedItemStore::class )
41  ->disableOriginalConstructor()
42  ->getMock();
43  }
44 
48  public function testConstruction( $user, LinkTarget $linkTarget, $notifTimestamp ) {
49  $item = new WatchedItem( $user, $linkTarget, $notifTimestamp );
50 
51  $this->assertSame( $user, $item->getUser() );
52  $this->assertSame( $linkTarget, $item->getLinkTarget() );
53  $this->assertSame( $notifTimestamp, $item->getNotificationTimestamp() );
54 
55  // The below tests the internal WatchedItem::getTitle method
56  $this->assertInstanceOf( 'Title', $item->getTitle() );
57  $this->assertSame( $linkTarget->getDBkey(), $item->getTitle()->getDBkey() );
58  $this->assertSame( $linkTarget->getFragment(), $item->getTitle()->getFragment() );
59  $this->assertSame( $linkTarget->getNamespace(), $item->getTitle()->getNamespace() );
60  $this->assertSame( $linkTarget->getText(), $item->getTitle()->getText() );
61  }
62 
66  public function testFromUserTitle( $user, $linkTarget, $timestamp ) {
67  $store = $this->getMockWatchedItemStore();
68  $store->expects( $this->once() )
69  ->method( 'loadWatchedItem' )
70  ->with( $user, $linkTarget )
71  ->will( $this->returnValue( new WatchedItem( $user, $linkTarget, $timestamp ) ) );
72  $this->setService( 'WatchedItemStore', $store );
73 
75 
76  $this->assertEquals( $user, $item->getUser() );
77  $this->assertEquals( $linkTarget, $item->getLinkTarget() );
78  $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
79  }
80 
81  public function testAddWatch() {
82  $title = Title::newFromText( 'SomeTitle' );
83  $timestamp = null;
84  $checkRights = 0;
85 
87  $user = $this->getMock( User::class );
88  $user->expects( $this->once() )
89  ->method( 'addWatch' )
90  ->with( $title, $checkRights );
91 
92  $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
93  $this->assertTrue( $item->addWatch() );
94  }
95 
96  public function testRemoveWatch() {
97  $title = Title::newFromText( 'SomeTitle' );
98  $timestamp = null;
99  $checkRights = 0;
100 
102  $user = $this->getMock( User::class );
103  $user->expects( $this->once() )
104  ->method( 'removeWatch' )
105  ->with( $title, $checkRights );
106 
107  $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
108  $this->assertTrue( $item->removeWatch() );
109  }
110 
111  public function provideBooleans() {
112  return [
113  [ true ],
114  [ false ],
115  ];
116  }
117 
121  public function testIsWatched( $returnValue ) {
122  $title = Title::newFromText( 'SomeTitle' );
123  $timestamp = null;
124  $checkRights = 0;
125 
127  $user = $this->getMock( User::class );
128  $user->expects( $this->once() )
129  ->method( 'isWatched' )
130  ->with( $title, $checkRights )
131  ->will( $this->returnValue( $returnValue ) );
132 
133  $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
134  $this->assertEquals( $returnValue, $item->isWatched() );
135  }
136 
137  public function testDuplicateEntries() {
138  $oldTitle = Title::newFromText( 'OldTitle' );
139  $newTitle = Title::newFromText( 'NewTitle' );
140 
141  $store = $this->getMockWatchedItemStore();
142  $store->expects( $this->once() )
143  ->method( 'duplicateAllAssociatedEntries' )
144  ->with( $oldTitle, $newTitle );
145  $this->setService( 'WatchedItemStore', $store );
146 
148  }
149 
150 }
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
getNamespace()
Get the namespace index.
setService($name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown...
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:1936
if($limit) $timestamp
static duplicateEntries(Title $oldTitle, Title $newTitle)
testConstruction($user, LinkTarget $linkTarget, $notifTimestamp)
provideUserTitleTimestamp
getDBkey()
Get the main part with underscores.
const IGNORE_USER_RIGHTS
Definition: User.php:85
Representation of a pair of user and title for watchlist entries.
Definition: WatchedItem.php:32
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
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 fromUserTitle($user, $title, $checkRights=User::CHECK_USER_RIGHTS)