Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / specialpage / SpecialPageTest.php
blobdcf5e3373ab4e0406145685530281c2e03a4c99e
1 <?php
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;
12 use UserNotLoggedIn;
14 /**
15 * @covers \MediaWiki\SpecialPage\SpecialPage
17 * @group Database
19 * @author Katie Filbert < aude.wiki@gmail.com >
21 class SpecialPageTest extends MediaWikiIntegrationTestCase {
23 use TempUserTestTrait;
25 protected function setUp(): void {
26 parent::setUp();
28 $this->overrideConfigValues( [
29 MainConfigNames::Script => '/index.php',
30 MainConfigNames::LanguageCode => 'en',
31 ] );
34 /**
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() {
44 return [
45 [ 'UserLogin', 'Userlogin' ]
49 public function testInvalidGetTitleFor() {
50 $this->expectPHPError(
51 E_USER_NOTICE,
52 function () {
53 $title = SpecialPage::getTitleFor( 'cat' );
54 $expected = Title::makeTitle( NS_SPECIAL, 'Cat' );
55 $this->assertEquals( $expected, $title );
60 /**
61 * @dataProvider getTitleForWithWarningProvider
63 public function testGetTitleForWithWarning( $expected, $name ) {
64 $this->expectPHPError(
65 E_USER_NOTICE,
66 function () use ( $name, $expected ) {
67 $title = SpecialPage::getTitleFor( $name );
68 $this->assertEquals( $expected, $title );
73 public static function getTitleForWithWarningProvider() {
74 return [
75 [ Title::makeTitle( NS_SPECIAL, 'UserLogin' ), 'UserLogin' ]
79 /**
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() {
97 $lang = 'en';
99 $expected1 = wfMessage( 'exception-nologin-text' )->inLanguage( $lang )->text();
100 $expected2 = wfMessage( 'about' )->inLanguage( $lang )->text();
102 return [
103 [ $expected1 ],
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() {
139 $lang = 'en';
141 $expected1 = wfMessage( 'exception-nologin-text' )->inLanguage( $lang )->text();
142 $expected2 = wfMessage( 'about' )->inLanguage( $lang )->text();
144 return [
145 [ $expected1 ],
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 );