X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Flibs%2FJavaScriptMinifierTest.php;h=16048bf4334e91d8f0fadf71675cc0a942a65633;hb=92ce940ec32b6af792ec6b5f4b8cd81080868307;hp=d6a104002bd492eb4a968bda91d819a2680bb288;hpb=577f3d79115173f4dd16bb46f6d0ef2c82b55add;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/libs/JavaScriptMinifierTest.php b/tests/phpunit/includes/libs/JavaScriptMinifierTest.php index d6a104002b..16048bf433 100644 --- a/tests/phpunit/includes/libs/JavaScriptMinifierTest.php +++ b/tests/phpunit/includes/libs/JavaScriptMinifierTest.php @@ -1,9 +1,22 @@ setMaxLineLength( 1000 ); + } + + private function setMaxLineLength( $val ) { + $classReflect = new ReflectionClass( JavaScriptMinifier::class ); + $propertyReflect = $classReflect->getProperty( 'maxLineLength' ); + $propertyReflect->setAccessible( true ); + $propertyReflect->setValue( JavaScriptMinifier::class, $val ); + } + public static function provideCases() { return [ @@ -86,6 +99,10 @@ class JavaScriptMinifierTest extends PHPUnit_Framework_TestCase { // FIXME: This is invalid, but currently tolerated [ "*/", "*/", false ], + // Cover failure case of incomplete char class in regexp (T75556) + // FIXME: This is invalid, but currently tolerated + [ "/a[b/.test", "/a[b/.test", false ], + // Cover failure case of incomplete string at end of file (T75556) // FIXME: This is invalid, but currently tolerated [ "'a", "'a", false ], @@ -148,9 +165,6 @@ class JavaScriptMinifierTest extends PHPUnit_Framework_TestCase { ], [ "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ], - // newline insertion after 1000 chars: break after the "++", not before - [ str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ], - // Unicode letter characters should pass through ok in identifiers (T33187) [ "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ], @@ -186,9 +200,9 @@ class JavaScriptMinifierTest extends PHPUnit_Framework_TestCase { // JSMin+'s parser will throw an exception if output is not valid JS. // suppression of warnings needed for stupid crap if ( $expectedValid ) { - MediaWiki\suppressWarnings(); + Wikimedia\suppressWarnings(); $parser = new JSParser(); - MediaWiki\restoreWarnings(); + Wikimedia\restoreWarnings(); $parser->parse( $minified, 'minify-test.js', 1 ); } @@ -199,40 +213,154 @@ class JavaScriptMinifierTest extends PHPUnit_Framework_TestCase { ); } - public static function provideExponentLineBreaking() { + public static function provideLineBreaker() { return [ [ - // This one gets interpreted all together by the prior code; - // no break at the 'E' happens. - '1.23456789E55', + // Regression tests for T34548. + // Must not break between 'E' and '+'. + 'var name = 1.23456789E55;', + [ + 'var', + 'name', + '=', + '1.23456789E55', + ';', + ], ], [ - // This one breaks under the bad code; splits between 'E' and '+' - '1.23456789E+5', + 'var name = 1.23456789E+5;', + [ + 'var', + 'name', + '=', + '1.23456789E+5', + ';', + ], ], [ - // This one breaks under the bad code; splits between 'E' and '-' - '1.23456789E-5', + 'var name = 1.23456789E-5;', + [ + 'var', + 'name', + '=', + '1.23456789E-5', + ';', + ], + ], + [ + // Must not break before '++' + 'if(x++);', + [ + 'if', + '(', + 'x++', + ')', + ';', + ], + ], + [ + // Regression test for T201606. + // Must not break between 'return' and Expression. + // Was caused by bad state after '{}' in property value. + <<assertEquals( $expected, $minified, "Line breaks must not occur in middle of exponent" ); + public function testLineBreaker( $code, array $expectedLines ) { + $this->setMaxLineLength( 1 ); + $actual = JavaScriptMinifier::minify( $code ); + $this->assertEquals( + array_merge( [ '' ], $expectedLines ), + explode( "\n", $actual ) + ); } }