Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/AbstractComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace MediaWiki\Extension\BootstrapComponents;

use MediaWiki\MediaWikiServices;
use \MWException;

/**
Expand Down Expand Up @@ -153,6 +154,9 @@ public function parseComponent( $parserRequest ) {
throw new MWException( 'Invalid ParserRequest supplied to component ' . $this->getComponentName() . '!' );
}
$this->getNestingController()->open( $this );
MediaWikiServices::getInstance()
->getService( 'BootstrapComponentsService' )
->registerComponentAsActive( $this->getComponentName() );
$this->initComponentData( $parserRequest );

$input = $this->prepareInput( $parserRequest );
Expand Down
1 change: 1 addition & 0 deletions src/HooksHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public function onParserAfterParse( $parser, &$text, $stripState ): bool {
}
foreach ( $this->getComponentLibrary()->getModulesFor( $activeComponent ) as $module ) {
$parser->getOutput()->addModuleStyles( [ $module ] );
$parser->getOutput()->addModules( [ $module ] );
}
}
return true;
Expand Down
48 changes: 48 additions & 0 deletions tests/phpunit/Unit/HooksHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use MediaWiki\Extension\BootstrapComponents\ComponentLibrary;
use MediaWiki\Extension\BootstrapComponents\HooksHandler;
use MediaWiki\Extension\BootstrapComponents\NestingController;
use Parser;
use ParserOutput;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -72,4 +74,50 @@ public function testOnGalleryGetModes() {
public function testOnOutputPageParserOutput() {
$this->assertTrue( true );
}

public function testOnParserAfterParseLoadsActiveComponentScriptsAndStyles() {
$loadedStyles = [];
$loadedModules = [];

$parserOutput = $this->createMock( ParserOutput::class );
$parserOutput->method( 'addModuleStyles' )
->willReturnCallback( function ( $m ) use ( &$loadedStyles ) {
$loadedStyles = array_merge( $loadedStyles, (array)$m );
} );
$parserOutput->method( 'addModules' )
->willReturnCallback( function ( $m ) use ( &$loadedModules ) {
$loadedModules = array_merge( $loadedModules, (array)$m );
} );

$parser = $this->createMock( Parser::class );
$parser->method( 'getOutput' )->willReturn( $parserOutput );

$service = $this->createMock( BootstrapComponentsService::class );
$service->method( 'getNameOfActiveSkin' )->willReturn( 'vector' );
$service->method( 'getActiveComponents' )->willReturn( [ 'tooltip', 'popover' ] );

$library = $this->createMock( ComponentLibrary::class );
$library->method( 'isRegistered' )->willReturn( true );
$library->method( 'getModulesFor' )
->willReturnCallback( function ( $name ) {
return [ 'ext.bootstrapComponents.' . $name . '.fix' ];
} );

$handler = new HooksHandler(
$service,
$library,
$this->createMock( NestingController::class )
);

$text = '';
$handler->onParserAfterParse( $parser, $text, null );

// Per-component fix modules must reach BOTH addModuleStyles and addModules
// (style-only modules treat addModules as a no-op; modules with a scripts
// entry get their JS init via that call).
$this->assertContains( 'ext.bootstrapComponents.tooltip.fix', $loadedStyles );
$this->assertContains( 'ext.bootstrapComponents.popover.fix', $loadedStyles );
$this->assertContains( 'ext.bootstrapComponents.tooltip.fix', $loadedModules );
$this->assertContains( 'ext.bootstrapComponents.popover.fix', $loadedModules );
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see lots of LOC, mocks, and binding to details for testing one line of production code. Highly sus and likely notably net-negative

}
}
Loading