MediaWiki  1.30.0
WatchedItemUnitTest.php
Go to the documentation of this file.
1 <?php
3 
10 
16  private function getMockUser( $id ) {
17  $user = $this->createMock( 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->createMock( 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->createMock( 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->createMock( 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 }
WatchedItemUnitTest\testIsWatched
testIsWatched( $returnValue)
provideBooleans
Definition: WatchedItemUnitTest.php:121
$user
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 account $user
Definition: hooks.txt:244
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:268
MediaWiki\Linker\LinkTarget\getText
getText()
Returns the link in text form, without namespace prefix or fragment.
WatchedItemUnitTest\testFromUserTitle
testFromUserTitle( $user, $linkTarget, $timestamp)
provideUserTitleTimestamp
Definition: WatchedItemUnitTest.php:66
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
WatchedItemUnitTest
Definition: WatchedItemUnitTest.php:9
WatchedItemUnitTest\getMockWatchedItemStore
getMockWatchedItemStore()
Definition: WatchedItemUnitTest.php:39
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
php
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
WatchedItemUnitTest\provideUserTitleTimestamp
provideUserTitleTimestamp()
Definition: WatchedItemUnitTest.php:27
WatchedItemUnitTest\testDuplicateEntries
testDuplicateEntries()
Definition: WatchedItemUnitTest.php:137
MediaWiki\Linker\LinkTarget\getNamespace
getNamespace()
Get the namespace index.
WatchedItemUnitTest\testRemoveWatch
testRemoveWatch()
Definition: WatchedItemUnitTest.php:96
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
WatchedItemUnitTest\testAddWatch
testAddWatch()
Definition: WatchedItemUnitTest.php:81
$oldTitle
versus $oldTitle
Definition: globals.txt:16
WatchedItem\duplicateEntries
static duplicateEntries(Title $oldTitle, Title $newTitle)
Definition: WatchedItem.php:194
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
WatchedItem
Representation of a pair of user and title for watchlist entries.
Definition: WatchedItem.php:32
MediaWiki\Linker\LinkTarget\getDBkey
getDBkey()
Get the main part with underscores.
WatchedItemUnitTest\provideBooleans
provideBooleans()
Definition: WatchedItemUnitTest.php:111
MediaWiki\Linker\LinkTarget\getFragment
getFragment()
Get the link fragment (i.e.
WatchedItem\fromUserTitle
static fromUserTitle( $user, $title, $checkRights=User::CHECK_USER_RIGHTS)
Definition: WatchedItem.php:154
true
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:1965
class
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
User\IGNORE_USER_RIGHTS
const IGNORE_USER_RIGHTS
Definition: User.php:88
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:27
WatchedItemUnitTest\getMockUser
getMockUser( $id)
Definition: WatchedItemUnitTest.php:16
MediaWikiTestCase\setService
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
Definition: MediaWikiTestCase.php:622
WatchedItemUnitTest\testConstruction
testConstruction( $user, LinkTarget $linkTarget, $notifTimestamp)
provideUserTitleTimestamp
Definition: WatchedItemUnitTest.php:48
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36