3 namespace MediaWiki\Tests\SpecialPage
;
5 use MediaWiki\MainConfigNames
;
6 use MediaWiki\Request\FauxRequest
;
7 use MediaWiki\SpecialPage\SpecialPage
;
8 use MediaWiki\Tests\User\TempUser\TempUserTestTrait
;
9 use MediaWiki\Title\Title
;
10 use MediaWiki\User\User
;
11 use MediaWikiIntegrationTestCase
;
15 * @covers \MediaWiki\SpecialPage\SpecialPage
19 * @author Katie Filbert < aude.wiki@gmail.com >
21 class SpecialPageTest
extends MediaWikiIntegrationTestCase
{
23 use TempUserTestTrait
;
25 protected function setUp(): void
{
28 $this->overrideConfigValues( [
29 MainConfigNames
::Script
=> '/index.php',
30 MainConfigNames
::LanguageCode
=> 'en',
35 * @dataProvider getTitleForProvider
37 public function testGetTitleFor( $expectedName, $name ) {
38 $title = SpecialPage
::getTitleFor( $name );
39 $expected = Title
::makeTitle( NS_SPECIAL
, $expectedName );
40 $this->assertEquals( $expected, $title );
43 public static function getTitleForProvider() {
45 [ 'UserLogin', 'Userlogin' ]
49 public function testInvalidGetTitleFor() {
50 $this->expectPHPError(
53 $title = SpecialPage
::getTitleFor( 'cat' );
54 $expected = Title
::makeTitle( NS_SPECIAL
, 'Cat' );
55 $this->assertEquals( $expected, $title );
61 * @dataProvider getTitleForWithWarningProvider
63 public function testGetTitleForWithWarning( $expected, $name ) {
64 $this->expectPHPError(
66 function () use ( $name, $expected ) {
67 $title = SpecialPage
::getTitleFor( $name );
68 $this->assertEquals( $expected, $title );
73 public static function getTitleForWithWarningProvider() {
75 [ Title
::makeTitle( NS_SPECIAL
, 'UserLogin' ), 'UserLogin' ]
80 * @dataProvider requireLoginAnonProvider
82 public function testRequireLoginAnon( $expected, ...$params ) {
83 $specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
85 $user = User
::newFromId( 0 );
86 $specialPage->getContext()->setUser( $user );
87 $specialPage->getContext()->setLanguage(
88 $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ) );
90 $this->expectException( UserNotLoggedIn
::class );
91 $this->expectExceptionMessage( $expected );
93 $specialPage->requireLogin( ...$params );
96 public static function requireLoginAnonProvider() {
99 $expected1 = wfMessage( 'exception-nologin-text' )->inLanguage( $lang )->text();
100 $expected2 = wfMessage( 'about' )->inLanguage( $lang )->text();
104 [ $expected2, 'about' ],
105 [ $expected2, 'about', 'about' ],
109 public function testRequireLoginNotAnon() {
110 $specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
112 $user = $this->getTestSysop()->getUser();
113 $specialPage->getContext()->setUser( $user );
115 $specialPage->requireLogin();
117 // no exception thrown, logged in use can access special page
118 $this->assertTrue( true );
122 * @dataProvider provideRequireNamedUserAnon
124 public function testRequireNamedUserAnon( $expected, ...$params ) {
125 $specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
127 $user = User
::newFromId( 0 );
128 $specialPage->getContext()->setUser( $user );
129 $specialPage->getContext()->setLanguage(
130 $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ) );
132 $this->expectException( UserNotLoggedIn
::class );
133 $this->expectExceptionMessage( $expected );
135 $specialPage->requireLogin( ...$params );
138 public static function provideRequireNamedUserAnon() {
141 $expected1 = wfMessage( 'exception-nologin-text' )->inLanguage( $lang )->text();
142 $expected2 = wfMessage( 'about' )->inLanguage( $lang )->text();
146 [ $expected2, 'about' ],
147 [ $expected2, 'about', 'about' ],
151 public function testRequireNamedUserForTempUser() {
152 $this->enableAutoCreateTempUser();
153 $specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
155 $user = $this->getServiceContainer()->getTempUserCreator()->create( null, new FauxRequest() )->getUser();
156 $specialPage->getContext()->setUser( $user );
158 $this->expectException( UserNotLoggedIn
::class );
159 $specialPage->requireNamedUser();
162 public function testRequireNamedUserForNamedUser() {
163 $specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
165 $user = $this->getTestSysop()->getUser();
166 $specialPage->getContext()->setUser( $user );
168 $specialPage->requireNamedUser();
170 // no exception thrown, so the test passes.
171 $this->assertTrue( true );