MediaWiki  1.30.2
WatchedItemUnitTest.php
Go to the documentation of this file.
1 <?php
3 
10 
11  public function setUp() {
12  parent::setUp();
13 
14  $this->hideDeprecated( 'WatchedItem::fromUserTitle' );
15  $this->hideDeprecated( 'WatchedItem::addWatch' );
16  $this->hideDeprecated( 'WatchedItem::removeWatch' );
17  $this->hideDeprecated( 'WatchedItem::isWatched' );
18  $this->hideDeprecated( 'WatchedItem::duplicateEntries' );
19  $this->hideDeprecated( 'WatchedItem::batchAddWatch' );
20  }
21 
27  private function getMockUser( $id ) {
28  $user = $this->createMock( User::class );
29  $user->expects( $this->any() )
30  ->method( 'getId' )
31  ->will( $this->returnValue( $id ) );
32  $user->expects( $this->any() )
33  ->method( 'isAllowed' )
34  ->will( $this->returnValue( true ) );
35  return $user;
36  }
37 
38  public function provideUserTitleTimestamp() {
39  $user = $this->getMockUser( 111 );
40  return [
41  [ $user, Title::newFromText( 'SomeTitle' ), null ],
42  [ $user, Title::newFromText( 'SomeTitle' ), '20150101010101' ],
43  [ $user, new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
44  ];
45  }
46 
50  private function getMockWatchedItemStore() {
51  return $this->getMockBuilder( WatchedItemStore::class )
52  ->disableOriginalConstructor()
53  ->getMock();
54  }
55 
59  public function testConstruction( $user, LinkTarget $linkTarget, $notifTimestamp ) {
60  $item = new WatchedItem( $user, $linkTarget, $notifTimestamp );
61 
62  $this->assertSame( $user, $item->getUser() );
63  $this->assertSame( $linkTarget, $item->getLinkTarget() );
64  $this->assertSame( $notifTimestamp, $item->getNotificationTimestamp() );
65 
66  // The below tests the internal WatchedItem::getTitle method
67  $this->assertInstanceOf( 'Title', $item->getTitle() );
68  $this->assertSame( $linkTarget->getDBkey(), $item->getTitle()->getDBkey() );
69  $this->assertSame( $linkTarget->getFragment(), $item->getTitle()->getFragment() );
70  $this->assertSame( $linkTarget->getNamespace(), $item->getTitle()->getNamespace() );
71  $this->assertSame( $linkTarget->getText(), $item->getTitle()->getText() );
72  }
73 
77  public function testFromUserTitle( $user, $linkTarget, $timestamp ) {
78  $store = $this->getMockWatchedItemStore();
79  $store->expects( $this->once() )
80  ->method( 'loadWatchedItem' )
81  ->with( $user, $linkTarget )
82  ->will( $this->returnValue( new WatchedItem( $user, $linkTarget, $timestamp ) ) );
83  $this->setService( 'WatchedItemStore', $store );
84 
86 
87  $this->assertEquals( $user, $item->getUser() );
88  $this->assertEquals( $linkTarget, $item->getLinkTarget() );
89  $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
90  }
91 
92  public function testAddWatch() {
93  $title = Title::newFromText( 'SomeTitle' );
94  $timestamp = null;
95  $checkRights = 0;
96 
98  $user = $this->createMock( User::class );
99  $user->expects( $this->once() )
100  ->method( 'addWatch' )
101  ->with( $title, $checkRights );
102 
103  $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
104  $this->assertTrue( $item->addWatch() );
105  }
106 
107  public function testRemoveWatch() {
108  $title = Title::newFromText( 'SomeTitle' );
109  $timestamp = null;
110  $checkRights = 0;
111 
113  $user = $this->createMock( User::class );
114  $user->expects( $this->once() )
115  ->method( 'removeWatch' )
116  ->with( $title, $checkRights );
117 
118  $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
119  $this->assertTrue( $item->removeWatch() );
120  }
121 
122  public function provideBooleans() {
123  return [
124  [ true ],
125  [ false ],
126  ];
127  }
128 
132  public function testIsWatched( $returnValue ) {
133  $title = Title::newFromText( 'SomeTitle' );
134  $timestamp = null;
135  $checkRights = 0;
136 
138  $user = $this->createMock( User::class );
139  $user->expects( $this->once() )
140  ->method( 'isWatched' )
141  ->with( $title, $checkRights )
142  ->will( $this->returnValue( $returnValue ) );
143 
144  $item = new WatchedItem( $user, $title, $timestamp, $checkRights );
145  $this->assertEquals( $returnValue, $item->isWatched() );
146  }
147 
148  public function testDuplicateEntries() {
149  $oldTitle = Title::newFromText( 'OldTitle' );
150  $newTitle = Title::newFromText( 'NewTitle' );
151 
152  $store = $this->getMockWatchedItemStore();
153  $store->expects( $this->once() )
154  ->method( 'duplicateAllAssociatedEntries' )
155  ->with( $oldTitle, $newTitle );
156  $this->setService( 'WatchedItemStore', $store );
157 
159  }
160 
161 }
WatchedItemUnitTest\testIsWatched
testIsWatched( $returnValue)
provideBooleans
Definition: WatchedItemUnitTest.php:132
$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:77
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:50
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:38
WatchedItemUnitTest\testDuplicateEntries
testDuplicateEntries()
Definition: WatchedItemUnitTest.php:148
MediaWiki\Linker\LinkTarget\getNamespace
getNamespace()
Get the namespace index.
WatchedItemUnitTest\testRemoveWatch
testRemoveWatch()
Definition: WatchedItemUnitTest.php:107
$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
MediaWikiTestCase\hideDeprecated
hideDeprecated( $function)
Don't throw a warning if $function is deprecated and called later.
Definition: MediaWikiTestCase.php:1467
WatchedItemUnitTest\testAddWatch
testAddWatch()
Definition: WatchedItemUnitTest.php:92
$oldTitle
versus $oldTitle
Definition: globals.txt:16
WatchedItemUnitTest\setUp
setUp()
Definition: WatchedItemUnitTest.php:11
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:122
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:27
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:59
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36