2017-02-20 04:33:24 +00:00
|
|
|
<?php
|
|
|
|
|
2024-10-20 11:21:02 +00:00
|
|
|
use MediaWiki\Content\Content;
|
|
|
|
use MediaWiki\Content\CssContent;
|
|
|
|
use MediaWiki\Content\WikitextContent;
|
2022-02-06 14:39:49 +00:00
|
|
|
use MediaWiki\Extension\TemplateStyles\TemplateStylesContent;
|
2024-07-29 11:02:33 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2022-02-06 14:39:49 +00:00
|
|
|
|
2017-02-20 04:33:24 +00:00
|
|
|
/**
|
|
|
|
* @group TemplateStyles
|
2022-02-06 14:39:49 +00:00
|
|
|
* @covers \MediaWiki\Extension\TemplateStyles\TemplateStylesContent
|
2017-02-20 04:33:24 +00:00
|
|
|
*/
|
|
|
|
class TemplateStylesContentTest extends TextContentTest {
|
|
|
|
|
2021-07-23 22:49:15 +00:00
|
|
|
protected function setUp(): void {
|
2017-02-20 04:33:24 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
2024-07-29 11:02:33 +00:00
|
|
|
$this->overrideConfigValues( [
|
2024-11-07 10:02:52 +00:00
|
|
|
MainConfigNames::TextModelsToParse => [ 'sanitized-css' ],
|
2024-07-29 11:02:33 +00:00
|
|
|
'TemplateStylesMaxStylesheetSize' => 1024000,
|
2017-02-20 04:33:24 +00:00
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function newContent( $text ) {
|
|
|
|
return new TemplateStylesContent( $text );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function dataGetParserOutput() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'Template:Test/styles.css',
|
|
|
|
'sanitized-css',
|
|
|
|
".hello { content: 'world'; color: bogus; }\n\n<ok>\n",
|
2021-03-26 23:59:00 +00:00
|
|
|
// phpcs:ignore Generic.Files.LineLength
|
2017-02-20 04:33:24 +00:00
|
|
|
"<pre class=\"mw-code mw-css\" dir=\"ltr\">\n.hello { content: 'world'; color: bogus; }\n\n<ok>\n\n</pre>",
|
|
|
|
[
|
|
|
|
'Warnings' => [
|
|
|
|
'Unexpected end of stylesheet in rule at line 4 character 1.',
|
|
|
|
'Invalid or unsupported value for property <code>color</code> at line 1 character 35.',
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'Template:Test/styles.css',
|
|
|
|
'sanitized-css',
|
|
|
|
"/* hello [[world]] */\n",
|
|
|
|
"<pre class=\"mw-code mw-css\" dir=\"ltr\">\n/* hello [[world]] */\n\n</pre>",
|
|
|
|
[
|
|
|
|
'Links' => [
|
|
|
|
[ 'World' => 0 ]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function dataPreSaveTransform() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'hello this is ~~~',
|
|
|
|
'hello this is ~~~',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
|
|
|
|
'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
" Foo \n ",
|
|
|
|
" Foo",
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function dataPreloadTransform() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'hello this is ~~~',
|
|
|
|
'hello this is ~~~',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
|
|
|
|
'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetModel() {
|
|
|
|
$content = $this->newContent( 'hello world.' );
|
|
|
|
|
|
|
|
$this->assertEquals( 'sanitized-css', $content->getModel() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetContentHandler() {
|
|
|
|
$content = $this->newContent( 'hello world.' );
|
|
|
|
|
|
|
|
$this->assertEquals( 'sanitized-css', $content->getContentHandler()->getModelID() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirects aren't supported
|
|
|
|
*/
|
|
|
|
public static function provideUpdateRedirect() {
|
2021-03-26 23:59:00 +00:00
|
|
|
// phpcs:disable Generic.Files.LineLength
|
2017-02-20 04:33:24 +00:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
'#REDIRECT [[Someplace]]',
|
|
|
|
'#REDIRECT [[Someplace]]',
|
|
|
|
],
|
|
|
|
|
|
|
|
// The style supported by CssContent
|
|
|
|
[
|
|
|
|
'/* #REDIRECT */@import url(//example.org/w/index.php?title=MediaWiki:MonoBook.css&action=raw&ctype=text/css);',
|
|
|
|
'/* #REDIRECT */@import url(//example.org/w/index.php?title=MediaWiki:MonoBook.css&action=raw&ctype=text/css);',
|
|
|
|
],
|
|
|
|
];
|
2021-03-26 23:59:00 +00:00
|
|
|
// phpcs:enable
|
2017-02-20 04:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideGetRedirectTarget
|
|
|
|
*/
|
|
|
|
public function testGetRedirectTarget( $title, $text ) {
|
2024-07-29 11:02:33 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
MainConfigNames::Server => '//example.org',
|
|
|
|
MainConfigNames::ScriptPath => '/w',
|
|
|
|
MainConfigNames::Script => '/w/index.php',
|
2017-02-20 04:33:24 +00:00
|
|
|
] );
|
|
|
|
$content = $this->newContent( $text );
|
|
|
|
$target = $content->getRedirectTarget();
|
|
|
|
$this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function provideGetRedirectTarget() {
|
2021-03-26 23:59:00 +00:00
|
|
|
// phpcs:disable Generic.Files.LineLength
|
2017-02-20 04:33:24 +00:00
|
|
|
return [
|
|
|
|
[ null, "/* #REDIRECT */@import url(//example.org/w/index.php?title=MediaWiki:MonoBook.css&action=raw&ctype=text/css);" ],
|
|
|
|
[ null, "/* #REDIRECT */@import url(//example.org/w/index.php?title=User:FooBar/common.css&action=raw&ctype=text/css);" ],
|
|
|
|
[ null, "/* #REDIRECT */@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ],
|
|
|
|
[ null, "@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ],
|
|
|
|
[ null, "/* #REDIRECT */@import url(//example.com/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ],
|
|
|
|
];
|
2021-03-26 23:59:00 +00:00
|
|
|
// phpcs:enable
|
2017-02-20 04:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function dataEquals() {
|
|
|
|
return [
|
|
|
|
[ new TemplateStylesContent( 'hallo' ), null, false ],
|
|
|
|
[ new TemplateStylesContent( 'hallo' ), new TemplateStylesContent( 'hallo' ), true ],
|
|
|
|
[ new TemplateStylesContent( 'hallo' ), new CssContent( 'hallo' ), false ],
|
|
|
|
[ new TemplateStylesContent( 'hallo' ), new WikitextContent( 'hallo' ), false ],
|
|
|
|
[ new TemplateStylesContent( 'hallo' ), new TemplateStylesContent( 'HALLO' ), false ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataEquals
|
|
|
|
*/
|
2024-10-30 14:19:54 +00:00
|
|
|
public function testEquals( Content $a, ?Content $b = null, $equal = false ) {
|
2017-02-20 04:33:24 +00:00
|
|
|
$this->assertEquals( $equal, $a->equals( $b ) );
|
|
|
|
}
|
|
|
|
}
|