1<?php
2/**
3 * Copyright (C) 2018 Kunal Mehta <legoktm@debian.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21/**
22 * @covers ExtensionJsonValidator
23 */
24class ExtensionJsonValidatorTest extends MediaWikiUnitTestCase {
25
26	/**
27	 * @dataProvider provideValidate
28	 */
29	public function testValidate( $file, $expected ) {
30		// If a dependency is missing, skip this test.
31		$validator = new ExtensionJsonValidator( function ( $msg ) {
32			$this->markTestSkipped( $msg );
33		} );
34
35		if ( is_string( $expected ) ) {
36			$this->expectException( ExtensionJsonValidationError::class );
37			$this->expectExceptionMessage( $expected );
38		}
39
40		$dir = __DIR__ . '/../../../data/registration/';
41		$this->assertSame(
42			$expected,
43			$validator->validate( $dir . $file )
44		);
45	}
46
47	public function provideValidate() {
48		return [
49			[
50				'notjson.txt',
51				'notjson.txt is not valid JSON'
52			],
53			[
54				'duplicate_keys.json',
55				'Duplicate key: name'
56			],
57			[
58				'no_manifest_version.json',
59				'no_manifest_version.json does not have manifest_version set.'
60			],
61			[
62				'old_manifest_version.json',
63				'old_manifest_version.json is using a non-supported schema version'
64			],
65			[
66				'newer_manifest_version.json',
67				'newer_manifest_version.json is using a non-supported schema version'
68			],
69			[
70				'bad_spdx.json',
71				"bad_spdx.json did not pass validation.
72[license-name] Invalid SPDX license identifier, see <https://spdx.org/licenses/>"
73			],
74			[
75				'invalid.json',
76				"invalid.json did not pass validation.
77[license-name] Array value found, but a string is required"
78			],
79			[
80				'good.json',
81				true
82			],
83			[
84				'good_with_license_expressions.json',
85				true
86			],
87			[
88				'bad_url.json', 'bad_url.json did not pass validation.
89[url] Should use HTTPS for www.mediawiki.org URLs'
90			],
91			[
92				'bad_url2.json', 'bad_url2.json did not pass validation.
93[url] Should use www.mediawiki.org domain
94[url] Should use HTTPS for www.mediawiki.org URLs'
95			]
96		];
97	}
98
99}
100