From febb1aefe07d36851c2d5e8c70f2702af7c90399 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Wed, 16 May 2018 17:56:26 +0200 Subject: [PATCH] resourceloader: Allow style-only modules to have deprecation warnings The deprecation warning for the module 'mediawiki.ui' (used e.g. on Special:UserLogin) is now actually shown. Change-Id: If35a106c77622dbf7e8b5628fbea28f9e7ffd76d --- .../ResourceLoaderClientHtml.php | 17 ++++++++++++++-- .../resourceloader/ResourceLoaderModule.php | 2 +- .../ResourceLoaderClientHtmlTest.php | 20 +++++++++++++++++-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/includes/resourceloader/ResourceLoaderClientHtml.php b/includes/resourceloader/ResourceLoaderClientHtml.php index d0a9c4248a..80825ffe3e 100644 --- a/includes/resourceloader/ResourceLoaderClientHtml.php +++ b/includes/resourceloader/ResourceLoaderClientHtml.php @@ -139,7 +139,8 @@ class ResourceLoaderClientHtml { 'styles' => [], 'general' => [], ], - + // Deprecations for style-only modules + 'styledeprecations' => [], ]; foreach ( $this->modules as $name ) { @@ -204,6 +205,10 @@ class ResourceLoaderClientHtml { $data['styles'][] = $name; } } + $deprecation = $module->getDeprecationInformation(); + if ( $deprecation ) { + $data['styledeprecations'][] = $deprecation; + } } foreach ( $this->moduleScripts as $name ) { @@ -307,7 +312,15 @@ class ResourceLoaderClientHtml { ); } - // External stylesheets + // Deprecations for only=styles modules + if ( $data['styledeprecations'] ) { + $chunks[] = ResourceLoader::makeInlineScript( + implode( '', $data['styledeprecations'] ), + $nonce + ); + } + + // External stylesheets (only=styles) if ( $data['styles'] ) { $chunks[] = $this->getLoad( $data['styles'], diff --git a/includes/resourceloader/ResourceLoaderModule.php b/includes/resourceloader/ResourceLoaderModule.php index 6d1529b11a..7351cb3ef5 100644 --- a/includes/resourceloader/ResourceLoaderModule.php +++ b/includes/resourceloader/ResourceLoaderModule.php @@ -139,7 +139,7 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface { * * @return string JavaScript code */ - protected function getDeprecationInformation() { + public function getDeprecationInformation() { $deprecationInfo = $this->deprecated; if ( $deprecationInfo ) { $name = $this->getName(); diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php index e763a19466..829a2195db 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php @@ -72,6 +72,10 @@ class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase { 'shouldEmbed' => true, 'styles' => '.shouldembed{}', ], + 'test.styles.deprecated' => [ + 'type' => ResourceLoaderModule::LOAD_STYLES, + 'deprecated' => 'Deprecation message.', + ], 'test.scripts' => [], 'test.scripts.user' => [ 'group' => 'user' ], @@ -125,6 +129,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase { 'test.styles.private', 'test.styles.pure', 'test.styles.shouldembed', + 'test.styles.deprecated', 'test.unregistered.styles', ] ); $client->setModuleScripts( [ @@ -145,6 +150,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase { 'test.styles.user.empty' => 'ready', 'test.styles.private' => 'ready', 'test.styles.shouldembed' => 'ready', + 'test.styles.deprecated' => 'ready', 'test.scripts' => 'loading', 'test.scripts.user' => 'loading', 'test.scripts.user.empty' => 'ready', @@ -155,6 +161,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase { ], 'styles' => [ 'test.styles.pure', + 'test.styles.deprecated', ], 'scripts' => [ 'test.scripts', @@ -169,6 +176,13 @@ class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase { 'test.user', ], ], + 'styledeprecations' => [ + Xml::encodeJsCall( + 'mw.log.warn', + [ 'This page is using the deprecated ResourceLoader module "test.styles.deprecated". +Deprecation message.' ] + ) + ], ]; $access = TestingAccessWrapper::newFromObject( $client ); @@ -195,6 +209,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase { $client->setModuleStyles( [ 'test.styles.pure', 'test.styles.private', + 'test.styles.deprecated', ] ); $client->setModuleScripts( [ 'test.scripts', @@ -207,12 +222,13 @@ class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase { $expected = '' . "\n" . '' . "\n" - . '' . "\n" + . '' . "\n" . '' . "\n" . ''; // phpcs:enable -- 2.20.1