resourceloader: Include lessVars in FileModule definition summary
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 27 Jul 2017 01:53:37 +0000 (18:53 -0700)
committerKrinkle <krinklemail@gmail.com>
Thu, 27 Jul 2017 03:02:09 +0000 (03:02 +0000)
This already worked as expected for any module that uses the new
enableModuleContentVersion model, but for the majority of file modules
this is not yet the case for performance reasons. As such, make
sure lessVars are included in our manual tracking.

Include it conditionally to avoid changing the array for other modules,
which would needlessly invalidate their cache.

Bug: T171809
Change-Id: Ib250068e0ecfc29a09ca33c23bef901ee0482bf2

includes/resourceloader/ResourceLoaderFileModule.php
tests/common/TestsAutoLoader.php
tests/phpunit/ResourceLoaderTestCase.php
tests/phpunit/includes/resourceloader/ResourceLoaderFileModuleTest.php

index 79b8e79..4675191 100644 (file)
@@ -580,6 +580,12 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        'fileHashes' => $this->getFileHashes( $context ),
                        'messageBlob' => $this->getMessageBlob( $context ),
                ];
                        'fileHashes' => $this->getFileHashes( $context ),
                        'messageBlob' => $this->getMessageBlob( $context ),
                ];
+
+               $lessVars = $this->getLessVars( $context );
+               if ( $lessVars ) {
+                       $summary[] = [ 'lessVars' => $lessVars ];
+               }
+
                return $summary;
        }
 
                return $summary;
        }
 
index 3de3ba7..9b9ea6d 100644 (file)
@@ -56,6 +56,7 @@ $wgAutoloadClasses += [
        'MediaWikiLangTestCase' => "$testDir/phpunit/MediaWikiLangTestCase.php",
        'ResourceLoaderTestCase' => "$testDir/phpunit/ResourceLoaderTestCase.php",
        'ResourceLoaderTestModule' => "$testDir/phpunit/ResourceLoaderTestCase.php",
        'MediaWikiLangTestCase' => "$testDir/phpunit/MediaWikiLangTestCase.php",
        'ResourceLoaderTestCase' => "$testDir/phpunit/ResourceLoaderTestCase.php",
        'ResourceLoaderTestModule' => "$testDir/phpunit/ResourceLoaderTestCase.php",
+       'ResourceLoaderFileTestModule' => "$testDir/phpunit/ResourceLoaderTestCase.php",
        'ResourceLoaderFileModuleTestModule' => "$testDir/phpunit/ResourceLoaderTestCase.php",
        'EmptyResourceLoader' => "$testDir/phpunit/ResourceLoaderTestCase.php",
        'TestUser' => "$testDir/phpunit/includes/TestUser.php",
        'ResourceLoaderFileModuleTestModule' => "$testDir/phpunit/ResourceLoaderTestCase.php",
        'EmptyResourceLoader' => "$testDir/phpunit/ResourceLoaderTestCase.php",
        'TestUser' => "$testDir/phpunit/includes/TestUser.php",
index d8f89fb..f75cc22 100644 (file)
@@ -153,6 +153,22 @@ class ResourceLoaderTestModule extends ResourceLoaderModule {
        }
 }
 
        }
 }
 
+class ResourceLoaderFileTestModule extends ResourceLoaderFileModule {
+       protected $lessVars = [];
+
+       public function __construct( $options = [], $test = [] ) {
+               parent::__construct( $options );
+
+               foreach ( $test as $key => $value ) {
+                       $this->$key = $value;
+               }
+       }
+
+       public function getLessVars( ResourceLoaderContext $context ) {
+               return $this->lessVars;
+       }
+}
+
 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
 }
 
 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
 }
 
index ded56e9..e82bab7 100644 (file)
@@ -331,4 +331,23 @@ class ResourceLoaderFileModuleTest extends ResourceLoaderTestCase {
                        'Leading BOM removed when concatenating files'
                );
        }
                        'Leading BOM removed when concatenating files'
                );
        }
+
+       /**
+        * @covers ResourceLoaderFileModule::getDefinitionSummary
+        */
+       public function testGetVersionHash() {
+               $context = $this->getResourceLoaderContext();
+
+               // Less variables
+               $module = new ResourceLoaderFileTestModule();
+               $version = $module->getVersionHash( $context );
+               $module = new ResourceLoaderFileTestModule( [], [
+                       'lessVars' => [ 'key' => 'value' ],
+               ] );
+               $this->assertNotEquals(
+                       $version,
+                       $module->getVersionHash( $context ),
+                       'Using less variables is significant'
+               );
+       }
 }
 }