wiki.techinc.nl/tests/phpunit/includes/content/JavaScriptContentHandlerTest.php
Timo Tijhof faf4cb3384 content: Improve roundtrip tests for JavaScriptContent
* Re-use the same data provider explicitly instead of manually
  keeping them in sync.

* Add test case names as array keys to improve debugging.

* Widen `@covers` annotations in unit tests.

Bug: T107289
Change-Id: I9a321400855ddd1f56334f6ecf85590fd8ed4aaf
2024-07-05 19:28:14 +01:00

54 lines
1.9 KiB
PHP

<?php
use MediaWiki\Content\JavaScriptContent;
use MediaWiki\Content\JavaScriptContentHandler;
use MediaWiki\MainConfigNames;
use MediaWiki\Title\Title;
/**
* @covers \JavaScriptContentHandler
*/
class JavaScriptContentHandlerTest extends MediaWikiLangTestCase {
/**
* @dataProvider provideMakeRedirectContent
*/
public function testMakeRedirectContent( $title, $expected ) {
$this->overrideConfigValues( [
MainConfigNames::Server => '//example.org',
MainConfigNames::Script => '/w/index.php',
] );
$ch = new JavaScriptContentHandler();
$content = $ch->makeRedirectContent( Title::newFromText( $title ) );
$this->assertInstanceOf( JavaScriptContent::class, $content );
$this->assertEquals( $expected, $content->serialize( CONTENT_FORMAT_JAVASCRIPT ) );
}
/**
* This is re-used by JavaScriptContentTest to assert roundtrip
*/
public static function provideMakeRedirectContent() {
return [
'MediaWiki namespace page' => [
'MediaWiki:MonoBook.js',
'/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=MediaWiki:MonoBook.js&action=raw&ctype=text/javascript");'
],
'User subpage' => [
'User:FooBar/common.js',
'/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:FooBar/common.js&action=raw&ctype=text/javascript");'
],
'Gadget page' => [
'Gadget:FooBaz.js',
'/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=Gadget:FooBaz.js&action=raw&ctype=text/javascript");'
],
'Unicode basename' => [
'User:😂/unicode.js',
'/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:%F0%9F%98%82/unicode.js&action=raw&ctype=text/javascript");'
],
'Ampersand basename' => [
'User:Penn & Teller/ampersand.js',
'/* #REDIRECT */mw.loader.load("//example.org/w/index.php?title=User:Penn_%26_Teller/ampersand.js&action=raw&ctype=text/javascript");'
],
];
}
}