ResourceLoader: Follow-up creation of ResourceLoaderEntryPoint
Follows-up Iadea44b7867f48a2be6ccbf00c0e56911a5af74e. ResourceLoaderEntryPoint: * Rename from Resource to ResourceLoader, as it is the entry point for ResourceLoader. * Fix accidental use of ingroup on the file docblock instead of the class. https://phabricator.wikimedia.org/F42046908 * Fix broken "see" reference to "/load.php" on the ResourceEntryPoint class on doc.wikimedia.org. Files are indexed in Doxygen by full name without leading slash. * Remove load.php from docgroup, akin to index.php. tests: * Remove unrelated Rest namespace from test suite. * Remove redundant default values in newResourceLoader(), especially null values were confusing by showing an apparent intent to set a value without actually isplacing the default, given that isset/?? treat set null as not set. * Simplify test by using the built-in startup module instead. EntryPoint: * Remove comment in test about doPostOutputShutdown. It sounded like it was explaining that we use getCapturedOutput() instead of run() because the latter calls doPostOutputShutdown which we want to avoid. Except, we don't avoid it, since run() is called and does call doPostOutputShutdown. Unclear what it was explaining. * Improve docs around getCapturedOutput() in the base class, and make explicit that this seemingly unused complexity exists for the purpose of testing. Bug: T354216 Change-Id: I5139fda90a3d5ac7ea1ed917c8045d1a09961d78
This commit is contained in:
parent
260fc1b50f
commit
be2dbcc219
6 changed files with 98 additions and 105 deletions
|
|
@ -1916,8 +1916,8 @@ $wgAutoloadLocalClasses = [
|
|||
'MediaWiki\\ResourceLoader\\OOUIIconPackModule' => __DIR__ . '/includes/ResourceLoader/OOUIIconPackModule.php',
|
||||
'MediaWiki\\ResourceLoader\\OOUIImageModule' => __DIR__ . '/includes/ResourceLoader/OOUIImageModule.php',
|
||||
'MediaWiki\\ResourceLoader\\OOUIModule' => __DIR__ . '/includes/ResourceLoader/OOUIModule.php',
|
||||
'MediaWiki\\ResourceLoader\\ResourceEntryPoint' => __DIR__ . '/includes/ResourceLoader/ResourceEntryPoint.php',
|
||||
'MediaWiki\\ResourceLoader\\ResourceLoader' => __DIR__ . '/includes/ResourceLoader/ResourceLoader.php',
|
||||
'MediaWiki\\ResourceLoader\\ResourceLoaderEntryPoint' => __DIR__ . '/includes/ResourceLoader/ResourceLoaderEntryPoint.php',
|
||||
'MediaWiki\\ResourceLoader\\SiteModule' => __DIR__ . '/includes/ResourceLoader/SiteModule.php',
|
||||
'MediaWiki\\ResourceLoader\\SiteStylesModule' => __DIR__ . '/includes/ResourceLoader/SiteStylesModule.php',
|
||||
'MediaWiki\\ResourceLoader\\SkinModule' => __DIR__ . '/includes/ResourceLoader/SkinModule.php',
|
||||
|
|
|
|||
|
|
@ -944,12 +944,19 @@ abstract class MediaWikiEntryPoint {
|
|||
}
|
||||
|
||||
/**
|
||||
* Enables output capture in the current output buffer.
|
||||
* The caller may need to call ob_start() to establish a buffer.
|
||||
* The captured output can be retrieved using getCapturedOutput().
|
||||
* When output capture is enabled, flushOutputBuffer() will not actually
|
||||
* send output.
|
||||
* Enable capturing of the current output buffer.
|
||||
*
|
||||
* There may be mutiple levels of output buffering. The level
|
||||
* we are currently at, at the time of calling this method,
|
||||
* is the level that will be captured to later retrieve via
|
||||
* getCapturedOutput().
|
||||
*
|
||||
* When capturing is active, flushOutputBuffer() will not actually
|
||||
* write to the real STDOUT, but instead write only to the capture.
|
||||
*
|
||||
* This exists to ease testing.
|
||||
*
|
||||
* @internal For use in PHPUnit tests
|
||||
* @see ob_start()
|
||||
* @see getCapturedOutput();
|
||||
*/
|
||||
|
|
@ -998,13 +1005,17 @@ abstract class MediaWikiEntryPoint {
|
|||
}
|
||||
|
||||
/**
|
||||
* Commits all output buffers down to the capture buffer level,
|
||||
* then clears the capture buffer and returns its contents.
|
||||
* Stop capturing and return all output
|
||||
*
|
||||
* Needs enableOutputCapture() to be called before run().
|
||||
* It flushes and drains all output buffers, but lets it go
|
||||
* to a return value instead of the real STDOUT.
|
||||
*
|
||||
* You must call enableOutputCapture() and run() before getCapturedOutput().
|
||||
*
|
||||
* @internal For use in PHPUnit tests
|
||||
* @see enableOutputCapture();
|
||||
* @see ob_end_clean
|
||||
* @return string HTTP response body
|
||||
*/
|
||||
public function getCapturedOutput(): string {
|
||||
if ( $this->outputCaptureLevel === null ) {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
/**
|
||||
* Entry point implementation for @ref ResourceLoader, which serves static CSS/JavaScript
|
||||
* via @ref MediaWiki\ResourceLoader\Module Module subclasses.
|
||||
*
|
||||
* @see /load.php The web entry point
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
|
|
@ -21,8 +16,6 @@
|
|||
* http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* @file
|
||||
* @ingroup entrypoint
|
||||
* @ingroup ResourceLoader
|
||||
*/
|
||||
|
||||
namespace MediaWiki\ResourceLoader;
|
||||
|
|
@ -30,7 +23,15 @@ namespace MediaWiki\ResourceLoader;
|
|||
use MediaWiki\MediaWikiEntryPoint;
|
||||
use Profiler;
|
||||
|
||||
class ResourceEntryPoint extends MediaWikiEntryPoint {
|
||||
/**
|
||||
* Entry point implementation for @ref ResourceLoader, which serves static CSS/JavaScript
|
||||
* via @ref MediaWiki\ResourceLoader\Module Module subclasses.
|
||||
*
|
||||
* @see load.php
|
||||
* @ingroup ResourceLoader
|
||||
* @ingroup entrypoint
|
||||
*/
|
||||
class ResourceLoaderEntryPoint extends MediaWikiEntryPoint {
|
||||
|
||||
/**
|
||||
* Main entry point
|
||||
10
load.php
10
load.php
|
|
@ -1,8 +1,6 @@
|
|||
<?php
|
||||
/**
|
||||
* The web entry point for @ref ResourceLoader, which serves static CSS/JavaScript.
|
||||
*
|
||||
* @see MediaWiki\ResourceLoader\ResourceEntryPoint The implementation.
|
||||
* @see MediaWiki\ResourceLoader\ResourceLoaderEntryPoint
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
|
@ -20,8 +18,6 @@
|
|||
* http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* @file
|
||||
* @ingroup entrypoint
|
||||
* @ingroup ResourceLoader
|
||||
* @author Roan Kattouw
|
||||
* @author Trevor Parscal
|
||||
*/
|
||||
|
|
@ -29,7 +25,7 @@
|
|||
use MediaWiki\Context\RequestContext;
|
||||
use MediaWiki\EntryPointEnvironment;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use MediaWiki\ResourceLoader\ResourceEntryPoint;
|
||||
use MediaWiki\ResourceLoader\ResourceLoaderEntryPoint;
|
||||
|
||||
// This endpoint is supposed to be independent of request cookies and other
|
||||
// details of the session. Enforce this constraint with respect to session use.
|
||||
|
|
@ -39,7 +35,7 @@ define( 'MW_ENTRY_POINT', 'load' );
|
|||
|
||||
require __DIR__ . '/includes/WebStart.php';
|
||||
|
||||
( new ResourceEntryPoint(
|
||||
( new ResourceLoaderEntryPoint(
|
||||
RequestContext::getMain(),
|
||||
new EntryPointEnvironment(),
|
||||
MediaWikiServices::getInstance()
|
||||
|
|
|
|||
|
|
@ -1,81 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Tests\Rest;
|
||||
|
||||
use HashBagOStuff;
|
||||
use MediaWiki\MainConfigNames;
|
||||
use MediaWiki\Request\FauxRequest;
|
||||
use MediaWiki\ResourceLoader\ResourceEntryPoint;
|
||||
use MediaWiki\ResourceLoader\ResourceLoader;
|
||||
use MediaWiki\Tests\MockEnvironment;
|
||||
use MediaWikiIntegrationTestCase;
|
||||
use Psr\Log\NullLogger;
|
||||
use Wikimedia\DependencyStore\KeyValueDependencyStore;
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\ResourceLoader\ResourceLoader
|
||||
* @group Database
|
||||
*/
|
||||
class ResourceEntryPointTest extends MediaWikiIntegrationTestCase {
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->overrideConfigValue( MainConfigNames::ShowExceptionDetails, true );
|
||||
$this->setService( 'ResourceLoader', [ $this, 'newResourceLoader' ] );
|
||||
}
|
||||
|
||||
public function newResourceLoader() {
|
||||
$rl = new ResourceLoader(
|
||||
$this->getServiceContainer()->getMainConfig(),
|
||||
new NullLogger(),
|
||||
new KeyValueDependencyStore( new HashBagOStuff() ),
|
||||
[
|
||||
'loadScript' => '/w/load.php',
|
||||
'maxageVersioned' => null,
|
||||
'maxageUnversioned' => null,
|
||||
]
|
||||
);
|
||||
|
||||
$rl->register( include MW_INSTALL_PATH . '/resources/Resources.php' );
|
||||
return $rl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MockEnvironment $env
|
||||
*
|
||||
* @return ResourceEntryPoint
|
||||
*/
|
||||
private function getEntryPoint( MockEnvironment $env ): ResourceEntryPoint {
|
||||
$entryPoint = new ResourceEntryPoint(
|
||||
$env->makeFauxContext(),
|
||||
$env,
|
||||
$this->getServiceContainer()
|
||||
);
|
||||
|
||||
return $entryPoint;
|
||||
}
|
||||
|
||||
public function testResource() {
|
||||
$uri = '/w/load.php';
|
||||
|
||||
$request = new FauxRequest( [ 'modules' => 'site' ] );
|
||||
$request->setRequestURL( $uri );
|
||||
|
||||
$env = new MockEnvironment( $request );
|
||||
$entryPoint = $this->getEntryPoint( $env );
|
||||
|
||||
$entryPoint->enableOutputCapture();
|
||||
$entryPoint->run();
|
||||
|
||||
// NOTE: MediaWikiEntryPoint::doPostOutputShutdown flushes all output buffers
|
||||
$content = $entryPoint->getCapturedOutput();
|
||||
|
||||
$this->assertStringContainsString( 'mw.loader.impl', $content );
|
||||
$this->assertStringContainsString( 'site@', $content );
|
||||
|
||||
// TODO: Assert headers. We'll have to make ResourceLoader use WebResponse
|
||||
// instead of calling the global header() function.
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Tests\ResourceLoader;
|
||||
|
||||
use MediaWiki\MainConfigNames;
|
||||
use MediaWiki\Request\FauxRequest;
|
||||
use MediaWiki\ResourceLoader\ResourceLoader;
|
||||
use MediaWiki\ResourceLoader\ResourceLoaderEntryPoint;
|
||||
use MediaWiki\Tests\MockEnvironment;
|
||||
use MediaWikiIntegrationTestCase;
|
||||
|
||||
/**
|
||||
* @covers \MediaWiki\ResourceLoader\ResourceLoader
|
||||
* @group Database
|
||||
*/
|
||||
class ResourceLoaderEntryPointTest extends MediaWikiIntegrationTestCase {
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->overrideConfigValue( MainConfigNames::ShowExceptionDetails, true );
|
||||
$this->setService( 'ResourceLoader', [ $this, 'newResourceLoader' ] );
|
||||
}
|
||||
|
||||
public function newResourceLoader() {
|
||||
// See also ResourceLoaderTestCase::getResourceLoaderContext
|
||||
return new ResourceLoader(
|
||||
$this->getServiceContainer()->getMainConfig(),
|
||||
null,
|
||||
null,
|
||||
[
|
||||
'loadScript' => '/w/load.php',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private function getEntryPoint( FauxRequest $request ) {
|
||||
$env = new MockEnvironment( $request );
|
||||
return new ResourceLoaderEntryPoint(
|
||||
$env->makeFauxContext(),
|
||||
$env,
|
||||
$this->getServiceContainer()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Further tested in StartUpModuleTest and ResourceLoaderTest
|
||||
*/
|
||||
public function testExecute() {
|
||||
// Simulate a real request, such as the one from RL\ClientHtml::getHeadHtml
|
||||
$request = new FauxRequest( [ 'modules' => 'startup', 'only' => 'scripts', 'raw' => '1' ] );
|
||||
$request->setRequestURL( '/w/load.php' );
|
||||
$entryPoint = $this->getEntryPoint( $request );
|
||||
|
||||
$entryPoint->enableOutputCapture();
|
||||
$entryPoint->run();
|
||||
$content = $entryPoint->getCapturedOutput();
|
||||
|
||||
$this->assertStringContainsString( 'function isCompatible', $content );
|
||||
$this->assertStringContainsString( 'mw.loader.addSource(', $content );
|
||||
|
||||
// TODO: Assert headers once ResourceLoader doesn't call header()
|
||||
// directly (maybe a library version of WebResponse).
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue