Skip to content

Commit

Permalink
fixed unit tests
Browse files Browse the repository at this point in the history
* fix unit tests for 1.40+
* delegate handling of suppressed modal from magic word to class
  BootstrapComponentsService
  • Loading branch information
oetterer committed Jul 13, 2024
1 parent f5a9c9f commit 6f918cc
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 173 deletions.
21 changes: 21 additions & 0 deletions src/BootstrapComponentsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class BootstrapComponentsService
*/
private Config $mainConfig;

/**
* @var bool
*/
private bool $modalsSuppressedByMagicWord;

/**
* Holds the name of the skin we use (or false, if there is no skin).
Expand All @@ -31,6 +35,15 @@ class BootstrapComponentsService
public function __construct( Config $mainConfig ) {
$this->mainConfig = $mainConfig;
$this->activeComponents = [];
$this->modalsSuppressedByMagicWord = false;
}


/**
* @return bool
*/
public function areModalsSuppressedByMagicWord(): bool {
return $this->modalsSuppressedByMagicWord;
}

/**
Expand Down Expand Up @@ -61,6 +74,14 @@ public function registerComponentAsActive( string $componentName ): void {
$this->activeComponents[$componentName] = true;
}

/**
* @param bool $modalsSuppressedByMagicWord
* @return void
*/
public function setModalsSuppressedByMagicWord( bool $modalsSuppressedByMagicWord ): void {
$this->modalsSuppressedByMagicWord = $modalsSuppressedByMagicWord;
}

/**
* Returns true, if active skin is vector
*
Expand Down
11 changes: 7 additions & 4 deletions src/Hooks/OutputPageParserOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
use MediaWiki\Extension\BootstrapComponents\BootstrapComponentsService;
use OutputPage;
use ParserOutput;
/*
* TODO: When dropping support for 1.39, use these:
use MediaWiki\Output\OutputPage;
use MediaWiki\Parser\ParserOutput;
*/

/**
* Class OutputPageParserOutput
Expand Down Expand Up @@ -126,16 +131,14 @@ protected function getBootstrapComponentsService(): BootstrapComponentsService {
/**
* @return OutputPage
*/
protected function getOutputPage(): OutputPage
{
protected function getOutputPage(): OutputPage {
return $this->outputPage;
}

/**
* @return ParserOutput
*/
protected function getParserOutput(): ParserOutput
{
protected function getParserOutput(): ParserOutput {
return $this->parserOutput;
}
}
12 changes: 4 additions & 8 deletions src/HooksHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use MediaWiki\Hook\ParserFirstCallInitHook;
use MediaWiki\Hook\SetupAfterCacheHook;
use MediaWiki\MediaWikiServices;
use Parser;
use MediaWiki\Parser\Parser;
use SMW\Utils\File;
use StripState;

Expand Down Expand Up @@ -168,14 +168,10 @@ public function onImageBeforeProduceHTML(
* @return bool
*/
public function onInternalParseBeforeLinks( $parser, &$text, $stripState ): bool {
// we do not use our ParserOutputHelper class here, for we would need to reset it in integration tests.
// resetting our factory build classes is unfortunately a little skittish
$parser->getOutput()->setExtensionData(
BootstrapComponents::EXTENSION_DATA_NO_IMAGE_MODAL,
MediaWikiServices::getInstance()->getMagicWordFactory()->get( 'BSC_NO_IMAGE_MODAL' )
->matchAndRemove( $text )
$this->getBootstrapComponentsService()->setModalsSuppressedByMagicWord(
MediaWikiServices::getInstance()
->getMagicWordFactory()->get( 'BSC_NO_IMAGE_MODAL' )->matchAndRemove( $text )
);

return true;
}

Expand Down
32 changes: 7 additions & 25 deletions src/ImageModal.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Contains the class for replacing image normal image display with a modal.
* Contains the class for replacing normal image display with a modal.
*
* @copyright (C) 2018, Tobias Oetterer, Paderborn University
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 (or later)
Expand All @@ -26,7 +26,6 @@

namespace MediaWiki\Extension\BootstrapComponents;

use DummyLinker;
use File;
use Html;
use MediaTransformOutput;
Expand Down Expand Up @@ -54,11 +53,6 @@ class ImageModal implements NestableInterface {
*/
private BootstrapComponentsService $bootstrapComponentService;

/**
* @var DummyLinker $dummyLinker
*/
private DummyLinker $dummyLinker;

/**
* @var File $file
*/
Expand Down Expand Up @@ -97,7 +91,7 @@ class ImageModal implements NestableInterface {
/**
* ImageModal constructor.
*
* @param DummyLinker $dummyLinker
* @param null $null (was DummyLinker $dummyLinker)
* @param Title $title
* @param File $file
* @param NestingController $nestingController
Expand All @@ -107,12 +101,11 @@ class ImageModal implements NestableInterface {
* @throws MWException cascading {@see ApplicationFactory} methods
*/
public function __construct(
DummyLinker $dummyLinker, Title $title, File $file,
$null, Title $title, File $file,
NestingController $nestingController, BootstrapComponentsService $bootstrapComponentService,
ParserOutputHelper $parserOutputHelper = null
) {
$this->file = $file;
$this->dummyLinker = $dummyLinker;
$this->title = $title;

$this->nestingController = $nestingController;
Expand Down Expand Up @@ -195,6 +188,7 @@ public function parse( array &$frameParams, array &$handlerParams, &$time, &$res

if ( $res === '' ) {
// ImageModal::turnParamsIntoModal returns the empty string, when something went wrong
// returning true means, delegating the image rendering back to \MediaWiki\Linker\Linker::makeImageLink
return true;
}
return false;
Expand Down Expand Up @@ -281,14 +275,6 @@ protected function getBootstrapComponentsService(): BootstrapComponentsService {
return $this->bootstrapComponentService;
}

/**
* @return DummyLinker
*/
/** @scrutinizer ignore-unused */
protected function getDummyLinker(): DummyLinker {
return $this->dummyLinker;
}

/**
* @return File
*/
Expand All @@ -312,7 +298,6 @@ protected function getParentComponent(): bool|NestableInterface|null {

/**
* @return ParserOutputHelper
* @deprecated
*/
protected function getParserOutputHelper(): ParserOutputHelper {
return $this->parserOutputHelper;
Expand Down Expand Up @@ -422,16 +407,13 @@ private function assertImageModalNotSuppressed( array $frameParams ): bool
) {
return false;
}
/** @see ParserOutputHelper::areImageModalsSuppressed as to why we need to use the global parser! */
$parser = MediaWikiServices::getInstance()->getParser();
// the is_null test has to be added because otherwise some unit tests will fail
return is_null( $parser->getOutput() )
|| !$parser->getOutput()->getExtensionData( BootstrapComponents::EXTENSION_DATA_NO_IMAGE_MODAL );
return !MediaWikiServices::getInstance()->getService( 'BootstrapComponentsService' )
->areModalsSuppressedByMagicWord();
}

/**
* @param MediaTransformOutput $img
* @param array $sanitizedFrameParams
* @param array $sanitizedFrameParams
*
* @return string
*/
Expand Down
1 change: 1 addition & 0 deletions src/ModalBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace MediaWiki\Extension\BootstrapComponents;

use \Html;
use MediaWiki\MediaWikiServices;

/**
* Class ModalBase
Expand Down
41 changes: 28 additions & 13 deletions src/ParserOutputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,21 @@

namespace MediaWiki\Extension\BootstrapComponents;

use \Html;
use \ParserOutput;
use \Title;
/*
* TODO: When dropping support for MW1.39, use these class imports:
use MediaWiki\Html\Html;
use MediaWiki\Message\Message;
use MediaWiki\Parser\Parser;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Title\Title;
*/

use Html;
use Message;
use Parser;
use ParserOutput;
use Title;


/**
* Class ParserOutputHelper
Expand All @@ -54,7 +66,7 @@ class ParserOutputHelper {
private $articleTrackedOnError;

/**
* @var \Parser $parser
* @var Parser $parser
*/
private $parser;

Expand All @@ -64,7 +76,7 @@ class ParserOutputHelper {
*
* Do not instantiate directly, but use {@see ApplicationFactory::getParserOutputHelper} instead.
*
* @param \Parser $parser
* @param Parser $parser
*
* @see ApplicationFactory::getParserOutputHelper
*/
Expand Down Expand Up @@ -115,7 +127,7 @@ public function addTrackingCategory() {
* Unless I find a solution for the integration test problem, I cannot use an instance of
* ParserOutputHelper in ImageModal to ascertain this. In integration tests, "we" use a
* different parser than the InternalParseBeforeLinks-Hook. At least, after I added
* Scribunto _unit_ tests. All messes up, I'm afraid. ImageModal better use global parser, and
* Scribunto _unit_ tests. All messed up, I'm afraid. ImageModal better use global parser, and
* for the time being this method will be
* @deprecated
*
Expand Down Expand Up @@ -157,22 +169,25 @@ public function injectLater( $id, $rawHtml ) {
*
* @return string
*/
public function renderErrorMessage( $errorMessageName ) {
public function renderErrorMessage( string $errorMessageName ): string {
if ( !$errorMessageName || !trim( $errorMessageName ) ) {
return '';
}
$this->addErrorTrackingCategory();
return Html::rawElement(
'span',
[ 'class' => 'error' ],
wfMessage( trim( $errorMessageName ) )->inContentLanguage()->title( $this->parser->getTitle() )->parse()
(new Message( trim( $errorMessageName ) ))->inContentLanguage()->page(
$this->getParser()->getPage()
)->parse()
);

}

/**
* @return \Parser
* @return Parser
*/
protected function getParser() {
protected function getParser(): Parser {
return $this->parser;
}

Expand All @@ -181,11 +196,11 @@ protected function getParser() {
*
* @param String $trackingCategoryMessageName name of the message, containing the tracking category
*/
private function placeTrackingCategory( $trackingCategoryMessageName ) {
$categoryMessage = wfMessage( $trackingCategoryMessageName )->inContentLanguage();
private function placeTrackingCategory( string $trackingCategoryMessageName ): void {
$categoryMessage = (new Message( $trackingCategoryMessageName ))->inContentLanguage();
$parserOutput = $this->parser->getOutput();
if ( !$categoryMessage->isDisabled() && is_a( $parserOutput, ParserOutput::class ) ) {
// Q: when do we expect \Parser->getOutput() no to be a \ParserOutput? A:During tests.
// Q: when do we expect Parser->getOutput() no to be a ParserOutput? A:During tests.
$cat = Title::makeTitleSafe( NS_CATEGORY, $categoryMessage->text() );
if ( $cat ) {
$sort = (string)$parserOutput->getPageProperty('defaultsort') ?? '';
Expand Down
17 changes: 9 additions & 8 deletions tests/phpunit/Unit/AbstractComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@ public function testSimpleOutput( string $component ) {
$parsedString = reset( $parsedString );
}
$this->assertIsString( $parsedString );
$this->assertRegExp(
'/class="[^"]*test-class"/',
$parsedString
);
$this->assertRegExp(
'/style="[^"]*color:black"/',
$parsedString
);

// TODO when we drop support for MW1.39
if ( version_compare( $GLOBALS['wgVersion'], '1.40', 'lt' ) ) {
$this->assertRegExp( '/class="[^"]*test-class"/', $parsedString );
$this->assertRegExp( '/style="[^"]*color:black"/', $parsedString );
} else {
$this->assertMatchesRegularExpression( '/class="[^"]*test-class"/', $parsedString );
$this->assertMatchesRegularExpression( '/style="[^"]*color:black"/', $parsedString );
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/Unit/BootstrapComponentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testCanGetNameOfActiveSkin() {
);
}

public function testRegisterModules() {
public function testRegisterComponentAsActive() {
$instance = new BootstrapComponentsService( $this->getMockBuilder( Config::class )->getMock() );

$instance->registerComponentAsActive( 'Foo' );
Expand All @@ -70,6 +70,7 @@ public function testPrivateCanDetectSkinInUse() {

$reflection = new ReflectionClass( BootstrapComponentsService::class );
$method = $reflection->getMethod( 'detectSkinInUse' );
$method->setAccessible( true );

// this is default
$this->assertEquals(
Expand Down
20 changes: 14 additions & 6 deletions tests/phpunit/Unit/Components/AlertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AlertTest extends ComponentsTestBase {
public function testCanConstruct() {

$this->assertInstanceOf(
'MediaWiki\\Extension\\BootstrapComponents\\Components\\Alert',
Alert::class,
new Alert(
$this->getComponentLibrary(),
$this->getParserOutputHelper(),
Expand All @@ -55,13 +55,21 @@ public function testCanRender( $input, $arguments, $expectedOutput ) {

$parserRequest = $this->buildParserRequest( $input, $arguments );

/** @noinspection PhpParamsInspection */
$generatedOutput = $instance->parseComponent( $parserRequest );

$this->assertRegExp(
'~^<div.+class="alert alert-.+".*role="alert".*>' . $this->input . '(<button.*button>)?</div>$~',
$generatedOutput
);
// TODO when we drop support for MW1.39
if ( version_compare( $GLOBALS['wgVersion'], '1.40', 'lt' ) ) {
$this->assertRegExp(
'~^<div.+class="alert alert-.+".*role="alert".*>' . $this->input . '(<button.*button>)?</div>$~',
$generatedOutput
);
} else {
$this->assertMatchesRegularExpression(
'~^<div.+class="alert alert-.+".*role="alert".*>' . $this->input . '(<button.*button>)?</div>$~',
$generatedOutput
);
}

$this->assertEquals( $expectedOutput, $generatedOutput );
}

Expand Down
Loading

0 comments on commit 6f918cc

Please sign in to comment.