1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
<?php
declare( strict_types=1 );
namespace MediaWiki\Tests;
use MediaWiki\Api\ApiMain;
use MediaWiki\Api\ApiQuery;
use MediaWiki\Auth\PreAuthenticationProvider;
use MediaWiki\Auth\PrimaryAuthenticationProvider;
use MediaWiki\Auth\SecondaryAuthenticationProvider;
use MediaWiki\Content\ContentHandler;
use MediaWiki\Http\HttpRequestFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Tests\Api\ApiTestContext;
use MediaWikiIntegrationTestCase;
use Wikimedia\Http\MultiHttpClient;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\LBFactory;
/**
* Base class for testing extension.json.
*
* While {@link \ExtensionJsonValidationTest} tests basic validity of all
* extension.json and skin.json files that are available,
* individual extensions can use this class to opt into further testing
* by adding a test class extending this base class.
*
* This includes tests for object factory specifications
* (API modules, special pages, hook handlers, etc.) to ensure that:
* * The specifications are valid,
* i.e. they can be created without any errors.
* This protects, for instance, against misconfigured services.
* (This works best if the constructor factory function being called
* declares types for the parameters it receives.)
* * No HTTP or database connections are made during initialization.
* Opening connections already when an object is created,
* not only when it is used, is a potential performance issue.
* * Optionally: Each specification's list of services is sorted.
* Prescribing an automatically testable order frees the developer
* from having to think about the most logical order for any services.
*
* @license GPL-2.0-or-later
*/
abstract class ExtensionJsonTestBase extends MediaWikiIntegrationTestCase {
/**
* @var string The path to the extension.json file.
* Should be specified as `__DIR__ . '/.../extension.json'`.
*/
protected string $extensionJsonPath;
/**
* @var string|null The prefix of the extension's own services in the service container.
* If non-null, all services lists must be sorted,
* and first list all services outside the extension (without the prefix),
* then all services within the extension (with the prefix), in alphabetical order.
* If null (default), the order of services lists is not tested.
* To require all services to be listed in alphabetical order,
* regardless of whether they belong to the extension or not,
* set this to the empty string.
* @see ExtensionServicesTestBase::$serviceNamePrefix
*/
protected ?string $serviceNamePrefix = null;
/**
* @var array[] Cache for extension.json, shared between all tests.
* Maps {@link $extensionJsonPath} values to parsed extension.json contents.
*/
private static array $extensionJsonCache = [];
protected function setUp(): void {
parent::setUp();
// Factory methods should never access the database or do http requests
// https://phabricator.wikimedia.org/T243729
$this->disallowDBAccess();
$this->disallowHttpAccess();
}
final protected function getExtensionJson(): array {
if ( !array_key_exists( $this->extensionJsonPath, self::$extensionJsonCache ) ) {
self::$extensionJsonCache[$this->extensionJsonPath] = json_decode(
file_get_contents( $this->extensionJsonPath ),
true,
512,
JSON_THROW_ON_ERROR
);
}
return self::$extensionJsonCache[$this->extensionJsonPath];
}
/** @dataProvider provideHookHandlerNames */
public function testHookHandler( string $hookHandlerName ): void {
$specification = $this->getExtensionJson()['HookHandlers'][$hookHandlerName];
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory();
$objectFactory->createObject( $specification, [
'allowClassName' => true,
] );
$this->assertTrue( true );
}
public function provideHookHandlerNames(): iterable {
foreach ( $this->getExtensionJson()['HookHandlers'] ?? [] as $hookHandlerName => $specification ) {
yield [ $hookHandlerName ];
}
}
/** @dataProvider provideContentModelIDs */
public function testContentHandler( string $contentModelID ): void {
$specification = $this->getExtensionJson()['ContentHandlers'][$contentModelID];
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory();
$objectFactory->createObject( $specification, [
'assertClass' => ContentHandler::class,
'allowCallable' => true,
'allowClassName' => true,
'extraArgs' => [ $contentModelID ],
] );
$this->assertTrue( true );
}
public function provideContentModelIDs(): iterable {
foreach ( $this->getExtensionJson()['ContentHandlers'] ?? [] as $contentModelID => $specification ) {
yield [ $contentModelID ];
}
}
/** @dataProvider provideApiModuleNames */
public function testApiModule( string $moduleName ): void {
$specification = $this->getExtensionJson()['APIModules'][$moduleName];
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory();
$objectFactory->createObject( $specification, [
'allowClassName' => true,
'extraArgs' => [ $this->mockApiMain(), 'modulename' ],
] );
$this->assertTrue( true );
}
public function provideApiModuleNames(): iterable {
foreach ( $this->getExtensionJson()['APIModules'] ?? [] as $moduleName => $specification ) {
yield [ $moduleName ];
}
}
/** @dataProvider provideApiQueryModuleListsAndNames */
public function testApiQueryModule( string $moduleList, string $moduleName ): void {
$specification = $this->getExtensionJson()[$moduleList][$moduleName];
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory();
$objectFactory->createObject( $specification, [
'allowClassName' => true,
'extraArgs' => [ $this->mockApiQuery(), 'query' ],
] );
$this->assertTrue( true );
}
public function provideApiQueryModuleListsAndNames(): iterable {
foreach ( [ 'APIListModules', 'APIMetaModules', 'APIPropModules' ] as $moduleList ) {
foreach ( $this->getExtensionJson()[$moduleList] ?? [] as $moduleName => $specification ) {
yield [ $moduleList, $moduleName ];
}
}
}
/** @dataProvider provideSpecialPageNames */
public function testSpecialPage( string $specialPageName ): void {
$specification = $this->getExtensionJson()['SpecialPages'][$specialPageName];
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory();
$objectFactory->createObject( $specification, [
'allowClassName' => true,
] );
$this->assertTrue( true );
}
public function provideSpecialPageNames(): iterable {
foreach ( $this->getExtensionJson()['SpecialPages'] ?? [] as $specialPageName => $specification ) {
yield [ $specialPageName ];
}
}
/** @dataProvider provideAuthenticationProviders */
public function testAuthenticationProviders( string $providerType, string $providerName, string $providerClass ): void {
$specification = $this->getExtensionJson()['AuthManagerAutoConfig'][$providerType][$providerName];
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory();
$objectFactory->createObject( $specification, [
'assertClass' => $providerClass,
] );
$this->assertTrue( true );
}
public function provideAuthenticationProviders(): iterable {
$config = $this->getExtensionJson()['AuthManagerAutoConfig'] ?? [];
$types = [
'preauth' => PreAuthenticationProvider::class,
'primaryauth' => PrimaryAuthenticationProvider::class,
'secondaryauth' => SecondaryAuthenticationProvider::class,
];
foreach ( $types as $providerType => $providerClass ) {
foreach ( $config[$providerType] ?? [] as $providerName => $specification ) {
yield [ $providerType, $providerName, $providerClass ];
}
}
}
/** @dataProvider provideSessionProviders */
public function testSessionProviders( string $providerName ): void {
$specification = $this->getExtensionJson()['SessionProviders'][$providerName];
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory();
$objectFactory->createObject( $specification );
$this->assertTrue( true );
}
public function provideSessionProviders(): iterable {
foreach ( $this->getExtensionJson()['SessionProviders'] ?? [] as $providerName => $specification ) {
yield [ $providerName ];
}
}
/** @dataProvider provideServicesLists */
public function testServicesSorted( array $services ): void {
$sortedServices = $services;
usort( $sortedServices, function ( $serviceA, $serviceB ) {
$isExtensionServiceA = str_starts_with( $serviceA, $this->serviceNamePrefix );
$isExtensionServiceB = str_starts_with( $serviceB, $this->serviceNamePrefix );
if ( $isExtensionServiceA !== $isExtensionServiceB ) {
return $isExtensionServiceA ? 1 : -1;
}
return strcmp( $serviceA, $serviceB );
} );
$this->assertSame( $sortedServices, $services,
'Services should be sorted: first all MediaWiki services, ' .
"then all {$this->serviceNamePrefix}* ones." );
}
public function provideServicesLists(): iterable {
if ( $this->serviceNamePrefix === null ) {
return; // do not test sorting
}
foreach ( $this->provideSpecifications() as $name => $specification ) {
if (
is_array( $specification ) &&
array_key_exists( 'services', $specification )
) {
yield $name => [ $specification['services'] ];
}
}
}
public function provideSpecifications(): iterable {
foreach ( $this->provideHookHandlerNames() as [ $hookHandlerName ] ) {
yield "HookHandlers/$hookHandlerName" => $this->getExtensionJson()['HookHandlers'][$hookHandlerName];
}
foreach ( $this->provideContentModelIDs() as [ $contentModelID ] ) {
yield "ContentHandlers/$contentModelID" => $this->getExtensionJson()['ContentHandlers'][$contentModelID];
}
foreach ( $this->provideApiModuleNames() as [ $moduleName ] ) {
yield "APIModules/$moduleName" => $this->getExtensionJson()['APIModules'][$moduleName];
}
foreach ( $this->provideApiQueryModuleListsAndNames() as [ $moduleList, $moduleName ] ) {
yield "$moduleList/$moduleName" => $this->getExtensionJson()[$moduleList][$moduleName];
}
foreach ( $this->provideSpecialPageNames() as [ $specialPageName ] ) {
yield "SpecialPages/$specialPageName" => $this->getExtensionJson()['SpecialPages'][$specialPageName];
}
foreach ( $this->provideAuthenticationProviders() as [ $providerType, $providerName, $providerClass ] ) {
yield "AuthManagerAutoConfig/$providerType/$providerName" => $this->getExtensionJson()['AuthManagerAutoConfig'][$providerType][$providerName];
}
foreach ( $this->provideSessionProviders() as [ $providerName ] ) {
yield "SessionProviders/$providerName" => $this->getExtensionJson()['SessionProviders'][$providerName];
}
}
private function disallowDBAccess() {
$this->setService(
'DBLoadBalancerFactory',
function () {
$lb = $this->createMock( ILoadBalancer::class );
$lb->expects( $this->never() )
->method( 'getMaintenanceConnectionRef' );
$lb->method( 'getLocalDomainID' )
->willReturn( 'banana' );
// This IDatabase will fail when actually trying to do database actions
$db = $this->createNoOpMock( IDatabase::class );
$lb->method( 'getConnection' )
->willReturn( $db );
$lbFactory = $this->createMock( LBFactory::class );
$lbFactory->method( 'getMainLB' )
->willReturn( $lb );
$lbFactory->method( 'getLocalDomainID' )
->willReturn( 'banana' );
return $lbFactory;
}
);
}
private function disallowHttpAccess() {
$this->setService(
'HttpRequestFactory',
function () {
$factory = $this->createMock( HttpRequestFactory::class );
$factory->expects( $this->never() )
->method( 'create' );
$factory->expects( $this->never() )
->method( 'request' );
$factory->expects( $this->never() )
->method( 'get' );
$factory->expects( $this->never() )
->method( 'post' );
$factory->method( 'createMultiClient' )
->willReturn( $this->createMock( MultiHttpClient::class ) );
return $factory;
}
);
}
private function mockApiMain(): ApiMain {
$request = new FauxRequest();
$ctx = new ApiTestContext();
$ctx = $ctx->newTestContext( $request );
return new ApiMain( $ctx );
}
private function mockApiQuery(): ApiQuery {
return $this->mockApiMain()->getModuleManager()->getModule( 'query' );
}
}
|