Repository navigation
Plugin Directory: Check all block.json validation errors during import #1010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
gigitux
wants to merge
5
commits into
WordPress:trunk
from
gigitux:fix/block-import-validation-errors
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9868785
Plugin Directory: Check all block.json validation errors during import.
gigitux a1d9217
Plugin Directory: Inspect block validation errors individually.
gigitux 4b5e0f4
Plugin Directory: Match block validation properties explicitly.
gigitux 4f6bd68
mock request
gigitux 34dcb68
Merge branch 'trunk' into fix/block-import-validation-errors
gigitux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
...blic_html/wp-content/plugins/plugin-directory/tests/Import_Block_JSON_Validation_Test.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| <?php | ||
| /** | ||
| * Tests for block.json validation during plugin imports. | ||
| * | ||
| * @package WordPressdotorg\Plugin_Directory\Tests | ||
| */ | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| use PHPUnit\Framework\TestCase; | ||
| use WordPressdotorg\Plugin_Directory\CLI\Import; | ||
|
|
||
| /** | ||
| * Tests that tolerated schema errors do not hide errors that prevent listing. | ||
| * | ||
| * @group import | ||
| */ | ||
| #[Group( 'import' )] | ||
| class Import_Block_JSON_Validation_Test extends TestCase { | ||
|
|
||
| /** | ||
| * Temporary block.json path. | ||
| * | ||
| * @var string | ||
| */ | ||
| private $block_file; | ||
|
|
||
| /** | ||
| * Create a temporary directory for block metadata and mock schema requests. | ||
| */ | ||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| $directory = sys_get_temp_dir() . '/import-block-json-' . wp_generate_uuid4(); | ||
| wp_mkdir_p( $directory ); | ||
| $this->block_file = $directory . '/block.json'; | ||
|
|
||
| add_filter( 'pre_http_request', array( $this, 'mock_schema_request' ), 10, 3 ); | ||
| } | ||
|
|
||
| /** | ||
| * Remove temporary files and the schema request mock. | ||
| */ | ||
| protected function tearDown(): void { | ||
| remove_filter( 'pre_http_request', array( $this, 'mock_schema_request' ), 10 ); | ||
|
|
||
| if ( file_exists( $this->block_file ) ) { | ||
| wp_delete_file( $this->block_file ); | ||
| } | ||
| // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- Remove the temporary test directory. | ||
| rmdir( dirname( $this->block_file ) ); | ||
|
|
||
| parent::tearDown(); | ||
| } | ||
|
|
||
| /** | ||
| * Serve a fixed copy of https://schemas.wp.org/trunk/block.json. | ||
| * | ||
| * @param false|array|WP_Error $preempt Existing short-circuit response. | ||
| * @param array $args HTTP request arguments. | ||
| * @param string $url HTTP request URL. | ||
| * @return false|array|WP_Error | ||
| */ | ||
| public function mock_schema_request( $preempt, $args, $url ) { | ||
| if ( 'https://schemas.wp.org/trunk/block.json' !== $url ) { | ||
| return $preempt; | ||
| } | ||
|
|
||
| return array( | ||
| 'headers' => array(), | ||
| // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Read the fixed test schema. | ||
| 'body' => file_get_contents( __DIR__ . '/fixtures/block-schema.json' ), | ||
| 'response' => array( | ||
| 'code' => 200, | ||
| 'message' => 'OK', | ||
| ), | ||
| 'cookies' => array(), | ||
| 'filename' => null, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * A missing apiVersion must not mask the conditional missing-script error. | ||
| * | ||
| * @dataProvider block_metadata_provider | ||
| * @param array $metadata Block metadata to import. | ||
| * @param int $expected_count Expected number of imported blocks. | ||
| */ | ||
| #[DataProvider( 'block_metadata_provider' )] | ||
| public function test_block_json_validation( array $metadata, int $expected_count ): void { | ||
| $metadata = array_merge( array( 'name' => 'test/example' ), $metadata ); | ||
| // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- Write a temporary test fixture. | ||
| file_put_contents( $this->block_file, wp_json_encode( $metadata ) ); | ||
|
|
||
| $blocks = Import::find_blocks_in_file( $this->block_file ); | ||
|
|
||
| $this->assertCount( $expected_count, $blocks ); | ||
| if ( $expected_count ) { | ||
| $this->assertEquals( (object) $metadata, $blocks[0] ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A missing required name is rejected even when a script is present. | ||
| */ | ||
| public function test_missing_name_rejects_block(): void { | ||
| $metadata = array( | ||
| 'apiVersion' => 3, | ||
| 'title' => 'Example', | ||
| 'script' => 'file:./index.js', | ||
| ); | ||
| // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- Write a temporary test fixture. | ||
| file_put_contents( $this->block_file, wp_json_encode( $metadata ) ); | ||
|
|
||
| $this->assertCount( 0, Import::find_blocks_in_file( $this->block_file ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Cover the script requirement with and without a tolerated apiVersion error. | ||
| * | ||
| * @return array | ||
| */ | ||
| public static function block_metadata_provider(): array { | ||
| return array( | ||
| 'missing apiVersion and scripts' => array( array(), 0 ), | ||
| 'apiVersion without scripts' => array( array( 'apiVersion' => 3 ), 0 ), | ||
| 'missing apiVersion with script' => array( array( 'script' => 'file:./index.js' ), 1 ), | ||
| 'missing apiVersion with editorScript' => array( array( 'editorScript' => 'file:./index.js' ), 1 ), | ||
| 'valid apiVersion with script' => array( | ||
| array( | ||
| 'apiVersion' => 3, | ||
| 'script' => 'file:./index.js', | ||
| ), | ||
| 1, | ||
| ), | ||
| 'valid apiVersion with editorScript' => array( | ||
| array( | ||
| 'apiVersion' => 3, | ||
| 'editorScript' => 'file:./index.js', | ||
| ), | ||
| 1, | ||
| ), | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.