expectWarning() and friends have been deprecated in PHPUnit 9.6, and removed in PHPUnit 10. Unfortunately, there is no simple replacement because PHPUnit no longer converts them to exceptions in the first place. In fact, Sebastian Bergmann explicitly stated that he does not consider the use case of > a library developer to verify a code block warns its consumer when > certain action is performed worth supporting. So, add an ad-hoc replacement for all the deprecated methods. This is quite ugly, but it's simple enough given the low number of usages. On the bright side, this new method does not halt the test when the warning is triggered. This seems to align with the developers' expectation, seen in a few existing tests, that any code following the notice will be executed. Bug: T342110 Change-Id: I214abfed4280834840c115777ed78eb0a5570da9
117 lines
2.9 KiB
PHP
117 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Tests\SpecialPage;
|
|
|
|
use MediaWiki\MainConfigNames;
|
|
use MediaWiki\SpecialPage\SpecialPage;
|
|
use MediaWiki\Title\Title;
|
|
use MediaWiki\User\User;
|
|
use MediaWikiIntegrationTestCase;
|
|
use UserNotLoggedIn;
|
|
|
|
/**
|
|
* @covers \MediaWiki\SpecialPage\SpecialPage
|
|
*
|
|
* @group Database
|
|
*
|
|
* @author Katie Filbert < aude.wiki@gmail.com >
|
|
*/
|
|
class SpecialPageTest extends MediaWikiIntegrationTestCase {
|
|
|
|
protected function setUp(): void {
|
|
parent::setUp();
|
|
|
|
$this->overrideConfigValues( [
|
|
MainConfigNames::Script => '/index.php',
|
|
MainConfigNames::LanguageCode => 'en',
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider getTitleForProvider
|
|
*/
|
|
public function testGetTitleFor( $expectedName, $name ) {
|
|
$title = SpecialPage::getTitleFor( $name );
|
|
$expected = Title::makeTitle( NS_SPECIAL, $expectedName );
|
|
$this->assertEquals( $expected, $title );
|
|
}
|
|
|
|
public static function getTitleForProvider() {
|
|
return [
|
|
[ 'UserLogin', 'Userlogin' ]
|
|
];
|
|
}
|
|
|
|
public function testInvalidGetTitleFor() {
|
|
$this->expectPHPError(
|
|
E_USER_NOTICE,
|
|
function () {
|
|
$title = SpecialPage::getTitleFor( 'cat' );
|
|
$expected = Title::makeTitle( NS_SPECIAL, 'Cat' );
|
|
$this->assertEquals( $expected, $title );
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider getTitleForWithWarningProvider
|
|
*/
|
|
public function testGetTitleForWithWarning( $expected, $name ) {
|
|
$this->expectPHPError(
|
|
E_USER_NOTICE,
|
|
function () use ( $name, $expected ) {
|
|
$title = SpecialPage::getTitleFor( $name );
|
|
$this->assertEquals( $expected, $title );
|
|
}
|
|
);
|
|
}
|
|
|
|
public static function getTitleForWithWarningProvider() {
|
|
return [
|
|
[ 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(
|
|
$this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ) );
|
|
|
|
$this->expectException( UserNotLoggedIn::class );
|
|
$this->expectExceptionMessage( $expected );
|
|
|
|
// $specialPage->requireLogin( [ $reason [, $title ] ] )
|
|
$specialPage->requireLogin( ...array_filter( [ $reason, $title ] ) );
|
|
}
|
|
|
|
public static function requireLoginAnonProvider() {
|
|
$lang = 'en';
|
|
|
|
$expected1 = wfMessage( 'exception-nologin-text' )->inLanguage( $lang )->text();
|
|
$expected2 = wfMessage( 'about' )->inLanguage( $lang )->text();
|
|
|
|
return [
|
|
[ $expected1, null, null ],
|
|
[ $expected2, 'about', null ],
|
|
[ $expected2, 'about', 'about' ],
|
|
];
|
|
}
|
|
|
|
public function testRequireLoginNotAnon() {
|
|
$specialPage = new SpecialPage( 'Watchlist', 'viewmywatchlist' );
|
|
|
|
$user = $this->getTestSysop()->getUser();
|
|
$specialPage->getContext()->setUser( $user );
|
|
|
|
$specialPage->requireLogin();
|
|
|
|
// no exception thrown, logged in use can access special page
|
|
$this->assertTrue( true );
|
|
}
|
|
}
|