2016-02-28 09:19:14 +00:00
|
|
|
<?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
|
|
|
|
|
*/
|
|
|
|
|
|
2016-09-28 02:56:07 +00:00
|
|
|
use Composer\Spdx\SpdxLicenses;
|
|
|
|
|
use JsonSchema\Validator;
|
|
|
|
|
|
2016-02-28 09:19:14 +00:00
|
|
|
/**
|
|
|
|
|
* 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();
|
2016-09-28 02:56:07 +00:00
|
|
|
if ( !class_exists( Validator::class ) ) {
|
2016-02-28 09:19:14 +00:00
|
|
|
$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"
|
|
|
|
|
);
|
|
|
|
|
|
2016-09-28 02:56:07 +00:00
|
|
|
$licenseError = false;
|
|
|
|
|
if ( class_exists( SpdxLicenses::class ) && isset( $data->{'license-name'} )
|
|
|
|
|
// Check if it's a string, if not, schema validation will display an error
|
|
|
|
|
&& is_string( $data->{'license-name'} )
|
|
|
|
|
) {
|
|
|
|
|
$licenses = new SpdxLicenses();
|
|
|
|
|
$valid = $licenses->validate( $data->{'license-name'} );
|
|
|
|
|
if ( !$valid ) {
|
|
|
|
|
$licenseError = '[license-name] Invalid SPDX license identifier, '
|
|
|
|
|
. 'see <https://spdx.org/licenses/>';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$validator = new Validator;
|
Upgrade justinrainbow/json-schema to ~3.0
The release between 1.6.1 and 3.0.0 has a huge amount of code
maintenance changes, as well as internal optimization and some
"visible" changes (as well as the one mentioned in the linked
task).
However, it's a version jump over 2 major versions, which is,
by it's definition a major change ;). Nonetheless, the (for us)
important api has changed marginally: Instead of using the
JsonSchema\Uri\UriRetriever class to retrieve the schema, we now use
the $ref keyword to reference the json schema file (which also is
an internal optimization). In this way, we let the json-schema library
decide, how to resolve a ref (and the schema) instead of relying
on the UriRetriever api to be public and stable.
The versions also include various bug fixes (which, as far as I know,
doesn't apply to us).
I tested this change with various combinations of valid and invalid
extension.json schemas (version 2 as well as version 3). Given that
there were no major changes to the schema interpretation itself, and
the good test coverage of the library, there shouldn't be a high risk
because of this change.
The full list of changes can be found at:
https://github.com/justinrainbow/json-schema/compare/1.6.1...3.0.0
as well as the changelogs of the single versions:
https://github.com/justinrainbow/json-schema/releases
Bug: T141281
Depends-On: I5687286da9f7fa2bb2b84699fa43ab3c2547fe03
Change-Id: Ie37e2ebc48684783abf8d99d2f775ee6a5988da7
2016-08-15 17:36:00 +00:00
|
|
|
$validator->check( $data, (object) [ '$ref' => 'file://' . $schemaPath ] );
|
2016-09-28 02:56:07 +00:00
|
|
|
if ( $validator->isValid() && !$licenseError ) {
|
2016-02-28 09:19:14 +00:00
|
|
|
// All good.
|
|
|
|
|
$this->assertTrue( true );
|
|
|
|
|
} else {
|
|
|
|
|
$out = "$path did pass validation.\n";
|
|
|
|
|
foreach ( $validator->getErrors() as $error ) {
|
|
|
|
|
$out .= "[{$error['property']}] {$error['message']}\n";
|
|
|
|
|
}
|
2016-09-28 02:56:07 +00:00
|
|
|
if ( $licenseError ) {
|
|
|
|
|
$out .= "$licenseError\n";
|
|
|
|
|
}
|
2016-02-28 09:19:14 +00:00
|
|
|
$this->assertTrue( false, $out );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|