Pmiazga has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/347523 )
Change subject: Sanitize gadget name
......................................................................
Sanitize gadget name
MediaWikiGadgetsDefinition does some basic gadget name sanitization
and we have to do the same when checking "is gadget enabled for user"
Changes:
- sanitize gadget name same way as
MediaWikiGadgetsDefinitionRepo::newFromDefinition() does.
- add try{} catch() when loading gadget as getGadget might throw an
exception
Bug: T160081
Change-Id: Ia7a57e9dcfa3b25129d6d2bf75795372fad2b251
---
M includes/PopupsGadgetsIntegration.php
M tests/phpunit/PopupsGadgetsIntegrationTest.php
2 files changed, 65 insertions(+), 2 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Popups
refs/changes/23/347523/1
diff --git a/includes/PopupsGadgetsIntegration.php
b/includes/PopupsGadgetsIntegration.php
index addc0d3..763999d 100644
--- a/includes/PopupsGadgetsIntegration.php
+++ b/includes/PopupsGadgetsIntegration.php
@@ -51,9 +51,19 @@
*/
public function __construct( Config $config , ExtensionRegistry
$extensionRegistry ) {
$this->extensionRegistry = $extensionRegistry;
- $this->navPopupsGadgetName = $config->get(
self::CONFIG_NAVIGATION_POPUPS_NAME );
+ $this->navPopupsGadgetName = $this->sanitizeGadgetName(
+ $config->get( self::CONFIG_NAVIGATION_POPUPS_NAME ) );
}
+ /**
+ * Sanitize gadget name
+ *
+ * @param $gadgetName
+ * @return string
+ */
+ private function sanitizeGadgetName( $gadgetName ) {
+ return str_replace( ' ', '_', trim( $gadgetName ) );
+ }
/**
* @return bool
*/
@@ -73,7 +83,12 @@
$gadgetsRepo = \GadgetRepo::singleton();
$match = array_search( $this->navPopupsGadgetName,
$gadgetsRepo->getGadgetIds() );
if ( $match !== false ) {
- return $gadgetsRepo->getGadget(
$this->navPopupsGadgetName )->isEnabled( $user );
+ try {
+ return $gadgetsRepo->getGadget(
$this->navPopupsGadgetName )
+ ->isEnabled( $user );
+ } catch ( \InvalidArgumentException $e ) {
+ return false;
+ }
}
}
return false;
diff --git a/tests/phpunit/PopupsGadgetsIntegrationTest.php
b/tests/phpunit/PopupsGadgetsIntegrationTest.php
index c93fd68..45d96f6 100644
--- a/tests/phpunit/PopupsGadgetsIntegrationTest.php
+++ b/tests/phpunit/PopupsGadgetsIntegrationTest.php
@@ -145,6 +145,54 @@
}
/**
+ * Test the edge case when GadgetsRepo::getGadget throws an exxception
+ * @covers ::conflictsWithNavPopupsGadget
+ */
+ public function testConflictsWithNavPopupsGadgetWhenGadgetNotExists() {
+ $this->checkRequiredDependencies();
+
+ $user = $this->getTestUser()->getUser();
+
+ $gadgetRepoMock = $this->getMock( GadgetRepo::class,
+ [ 'getGadgetIds', 'getGadget' ] );
+
+ $gadgetRepoMock->expects( $this->once() )
+ ->method( 'getGadgetids' )
+ ->willReturn( [ self::NAV_POPUPS_GADGET_NAME ]
);
+
+ $gadgetRepoMock->expects( $this->once() )
+ ->method( 'getGadget' )
+ ->with( self::NAV_POPUPS_GADGET_NAME )
+ ->willThrowException( new
InvalidArgumentException() );
+
+ $this->executeConflictsWithNavPopupsGadgetSafeCheck(
$user, $gadgetRepoMock,
+ self::GADGET_DISABLED );
+ }
+
+ /**
+ * @covers ::sanitizeGadgetName
+ * @dataProvider provideGadgetNamesWithSanitizedVersion
+ */
+ public function testConflictsWithNavPopupsGadgetNameSanitization(
$name, $sanitized ) {
+ $this->checkRequiredDependencies();
+ $integration = new PopupsGadgetsIntegration(
$this->getConfigMock(),
+ $this->getExtensionRegistryMock( true ) );
+
+ $integrationTest =
TestingAccessWrapper::newFromObject($integration);
+ $this->assertEquals( $sanitized,
$integrationTest->sanitizeGadgetName( $name ) );
+ }
+
+ /**
+ * @return array
+ */
+ public function provideGadgetNamesWithSanitizedVersion() {
+ return [
+ [ ' Popups ', 'Popups'],
+ [ 'Navigation_popups-API', 'Navigation_popups-API' ],
+ [ 'Navigation popups ', 'Navigation_popups']
+ ];
+ }
+ /**
* Execute test and restore orGadgetRepo
*
* @param $user
--
To view, visit https://gerrit.wikimedia.org/r/347523
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia7a57e9dcfa3b25129d6d2bf75795372fad2b251
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Popups
Gerrit-Branch: master
Gerrit-Owner: Pmiazga <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits