Skip to content

Commit ef33ab6

Browse files
committed
further test fiexs
1 parent 4c8d30a commit ef33ab6

File tree

5 files changed

+100
-90
lines changed

5 files changed

+100
-90
lines changed

.github/workflows/docker-tests.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,19 @@ jobs:
213213
WP_VERSION="${{ matrix.wordpress }}"
214214
fi
215215
216-
cat > tests/docker/.env << EOF
217-
PHP_VERSION=$PHP_VERSION
218-
WP_VERSION=$WP_VERSION
219-
TEST_PHP_VERSION=$PHP_VERSION
220-
TEST_WP_VERSION=$WP_VERSION
221-
WP_VERSION_LATEST=${{ needs.detect-wp-versions.outputs.latest }}
222-
WP_VERSION_PREVIOUS=${{ needs.detect-wp-versions.outputs.previous }}
223-
PLUGIN_SOURCE=${{ env.SHIELD_PACKAGE_PATH }}
224-
SHIELD_PACKAGE_PATH=${{ env.SHIELD_PACKAGE_PATH }}
225-
SHIELD_TEST_IMAGE=shield-test-runner:php$PHP_VERSION-wp$WP_VERSION
226-
SHIELD_TEST_IMAGE_LATEST=shield-test-runner:php$PHP_VERSION-wp$WP_VERSION
227-
SHIELD_TEST_IMAGE_PREVIOUS=shield-test-runner:php$PHP_VERSION-wp$WP_VERSION
228-
EOF
216+
{
217+
echo "PHP_VERSION=$PHP_VERSION"
218+
echo "WP_VERSION=$WP_VERSION"
219+
echo "TEST_PHP_VERSION=$PHP_VERSION"
220+
echo "TEST_WP_VERSION=$WP_VERSION"
221+
echo "WP_VERSION_LATEST=${{ needs.detect-wp-versions.outputs.latest }}"
222+
echo "WP_VERSION_PREVIOUS=${{ needs.detect-wp-versions.outputs.previous }}"
223+
echo "PLUGIN_SOURCE=${{ env.SHIELD_PACKAGE_PATH }}"
224+
echo "SHIELD_PACKAGE_PATH=${{ env.SHIELD_PACKAGE_PATH }}"
225+
echo "SHIELD_TEST_IMAGE=shield-test-runner:php${PHP_VERSION}-wp${WP_VERSION}"
226+
echo "SHIELD_TEST_IMAGE_LATEST=shield-test-runner:php${PHP_VERSION}-wp${WP_VERSION}"
227+
echo "SHIELD_TEST_IMAGE_PREVIOUS=shield-test-runner:php${PHP_VERSION}-wp${WP_VERSION}"
228+
} > tests/docker/.env
229229
230230
echo "Docker environment configured:"
231231
echo "PHP Version: $PHP_VERSION"

bin/run-tests-docker.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ else
188188
composer install --no-interaction --no-cache --no-dev
189189
cd ../..
190190
fi
191+
192+
# Generate plugin.json from modular spec files (source testing only)
193+
# In package mode, PluginPackager already generates plugin.json in the package
194+
echo "Generating plugin.json from plugin-spec/ files..."
195+
php bin/build-config.php
191196
fi
192197

193198
# Run tests with environment variable support

tests/Integration/FilesHaveJsonFormatTest.php

Lines changed: 51 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/Unit/Helpers/PluginPathsTraitTest.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,29 @@ class PluginPathsTraitTest extends TestCase {
99

1010
use PluginPathsTrait;
1111

12+
/**
13+
* Store original SHIELD_PACKAGE_PATH so we can restore it after tests that modify it.
14+
* @var string|false
15+
*/
16+
private $originalPackagePath;
17+
1218
protected function set_up() :void {
1319
parent::set_up();
14-
$this->clearPackageEnv();
20+
// Preserve any incoming SHIELD_PACKAGE_PATH (e.g., from Docker/CI package-mode runs)
21+
$this->originalPackagePath = getenv( 'SHIELD_PACKAGE_PATH' );
1522
}
1623

1724
protected function tear_down() :void {
18-
$this->clearPackageEnv();
25+
// Restore original env state
26+
$this->restorePackageEnv();
1927
parent::tear_down();
2028
}
2129

2230
/**
2331
* @runInSeparateProcess
2432
*/
2533
public function testGetPluginRootDefaultsToSourceDirectory() :void {
34+
// This test explicitly needs source-mode (no package path)
2635
$this->clearPackageEnv();
2736
$expectedRoot = realpath( __DIR__.'/../../..' );
2837
$this->assertNotFalse( $expectedRoot, 'Expected project root to resolve' );
@@ -44,18 +53,19 @@ public function testGetPluginRootHonoursPackagePath() :void {
4453
$this->assertSame( $tempDir, $this->getPluginRoot(), 'Plugin root should honour SHIELD_PACKAGE_PATH' );
4554
$this->assertSame( $tempDir.'/icwp-wpsf.php', $this->getPluginFilePath( 'icwp-wpsf.php' ) );
4655

47-
// Cleanup
48-
$this->clearPackageEnv();
56+
// Cleanup temp files (env restored by tear_down)
4957
unlink( $tempDir.'/icwp-wpsf.php' );
5058
rmdir( $tempDir );
5159
}
5260

5361
public function testGetPluginFileContentsReadsFile() :void {
62+
// Honors SHIELD_PACKAGE_PATH if set (package mode) or falls back to source
5463
$content = $this->getPluginFileContents( 'plugin.json', 'plugin.json' );
5564
$this->assertNotEmpty( $content, 'plugin.json should contain data' );
5665
}
5766

5867
public function testDecodePluginJsonFileMatchesManualDecode() :void {
68+
// Honors SHIELD_PACKAGE_PATH if set (package mode) or falls back to source
5969
$manual = json_decode( $this->getPluginFileContents( 'plugin.json', 'plugin.json manual read' ), true );
6070
$this->assertSame( $manual, $this->decodePluginJsonFile( 'plugin.json', 'plugin.json' ) );
6171
}
@@ -64,8 +74,25 @@ public function testGetPluginJsonPathShortcut() :void {
6474
$this->assertSame( $this->getPluginFilePath( 'plugin.json' ), $this->getPluginJsonPath() );
6575
}
6676

77+
/**
78+
* Clear SHIELD_PACKAGE_PATH for tests that need source-mode behavior.
79+
*/
6780
private function clearPackageEnv() :void {
6881
putenv( 'SHIELD_PACKAGE_PATH' );
6982
unset( $_ENV['SHIELD_PACKAGE_PATH'], $_SERVER['SHIELD_PACKAGE_PATH'] );
7083
}
84+
85+
/**
86+
* Restore SHIELD_PACKAGE_PATH to its original value.
87+
*/
88+
private function restorePackageEnv() :void {
89+
if ( $this->originalPackagePath !== false ) {
90+
putenv( 'SHIELD_PACKAGE_PATH='.$this->originalPackagePath );
91+
$_ENV['SHIELD_PACKAGE_PATH'] = $this->originalPackagePath;
92+
$_SERVER['SHIELD_PACKAGE_PATH'] = $this->originalPackagePath;
93+
}
94+
else {
95+
$this->clearPackageEnv();
96+
}
97+
}
7198
}

tests/docker/docker-compose.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ services:
6363
environment:
6464
TEST_PHP_VERSION: ${PHP_VERSION:-8.2}
6565
TEST_WP_VERSION: ${WP_VERSION_LATEST:-6.9}
66-
SHIELD_PACKAGE_PATH: ${SHIELD_PACKAGE_PATH}
67-
PLUGIN_SOURCE: ${PLUGIN_SOURCE}
6866
command: bin/run-tests-docker.sh wordpress_test_latest root testpass mysql-latest ${WP_VERSION_LATEST:-6.9}
6967

7068
# Test runner for WordPress previous
@@ -84,6 +82,4 @@ services:
8482
environment:
8583
TEST_PHP_VERSION: ${PHP_VERSION:-8.2}
8684
TEST_WP_VERSION: ${WP_VERSION_PREVIOUS:-6.8.3}
87-
SHIELD_PACKAGE_PATH: ${SHIELD_PACKAGE_PATH}
88-
PLUGIN_SOURCE: ${PLUGIN_SOURCE}
8985
command: bin/run-tests-docker.sh wordpress_test_previous root testpass mysql-previous ${WP_VERSION_PREVIOUS:-6.8.3}

0 commit comments

Comments
 (0)