wiki.techinc.nl/tests/phpunit/includes/SpecialPageTest.php
Tyler Romeo d0439af89f Make UserNotLoggedIn redirect to login page
For pages like Special:Watchlist that throw
a UserNotLoggedIn exception when the user is
anonymous, this patch makes the page redirect
to the login page automatically.

This is instead
of the current behavior of showing a link to
the login page that the user must click.

(Also, Special:Userlogin has existing functionality
that will redirect the user back once they are
logged in.)

Bug: 15484
Change-Id: Idd9325374cb5dc13c4c057f45f88a33bdff523a9
2014-08-07 13:38:16 -04:00

105 lines
2.6 KiB
PHP

<?php
/**
* @covers SpecialPage
*
* @group Database
*
* @licence GNU GPL v2+
* @author Katie Filbert < aude.wiki@gmail.com >
*/
class SpecialPageTest extends MediaWikiTestCase {
protected function setUp() {
parent::setUp();
$this->setMwGlobals( array(
'wgScript' => '/index.php',
'wgContLang' => Language::factory( 'en' )
) );
}
/**
* @dataProvider getTitleForProvider
*/
public function testGetTitleFor( $expectedName, $name ) {
$title = SpecialPage::getTitleFor( $name );
$expected = Title::makeTitle( NS_SPECIAL, $expectedName );
$this->assertEquals( $expected, $title );
}
public function getTitleForProvider() {
return array(
array( 'UserLogin', 'Userlogin' )
);
}
/**
* @expectedException PHPUnit_Framework_Error_Notice
*/
public function testInvalidGetTitleFor() {
$title = SpecialPage::getTitleFor( 'cat' );
$expected = Title::makeTitle( NS_SPECIAL, 'Cat' );
$this->assertEquals( $expected, $title );
}
/**
* @expectedException PHPUnit_Framework_Error_Notice
* @dataProvider getTitleForWithWarningProvider
*/
public function testGetTitleForWithWarning( $expected, $name ) {
$title = SpecialPage::getTitleFor( $name );
$this->assertEquals( $expected, $title );
}
public function getTitleForWithWarningProvider() {
return array(
array( Title::makeTitle( NS_SPECIAL, 'UserLogin' ), 'UserLogin' )
);
}
/**
* @dataProvider requireLoginAnonProvider
*/
public function testRequireLoginAnon( $expected, $reason, $title ) {
$specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
$user = User::newFromId( 0 );
$specialPage->getContext()->setUser( $user );
$specialPage->getContext()->setLanguage( Language::factory( 'en' ) );
$this->setExpectedException( 'UserNotLoggedIn', $expected );
// $specialPage->requireLogin( [ $reason [, $title ] ] )
call_user_func_array(
array( $specialPage, 'requireLogin' ),
array_filter( array( $reason, $title ) )
);
}
public function requireLoginAnonProvider() {
$lang = 'en';
$expected1 = wfMessage( 'exception-nologin-text' )->inLanguage( $lang )->text();
$expected2 = wfMessage( 'about' )->inLanguage( $lang )->text();
return array(
array( $expected1, null, null ),
array( $expected2, 'about', null ),
array( $expected2, 'about', 'about' ),
);
}
public function testRequireLoginNotAnon() {
$specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
$user = User::newFromName( "UTSysop" );
$specialPage->getContext()->setUser( $user );
$specialPage->requireLogin();
// no exception thrown, logged in use can access special page
$this->assertTrue( true );
}
}