mediawiki-extensions-Templa.../tests/phpunit/TemplateStylesFontFaceAtRuleSanitizer.php
Brad Jorsch b301a30abf Use wikimedia/css-sanitizer, and rewrite the hooking
wikimedia/css-sanitizer provides a real CSS parser, which should be
safer than poking at things with regular expressions.

Instead of the strange hybrid model that tried to both process inline
CSS and save CSS when the template is saved, it now looks for
<templatestyles src="Title" /> during the parse to do all the
transclusion of styles.

The output method is "<style> tags in the body", pending someone
implementing T160563.

It now also registers a "sanitized-css" content model, which should pick
up the CSS syntax highlighting and will validate the submitted CSS on
submit and prevent a save if it's not valid.

This patch also takes advantage of LGPL-2.x § 3 to relicense the
extension as GPL-2.0+, although at this point none of the LGPL code
remains anyway.

Bug: T133408
Bug: T136054
Bug: T135788
Bug: T135789
Change-Id: I993e6f18d32a43aac8398743133d227b05133bbd
Depends-On: If4eb5bf71f94fa366ec4eddb6964e8f4df6b824a
2017-06-07 15:14:09 +00:00

91 lines
2.4 KiB
PHP

<?php
use Wikimedia\CSS\Grammar\MatcherFactory;
use Wikimedia\CSS\Parser\Parser;
use Wikimedia\CSS\Util;
/**
* @group TemplateStyles
*/
class TemplateSantitizerFontFaceAtRuleSanitizerTest extends PHPUnit_Framework_TestCase {
/**
* @dataProvider provideRules
* @param string $input
* @param bool $handled
* @param string|null $output
* @param string|null $minified
* @param array $errors
* @param array $options
*/
public function testRules( $input, $handled, $output, $minified, $errors = [], $options = [] ) {
$san = new TemplateStylesFontFaceAtRuleSanitizer( new MatcherFactory() );
$rule = Parser::newFromString( $input )->parseRule();
$oldRule = clone( $rule );
$this->assertSame( $handled, $san->handlesRule( $rule ) );
$ret = $san->sanitize( $rule );
$this->assertSame( $errors, $san->getSanitizationErrors() );
if ( $output === null ) {
$this->assertNull( $ret );
} else {
$this->assertNotNull( $ret );
$this->assertSame( $output, (string)$ret );
$this->assertSame( $minified, Util::stringify( $ret, [ 'minify' => true ] ) );
}
$this->assertEquals( (string)$oldRule, (string)$rule, 'Rule wasn\'t overwritten' );
}
public static function provideRules() {
return [
'non-prefixed font family as string' => [
'@font-face {
font-family: "foo bar";
}',
true,
'@font-face {}',
'@font-face{}',
[
[ 'bad-value-for-property', 2, 19, 'font-family' ],
]
],
'non-prefixed font family as idents' => [
'@font-face {
font-family: foo bar;
}',
true,
'@font-face {}',
'@font-face{}',
[
[ 'bad-value-for-property', 2, 19, 'font-family' ],
]
],
'prefixed font family as string' => [
'@font-face {
font-family: "TemplateStyles foo bar";
}',
true,
'@font-face { font-family: "TemplateStyles foo bar"; }',
'@font-face{font-family:"TemplateStyles foo bar"}',
],
'non-prefixed font family as idents (1)' => [
'@font-face {
font-family: TemplateStyles foo bar;
}',
true,
'@font-face { font-family: TemplateStyles foo bar; }',
'@font-face{font-family:TemplateStyles foo bar}',
],
'non-prefixed font family as idents (2)' => [
'@font-face {
font-family: TemplateStylesFoo bar;
}',
true,
'@font-face { font-family: TemplateStylesFoo bar; }',
'@font-face{font-family:TemplateStylesFoo bar}',
],
];
}
}