Split title mocking methods out of HandlerTestTrait.
The functionality of creating title mocks is generally useful and this will also allow to make HandlerTestTrait more narrow. Bug: T264058 Change-Id: I76eca48dfcff65a6203fccde5366912a2d66c495
This commit is contained in:
parent
1a94be9c2a
commit
7854dd5fc9
9 changed files with 114 additions and 78 deletions
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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( [
|
||||
|
|
|
|||
|
|
@ -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 = [
|
||||
|
|
|
|||
|
|
@ -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( [
|
||||
|
|
|
|||
96
tests/phpunit/mocks/MockTitleTrait.php
Normal file
96
tests/phpunit/mocks/MockTitleTrait.php
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Interwiki\InterwikiLookup;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
|
||||
trait MockTitleTrait {
|
||||
|
||||
/** @var int */
|
||||
private $pageIdCounter = 0;
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 = [] );
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue