| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace MediaWiki\Tests; |
| 6 |
|
| 7 |
use MediaWiki\Api\ApiMain; |
| 8 |
use MediaWiki\Api\ApiQuery; |
| 9 |
use MediaWiki\Auth\PreAuthenticationProvider; |
| 10 |
use MediaWiki\Auth\PrimaryAuthenticationProvider; |
| 11 |
use MediaWiki\Auth\SecondaryAuthenticationProvider; |
| 12 |
use MediaWiki\Content\ContentHandler; |
| 13 |
use MediaWiki\Http\HttpRequestFactory; |
| 14 |
use MediaWiki\MediaWikiServices; |
| 15 |
use MediaWiki\Request\FauxRequest; |
| 16 |
use MediaWiki\Tests\Api\ApiTestContext; |
| 17 |
use MediaWikiIntegrationTestCase; |
| 18 |
use Wikimedia\Http\MultiHttpClient; |
| 19 |
use Wikimedia\Rdbms\IDatabase; |
| 20 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 21 |
use Wikimedia\Rdbms\LBFactory; |
| 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 |
abstract class ExtensionJsonTestBase extends MediaWikiIntegrationTestCase { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
protected string $extensionJsonPath; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
protected ?string $serviceNamePrefix = null; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private static array $extensionJsonCache = []; |
| 73 |
|
| 74 |
protected function setUp(): void { |
| 75 |
parent::setUp(); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
$this->disallowDBAccess(); |
| 80 |
$this->disallowHttpAccess(); |
| 81 |
} |
| 82 |
|
| 83 |
final protected function getExtensionJson(): array { |
| 84 |
if ( !array_key_exists( $this->extensionJsonPath, self::$extensionJsonCache ) ) { |
| 85 |
self::$extensionJsonCache[$this->extensionJsonPath] = json_decode( |
| 86 |
file_get_contents( $this->extensionJsonPath ), |
| 87 |
true, |
| 88 |
512, |
| 89 |
JSON_THROW_ON_ERROR |
| 90 |
); |
| 91 |
} |
| 92 |
return self::$extensionJsonCache[$this->extensionJsonPath]; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public function testHookHandler( string $hookHandlerName ): void { |
| 97 |
$specification = $this->getExtensionJson()['HookHandlers'][$hookHandlerName]; |
| 98 |
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory(); |
| 99 |
$objectFactory->createObject( $specification, [ |
| 100 |
'allowClassName' => true, |
| 101 |
] ); |
| 102 |
$this->assertTrue( true ); |
| 103 |
} |
| 104 |
|
| 105 |
public function provideHookHandlerNames(): iterable { |
| 106 |
foreach ( $this->getExtensionJson()['HookHandlers'] ?? [] as $hookHandlerName => $specification ) { |
| 107 |
yield [ $hookHandlerName ]; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
public function testContentHandler( string $contentModelID ): void { |
| 113 |
$specification = $this->getExtensionJson()['ContentHandlers'][$contentModelID]; |
| 114 |
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory(); |
| 115 |
$objectFactory->createObject( $specification, [ |
| 116 |
'assertClass' => ContentHandler::class, |
| 117 |
'allowCallable' => true, |
| 118 |
'allowClassName' => true, |
| 119 |
'extraArgs' => [ $contentModelID ], |
| 120 |
] ); |
| 121 |
$this->assertTrue( true ); |
| 122 |
} |
| 123 |
|
| 124 |
public function provideContentModelIDs(): iterable { |
| 125 |
foreach ( $this->getExtensionJson()['ContentHandlers'] ?? [] as $contentModelID => $specification ) { |
| 126 |
yield [ $contentModelID ]; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
public function testApiModule( string $moduleName ): void { |
| 132 |
$specification = $this->getExtensionJson()['APIModules'][$moduleName]; |
| 133 |
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory(); |
| 134 |
$objectFactory->createObject( $specification, [ |
| 135 |
'allowClassName' => true, |
| 136 |
'extraArgs' => [ $this->mockApiMain(), 'modulename' ], |
| 137 |
] ); |
| 138 |
$this->assertTrue( true ); |
| 139 |
} |
| 140 |
|
| 141 |
public function provideApiModuleNames(): iterable { |
| 142 |
foreach ( $this->getExtensionJson()['APIModules'] ?? [] as $moduleName => $specification ) { |
| 143 |
yield [ $moduleName ]; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
public function testApiQueryModule( string $moduleList, string $moduleName ): void { |
| 149 |
$specification = $this->getExtensionJson()[$moduleList][$moduleName]; |
| 150 |
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory(); |
| 151 |
$objectFactory->createObject( $specification, [ |
| 152 |
'allowClassName' => true, |
| 153 |
'extraArgs' => [ $this->mockApiQuery(), 'query' ], |
| 154 |
] ); |
| 155 |
$this->assertTrue( true ); |
| 156 |
} |
| 157 |
|
| 158 |
public function provideApiQueryModuleListsAndNames(): iterable { |
| 159 |
foreach ( [ 'APIListModules', 'APIMetaModules', 'APIPropModules' ] as $moduleList ) { |
| 160 |
foreach ( $this->getExtensionJson()[$moduleList] ?? [] as $moduleName => $specification ) { |
| 161 |
yield [ $moduleList, $moduleName ]; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
public function testSpecialPage( string $specialPageName ): void { |
| 168 |
$specification = $this->getExtensionJson()['SpecialPages'][$specialPageName]; |
| 169 |
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory(); |
| 170 |
$objectFactory->createObject( $specification, [ |
| 171 |
'allowClassName' => true, |
| 172 |
] ); |
| 173 |
$this->assertTrue( true ); |
| 174 |
} |
| 175 |
|
| 176 |
public function provideSpecialPageNames(): iterable { |
| 177 |
foreach ( $this->getExtensionJson()['SpecialPages'] ?? [] as $specialPageName => $specification ) { |
| 178 |
yield [ $specialPageName ]; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
public function testAuthenticationProviders( string $providerType, string $providerName, string $providerClass ): void { |
| 184 |
$specification = $this->getExtensionJson()['AuthManagerAutoConfig'][$providerType][$providerName]; |
| 185 |
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory(); |
| 186 |
$objectFactory->createObject( $specification, [ |
| 187 |
'assertClass' => $providerClass, |
| 188 |
] ); |
| 189 |
$this->assertTrue( true ); |
| 190 |
} |
| 191 |
|
| 192 |
public function provideAuthenticationProviders(): iterable { |
| 193 |
$config = $this->getExtensionJson()['AuthManagerAutoConfig'] ?? []; |
| 194 |
|
| 195 |
$types = [ |
| 196 |
'preauth' => PreAuthenticationProvider::class, |
| 197 |
'primaryauth' => PrimaryAuthenticationProvider::class, |
| 198 |
'secondaryauth' => SecondaryAuthenticationProvider::class, |
| 199 |
]; |
| 200 |
|
| 201 |
foreach ( $types as $providerType => $providerClass ) { |
| 202 |
foreach ( $config[$providerType] ?? [] as $providerName => $specification ) { |
| 203 |
yield [ $providerType, $providerName, $providerClass ]; |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
public function testSessionProviders( string $providerName ): void { |
| 210 |
$specification = $this->getExtensionJson()['SessionProviders'][$providerName]; |
| 211 |
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory(); |
| 212 |
$objectFactory->createObject( $specification ); |
| 213 |
$this->assertTrue( true ); |
| 214 |
} |
| 215 |
|
| 216 |
public function provideSessionProviders(): iterable { |
| 217 |
foreach ( $this->getExtensionJson()['SessionProviders'] ?? [] as $providerName => $specification ) { |
| 218 |
yield [ $providerName ]; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
public function testServicesSorted( array $services ): void { |
| 224 |
$sortedServices = $services; |
| 225 |
usort( $sortedServices, function ( $serviceA, $serviceB ) { |
| 226 |
$isExtensionServiceA = str_starts_with( $serviceA, $this->serviceNamePrefix ); |
| 227 |
$isExtensionServiceB = str_starts_with( $serviceB, $this->serviceNamePrefix ); |
| 228 |
if ( $isExtensionServiceA !== $isExtensionServiceB ) { |
| 229 |
return $isExtensionServiceA ? 1 : -1; |
| 230 |
} |
| 231 |
return strcmp( $serviceA, $serviceB ); |
| 232 |
} ); |
| 233 |
|
| 234 |
$this->assertSame( $sortedServices, $services, |
| 235 |
'Services should be sorted: first all MediaWiki services, ' . |
| 236 |
"then all {$this->serviceNamePrefix}* ones." ); |
| 237 |
} |
| 238 |
|
| 239 |
public function provideServicesLists(): iterable { |
| 240 |
if ( $this->serviceNamePrefix === null ) { |
| 241 |
return; |
| 242 |
} |
| 243 |
foreach ( $this->provideSpecifications() as $name => $specification ) { |
| 244 |
if ( |
| 245 |
is_array( $specification ) && |
| 246 |
array_key_exists( 'services', $specification ) |
| 247 |
) { |
| 248 |
yield $name => [ $specification['services'] ]; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
public function provideSpecifications(): iterable { |
| 254 |
foreach ( $this->provideHookHandlerNames() as [ $hookHandlerName ] ) { |
| 255 |
yield "HookHandlers/$hookHandlerName" => $this->getExtensionJson()['HookHandlers'][$hookHandlerName]; |
| 256 |
} |
| 257 |
|
| 258 |
foreach ( $this->provideContentModelIDs() as [ $contentModelID ] ) { |
| 259 |
yield "ContentHandlers/$contentModelID" => $this->getExtensionJson()['ContentHandlers'][$contentModelID]; |
| 260 |
} |
| 261 |
|
| 262 |
foreach ( $this->provideApiModuleNames() as [ $moduleName ] ) { |
| 263 |
yield "APIModules/$moduleName" => $this->getExtensionJson()['APIModules'][$moduleName]; |
| 264 |
} |
| 265 |
|
| 266 |
foreach ( $this->provideApiQueryModuleListsAndNames() as [ $moduleList, $moduleName ] ) { |
| 267 |
yield "$moduleList/$moduleName" => $this->getExtensionJson()[$moduleList][$moduleName]; |
| 268 |
} |
| 269 |
|
| 270 |
foreach ( $this->provideSpecialPageNames() as [ $specialPageName ] ) { |
| 271 |
yield "SpecialPages/$specialPageName" => $this->getExtensionJson()['SpecialPages'][$specialPageName]; |
| 272 |
} |
| 273 |
|
| 274 |
foreach ( $this->provideAuthenticationProviders() as [ $providerType, $providerName, $providerClass ] ) { |
| 275 |
yield "AuthManagerAutoConfig/$providerType/$providerName" => $this->getExtensionJson()['AuthManagerAutoConfig'][$providerType][$providerName]; |
| 276 |
} |
| 277 |
|
| 278 |
foreach ( $this->provideSessionProviders() as [ $providerName ] ) { |
| 279 |
yield "SessionProviders/$providerName" => $this->getExtensionJson()['SessionProviders'][$providerName]; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
private function disallowDBAccess() { |
| 284 |
$this->setService( |
| 285 |
'DBLoadBalancerFactory', |
| 286 |
function () { |
| 287 |
$lb = $this->createMock( ILoadBalancer::class ); |
| 288 |
$lb->expects( $this->never() ) |
| 289 |
->method( 'getMaintenanceConnectionRef' ); |
| 290 |
$lb->method( 'getLocalDomainID' ) |
| 291 |
->willReturn( 'banana' ); |
| 292 |
|
| 293 |
|
| 294 |
$db = $this->createNoOpMock( IDatabase::class ); |
| 295 |
$lb->method( 'getConnection' ) |
| 296 |
->willReturn( $db ); |
| 297 |
|
| 298 |
$lbFactory = $this->createMock( LBFactory::class ); |
| 299 |
$lbFactory->method( 'getMainLB' ) |
| 300 |
->willReturn( $lb ); |
| 301 |
$lbFactory->method( 'getLocalDomainID' ) |
| 302 |
->willReturn( 'banana' ); |
| 303 |
|
| 304 |
return $lbFactory; |
| 305 |
} |
| 306 |
); |
| 307 |
} |
| 308 |
|
| 309 |
private function disallowHttpAccess() { |
| 310 |
$this->setService( |
| 311 |
'HttpRequestFactory', |
| 312 |
function () { |
| 313 |
$factory = $this->createMock( HttpRequestFactory::class ); |
| 314 |
$factory->expects( $this->never() ) |
| 315 |
->method( 'create' ); |
| 316 |
$factory->expects( $this->never() ) |
| 317 |
->method( 'request' ); |
| 318 |
$factory->expects( $this->never() ) |
| 319 |
->method( 'get' ); |
| 320 |
$factory->expects( $this->never() ) |
| 321 |
->method( 'post' ); |
| 322 |
$factory->method( 'createMultiClient' ) |
| 323 |
->willReturn( $this->createMock( MultiHttpClient::class ) ); |
| 324 |
return $factory; |
| 325 |
} |
| 326 |
); |
| 327 |
} |
| 328 |
|
| 329 |
private function mockApiMain(): ApiMain { |
| 330 |
$request = new FauxRequest(); |
| 331 |
$ctx = new ApiTestContext(); |
| 332 |
$ctx = $ctx->newTestContext( $request ); |
| 333 |
return new ApiMain( $ctx ); |
| 334 |
} |
| 335 |
|
| 336 |
private function mockApiQuery(): ApiQuery { |
| 337 |
return $this->mockApiMain()->getModuleManager()->getModule( 'query' ); |
| 338 |
} |
| 339 |
|
| 340 |
} |