From eb6b4d8e7e6d9c63b3b987d852f631297a6e0d7f Mon Sep 17 00:00:00 2001 From: MGChecker Date: Tue, 28 Aug 2018 04:39:16 +0200 Subject: [PATCH] registration: Add ability to check if a specific extension version is loaded As it's quite common that extensions and skins interact with each other, it's useful to have a simple way to check if an extension version satisfies a given constraint, as extensions change over time. Bug: T202955 Change-Id: I19f9713caf89d647072a2bd7d598e739be383f4a --- includes/registration/ExtensionRegistry.php | 20 +++++++++++++++++-- .../data/registration/good_with_version.json | 13 ++++++++++++ .../registration/ExtensionRegistryTest.php | 19 ++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/data/registration/good_with_version.json diff --git a/includes/registration/ExtensionRegistry.php b/includes/registration/ExtensionRegistry.php index d21ae412f1..8429afa843 100644 --- a/includes/registration/ExtensionRegistry.php +++ b/includes/registration/ExtensionRegistry.php @@ -1,5 +1,7 @@ loaded[$name] ); + public function isLoaded( $name, $constraint = '*' ) { + $isLoaded = isset( $this->loaded[$name] ); + if ( $constraint === '*' || !$isLoaded ) { + return $isLoaded; + } + // if a specific constraint is requested, but no version is set, throw an exception + if ( !isset( $this->loaded[$name]['version'] ) ) { + $msg = "{$name} does not expose its version, but an extension or a skin" + . " requires: {$constraint}."; + throw new LogicException( $msg ); + } + + return SemVer::satisfies( $this->loaded[$name]['version'], $constraint ); } /** diff --git a/tests/phpunit/data/registration/good_with_version.json b/tests/phpunit/data/registration/good_with_version.json new file mode 100644 index 0000000000..586ba7ccb8 --- /dev/null +++ b/tests/phpunit/data/registration/good_with_version.json @@ -0,0 +1,13 @@ +{ + "name": "FooBar", + "version": "1.2.3", + "attributes": { + "FooBar": { + "Attr": [ "test" ] + }, + "NotLoaded": { + "Attr": [ "test2" ] + } + }, + "manifest_version": 2 +} diff --git a/tests/phpunit/includes/registration/ExtensionRegistryTest.php b/tests/phpunit/includes/registration/ExtensionRegistryTest.php index 7120a91a97..1baa79c160 100644 --- a/tests/phpunit/includes/registration/ExtensionRegistryTest.php +++ b/tests/phpunit/includes/registration/ExtensionRegistryTest.php @@ -57,10 +57,29 @@ class ExtensionRegistryTest extends MediaWikiTestCase { $registry->loadFromQueue(); $this->assertArrayHasKey( 'FooBar', $registry->getAllThings() ); $this->assertTrue( $registry->isLoaded( 'FooBar' ) ); + $this->assertTrue( $registry->isLoaded( 'FooBar', '*' ) ); $this->assertSame( [ 'test' ], $registry->getAttribute( 'FooBarAttr' ) ); $this->assertSame( [], $registry->getAttribute( 'NotLoadedAttr' ) ); } + public function testLoadFromQueueWithConstraintWithVersion() { + $registry = new ExtensionRegistry(); + $registry->queue( "{$this->dataDir}/good_with_version.json" ); + $registry->loadFromQueue(); + $this->assertTrue( $registry->isLoaded( 'FooBar', '>= 1.2.0' ) ); + $this->assertFalse( $registry->isLoaded( 'FooBar', '^1.3.0' ) ); + } + + /** + * @expectedException LogicException + */ + public function testLoadFromQueueWithConstraintWithoutVersion() { + $registry = new ExtensionRegistry(); + $registry->queue( "{$this->dataDir}/good.json" ); + $registry->loadFromQueue(); + $registry->isLoaded( 'FooBar', '>= 1.2.0' ); + } + /** * @expectedException PHPUnit_Framework_Error */ -- 2.20.1