Merge "resourceloader: Include global LESS variables in LESS cache key"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderTest.php
index f45f8ae..71a6339 100644 (file)
@@ -8,9 +8,7 @@ class ResourceLoaderTest extends ResourceLoaderTestCase {
                parent::setUp();
 
                $this->setMwGlobals( [
-                       'wgResourceLoaderLESSImportPaths' => [
-                               dirname( dirname( __DIR__ ) ) . '/data/less/common',
-                       ],
+                       'wgResourceLoaderLESSImportPaths' => [],
                        'wgResourceLoaderLESSVars' => [
                                'foo'  => '2px',
                                'Foo' => '#eeeeee',
@@ -86,7 +84,7 @@ class ResourceLoaderTest extends ResourceLoaderTestCase {
         */
        public function testRegisterInvalidName() {
                $resourceLoader = new EmptyResourceLoader();
-               $this->setExpectedException( 'MWException', "name 'test!invalid' is invalid" );
+               $this->setExpectedException( MWException::class, "name 'test!invalid' is invalid" );
                $resourceLoader->register( 'test!invalid', new ResourceLoaderTestModule() );
        }
 
@@ -95,10 +93,26 @@ class ResourceLoaderTest extends ResourceLoaderTestCase {
         */
        public function testRegisterInvalidType() {
                $resourceLoader = new EmptyResourceLoader();
-               $this->setExpectedException( 'MWException', 'ResourceLoader module info type error' );
+               $this->setExpectedException( MWException::class, 'ResourceLoader module info type error' );
                $resourceLoader->register( 'test', new stdClass() );
        }
 
+       /**
+        * @covers ResourceLoader::register
+        */
+       public function testRegisterDuplicate() {
+               $logger = $this->getMockBuilder( Psr\Log\LoggerInterface::class )->getMock();
+               $logger->expects( $this->once() )
+                       ->method( 'warning' );
+               $resourceLoader = new EmptyResourceLoader( null, $logger );
+
+               $module1 = new ResourceLoaderTestModule();
+               $module2 = new ResourceLoaderTestModule();
+               $resourceLoader->register( 'test', $module1 );
+               $resourceLoader->register( 'test', $module2 );
+               $this->assertSame( $module2, $resourceLoader->getModule( 'test' ) );
+       }
+
        /**
         * @covers ResourceLoader::getModuleNames
         */
@@ -248,6 +262,20 @@ class ResourceLoaderTest extends ResourceLoaderTestCase {
                $this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
        }
 
+       /**
+        * @covers ResourceLoader::getLessCompiler
+        */
+       public function testLessImportDirs() {
+               $rl = new EmptyResourceLoader();
+               $lc = $rl->getLessCompiler( $rl->getLessVars() );
+               $basePath = dirname( dirname( __DIR__ ) ) . '/data/less';
+               $lc->SetImportDirs( [
+                        "$basePath/common" => '',
+               ] );
+               $css = $lc->parseFile( "$basePath/module/use-import-dir.less" )->getCss();
+               $this->assertStringEqualsFile( "$basePath/module/styles.css", $css );
+       }
+
        public static function providePackedModules() {
                return [
                        [
@@ -261,7 +289,7 @@ class ResourceLoaderTest extends ResourceLoaderTestCase {
                                'jquery.foo,bar|jquery.ui.baz,quux',
                        ],
                        [
-                               'Regression fixed in r88706 with dotless names',
+                               'Regression fixed in r87497 (7fee86c38e) with dotless names',
                                [ 'foo', 'bar', 'baz' ],
                                'foo,bar,baz',
                        ],
@@ -336,7 +364,9 @@ class ResourceLoaderTest extends ResourceLoaderTestCase {
         */
        public function testAddSourceDupe() {
                $rl = new ResourceLoader;
-               $this->setExpectedException( 'MWException', 'ResourceLoader duplicate source addition error' );
+               $this->setExpectedException(
+                       MWException::class, 'ResourceLoader duplicate source addition error'
+               );
                $rl->addSource( 'foo', 'https://example.org/w/load.php' );
                $rl->addSource( 'foo', 'https://example.com/w/load.php' );
        }
@@ -346,7 +376,7 @@ class ResourceLoaderTest extends ResourceLoaderTestCase {
         */
        public function testAddSourceInvalid() {
                $rl = new ResourceLoader;
-               $this->setExpectedException( 'MWException', 'with no "loadScript" key' );
+               $this->setExpectedException( MWException::class, 'with no "loadScript" key' );
                $rl->addSource( 'foo',  [ 'x' => 'https://example.org/w/load.php' ] );
        }
 
@@ -446,7 +476,7 @@ mw.example();
                ResourceLoader::clearCache();
                $this->setMwGlobals( 'wgResourceLoaderDebug', true );
 
-               $rl = TestingAccessWrapper::newFromClass( 'ResourceLoader' );
+               $rl = TestingAccessWrapper::newFromClass( ResourceLoader::class );
                $this->assertEquals(
                        $case['expected'],
                        $rl->makeLoaderImplementScript(
@@ -465,8 +495,8 @@ mw.example();
         * @covers ResourceLoader::makeLoaderImplementScript
         */
        public function testMakeLoaderImplementScriptInvalid() {
-               $this->setExpectedException( 'MWException', 'Invalid scripts error' );
-               $rl = TestingAccessWrapper::newFromClass( 'ResourceLoader' );
+               $this->setExpectedException( MWException::class, 'Invalid scripts error' );
+               $rl = TestingAccessWrapper::newFromClass( ResourceLoader::class );
                $rl->makeLoaderImplementScript(
                        'test', // name
                        123, // scripts
@@ -566,11 +596,11 @@ mw.example();
                }
        }
 
-       protected function getFailFerryMock() {
+       protected function getFailFerryMock( $getter = 'getScript' ) {
                $mock = $this->getMockBuilder( ResourceLoaderTestModule::class )
-                       ->setMethods( [ 'getScript' ] )
+                       ->setMethods( [ $getter ] )
                        ->getMock();
-               $mock->method( 'getScript' )->will( $this->throwException(
+               $mock->method( $getter )->will( $this->throwException(
                        new Exception( 'Ferry not found' )
                ) );
                return $mock;
@@ -584,6 +614,14 @@ mw.example();
                return $mock;
        }
 
+       protected function getSimpleStyleModuleMock( $styles = '' ) {
+               $mock = $this->getMockBuilder( ResourceLoaderTestModule::class )
+                       ->setMethods( [ 'getStyles' ] )
+                       ->getMock();
+               $mock->method( 'getStyles' )->willReturn( [ '' => $styles ] );
+               return $mock;
+       }
+
        /**
         * @covers ResourceLoader::getCombinedVersion
         */
@@ -740,6 +778,43 @@ mw.example();
                );
        }
 
+       /**
+        * Verify that exceptions in PHP for one module will not break others
+        * (stylesheet response).
+        *
+        * @covers ResourceLoader::makeModuleResponse
+        */
+       public function testMakeModuleResponseErrorCSS() {
+               $modules = [
+                       'foo' => self::getSimpleStyleModuleMock( '.foo{}' ),
+                       'ferry' => self::getFailFerryMock( 'getStyles' ),
+                       'bar' => self::getSimpleStyleModuleMock( '.bar{}' ),
+               ];
+               $rl = new EmptyResourceLoader();
+               $rl->register( $modules );
+               $context = $this->getResourceLoaderContext(
+                       [
+                               'modules' => 'foo|ferry|bar',
+                               'only' => 'styles',
+                               'debug' => 'false',
+                       ],
+                       $rl
+               );
+
+               // Disable log from makeModuleResponse via outputErrorAndLog
+               $this->setLogger( 'exception', new Psr\Log\NullLogger() );
+
+               $response = $rl->makeModuleResponse( $context, $modules );
+               $errors = $rl->getErrors();
+
+               $this->assertCount( 2, $errors );
+               $this->assertRegExp( '/Ferry not found/', $errors[0] );
+               $this->assertRegExp( '/Problem.+\n\s*"ferry":\s*"error"/m', $errors[1] );
+               $this->assertEquals(
+                       '.foo{}.bar{}',
+                       $response
+               );
+       }
        /**
         * Verify that when building the startup module response,
         * an exception from one module class will not break the entire
@@ -753,7 +828,7 @@ mw.example();
                        'foo' => self::getSimpleModuleMock( 'foo();' ),
                        'ferry' => self::getFailFerryMock(),
                        'bar' => self::getSimpleModuleMock( 'bar();' ),
-                       'startup' => [ 'class' => 'ResourceLoaderStartUpModule' ],
+                       'startup' => [ 'class' => ResourceLoaderStartUpModule::class ],
                ] );
                $context = $this->getResourceLoaderContext(
                        [