@@ -12,76 +12,58 @@ class FilesHaveJsonFormatTest extends PolyfillTestCase {
1212
1313 use PluginPathsTrait;
1414
15- public function testMainPluginJsonIsValid () :void {
16- $ jsonParsed = $ this ->decodePluginJsonFile ( 'plugin.json ' , 'plugin.json ' );
17-
18- // Verify key structure exists
19- $ this ->assertArrayHasKey ( 'properties ' , $ jsonParsed , 'plugin.json should have properties key ' );
20- $ this ->assertArrayHasKey ( 'requirements ' , $ jsonParsed , 'plugin.json should have requirements key ' );
21- $ this ->assertArrayHasKey ( 'paths ' , $ jsonParsed , 'plugin.json should have paths key ' );
22-
23- // Verify nested properties
24- $ properties = $ jsonParsed ['properties ' ];
25- $ this ->assertArrayHasKey ( 'slug_plugin ' , $ properties , 'properties should have slug_plugin key ' );
26- $ this ->assertArrayHasKey ( 'version ' , $ properties , 'properties should have version key ' );
27- $ this ->assertArrayHasKey ( 'text_domain ' , $ properties , 'properties should have text_domain key ' );
28- }
15+ public function testMainPluginJsonIsValid () :void {
16+ $ jsonParsed = $ this ->decodePluginJsonFile ( 'plugin.json ' , 'plugin.json ' );
2917
30- public function testChangelogJsonIsValid () : void {
31- $ jsonParsed = $ this ->decodePluginJsonFile ( 'cl.json ' , ' cl .json ' );
32- $ this ->assertIsArray ( $ jsonParsed , 'cl .json should contain valid JSON ' );
33- }
18+ // Verify key structure exists
19+ $ this ->assertArrayHasKey ( 'properties ' , $ jsonParsed , ' plugin .json should have properties key ' );
20+ $ this ->assertArrayHasKey ( ' requirements ' , $ jsonParsed , 'plugin .json should have requirements key ' );
21+ $ this -> assertArrayHasKey ( ' paths ' , $ jsonParsed , ' plugin.json should have paths key ' );
3422
35- /**
36- * Test that all JSON files in root directory are valid
37- */
38- public function testAllRootJsonFilesAreValid () :void {
39- $ rootPath = $ this ->getPluginRoot ();
40-
41- // Get all JSON files in root directory
42- $ jsonFiles = $ this ->getJsonFiles ( $ rootPath );
43-
44- // Exclude files that are already tested specifically
45- $ excludeFiles = ['plugin.json ' , 'cl.json ' ];
46- $ jsonFiles = array_diff ( $ jsonFiles , $ excludeFiles );
47-
48- if ( empty ( $ jsonFiles ) ) {
49- $ this ->markTestSkipped ( 'No additional JSON files found in root directory to test. ' );
50- return ;
51- }
52-
53- foreach ( $ jsonFiles as $ file ) {
54- $ filePath = $ this ->getPluginFilePath ( $ file );
55- $ jsonContent = file_get_contents ( $ filePath );
56-
57- // Test that file can be parsed as JSON
58- $ jsonParsed = json_decode ( $ jsonContent , true );
59- $ lastError = json_last_error ();
60-
61- $ this ->assertEquals (
62- JSON_ERROR_NONE ,
63- $ lastError ,
64- "File {$ file } should contain valid JSON. Error: " . json_last_error_msg ()
65- );
66-
67- // Only check for array/object if parsing succeeded
68- if ( $ lastError === JSON_ERROR_NONE ) {
69- $ this ->assertThat (
70- $ jsonParsed ,
71- $ this ->logicalOr (
72- $ this ->isType ('array ' ),
73- $ this ->isNull ()
74- ),
75- "File {$ file } should decode to array or null "
76- );
77- }
78- }
79- }
23+ // Verify nested properties
24+ $ properties = $ jsonParsed ['properties ' ];
25+ $ this ->assertArrayHasKey ( 'slug_plugin ' , $ properties , 'properties should have slug_plugin key ' );
26+ $ this ->assertArrayHasKey ( 'version ' , $ properties , 'properties should have version key ' );
27+ $ this ->assertArrayHasKey ( 'text_domain ' , $ properties , 'properties should have text_domain key ' );
28+ }
8029
81- private function getJsonFiles ( string $ path ) :array {
82- $ allItems = scandir ( $ path );
83- return array_filter ( $ allItems , function ( string $ item ) use ( $ path ) {
84- return !is_dir ( $ path . '/ ' . $ item ) && pathinfo ( $ item , PATHINFO_EXTENSION ) === 'json ' ;
85- } );
86- }
30+ public function testChangelogJsonIsValid () :void {
31+ $ jsonParsed = $ this ->decodePluginJsonFile ( 'cl.json ' , 'cl.json ' );
32+ $ this ->assertIsArray ( $ jsonParsed , 'cl.json should contain valid JSON ' );
33+ }
34+
35+ /**
36+ * Test that development JSON files in root directory are valid.
37+ * These files (composer.json, package.json, etc.) are excluded from packages
38+ * via .gitattributes export-ignore rules, so this test only runs in source mode.
39+ */
40+ public function testDevelopmentJsonFilesAreValid () :void {
41+ if ( $ this ->isTestingPackage () ) {
42+ $ this ->markTestSkipped (
43+ 'Development JSON files are excluded from packages via .gitattributes export-ignore. '
44+ );
45+ return ;
46+ }
47+
48+ $ rootPath = $ this ->getPluginRoot ();
49+ $ this ->assertDirectoryExists ( $ rootPath );
50+
51+ // Development JSON files that should exist in source
52+ $ devJsonFiles = [ 'composer.json ' , 'package.json ' , 'package-lock.json ' , 'patchwork.json ' ];
53+
54+ foreach ( $ devJsonFiles as $ file ) {
55+ $ filePath = $ this ->getPluginFilePath ( $ file );
56+ $ this ->assertFileExists ( $ filePath , "{$ file } should exist in source directory " );
57+
58+ $ content = file_get_contents ( $ filePath );
59+ $ this ->assertNotFalse ( $ content , "Should be able to read {$ file }" );
60+
61+ json_decode ( $ content , true );
62+ $ this ->assertSame (
63+ JSON_ERROR_NONE ,
64+ json_last_error (),
65+ "{$ file } should contain valid JSON: " . json_last_error_msg ()
66+ );
67+ }
68+ }
8769}
0 commit comments