-
Notifications
You must be signed in to change notification settings - Fork 35
Account for package metadata that doesn't include filename when deriving hashed filename #496
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
Open
kasparsd
wants to merge
7
commits into
fairpm:release_1.4.1
Choose a base branch
from
kasparsd:fix-add-plugins-error
base: release_1.4.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−12
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
09abd75
Ensure the expected return type
kasparsd df45edd
Account for package metadata that doesn’t include filename
kasparsd a56991c
evaluate before comparing
kasparsd 76d2870
Ensure we match against the tail
kasparsd e66cc29
Test missing metadata type property
kasparsd a29caf5
Test that a hash-like substring in the middle of the slug still gets …
kasparsd 7d72ed6
Merge branch 'release_1.4.1' into fix-add-plugins-error
kasparsd 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
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
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,178 @@ | ||
| <?php | ||
| /** | ||
| * Tests for FAIR\Packages\get_hashed_filename(). | ||
| * | ||
| * @package FAIR | ||
| */ | ||
|
|
||
| use FAIR\Packages\MetadataDocument; | ||
| use function FAIR\Packages\get_did_hash; | ||
| use function FAIR\Packages\get_hashed_filename; | ||
|
|
||
| /** | ||
| * Tests for FAIR\Packages\get_hashed_filename(). | ||
| * | ||
| * @covers FAIR\Packages\get_hashed_filename | ||
| */ | ||
| class GetHashedFilenameTest extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Test that plugin filenames append the DID hash to the directory name. | ||
| */ | ||
| public function test_should_hash_plugin_directory_name() { | ||
| $metadata = $this->create_metadata_document( | ||
| [ | ||
| 'filename' => 'example/example.php', | ||
| 'slug' => 'example', | ||
| 'type' => 'wp-plugin', | ||
| ] | ||
| ); | ||
|
|
||
| $this->assertSame( 'example-' . get_did_hash( $metadata->id ) . '/example.php', get_hashed_filename( $metadata ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that missing plugin filenames fall back to the slug. | ||
| */ | ||
| public function test_should_fall_back_to_slug_when_plugin_filename_missing() { | ||
| $metadata = $this->create_metadata_document( | ||
| [ | ||
| 'filename' => null, | ||
| 'slug' => 'example', | ||
| 'type' => 'wp-plugin', | ||
| ] | ||
| ); | ||
|
|
||
| $this->assertSame( 'example-' . get_did_hash( $metadata->id ) . '/example.php', get_hashed_filename( $metadata ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that malformed plugin filenames still produce a valid hashed path. | ||
| */ | ||
| public function test_should_recover_when_plugin_filename_has_no_main_file() { | ||
| $metadata = $this->create_metadata_document( | ||
| [ | ||
| 'filename' => 'example', | ||
| 'slug' => 'example', | ||
| 'type' => 'wp-plugin', | ||
| ] | ||
| ); | ||
|
|
||
| $this->assertSame( 'example-' . get_did_hash( $metadata->id ) . '/example.php', get_hashed_filename( $metadata ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that theme filenames append the DID hash to the slug. | ||
| */ | ||
| public function test_should_hash_theme_slug() { | ||
| $metadata = $this->create_metadata_document( | ||
| [ | ||
| 'filename' => 'example', | ||
| 'slug' => 'example', | ||
| 'type' => 'wp-theme', | ||
| ] | ||
| ); | ||
|
|
||
| $this->assertSame( 'example-' . get_did_hash( $metadata->id ), get_hashed_filename( $metadata ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that missing non-plugin filenames still fall back to the slug. | ||
| */ | ||
| public function test_should_fall_back_to_slug_for_non_plugin_when_filename_missing() { | ||
| $metadata = $this->create_metadata_document( | ||
| [ | ||
| 'filename' => null, | ||
| 'slug' => 'example', | ||
| 'type' => 'wp-theme', | ||
| ] | ||
| ); | ||
|
|
||
| $this->assertSame( 'example-' . get_did_hash( $metadata->id ), get_hashed_filename( $metadata ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that a pre-hashed plugin slug is not hashed twice. | ||
| */ | ||
| public function test_should_not_append_hash_twice_for_plugin_slug() { | ||
| $hash = get_did_hash( 'did:plc:example1234567890123456789' ); | ||
| $metadata = $this->create_metadata_document( | ||
| [ | ||
| 'filename' => 'example-' . $hash . '/example.php', | ||
| 'slug' => 'example-' . $hash, | ||
| 'type' => 'wp-plugin', | ||
| ] | ||
| ); | ||
|
|
||
| $this->assertSame( 'example-' . $hash . '/example.php', get_hashed_filename( $metadata ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that a hash-like substring in the middle of the slug still gets the DID hash appended. | ||
| */ | ||
| public function test_should_append_hash_when_same_value_appears_in_middle_of_plugin_slug() { | ||
| $hash = get_did_hash( 'did:plc:example1234567890123456789' ); | ||
| $slug = 'vendor-' . $hash . '-plugin'; | ||
| $metadata = $this->create_metadata_document( | ||
| [ | ||
| 'filename' => $slug . '/' . $slug . '.php', | ||
| 'slug' => $slug, | ||
| 'type' => 'wp-plugin', | ||
| ] | ||
| ); | ||
|
|
||
| $this->assertSame( $slug . '-' . $hash . '/' . $slug . '.php', get_hashed_filename( $metadata ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that empty plugin filenames behave the same as missing filenames. | ||
| */ | ||
| public function test_should_fall_back_to_slug_when_plugin_filename_is_empty_string() { | ||
| $metadata = $this->create_metadata_document( | ||
| [ | ||
| 'filename' => '', | ||
| 'slug' => 'example', | ||
| 'type' => 'wp-plugin', | ||
| ] | ||
| ); | ||
|
|
||
| $this->assertSame( 'example-' . get_did_hash( $metadata->id ) . '/example.php', get_hashed_filename( $metadata ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that a missing type is treated as non-plugin metadata. | ||
| * | ||
| * This covers the operator precedence difference between | ||
| * `'wp-plugin' === ( $metadata->type ?? '' )` and | ||
| * `'wp-plugin' === $metadata->type ?? ''`. | ||
| */ | ||
| public function test_should_treat_missing_type_as_non_plugin_metadata() { | ||
| $metadata = (object) [ | ||
| 'id' => 'did:plc:example1234567890123456789', | ||
| 'slug' => 'example', | ||
| 'filename' => 'example/example.php', | ||
| ]; | ||
|
|
||
| $this->assertSame( 'example-' . get_did_hash( $metadata->id ), get_hashed_filename( $metadata ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Create a metadata document for testing. | ||
| * | ||
| * @param array $overrides Document overrides. | ||
| * @return MetadataDocument | ||
| */ | ||
| private function create_metadata_document( array $overrides ) : MetadataDocument { | ||
| $metadata = new MetadataDocument(); | ||
| $metadata->id = 'did:plc:example1234567890123456789'; | ||
| $metadata->type = 'wp-plugin'; | ||
| $metadata->slug = 'example'; | ||
| $metadata->filename = 'example/example.php'; | ||
|
|
||
| foreach ( $overrides as $key => $value ) { | ||
| $metadata->{$key} = $value; | ||
| } | ||
|
|
||
| return $metadata; | ||
| } | ||
| } |
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.