Skip to content
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

Fix: Add strict check to media sync hooks #34

Merged
merged 1 commit into from
Feb 14, 2025

Conversation

seanlanglands
Copy link
Contributor

@seanlanglands seanlanglands commented Feb 11, 2025

Description

Added strict check before adding media sync hooks.

Previously, if s3 bucket settings were saved and then later removed, the ! empty( $this->settings ) condition would still be truthy because the settings option is not empty. This would cause issues, as media sync hooks would still be added and cause errors during routine actions (e.g., deleting media in WP Admin and unintentionally running delete_attachment_from_s3).

Copy link
Contributor

@GaryJones GaryJones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please cherry pick these commits into two separate branches / PRs - one for adding the test suite, one for the settings validation and any test that includes.

One of these is a chore/maintenance, the other is a bug fix.

@seanlanglands seanlanglands force-pushed the fix/add-strict-check-to-media-sync-hooks branch from ff2c9f4 to ab3cc54 Compare February 11, 2025 21:34
@seanlanglands seanlanglands force-pushed the fix/add-strict-check-to-media-sync-hooks branch from ab3cc54 to 73a1b22 Compare February 11, 2025 22:00
@seanlanglands
Copy link
Contributor Author

Good call on that, @GaryJones. These are now separated, with #35 being the test suite.

Copy link
Contributor

@GaryJones GaryJones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving, as the comments aren't a blocker for this PR.

}

/**
* @testdox Allow media syncs when required settings set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you call phpunit with --testdox, then this line which duplicates the method name isn't needed. The --testdox flag will take the method name, strip a test_ prefix, and change underscores to spaces anyway.

Copy link
Contributor Author

@seanlanglands seanlanglands Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admittedly, it is redundant in these specific test cases (@testdox comments are the same as the final output based on the method name). I do like that @testdox in docblocks could be used as an alternative test description. If the @testdox would be redundant, is it best to omit the comment entirely?

Copy link
Contributor

@GaryJones GaryJones Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a test class method can't be made into a human-readable sentence, such that the @testdox would be needed, then sure, use it, but I'd question why the test class method couldn't be rewritten to make it a human-readable sentence in the first place.

I like running PHPUnit with --testdox and never felt the need to use @testdox, so if it would be redundant, then scrap it.

Comment on lines 54 to 59
parent::set_private_property(
$this->s3_media_sync::class,
$this->s3_media_sync,
'settings',
$this->complete_settings
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #35 (comment) - in that, having to force change the value of a private class property with Reflection seems like a huge code smell (I know you're working with what you've got for this particular hotfix). Having a Settings object, injected into the main class constructor, means that the class under test could just be instantiated here with a data provider of arrays / array key values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'm going to attempt to flush this out in #35.

@seanlanglands seanlanglands force-pushed the fix/add-strict-check-to-media-sync-hooks branch from 73a1b22 to 2c756dc Compare February 13, 2025 16:47
@seanlanglands
Copy link
Contributor Author

Removed S3_Media_Sync tests to be introduced later once we settle on a refactoring approach.

The tests are preserved here:
https://github.com/Automattic/s3-media-sync/tree/chore/class-s3-media-sync-tests

@seanlanglands seanlanglands added the bug Something isn't working label Feb 14, 2025
@seanlanglands seanlanglands merged commit a957645 into develop Feb 14, 2025
@seanlanglands seanlanglands deleted the fix/add-strict-check-to-media-sync-hooks branch February 14, 2025 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants