diff --git a/tests/common/TestsAutoLoader.php b/tests/common/TestsAutoLoader.php index 1f2ba3e4f8c..c82e2c9bdd2 100644 --- a/tests/common/TestsAutoLoader.php +++ b/tests/common/TestsAutoLoader.php @@ -213,6 +213,7 @@ $wgAutoloadClasses += [ 'MockDjVuHandler' => "$testDir/phpunit/mocks/media/MockDjVuHandler.php", 'MockChangesListFilter' => "$testDir/phpunit/mocks/MockChangesListFilter.php", 'MockChangesListFilterGroup' => "$testDir/phpunit/mocks/MockChangesListFilterGroup.php", + 'MockTitleTrait' => "$testDir/phpunit/mocks/MockTitleTrait.php", 'MockWebRequest' => "$testDir/phpunit/mocks/MockWebRequest.php", 'NullHttpRequestFactory' => "$testDir/phpunit/mocks/NullHttpRequestFactory.php", 'NullMultiHttpClient' => "$testDir/phpunit/mocks/NullMultiHttpClient.php", diff --git a/tests/phpunit/integration/includes/Rest/Handler/CreationHandlerTest.php b/tests/phpunit/integration/includes/Rest/Handler/CreationHandlerTest.php index b8e74d32937..11cc04107b7 100644 --- a/tests/phpunit/integration/includes/Rest/Handler/CreationHandlerTest.php +++ b/tests/phpunit/integration/includes/Rest/Handler/CreationHandlerTest.php @@ -14,6 +14,7 @@ use MediaWiki\Storage\MutableRevisionRecord; use MediaWiki\Storage\SlotRecord; use MediaWikiIntegrationTestCase; use MediaWikiTitleCodec; +use MockTitleTrait; use PHPUnit\Framework\MockObject\MockObject; use Status; use Wikimedia\Message\MessageValue; @@ -27,6 +28,7 @@ use WikitextContent; class CreationHandlerTest extends MediaWikiIntegrationTestCase { use ActionModuleBasedHandlerTestTrait; + use MockTitleTrait; private function newHandler( $resultData, $throwException = null, $csrfSafe = false ) { $config = new HashConfig( [ diff --git a/tests/phpunit/integration/includes/Rest/Handler/LanguageLinksHandlerTest.php b/tests/phpunit/integration/includes/Rest/Handler/LanguageLinksHandlerTest.php index 47b665a67fc..623105fa45c 100644 --- a/tests/phpunit/integration/includes/Rest/Handler/LanguageLinksHandlerTest.php +++ b/tests/phpunit/integration/includes/Rest/Handler/LanguageLinksHandlerTest.php @@ -9,6 +9,7 @@ use MediaWiki\MediaWikiServices; use MediaWiki\Rest\Handler\LanguageLinksHandler; use MediaWiki\Rest\LocalizedHttpException; use MediaWiki\Rest\RequestData; +use MockTitleTrait; use Title; use Wikimedia\Message\MessageValue; @@ -20,6 +21,7 @@ use Wikimedia\Message\MessageValue; class LanguageLinksHandlerTest extends \MediaWikiIntegrationTestCase { use HandlerTestTrait; + use MockTitleTrait; public function addDBData() { $defaults = [ diff --git a/tests/phpunit/integration/includes/Rest/Handler/UpdateHandlerTest.php b/tests/phpunit/integration/includes/Rest/Handler/UpdateHandlerTest.php index 05c59aad100..07a25139dc1 100644 --- a/tests/phpunit/integration/includes/Rest/Handler/UpdateHandlerTest.php +++ b/tests/phpunit/integration/includes/Rest/Handler/UpdateHandlerTest.php @@ -14,6 +14,7 @@ use MediaWiki\Revision\RevisionLookup; use MediaWiki\Storage\MutableRevisionRecord; use MediaWiki\Storage\SlotRecord; use MediaWikiTitleCodec; +use MockTitleTrait; use PHPUnit\Framework\MockObject\MockObject; use Status; use Title; @@ -29,6 +30,7 @@ use WikitextContentHandler; class UpdateHandlerTest extends \MediaWikiLangTestCase { use ActionModuleBasedHandlerTestTrait; + use MockTitleTrait; private function newHandler( $resultData, $throwException = null, $csrfSafe = false ) { $config = new HashConfig( [ diff --git a/tests/phpunit/mocks/MockTitleTrait.php b/tests/phpunit/mocks/MockTitleTrait.php new file mode 100644 index 00000000000..d6aa6d6669f --- /dev/null +++ b/tests/phpunit/mocks/MockTitleTrait.php @@ -0,0 +1,96 @@ +pageIdCounter; + $ns = $props['namespace'] ?? 0; + $nsName = $ns ? "ns$ns:" : ''; + + $preText = $text; + $text = preg_replace( '/^[\w ]*?:/', '', $text ); + + // If no namespace prefix was given, add one if needed. + if ( $preText == $text && $ns ) { + $preText = $nsName . $text; + } + + /** @var Title|MockObject $title */ + $title = $this->createMock( Title::class ); + + $title->method( 'getText' )->willReturn( str_replace( '_', ' ', $text ) ); + $title->method( 'getDBkey' )->willReturn( str_replace( ' ', '_', $text ) ); + + $title->method( 'getPrefixedText' )->willReturn( str_replace( '_', ' ', $preText ) ); + $title->method( 'getPrefixedDBkey' )->willReturn( str_replace( ' ', '_', $preText ) ); + + $title->method( 'getArticleID' )->willReturn( $id ); + $title->method( 'getNamespace' )->willReturn( $props['namespace'] ?? 0 ); + $title->method( 'exists' )->willReturn( $id > 0 ); + $title->method( 'getTouched' )->willReturn( $id ? '20200101223344' : false ); + + return $title; + } + + /** + * @return MediaWikiTitleCodec + */ + private function makeMockTitleCodec() { + /** @var Language|MockObject $language */ + $language = $this->createNoOpMock( Language::class, [ 'ucfirst' ] ); + $language->method( 'ucfirst' )->willReturnCallback( 'ucfirst' ); + + /** @var GenderCache|MockObject $genderCache */ + $genderCache = $this->createNoOpMock( GenderCache::class ); + + /** @var InterwikiLookup|MockObject $interwikiLookup */ + $interwikiLookup = $this->createNoOpMock( InterwikiLookup::class ); + + /** @var NamespaceInfo|MockObject $namespaceInfo */ + $namespaceInfo = $this->createNoOpMock( NamespaceInfo::class, [ 'isCapitalized' ] ); + $namespaceInfo->method( 'isCapitalized' )->willReturn( true ); + + $titleCodec = new MediaWikiTitleCodec( + $language, + $genderCache, + [ 'en' ], + $interwikiLookup, + $namespaceInfo + ); + + return $titleCodec; + } + + /** + * Expected to be provided by the class, probably inherited from TestCase. + * + * @param string $originalClassName + * + * @return MockObject + */ + abstract protected function createMock( $originalClassName ): MockObject; + + /** + * Expected to be provided by the class, probably MediaWikiTestCaseTrait. + * + * @param string $type + * @param string[] $allow methods to allow + * + * @return MockObject + */ + abstract protected function createNoOpMock( $type, $allow = [] ); +} diff --git a/tests/phpunit/unit/includes/Rest/Handler/HandlerTestTrait.php b/tests/phpunit/unit/includes/Rest/Handler/HandlerTestTrait.php index 79d47ce8e5f..88c7171637d 100644 --- a/tests/phpunit/unit/includes/Rest/Handler/HandlerTestTrait.php +++ b/tests/phpunit/unit/includes/Rest/Handler/HandlerTestTrait.php @@ -2,9 +2,6 @@ namespace MediaWiki\Tests\Rest\Handler; -use GenderCache; -use Language; -use MediaWiki\Interwiki\InterwikiLookup; use MediaWiki\Linker\LinkTarget; use MediaWiki\Permissions\PermissionManager; use MediaWiki\Rest\Handler; @@ -17,11 +14,8 @@ use MediaWiki\Rest\Router; use MediaWiki\Rest\Validator\Validator; use MediaWiki\User\UserIdentityValue; use MediaWikiTestCaseTrait; -use MediaWikiTitleCodec; -use NamespaceInfo; use PHPUnit\Framework\Assert; use PHPUnit\Framework\MockObject\MockObject; -use Title; use User; use Wikimedia\Message\ITextFormatter; use Wikimedia\Message\MessageValue; @@ -33,15 +27,12 @@ use Wikimedia\Services\ServiceContainer; * This trait is intended to be used on subclasses of MediaWikiUnitTestCase * or MediaWikiIntegrationTestCase. * + * @stable to use * @package MediaWiki\Tests\Rest\Handler */ trait HandlerTestTrait { - use MediaWikiTestCaseTrait; - /** @var int */ - private $pageIdCounter = 0; - /** * Expected to be provided by the class, probably inherited from TestCase. * @@ -54,6 +45,7 @@ trait HandlerTestTrait { /** * Calls init() on the Handler, supplying a mock Router and ResponseFactory. * + * @internal to the trait * @param Handler $handler * @param RequestInterface $request * @param array $config @@ -86,6 +78,7 @@ trait HandlerTestTrait { /** * Calls validate() on the Handler, with an appropriate Validator supplied. * + * @internal to the trait * @param Handler $handler * @param null|Validator $validator * @throws HttpException @@ -112,6 +105,7 @@ trait HandlerTestTrait { /** * Creates a mock Validator to bypass actual request query, path, and/or body param validation * + * @internal to the trait * @param array $queryPathParams * @param array $bodyParams * @return Validator|MockObject @@ -228,44 +222,7 @@ trait HandlerTestTrait { } /** - * @param string $text - * @param array $props Additional properties to set. Supported keys: - * - id: int - * - namespace: int - * - * @return Title|MockObject - */ - private function makeMockTitle( $text, array $props = [] ) { - $id = $props['id'] ?? ++$this->pageIdCounter; - $ns = $props['namespace'] ?? 0; - $nsName = $ns ? "ns$ns:" : ''; - - $preText = $text; - $text = preg_replace( '/^[\w ]*?:/', '', $text ); - - // If no namespace prefix was given, add one if needed. - if ( $preText == $text && $ns ) { - $preText = $nsName . $text; - } - - /** @var Title|MockObject $title */ - $title = $this->createMock( Title::class ); - - $title->method( 'getText' )->willReturn( str_replace( '_', ' ', $text ) ); - $title->method( 'getDBkey' )->willReturn( str_replace( ' ', '_', $text ) ); - - $title->method( 'getPrefixedText' )->willReturn( str_replace( '_', ' ', $preText ) ); - $title->method( 'getPrefixedDBkey' )->willReturn( str_replace( ' ', '_', $preText ) ); - - $title->method( 'getArticleID' )->willReturn( $id ); - $title->method( 'getNamespace' )->willReturn( $props['namespace'] ?? 0 ); - $title->method( 'exists' )->willReturn( $id > 0 ); - $title->method( 'getTouched' )->willReturn( $id ? '20200101223344' : false ); - - return $title; - } - - /** + * @internal * @return PermissionManager|MockObject */ private function makeMockPermissionManager() { @@ -280,34 +237,4 @@ trait HandlerTestTrait { return $permissionManager; } - - /** - * @return MediaWikiTitleCodec - */ - private function makeMockTitleCodec() { - /** @var Language|MockObject $language */ - $language = $this->createNoOpMock( Language::class, [ 'ucfirst' ] ); - $language->method( 'ucfirst' )->willReturnCallback( 'ucfirst' ); - - /** @var GenderCache|MockObject $genderCache */ - $genderCache = $this->createNoOpMock( GenderCache::class ); - - /** @var InterwikiLookup|MockObject $interwikiLookup */ - $interwikiLookup = $this->createNoOpMock( InterwikiLookup::class ); - - /** @var NamespaceInfo|MockObject $namespaceInfo */ - $namespaceInfo = $this->createNoOpMock( NamespaceInfo::class, [ 'isCapitalized' ] ); - $namespaceInfo->method( 'isCapitalized' )->willReturn( true ); - - $titleCodec = new MediaWikiTitleCodec( - $language, - $genderCache, - [ 'en' ], - $interwikiLookup, - $namespaceInfo - ); - - return $titleCodec; - } - } diff --git a/tests/phpunit/unit/includes/Rest/Handler/MediaTestTrait.php b/tests/phpunit/unit/includes/Rest/Handler/MediaTestTrait.php index bf7fe196f4c..e30769e4f0f 100644 --- a/tests/phpunit/unit/includes/Rest/Handler/MediaTestTrait.php +++ b/tests/phpunit/unit/includes/Rest/Handler/MediaTestTrait.php @@ -3,6 +3,7 @@ namespace MediaWiki\Tests\Rest\Handler; use File; +use MockTitleTrait; use PHPUnit\Framework\MockObject\MockObject; use RepoGroup; use ThumbnailImage; @@ -16,6 +17,7 @@ use Title; trait MediaTestTrait { use HandlerTestTrait; + use MockTitleTrait; /** * @param Title|string $title diff --git a/tests/phpunit/unit/includes/Rest/Handler/SearchHandlerTest.php b/tests/phpunit/unit/includes/Rest/Handler/SearchHandlerTest.php index 3eafc8faff9..7701940ad79 100644 --- a/tests/phpunit/unit/includes/Rest/Handler/SearchHandlerTest.php +++ b/tests/phpunit/unit/includes/Rest/Handler/SearchHandlerTest.php @@ -12,6 +12,7 @@ use MediaWiki\Rest\LocalizedHttpException; use MediaWiki\Rest\RequestData; use MediaWiki\Search\Entity\SearchResultThumbnail; use MockSearchResultSet; +use MockTitleTrait; use PHPUnit\Framework\MockObject\MockObject; use SearchEngine; use SearchEngineFactory; @@ -29,6 +30,7 @@ use Wikimedia\Message\MessageValue; class SearchHandlerTest extends \MediaWikiUnitTestCase { use HandlerTestTrait; + use MockTitleTrait; /** * @var SearchEngine|MockObject|null diff --git a/tests/phpunit/unit/includes/Rest/Handler/UserContributionsHandlerTest.php b/tests/phpunit/unit/includes/Rest/Handler/UserContributionsHandlerTest.php index 82cc0d9ef4d..d4b4a2b5df1 100644 --- a/tests/phpunit/unit/includes/Rest/Handler/UserContributionsHandlerTest.php +++ b/tests/phpunit/unit/includes/Rest/Handler/UserContributionsHandlerTest.php @@ -13,6 +13,7 @@ use MediaWiki\Storage\MutableRevisionRecord; use MediaWiki\User\UserIdentityValue; use MediaWiki\User\UserNameUtils; use Message; +use MockTitleTrait; use PHPUnit\Framework\MockObject\MockObject; use RequestContext; use Wikimedia\Message\MessageValue; @@ -23,6 +24,7 @@ use Wikimedia\Message\MessageValue; class UserContributionsHandlerTest extends \MediaWikiUnitTestCase { use ContributionsTestTrait; use HandlerTestTrait; + use MockTitleTrait; private const DEFAULT_LIMIT = 20;