Add unit tests for site-wide enabled providers settings - #981
noruzzamans wants to merge 2 commits into
Conversation
The 0.16 settings screen and its enforcement filters had no coverage, so capability checks, save sanitization, and provider allow-list filtering could regress unnoticed.
068c773 to
fdadc94
Compare
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
🟡 Changes recommended
The promised user-profile bypass protection is untested and currently fails for spoofed admin query arguments.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Adds PHPUnit coverage for site-wide provider settings and enforcement.
Changes:
- Tests settings rendering, permissions, saving, and nonce validation.
- Tests provider allow-list filtering and core integration.
File summaries
| File | Description |
|---|---|
tests/class-two-factor-settings.php |
Adds 24 settings and provider-enforcement tests. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| public function test_filter_enabled_providers_not_bypassed_outside_admin() { | ||
| $_GET['page'] = 'two-factor-settings'; | ||
| update_option( Two_Factor_Core::ENABLED_PROVIDERS_OPTION_KEY, array( 'Two_Factor_Email' ) ); | ||
|
|
||
| $result = two_factor_filter_enabled_providers( $this->sample_provider_map() ); |
There was a problem hiding this comment.
Addressed in c2f9c24: restricted the settings screen bypass in two_factor_filter_enabled_providers() to verify $pagenow === 'options-general.php', current_user_can( 'manage_options' ), and current screen context, and added test_filter_enabled_providers_not_bypassed_on_profile_screen() covering admin requests on user profile screens.
|
Thanks for the PR @noruzzamans! Would you mind checking the open copilot feedback? |
Ensure disabled providers are not exposed on user profile screens when the page query parameter is spoofed, and add unit test coverage for profile screen context.
|
Thanks for flagging that, @masteradhoc! I've addressed the feedback in c2f9c24:
All 223 tests (700 assertions), PHPCS, and PHPStan pass cleanly locally. |
What?
Adds comprehensive PHPUnit unit test coverage for the site-wide Two-Factor settings screen (
Two_Factor_Settings) and the provider enforcement filter hooks intwo-factor.php, while restricting the admin screen bypass check to the actual settings screen to prevent query argument spoofing on user profile screens.Why?
The site-wide enabled-providers setting feature lacked dedicated unit test coverage. The settings renderer, save pipeline, and enforcement filters could regress unnoticed:
manage_optionscapability enforcement on settings view and form submission.null, which allows all registered providers by default) and an explicitly saved empty array (array(), which disables all providers).How?
two-factor.php):two_factor_filter_enabled_providers()to requiremanage_options, ensure$pagenow === 'options-general.php', and validate that the current screen belongs to the settings page.Tests_Two_Factor_Settingsintests/class-two-factor-settings.php(25 dedicated tests, 40 assertions) covering:test_settings_class_exists: VerifiesTwo_Factor_Settingsclass availability and bootstrap.test_admin_hooks_register_enforcement_filters: Verifies registration of enforcement filter hooks onadmin_init.test_render_settings_page_requires_manage_options: Enforcesmanage_optionscapability requirement on the settings page.test_render_settings_page_wrapper_requires_manage_options: Verifies capability protection on the settings wrapper callback.test_render_settings_page_outputs_provider_form: Verifies form rendering, nonce field output, and submit button generation.test_render_settings_page_checks_all_providers_by_default: Verifies all registered providers are checked by default when option is not yet saved.test_render_settings_page_checks_only_saved_providers: Verifies only configured providers are checked when settings are saved.test_render_settings_page_saves_sanitized_unique_providers: Verifies array sanitization, deduplication, and persistence of valid provider keys.test_render_settings_page_saves_empty_provider_list: Verifies saving with no providers selected correctly stores an empty array.test_render_settings_page_invalid_nonce_does_not_save: Verifies invalid or missing nonce terminates execution viawp_die()without persisting data.test_get_enabled_providers_option_returns_null_when_never_saved: Verifiestwo_factor_get_enabled_providers_option()returnsnullfor unset option.test_get_enabled_providers_option_returns_saved_array: Verifies retrieval of saved provider key arrays.test_get_enabled_providers_option_returns_empty_array_when_saved_empty: Distinguishes explicitly empty array from unset state.test_get_enabled_providers_option_returns_empty_array_for_non_array: Guards against corrupt/malformed non-array option values.test_filter_enabled_providers_passthrough_when_never_saved: Verifies passthrough behavior fortwo_factor_filter_enabled_providers()when unset.test_filter_enabled_providers_removes_disabled_providers: Verifies disallowed providers are stripped from active provider map.test_filter_enabled_providers_empty_list_disables_all: Verifies empty allow-list removes all registered providers.test_filter_enabled_providers_bypassed_on_settings_screen: Verifies settings screen bypass allows administrators to re-enable disabled providers.test_filter_enabled_providers_not_bypassed_outside_admin: Prevents spoofing bypass logic outside administrative context.test_filter_enabled_providers_not_bypassed_on_profile_screen: Ensures spoofed page query arguments on user profile screens do not bypass provider filtering.test_filter_enabled_providers_for_user_passthrough_when_never_saved: Verifies user provider passthrough when site-wide setting is unset.test_filter_enabled_providers_for_user_intersects_site_list: Verifies user-enabled providers are strictly intersected with site allow-list.test_filter_enabled_providers_for_user_empty_site_list: Verifies empty site allow-list strips all user providers.test_get_providers_honors_saved_site_allow_list: VerifiesTwo_Factor_Core::get_providers()honors site-wide allow-list.test_get_enabled_providers_for_user_honors_site_allow_list: VerifiesTwo_Factor_Core::get_enabled_providers_for_user()honors site-wide allow-list.Testing Instructions
Tests_Two_Factor_Settingstest suite:npm run composer -- test -- --filter Tests_Two_Factor_Settingsnpm testTesting that has already taken place:
wp-env(PHP 8.2, WordPress latest).Tests_Two_Factor_Settings— 25 tests, 40 assertions passed cleanly.WordPressstandard) and PHPStan — 0 errors, 0 warnings.Changelog Entry