Repository navigation
Validated submitted settings #600
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
352233f
fix: validate submitted settings
girishpanchal30 2f4f860
fix: validate settings payload structure
girishpanchal30 cba0f1b
refactor: settings handling
girishpanchal30 9533a56
test: cover design settings payloads
girishpanchal30 0b5ac3d
fix: require countdown details when saving classic modules
girishpanchal30 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| <?php | ||
| /** | ||
| * Tests for the settings form save handler. | ||
| */ | ||
|
|
||
| /** | ||
| * Test_Settings_Save. | ||
| */ | ||
| class Test_Settings_Save extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Seeded settings, built once: defaults embed the current time. | ||
| * | ||
| * @var array<string, array<string, mixed>> | ||
| */ | ||
| private $settings; | ||
|
|
||
| public function set_up(): void { | ||
| parent::set_up(); | ||
|
|
||
| $this->settings = WP_Maintenance_Mode::get_instance()->default_settings(); | ||
| update_option( 'wpmm_settings', $this->settings ); | ||
| update_option( 'wpmm_new_look', '0' ); | ||
| } | ||
|
|
||
| public function tear_down(): void { | ||
| $_POST = array(); | ||
| wp_set_current_user( 0 ); | ||
| parent::tear_down(); | ||
| } | ||
|
|
||
| /** | ||
| * Submit a settings tab form as an administrator. | ||
| * | ||
| * @param 'general'|'design'|'modules'|'bot' $tab Settings tab slug. | ||
| * @param array{options?: string|array<string, mixed>} $fields Submitted fields besides nonce and tab. | ||
| * @return string The message the request ended with. | ||
| */ | ||
| private function save_settings( string $tab, array $fields ): string { | ||
| $admin = wpmm_test_boot_admin(); | ||
|
|
||
| wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); | ||
|
|
||
| $_POST = array_merge( | ||
| array( | ||
| '_wpnonce' => wp_create_nonce( 'tab-' . $tab ), | ||
| 'tab' => $tab, | ||
| ), | ||
| $fields | ||
| ); | ||
|
|
||
| $redirect_catcher = function (): void { | ||
| // the handler ends with wp_safe_redirect() + exit | ||
| throw new WPDieException( 'redirected' ); | ||
| }; | ||
| add_filter( 'wp_redirect', $redirect_catcher ); | ||
|
|
||
| try { | ||
| $admin->save_plugin_settings(); | ||
| $message = 'no response'; | ||
| } catch ( WPDieException $e ) { | ||
| $message = $e->getMessage(); | ||
| } finally { | ||
| remove_filter( 'wp_redirect', $redirect_catcher ); | ||
| } | ||
|
|
||
| return $message; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, array{0: 'general'|'design', 1: array{options?: string|array{general?: string, design?: string}}}> | ||
| */ | ||
| public function malformed_payloads(): array { | ||
| return array( | ||
| 'options is a string' => array( 'general', array( 'options' => 'status' ) ), | ||
| 'options is an empty string' => array( 'general', array( 'options' => '' ) ), | ||
| 'options[general] is a string' => array( 'general', array( 'options' => array( 'general' => 'status' ) ) ), | ||
| 'options is missing' => array( 'general', array() ), | ||
| 'design options is a string' => array( 'design', array( 'options' => 'title' ) ), | ||
| 'options[design] is a string' => array( 'design', array( 'options' => array( 'design' => 'title' ) ) ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider malformed_payloads | ||
| * | ||
| * @param 'general'|'design' $tab Settings tab slug. | ||
| * @param array{options?: string|array{general?: string, design?: string}} $fields Submitted fields besides nonce and tab. | ||
| */ | ||
| public function test_malformed_payload_is_rejected_without_saving( string $tab, array $fields ): void { | ||
| $message = $this->save_settings( $tab, $fields ); | ||
|
|
||
| $this->assertSame( 'The submitted settings are invalid.', $message ); | ||
| $this->assertSame( $this->settings, get_option( 'wpmm_settings' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, array{0: 'general'|'design'|'modules'|'bot', 1: string, 2: string|array<array-key, string|list<string>>}> | ||
| */ | ||
| public function malformed_fields(): array { | ||
| return array( | ||
| 'heading_color is an array' => array( 'design', 'heading_color', array( '#000000' ) ), | ||
| 'bg_custom is an array' => array( 'design', 'bg_custom', array( 'https://example.org/bg.jpg' ) ), | ||
| 'backend_role is a string' => array( 'general', 'backend_role', 'administrator' ), | ||
| 'frontend_role is a string' => array( 'general', 'frontend_role', 'editor' ), | ||
| 'exclude is an array' => array( 'general', 'exclude', array( 'feed' ) ), | ||
| 'redirection is an array' => array( 'general', 'redirection', array( 'https://example.org' ) ), | ||
| 'countdown_details is a string' => array( 'modules', 'countdown_details', '1' ), | ||
| 'countdown day is an array' => array( 'modules', 'countdown_details', array( 'days' => array( '1' ), 'hours' => '1', 'minutes' => '0' ) ), | ||
| 'bot messages is a string' => array( 'bot', 'messages', 'hello' ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Submit an otherwise complete form for the tab with one field of the wrong type. | ||
| * | ||
| * @dataProvider malformed_fields | ||
| * | ||
| * @param 'general'|'design'|'modules'|'bot' $tab Settings tab slug. | ||
| * @param string $field Field name inside the tab. | ||
| * @param string|array<array-key, string|list<string>> $value Submitted value. | ||
| */ | ||
| public function test_malformed_field_is_rejected_without_saving( string $tab, string $field, $value ): void { | ||
| // the stored general defaults keep exclude as a list, while the form posts a string | ||
| $form = 'general' === $tab ? array( 'status' => '0' ) : $this->settings[ $tab ]; | ||
| $form[ $field ] = $value; | ||
|
|
||
| $message = $this->save_settings( $tab, array( 'options' => array( $tab => $form ) ) ); | ||
|
|
||
| $this->assertSame( 'The submitted settings are invalid.', $message ); | ||
| $this->assertSame( $this->settings, get_option( 'wpmm_settings' ) ); | ||
| } | ||
|
|
||
| public function test_classic_modules_save_without_countdown_details_is_rejected(): void { | ||
| $form = $this->settings['modules']; | ||
| unset( $form['countdown_details'] ); | ||
|
|
||
| $message = $this->save_settings( 'modules', array( 'options' => array( 'modules' => $form ) ) ); | ||
|
|
||
| $this->assertSame( 'The submitted settings are invalid.', $message ); | ||
| $this->assertSame( $this->settings, get_option( 'wpmm_settings' ) ); | ||
| } | ||
|
|
||
| public function test_new_look_modules_save_without_countdown_details_is_saved(): void { | ||
| update_option( 'wpmm_new_look', '1' ); | ||
| $form = $this->settings['modules']; | ||
| unset( $form['countdown_details'] ); | ||
| $form['ga_status'] = '1'; | ||
|
|
||
| $message = $this->save_settings( 'modules', array( 'options' => array( 'modules' => $form ) ) ); | ||
|
|
||
| $this->assertSame( 'redirected', $message ); | ||
| $this->assertSame( 1, get_option( 'wpmm_settings' )['modules']['ga_status'] ); | ||
| } | ||
|
|
||
| public function test_general_form_save_stores_status(): void { | ||
| $message = $this->save_settings( 'general', array( 'options' => array( 'general' => array( 'status' => '1' ) ) ) ); | ||
|
|
||
| $this->assertSame( 'redirected', $message ); | ||
| $this->assertSame( 1, get_option( 'wpmm_settings' )['general']['status'] ); | ||
| } | ||
| } |
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.