From 08b2cf66387856c623ff99bca4a83db2b7d8c9e6 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Fri, 9 Feb 2018 23:37:22 -0800 Subject: [PATCH 1/1] Add tests for ExtensionJsonValidator Change-Id: I883a502fc3ed6cd7b8651d9e5e78dba7177ead9c --- .../registration/ExtensionJsonValidator.php | 1 + tests/phpunit/data/registration/bad_spdx.json | 6 ++ tests/phpunit/data/registration/good.json | 4 + tests/phpunit/data/registration/invalid.json | 5 ++ .../registration/newer_manifest_version.json | 4 + .../registration/no_manifest_version.json | 3 + tests/phpunit/data/registration/notjson.txt | 1 + .../registration/old_manifest_version.json | 4 + .../ExtensionJsonValidatorTest.php | 84 +++++++++++++++++++ 9 files changed, 112 insertions(+) create mode 100644 tests/phpunit/data/registration/bad_spdx.json create mode 100644 tests/phpunit/data/registration/good.json create mode 100644 tests/phpunit/data/registration/invalid.json create mode 100644 tests/phpunit/data/registration/newer_manifest_version.json create mode 100644 tests/phpunit/data/registration/no_manifest_version.json create mode 100644 tests/phpunit/data/registration/notjson.txt create mode 100644 tests/phpunit/data/registration/old_manifest_version.json create mode 100644 tests/phpunit/includes/registration/ExtensionJsonValidatorTest.php diff --git a/includes/registration/ExtensionJsonValidator.php b/includes/registration/ExtensionJsonValidator.php index c8e5e1906b..7e3afaa803 100644 --- a/includes/registration/ExtensionJsonValidator.php +++ b/includes/registration/ExtensionJsonValidator.php @@ -40,6 +40,7 @@ class ExtensionJsonValidator { } /** + * @codeCoverageIgnore * @return bool */ public function checkDependencies() { diff --git a/tests/phpunit/data/registration/bad_spdx.json b/tests/phpunit/data/registration/bad_spdx.json new file mode 100644 index 0000000000..383ab047e4 --- /dev/null +++ b/tests/phpunit/data/registration/bad_spdx.json @@ -0,0 +1,6 @@ +{ + "name": "FooBar", + "license-name": "not a license identifier", + "manifest_version": 1 +} + diff --git a/tests/phpunit/data/registration/good.json b/tests/phpunit/data/registration/good.json new file mode 100644 index 0000000000..ad16c5e452 --- /dev/null +++ b/tests/phpunit/data/registration/good.json @@ -0,0 +1,4 @@ +{ + "name": "FooBar", + "manifest_version": 1 +} diff --git a/tests/phpunit/data/registration/invalid.json b/tests/phpunit/data/registration/invalid.json new file mode 100644 index 0000000000..4d1fa5895f --- /dev/null +++ b/tests/phpunit/data/registration/invalid.json @@ -0,0 +1,5 @@ +{ + "name": "FooBar", + "license-name": [ "array" ], + "manifest_version": 1 +} diff --git a/tests/phpunit/data/registration/newer_manifest_version.json b/tests/phpunit/data/registration/newer_manifest_version.json new file mode 100644 index 0000000000..29c668ee74 --- /dev/null +++ b/tests/phpunit/data/registration/newer_manifest_version.json @@ -0,0 +1,4 @@ +{ + "name": "FooBar", + "manifest_version": 999999 +} diff --git a/tests/phpunit/data/registration/no_manifest_version.json b/tests/phpunit/data/registration/no_manifest_version.json new file mode 100644 index 0000000000..1a6119f7c7 --- /dev/null +++ b/tests/phpunit/data/registration/no_manifest_version.json @@ -0,0 +1,3 @@ +{ + "name": "FooBar" +} diff --git a/tests/phpunit/data/registration/notjson.txt b/tests/phpunit/data/registration/notjson.txt new file mode 100644 index 0000000000..d47b460737 --- /dev/null +++ b/tests/phpunit/data/registration/notjson.txt @@ -0,0 +1 @@ +This is definitely not JSON. diff --git a/tests/phpunit/data/registration/old_manifest_version.json b/tests/phpunit/data/registration/old_manifest_version.json new file mode 100644 index 0000000000..f50faa1b85 --- /dev/null +++ b/tests/phpunit/data/registration/old_manifest_version.json @@ -0,0 +1,4 @@ +{ + "name": "FooBar", + "manifest_version": -2 +} diff --git a/tests/phpunit/includes/registration/ExtensionJsonValidatorTest.php b/tests/phpunit/includes/registration/ExtensionJsonValidatorTest.php new file mode 100644 index 0000000000..d69ad5973a --- /dev/null +++ b/tests/phpunit/includes/registration/ExtensionJsonValidatorTest.php @@ -0,0 +1,84 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +/** + * @covers ExtensionJsonValidator + */ +class ExtensionJsonValidatorTest extends MediaWikiTestCase { + + /** + * @dataProvider provideValidate + */ + public function testValidate( $file, $expected ) { + // If a dependency is missing, skip this test. + $validator = new ExtensionJsonValidator( function ( $msg ) { + $this->markTestSkipped( $msg ); + } ); + + if ( is_string( $expected ) ) { + $this->setExpectedException( + ExtensionJsonValidationError::class, + $expected + ); + } + + $dir = __DIR__ . '/../../data/registration/'; + $this->assertSame( + $expected, + $validator->validate( $dir . $file ) + ); + } + + public function provideValidate() { + return [ + [ + 'notjson.txt', + 'notjson.txt is not valid JSON' + ], + [ + 'no_manifest_version.json', + 'no_manifest_version.json does not have manifest_version set.' + ], + [ + 'old_manifest_version.json', + 'old_manifest_version.json is using a non-supported schema version' + ], + [ + 'newer_manifest_version.json', + 'newer_manifest_version.json is using a non-supported schema version' + ], + [ + 'bad_spdx.json', + "bad_spdx.json did not pass validation. +[license-name] Invalid SPDX license identifier, see " + ], + [ + 'invalid.json', + "invalid.json did not pass validation. +[license-name] Array value found, but a string is required" + ], + [ + 'good.json', + true + ], + ]; + } + +} -- 2.20.1