MediaWiki  1.33.0
SpecialPageTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
13 
14  protected function setUp() {
15  parent::setUp();
16 
17  $this->setContentLang( 'en' );
18  $this->setMwGlobals( [
19  'wgScript' => '/index.php',
20  ] );
21  }
22 
26  public function testGetTitleFor( $expectedName, $name ) {
28  $expected = Title::makeTitle( NS_SPECIAL, $expectedName );
29  $this->assertEquals( $expected, $title );
30  }
31 
32  public function getTitleForProvider() {
33  return [
34  [ 'UserLogin', 'Userlogin' ]
35  ];
36  }
37 
41  public function testInvalidGetTitleFor() {
43  $expected = Title::makeTitle( NS_SPECIAL, 'Cat' );
44  $this->assertEquals( $expected, $title );
45  }
46 
51  public function testGetTitleForWithWarning( $expected, $name ) {
53  $this->assertEquals( $expected, $title );
54  }
55 
56  public function getTitleForWithWarningProvider() {
57  return [
58  [ Title::makeTitle( NS_SPECIAL, 'UserLogin' ), 'UserLogin' ]
59  ];
60  }
61 
65  public function testRequireLoginAnon( $expected, $reason, $title ) {
66  $specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
67 
68  $user = User::newFromId( 0 );
69  $specialPage->getContext()->setUser( $user );
70  $specialPage->getContext()->setLanguage( Language::factory( 'en' ) );
71 
72  $this->setExpectedException( UserNotLoggedIn::class, $expected );
73 
74  // $specialPage->requireLogin( [ $reason [, $title ] ] )
75  call_user_func_array(
76  [ $specialPage, 'requireLogin' ],
77  array_filter( [ $reason, $title ] )
78  );
79  }
80 
81  public function requireLoginAnonProvider() {
82  $lang = 'en';
83 
84  $expected1 = wfMessage( 'exception-nologin-text' )->inLanguage( $lang )->text();
85  $expected2 = wfMessage( 'about' )->inLanguage( $lang )->text();
86 
87  return [
88  [ $expected1, null, null ],
89  [ $expected2, 'about', null ],
90  [ $expected2, 'about', 'about' ],
91  ];
92  }
93 
94  public function testRequireLoginNotAnon() {
95  $specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
96 
97  $user = User::newFromName( "UTSysop" );
98  $specialPage->getContext()->setUser( $user );
99 
100  $specialPage->requireLogin();
101 
102  // no exception thrown, logged in use can access special page
103  $this->assertTrue( true );
104  }
105 
106  public function provideBuildPrevNextNavigation() {
107  yield [ 0, 20, false, false ];
108  yield [ 17, 20, false, false ];
109  yield [ 0, 17, false, false ];
110  yield [ 0, 20, true, 'Foo' ];
111  yield [ 17, 20, true, 'Föö_Bär' ];
112  }
113 
117  public function testBuildPrevNextNavigation( $offset, $limit, $atEnd, $subPage ) {
118  $this->setUserLang( Language::factory( 'qqx' ) ); // disable i18n
119 
120  $specialPage = new SpecialPage( 'Watchlist' );
121  $specialPage = TestingAccessWrapper::newFromObject( $specialPage );
122 
123  $html = $specialPage->buildPrevNextNavigation(
124  $offset,
125  $limit,
126  [ 'x' => 25 ],
127  $atEnd,
128  $subPage
129  );
130 
131  $this->assertStringStartsWith( '(viewprevnext:', $html );
132 
133  preg_match_all( '!<a.*?</a>!', $html, $m, PREG_PATTERN_ORDER );
134  $links = $m[0];
135 
136  foreach ( $links as $a ) {
137  if ( $subPage ) {
138  $this->assertContains( 'Special:Watchlist/' . wfUrlencode( $subPage ), $a );
139  } else {
140  $this->assertContains( 'Special:Watchlist', $a );
141  $this->assertNotContains( 'Special:Watchlist/', $a );
142  }
143  $this->assertContains( 'x=25', $a );
144  }
145 
146  $i = 0;
147 
148  if ( $offset > 0 ) {
149  $this->assertContains(
150  'limit=' . $limit . '&amp;offset=' . max( 0, $offset - $limit ) . '&amp;',
151  $links[ $i ]
152  );
153  $this->assertContains( 'title="(prevn-title: ' . $limit . ')"', $links[$i] );
154  $this->assertContains( 'class="mw-prevlink"', $links[$i] );
155  $this->assertContains( '>(prevn: ' . $limit . ')<', $links[$i] );
156  $i += 1;
157  }
158 
159  if ( !$atEnd ) {
160  $this->assertContains(
161  'limit=' . $limit . '&amp;offset=' . ( $offset + $limit ) . '&amp;',
162  $links[ $i ]
163  );
164  $this->assertContains( 'title="(nextn-title: ' . $limit . ')"', $links[$i] );
165  $this->assertContains( 'class="mw-nextlink"', $links[$i] );
166  $this->assertContains( '>(nextn: ' . $limit . ')<', $links[$i] );
167  $i += 1;
168  }
169 
170  $this->assertCount( 5 + $i, $links );
171 
172  $this->assertContains( 'limit=20&amp;offset=' . $offset, $links[$i] );
173  $this->assertContains( 'title="(shown-title: 20)"', $links[$i] );
174  $this->assertContains( 'class="mw-numlink"', $links[$i] );
175  $this->assertContains( '>20<', $links[$i] );
176  $i += 4;
177 
178  $this->assertContains( 'limit=500&amp;offset=' . $offset, $links[$i] );
179  $this->assertContains( 'title="(shown-title: 500)"', $links[$i] );
180  $this->assertContains( 'class="mw-numlink"', $links[$i] );
181  $this->assertContains( '>500<', $links[$i] );
182  }
183 
184 }
SpecialPageTest\getTitleForProvider
getTitleForProvider()
Definition: SpecialPageTest.php:32
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:609
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
SpecialPageTest\testInvalidGetTitleFor
testInvalidGetTitleFor()
PHPUnit_Framework_Error_Notice.
Definition: SpecialPageTest.php:41
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
wfUrlencode
wfUrlencode( $s)
We want some things to be included as literal characters in our title URLs for prettiness,...
Definition: GlobalFunctions.php:333
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:585
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Definition: SpecialPage.php:82
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
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:53
SpecialPageTest\requireLoginAnonProvider
requireLoginAnonProvider()
Definition: SpecialPageTest.php:81
$html
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 an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1985
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
SpecialPageTest\getTitleForWithWarningProvider
getTitleForWithWarningProvider()
Definition: SpecialPageTest.php:56
SpecialPageTest\testGetTitleFor
testGetTitleFor( $expectedName, $name)
getTitleForProvider
Definition: SpecialPageTest.php:26
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
SpecialPageTest\setUp
setUp()
Definition: SpecialPageTest.php:14
SpecialPageTest
SpecialPage.
Definition: SpecialPageTest.php:12
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:576
MediaWikiTestCase\setUserLang
setUserLang( $lang)
Definition: MediaWikiTestCase.php:1057
SpecialPageTest\testBuildPrevNextNavigation
testBuildPrevNextNavigation( $offset, $limit, $atEnd, $subPage)
provideBuildPrevNextNavigation
Definition: SpecialPageTest.php:117
SpecialPageTest\provideBuildPrevNextNavigation
provideBuildPrevNextNavigation()
Definition: SpecialPageTest.php:106
null
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
MediaWikiTestCase\setContentLang
setContentLang( $lang)
Definition: MediaWikiTestCase.php:1066
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
SpecialPageTest\testRequireLoginAnon
testRequireLoginAnon( $expected, $reason, $title)
requireLoginAnonProvider
Definition: SpecialPageTest.php:65
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:36
SpecialPageTest\testGetTitleForWithWarning
testGetTitleForWithWarning( $expected, $name)
PHPUnit_Framework_Error_Notice getTitleForWithWarningProvider.
Definition: SpecialPageTest.php:51
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:215
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
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
SpecialPageTest\testRequireLoginNotAnon
testRequireLoginNotAnon()
Definition: SpecialPageTest.php:94