From 4831549b14248805dc4fce56fc4aa6e6da612bbe Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Thu, 10 Mar 2022 22:39:37 +0100 Subject: [PATCH] Replace Title::newFromText by Title::makeTitle * Prefer Title::makeTitle in unit tests where the title is known, this avoids title parsing and can improve the test performance. * Use Title::makeTitle for the user page like done in User::getUserPage Change-Id: I3901a6ed208a5ff90b216d01d3c4962cdcb48db1 --- includes/Menu/Entries/ProfileMenuEntry.php | 2 +- .../permissions/MinervaPagePermissionsTest.php | 16 ++++++++-------- tests/phpunit/skins/SkinMinervaTest.php | 4 ++-- tests/phpunit/skins/SkinUserPageHelperTest.php | 10 +++++----- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/includes/Menu/Entries/ProfileMenuEntry.php b/includes/Menu/Entries/ProfileMenuEntry.php index c48d299ba..ad7af4dbb 100644 --- a/includes/Menu/Entries/ProfileMenuEntry.php +++ b/includes/Menu/Entries/ProfileMenuEntry.php @@ -86,7 +86,7 @@ final class ProfileMenuEntry implements IProfileMenuEntry { return [ [ 'icon' => 'wikimedia-userAvatar-base20', 'text' => $this->customProfileLabel ?? $username, - 'href' => $this->customProfileURL ?? Title::newFromText( $username, NS_USER )->getLocalURL(), + 'href' => $this->customProfileURL ?? Title::makeTitle( NS_USER, $username )->getLocalURL(), 'class' => 'menu__item--user', 'data-event-name' => 'menu.' . ( $this->profileTrackingCode ?? self::DEFAULT_PROFILE_TRACKING_CODE ) diff --git a/tests/phpunit/permissions/MinervaPagePermissionsTest.php b/tests/phpunit/permissions/MinervaPagePermissionsTest.php index 6f20b9253..beb7b2e04 100644 --- a/tests/phpunit/permissions/MinervaPagePermissionsTest.php +++ b/tests/phpunit/permissions/MinervaPagePermissionsTest.php @@ -108,7 +108,7 @@ class MinervaPagePermissionsTest extends MediaWikiIntegrationTestCase { * @covers ::isAllowed */ public function testInvalidPageActionsArentAllowed() { - $perms = $this->buildPermissionsObject( Title::newFromText( 'test' ), [] ); + $perms = $this->buildPermissionsObject( Title::makeTitle( NS_MAIN, 'Test' ), [] ); $this->assertFalse( $perms->isAllowed( 'blah' ) ); $this->assertFalse( $perms->isAllowed( 'wah' ) ); @@ -118,7 +118,7 @@ class MinervaPagePermissionsTest extends MediaWikiIntegrationTestCase { * @covers ::isAllowed */ public function testValidPageActionsAreAllowed() { - $perms = $this->buildPermissionsObject( Title::newFromText( 'test' ) ); + $perms = $this->buildPermissionsObject( Title::makeTitle( NS_MAIN, 'Test' ) ); $this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::TALK ) ); $this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::WATCH ) ); } @@ -152,7 +152,7 @@ class MinervaPagePermissionsTest extends MediaWikiIntegrationTestCase { $contentHandler->method( 'supportsDirectApiEditing' ) ->willReturn( $supportsDirectApiEditing ); - $perms = $this->buildPermissionsObject( Title::newFromText( 'test' ), null, [], + $perms = $this->buildPermissionsObject( Title::makeTitle( NS_MAIN, 'Test' ), null, [], $contentHandler ); $this->assertEquals( $expected, $perms->isAllowed( IMinervaPagePermissions::CONTENT_EDIT ) ); @@ -162,7 +162,7 @@ class MinervaPagePermissionsTest extends MediaWikiIntegrationTestCase { * @covers ::isAllowed */ public function testPageActionsWhenOnUserPage() { - $perms = $this->buildPermissionsObject( Title::newFromText( 'User:Admin' ) ); + $perms = $this->buildPermissionsObject( Title::makeTitle( NS_USER, 'Admin' ) ); $this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::TALK ) ); } @@ -170,7 +170,7 @@ class MinervaPagePermissionsTest extends MediaWikiIntegrationTestCase { * @covers ::isAllowed */ public function testPageActionsWhenOnAnonUserPage() { - $perms = $this->buildPermissionsObject( Title::newFromText( 'User:1.1.1.1' ) ); + $perms = $this->buildPermissionsObject( Title::makeTitle( NS_USER, '1.1.1.1' ) ); $this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::TALK ) ); } @@ -200,7 +200,7 @@ class MinervaPagePermissionsTest extends MediaWikiIntegrationTestCase { */ public function testGlobalHideLanguageLinksTakesPrecedence() { $this->setMwGlobals( [ 'wgHideInterlanguageLinks' => true ] ); - $perms = $this->buildPermissionsObject( Title::newFromText( 'test' ) ); + $perms = $this->buildPermissionsObject( Title::makeTitle( NS_MAIN, 'Test' ) ); $this->assertFalse( $perms->isAllowed( IMinervaPagePermissions::SWITCH_LANGUAGE ) ); } @@ -246,7 +246,7 @@ class MinervaPagePermissionsTest extends MediaWikiIntegrationTestCase { * @covers ::isAllowed */ public function testWatchIsAllowedOnlyWhenWatchlistPermissionsAreGranted() { - $title = Title::newFromText( 'test_watchstar_permissions' ); + $title = Title::makeTitle( NS_MAIN, 'Test_watchstar_permissions' ); $userMock = $this->getMockBuilder( User::class ) ->disableOriginalConstructor() @@ -289,7 +289,7 @@ class MinervaPagePermissionsTest extends MediaWikiIntegrationTestCase { * @covers ::isAllowed */ public function testMoveAndDeleteAndProtectNotAllowedByDefault() { - $perms = $this->buildPermissionsObject( Title::newFromText( 'test' ), null ); + $perms = $this->buildPermissionsObject( Title::makeTitle( NS_MAIN, 'Test' ), null ); $this->assertFalse( $perms->isAllowed( IMinervaPagePermissions::MOVE ) ); $this->assertFalse( $perms->isAllowed( IMinervaPagePermissions::DELETE ) ); $this->assertFalse( $perms->isAllowed( IMinervaPagePermissions::PROTECT ) ); diff --git a/tests/phpunit/skins/SkinMinervaTest.php b/tests/phpunit/skins/SkinMinervaTest.php index e8a35423d..c8924d258 100644 --- a/tests/phpunit/skins/SkinMinervaTest.php +++ b/tests/phpunit/skins/SkinMinervaTest.php @@ -37,7 +37,7 @@ class SkinMinervaTest extends MediaWikiIntegrationTestCase { $this->overrideSkinOptions( [ SkinOptions::CATEGORIES => false ] ); $context = new RequestContext(); - $context->setTitle( Title::newFromText( 'Test' ) ); + $context->setTitle( Title::makeTitle( NS_MAIN, 'Test' ) ); $context->setOutput( $outputPage ); $skin = new SkinMinerva(); @@ -65,7 +65,7 @@ class SkinMinervaTest extends MediaWikiIntegrationTestCase { $this->overrideSkinOptions( [ SkinOptions::CATEGORIES => true ] ); $context = new RequestContext(); - $context->setTitle( Title::newFromText( 'Test' ) ); + $context->setTitle( Title::makeTitle( NS_MAIN, 'Test' ) ); $context->setOutput( $outputPage ); $skin = new SkinMinerva(); diff --git a/tests/phpunit/skins/SkinUserPageHelperTest.php b/tests/phpunit/skins/SkinUserPageHelperTest.php index 8497791fc..01e9bf54a 100644 --- a/tests/phpunit/skins/SkinUserPageHelperTest.php +++ b/tests/phpunit/skins/SkinUserPageHelperTest.php @@ -18,7 +18,7 @@ class SkinUserPageHelperTest extends MediaWikiIntegrationTestCase { * @covers ::__construct */ public function testTitleNotInUserNamespace() { - $title = Title::newFromText( 'Test Page' ); + $title = Title::makeTitle( NS_MAIN, 'Test_Page' ); $helper = new SkinUserPageHelper( $this->getServiceContainer()->getUserNameUtils(), $title ); $this->assertFalse( $helper->isUserPage() ); @@ -42,7 +42,7 @@ class SkinUserPageHelperTest extends MediaWikiIntegrationTestCase { * @covers ::fetchData */ public function testTitleisASubpage() { - $title = Title::newFromText( 'User:TestUser/subpage' ); + $title = Title::makeTitle( NS_USER, 'TestUser/subpage' ); $helper = new SkinUserPageHelper( $this->getServiceContainer()->getUserNameUtils(), $title ); $this->assertFalse( $helper->isUserPage() ); @@ -54,7 +54,7 @@ class SkinUserPageHelperTest extends MediaWikiIntegrationTestCase { * @covers ::buildPageUserObject */ public function testTitleisAnIP() { - $title = Title::newFromText( 'User:127.0.0.1' ); + $title = Title::makeTitle( NS_USER, '127.0.0.1' ); $helper = new SkinUserPageHelper( $this->getServiceContainer()->getUserNameUtils(), $title ); $this->assertTrue( $helper->isUserPage() ); @@ -66,7 +66,7 @@ class SkinUserPageHelperTest extends MediaWikiIntegrationTestCase { * @covers ::buildPageUserObject */ public function testTitleIsIPRange() { - $title = Title::newFromText( 'User:127.0.0.1/24' ); + $title = Title::makeTitle( NS_USER, '127.0.0.1/24' ); $helper = new SkinUserPageHelper( $this->getServiceContainer()->getUserNameUtils(), $title ); $this->assertFalse( $helper->isUserPage() ); @@ -78,7 +78,7 @@ class SkinUserPageHelperTest extends MediaWikiIntegrationTestCase { * @covers ::buildPageUserObject */ public function testTitleIsFakeUserPage() { - $title = Title::newFromText( 'User:Fake user' ); + $title = Title::makeTitle( NS_USER, 'Fake_user' ); $helper = new SkinUserPageHelper( $this->getServiceContainer()->getUserNameUtils(), $title ); $this->assertFalse( $helper->isUserPage() );