jenkins-bot has submitted this change and it was merged. Change subject: Add a structure test to validate all extension.json files ......................................................................
Add a structure test to validate all extension.json files Adds a structure test to validate that all loaded extension.json and skin.json files validate against the schema at docs/extension.schema.json. Bug: T128307 Change-Id: I2fd1caaa50c288821ab6847dc29d60e6554d9df5 --- A tests/phpunit/structure/ExtensionJsonValidationTest.php 1 file changed, 93 insertions(+), 0 deletions(-) Approvals: Legoktm: Looks good to me, but someone else must approve Jforrester: Looks good to me, approved jenkins-bot: Verified diff --git a/tests/phpunit/structure/ExtensionJsonValidationTest.php b/tests/phpunit/structure/ExtensionJsonValidationTest.php new file mode 100644 index 0000000..275c0d1 --- /dev/null +++ b/tests/phpunit/structure/ExtensionJsonValidationTest.php @@ -0,0 +1,93 @@ +<?php +/** + * 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. + * http://www.gnu.org/copyleft/gpl.html + */ + +/** + * Validates all loaded extensions and skins using the ExtensionRegistry + * against the extension.json schema in the docs/ folder. + */ +class ExtensionJsonValidationTest extends PHPUnit_Framework_TestCase { + + public function setUp() { + parent::setUp(); + if ( !class_exists( 'JsonSchema\Uri\UriRetriever' ) ) { + $this->markTestSkipped( + 'The JsonSchema library cannot be found,' . + ' please install it through composer to run extension.json validation tests.' + ); + } + + if ( !ExtensionRegistry::getInstance()->getAllThings() ) { + $this->markTestSkipped( + 'There are no extensions or skins loaded via the ExtensionRegistry' + ); + } + } + + public static function providePassesValidation() { + $values = []; + foreach ( ExtensionRegistry::getInstance()->getAllThings() as $thing ) { + $values[] = [ $thing['path'] ]; + } + + return $values; + } + + /** + * @dataProvider providePassesValidation + * @param string $path Path to thing's json file + */ + public function testPassesValidation( $path ) { + $data = json_decode( file_get_contents( $path ) ); + $this->assertInstanceOf( 'stdClass', $data, "$path is not valid JSON" ); + + $this->assertObjectHasAttribute( 'manifest_version', $data, + "$path does not have manifest_version set." ); + $version = $data->manifest_version; + if ( $version !== ExtensionRegistry::MANIFEST_VERSION ) { + $schemaPath = __DIR__ . "/../../../docs/extension.schema.v$version.json"; + } else { + $schemaPath = __DIR__ . '/../../../docs/extension.schema.json'; + } + + // Not too old + $this->assertTrue( + $version >= ExtensionRegistry::OLDEST_MANIFEST_VERSION, + "$path is using a non-supported schema version" + ); + // Not too new + $this->assertTrue( + $version <= ExtensionRegistry::MANIFEST_VERSION, + "$path is using a non-supported schema version" + ); + $retriever = new JsonSchema\Uri\UriRetriever(); + $schema = $retriever->retrieve( 'file://' . $schemaPath ); + + $validator = new JsonSchema\Validator(); + $validator->check( $data, $schema ); + if ( $validator->isValid() ) { + // All good. + $this->assertTrue( true ); + } else { + $out = "$path did pass validation.\n"; + foreach ( $validator->getErrors() as $error ) { + $out .= "[{$error['property']}] {$error['message']}\n"; + } + $this->assertTrue( false, $out ); + } + } +} -- To view, visit https://gerrit.wikimedia.org/r/273751 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I2fd1caaa50c288821ab6847dc29d60e6554d9df5 Gerrit-PatchSet: 3 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Legoktm <[email protected]> Gerrit-Reviewer: Florianschmidtwelzow <[email protected]> Gerrit-Reviewer: JanZerebecki <[email protected]> Gerrit-Reviewer: Jforrester <[email protected]> Gerrit-Reviewer: Legoktm <[email protected]> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
