Make cache object mandatory for MessageCache
[mediawiki.git] / tests / phpunit / structure / ExtensionJsonValidationTest.php
blobe11fd8a548a98e3abd68be48a640e20e4c3f12cd
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 use Composer\Spdx\SpdxLicenses;
20 use JsonSchema\Validator;
22 /**
23 * Validates all loaded extensions and skins using the ExtensionRegistry
24 * against the extension.json schema in the docs/ folder.
26 class ExtensionJsonValidationTest extends PHPUnit_Framework_TestCase {
28 public function setUp() {
29 parent::setUp();
30 if ( !class_exists( Validator::class ) ) {
31 $this->markTestSkipped(
32 'The JsonSchema library cannot be found,' .
33 ' please install it through composer to run extension.json validation tests.'
37 if ( !ExtensionRegistry::getInstance()->getAllThings() ) {
38 $this->markTestSkipped(
39 'There are no extensions or skins loaded via the ExtensionRegistry'
44 public static function providePassesValidation() {
45 $values = [];
46 foreach ( ExtensionRegistry::getInstance()->getAllThings() as $thing ) {
47 $values[] = [ $thing['path'] ];
50 return $values;
53 /**
54 * @dataProvider providePassesValidation
55 * @param string $path Path to thing's json file
57 public function testPassesValidation( $path ) {
58 $data = json_decode( file_get_contents( $path ) );
59 $this->assertInstanceOf( 'stdClass', $data, "$path is not valid JSON" );
61 $this->assertObjectHasAttribute( 'manifest_version', $data,
62 "$path does not have manifest_version set." );
63 $version = $data->manifest_version;
64 if ( $version !== ExtensionRegistry::MANIFEST_VERSION ) {
65 $schemaPath = __DIR__ . "/../../../docs/extension.schema.v$version.json";
66 } else {
67 $schemaPath = __DIR__ . '/../../../docs/extension.schema.json';
70 // Not too old
71 $this->assertTrue(
72 $version >= ExtensionRegistry::OLDEST_MANIFEST_VERSION,
73 "$path is using a non-supported schema version"
75 // Not too new
76 $this->assertTrue(
77 $version <= ExtensionRegistry::MANIFEST_VERSION,
78 "$path is using a non-supported schema version"
81 $licenseError = false;
82 if ( class_exists( SpdxLicenses::class ) && isset( $data->{'license-name'} )
83 // Check if it's a string, if not, schema validation will display an error
84 && is_string( $data->{'license-name'} )
85 ) {
86 $licenses = new SpdxLicenses();
87 $valid = $licenses->validate( $data->{'license-name'} );
88 if ( !$valid ) {
89 $licenseError = '[license-name] Invalid SPDX license identifier, '
90 . 'see <https://spdx.org/licenses/>';
94 $validator = new Validator;
95 $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
96 if ( $validator->isValid() && !$licenseError ) {
97 // All good.
98 $this->assertTrue( true );
99 } else {
100 $out = "$path did pass validation.\n";
101 foreach ( $validator->getErrors() as $error ) {
102 $out .= "[{$error['property']}] {$error['message']}\n";
104 if ( $licenseError ) {
105 $out .= "$licenseError\n";
107 $this->assertTrue( false, $out );