Merge "Add countUnreadNotifications to WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / structure / ExtensionJsonValidationTest.php
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.
7 *
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.
12 *
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
17 */
18
19 /**
20 * Validates all loaded extensions and skins using the ExtensionRegistry
21 * against the extension.json schema in the docs/ folder.
22 */
23 class ExtensionJsonValidationTest extends PHPUnit_Framework_TestCase {
24
25 public function setUp() {
26 parent::setUp();
27 if ( !class_exists( 'JsonSchema\Uri\UriRetriever' ) ) {
28 $this->markTestSkipped(
29 'The JsonSchema library cannot be found,' .
30 ' please install it through composer to run extension.json validation tests.'
31 );
32 }
33
34 if ( !ExtensionRegistry::getInstance()->getAllThings() ) {
35 $this->markTestSkipped(
36 'There are no extensions or skins loaded via the ExtensionRegistry'
37 );
38 }
39 }
40
41 public static function providePassesValidation() {
42 $values = [];
43 foreach ( ExtensionRegistry::getInstance()->getAllThings() as $thing ) {
44 $values[] = [ $thing['path'] ];
45 }
46
47 return $values;
48 }
49
50 /**
51 * @dataProvider providePassesValidation
52 * @param string $path Path to thing's json file
53 */
54 public function testPassesValidation( $path ) {
55 $data = json_decode( file_get_contents( $path ) );
56 $this->assertInstanceOf( 'stdClass', $data, "$path is not valid JSON" );
57
58 $this->assertObjectHasAttribute( 'manifest_version', $data,
59 "$path does not have manifest_version set." );
60 $version = $data->manifest_version;
61 if ( $version !== ExtensionRegistry::MANIFEST_VERSION ) {
62 $schemaPath = __DIR__ . "/../../../docs/extension.schema.v$version.json";
63 } else {
64 $schemaPath = __DIR__ . '/../../../docs/extension.schema.json';
65 }
66
67 // Not too old
68 $this->assertTrue(
69 $version >= ExtensionRegistry::OLDEST_MANIFEST_VERSION,
70 "$path is using a non-supported schema version"
71 );
72 // Not too new
73 $this->assertTrue(
74 $version <= ExtensionRegistry::MANIFEST_VERSION,
75 "$path is using a non-supported schema version"
76 );
77 $retriever = new JsonSchema\Uri\UriRetriever();
78 $schema = $retriever->retrieve( 'file://' . $schemaPath );
79
80 $validator = new JsonSchema\Validator();
81 $validator->check( $data, $schema );
82 if ( $validator->isValid() ) {
83 // All good.
84 $this->assertTrue( true );
85 } else {
86 $out = "$path did pass validation.\n";
87 foreach ( $validator->getErrors() as $error ) {
88 $out .= "[{$error['property']}] {$error['message']}\n";
89 }
90 $this->assertTrue( false, $out );
91 }
92 }
93 }