From 49bbfc7f1f184c2a22b0919a16b6c9dcac2e7536 Mon Sep 17 00:00:00 2001 From: Derick Alangi Date: Wed, 15 May 2019 15:24:46 +0100 Subject: [PATCH] GlobalFunctions: Tighten version number type for wfDeprecated() To avoid cases like: facddc4 and Ifaf6ab0d36bc02bd170, make sure the value of the mediawiki version must be a string (e.g. '1.33') or a boolean (e.g. `false`). For some reason, typos can slip through for this value to be a float. Let's safe guard for future cases like this. Change-Id: I52bdf94c957bda67548a937d51649e925195f926 --- includes/GlobalFunctions.php | 11 ++++++- .../includes/debug/DeprecationHelperTest.php | 31 +++++++++++-------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 05c4655b87..5f17ad8627 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -1037,9 +1037,18 @@ function wfLogDBError( $text, array $context = [] ) { * @param int $callerOffset How far up the call stack is the original * caller. 2 = function that called the function that called * wfDeprecated (Added in 1.20). + * + * @throws Exception If the MediaWiki version number is not a string or boolean. */ function wfDeprecated( $function, $version = false, $component = false, $callerOffset = 2 ) { - MWDebug::deprecated( $function, $version, $component, $callerOffset + 1 ); + if ( is_string( $version ) || is_bool( $version ) ) { + MWDebug::deprecated( $function, $version, $component, $callerOffset + 1 ); + } else { + throw new Exception( + "MediaWiki version must either be a string or a boolean. " . + "Example valid version: '1.33'" + ); + } } /** diff --git a/tests/phpunit/includes/debug/DeprecationHelperTest.php b/tests/phpunit/includes/debug/DeprecationHelperTest.php index b14d89c3f4..25dedbc2b0 100644 --- a/tests/phpunit/includes/debug/DeprecationHelperTest.php +++ b/tests/phpunit/includes/debug/DeprecationHelperTest.php @@ -37,10 +37,8 @@ class DeprecationHelperTest extends MediaWikiTestCase { public function provideGet() { return [ - [ 'protectedDeprecated', null, null ], [ 'protectedNonDeprecated', E_USER_ERROR, 'Cannot access non-public property TestDeprecatedClass::$protectedNonDeprecated' ], - [ 'privateDeprecated', null, null ], [ 'privateNonDeprecated', E_USER_ERROR, 'Cannot access non-public property TestDeprecatedClass::$privateNonDeprecated' ], [ 'nonExistent', E_USER_NOTICE, 'Undefined property: TestDeprecatedClass::$nonExistent' ], @@ -71,10 +69,8 @@ class DeprecationHelperTest extends MediaWikiTestCase { public function provideSet() { return [ - [ 'protectedDeprecated', null, null ], [ 'protectedNonDeprecated', E_USER_ERROR, 'Cannot access non-public property TestDeprecatedClass::$protectedNonDeprecated' ], - [ 'privateDeprecated', null, null ], [ 'privateNonDeprecated', E_USER_ERROR, 'Cannot access non-public property TestDeprecatedClass::$privateNonDeprecated' ], [ 'nonExistent', null, null ], @@ -100,15 +96,6 @@ class DeprecationHelperTest extends MediaWikiTestCase { } public function testSubclassGetSet() { - $this->assertDeprecationWarningIssued( function () { - $this->assertSame( 1, $this->testSubclass->getDeprecatedPrivateParentProperty() ); - } ); - $this->assertDeprecationWarningIssued( function () { - $this->testSubclass->setDeprecatedPrivateParentProperty( 0 ); - } ); - $wrapper = TestingAccessWrapper::newFromObject( $this->testSubclass ); - $this->assertSame( 0, $wrapper->privateDeprecated ); - $fullName = 'TestDeprecatedClass::$privateNonDeprecated'; $this->assertErrorTriggered( function () { $this->assertSame( null, $this->testSubclass->getNonDeprecatedPrivateParentProperty() ); @@ -165,4 +152,22 @@ class DeprecationHelperTest extends MediaWikiTestCase { $this->assertNotEmpty( $wrapper->deprecationWarnings ); } + /** + * Test bad MW version values to throw exceptions as expected + * + * @dataProvider provideBadMWVersion + */ + public function testBadMWVersion( $version, $expected ) { + $this->setExpectedException( $expected ); + + wfDeprecated( __METHOD__, $version ); + } + + public function provideBadMWVersion() { + return [ + [ 1, Exception::class ], + [ 1.33, Exception::class ], + [ null, Exception::class ] + ]; + } } -- 2.20.1