Add tests for ExtensionJsonValidator
authorKunal Mehta <legoktm@member.fsf.org>
Sat, 10 Feb 2018 07:37:22 +0000 (23:37 -0800)
committerKunal Mehta <legoktm@member.fsf.org>
Sat, 10 Feb 2018 07:37:22 +0000 (23:37 -0800)
Change-Id: I883a502fc3ed6cd7b8651d9e5e78dba7177ead9c

includes/registration/ExtensionJsonValidator.php
tests/phpunit/data/registration/bad_spdx.json [new file with mode: 0644]
tests/phpunit/data/registration/good.json [new file with mode: 0644]
tests/phpunit/data/registration/invalid.json [new file with mode: 0644]
tests/phpunit/data/registration/newer_manifest_version.json [new file with mode: 0644]
tests/phpunit/data/registration/no_manifest_version.json [new file with mode: 0644]
tests/phpunit/data/registration/notjson.txt [new file with mode: 0644]
tests/phpunit/data/registration/old_manifest_version.json [new file with mode: 0644]
tests/phpunit/includes/registration/ExtensionJsonValidatorTest.php [new file with mode: 0644]

index c8e5e19..7e3afaa 100644 (file)
@@ -40,6 +40,7 @@ class ExtensionJsonValidator {
        }
 
        /**
        }
 
        /**
+        * @codeCoverageIgnore
         * @return bool
         */
        public function checkDependencies() {
         * @return bool
         */
        public function checkDependencies() {
diff --git a/tests/phpunit/data/registration/bad_spdx.json b/tests/phpunit/data/registration/bad_spdx.json
new file mode 100644 (file)
index 0000000..383ab04
--- /dev/null
@@ -0,0 +1,6 @@
+{
+       "name": "FooBar",
+       "license-name": "not a license identifier",
+       "manifest_version": 1
+}
+
diff --git a/tests/phpunit/data/registration/good.json b/tests/phpunit/data/registration/good.json
new file mode 100644 (file)
index 0000000..ad16c5e
--- /dev/null
@@ -0,0 +1,4 @@
+{
+       "name": "FooBar",
+       "manifest_version": 1
+}
diff --git a/tests/phpunit/data/registration/invalid.json b/tests/phpunit/data/registration/invalid.json
new file mode 100644 (file)
index 0000000..4d1fa58
--- /dev/null
@@ -0,0 +1,5 @@
+{
+       "name": "FooBar",
+       "license-name": [ "array" ],
+       "manifest_version": 1
+}
diff --git a/tests/phpunit/data/registration/newer_manifest_version.json b/tests/phpunit/data/registration/newer_manifest_version.json
new file mode 100644 (file)
index 0000000..29c668e
--- /dev/null
@@ -0,0 +1,4 @@
+{
+       "name": "FooBar",
+       "manifest_version": 999999
+}
diff --git a/tests/phpunit/data/registration/no_manifest_version.json b/tests/phpunit/data/registration/no_manifest_version.json
new file mode 100644 (file)
index 0000000..1a6119f
--- /dev/null
@@ -0,0 +1,3 @@
+{
+       "name": "FooBar"
+}
diff --git a/tests/phpunit/data/registration/notjson.txt b/tests/phpunit/data/registration/notjson.txt
new file mode 100644 (file)
index 0000000..d47b460
--- /dev/null
@@ -0,0 +1 @@
+This is definitely not JSON.
diff --git a/tests/phpunit/data/registration/old_manifest_version.json b/tests/phpunit/data/registration/old_manifest_version.json
new file mode 100644 (file)
index 0000000..f50faa1
--- /dev/null
@@ -0,0 +1,4 @@
+{
+       "name": "FooBar",
+       "manifest_version": -2
+}
diff --git a/tests/phpunit/includes/registration/ExtensionJsonValidatorTest.php b/tests/phpunit/includes/registration/ExtensionJsonValidatorTest.php
new file mode 100644 (file)
index 0000000..d69ad59
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+/**
+ * Copyright (C) 2018 Kunal Mehta <legoktm@member.fsf.org>
+ *
+ * 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.
+ *
+ */
+
+/**
+ * @covers ExtensionJsonValidator
+ */
+class ExtensionJsonValidatorTest extends MediaWikiTestCase {
+
+       /**
+        * @dataProvider provideValidate
+        */
+       public function testValidate( $file, $expected ) {
+               // If a dependency is missing, skip this test.
+               $validator = new ExtensionJsonValidator( function ( $msg ) {
+                       $this->markTestSkipped( $msg );
+               } );
+
+               if ( is_string( $expected ) ) {
+                       $this->setExpectedException(
+                               ExtensionJsonValidationError::class,
+                               $expected
+                       );
+               }
+
+               $dir = __DIR__ . '/../../data/registration/';
+               $this->assertSame(
+                       $expected,
+                       $validator->validate( $dir . $file )
+               );
+       }
+
+       public function provideValidate() {
+               return [
+                       [
+                               'notjson.txt',
+                               'notjson.txt is not valid JSON'
+                       ],
+                       [
+                               'no_manifest_version.json',
+                               'no_manifest_version.json does not have manifest_version set.'
+                       ],
+                       [
+                               'old_manifest_version.json',
+                               'old_manifest_version.json is using a non-supported schema version'
+                       ],
+                       [
+                               'newer_manifest_version.json',
+                               'newer_manifest_version.json is using a non-supported schema version'
+                       ],
+                       [
+                               'bad_spdx.json',
+                               "bad_spdx.json did not pass validation.
+[license-name] Invalid SPDX license identifier, see <https://spdx.org/licenses/>"
+                       ],
+                       [
+                               'invalid.json',
+                               "invalid.json did not pass validation.
+[license-name] Array value found, but a string is required"
+                       ],
+                       [
+                               'good.json',
+                               true
+                       ],
+               ];
+       }
+
+}