-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Options: Remove pre-filter juggling #5498
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -689,14 +689,122 @@ public function data_stored_as_empty_string() { | |
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Test cases for testing whether update_network_option() will add a non-existent option. | ||
| */ | ||
| public function data_option_values() { | ||
| return array( | ||
| array( '1' ), | ||
| array( 1 ), | ||
| array( 1.0 ), | ||
| array( true ), | ||
| array( 'true' ), | ||
| array( '0' ), | ||
| array( 0 ), | ||
| array( 0.0 ), | ||
| array( false ), | ||
| array( '' ), | ||
| array( null ), | ||
| array( array() ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that a non-existent option is added only when the pre-filter matches the default 'false'. | ||
| * | ||
| * @ticket 59360 | ||
| * @dataProvider data_option_values | ||
| * | ||
| * @covers ::update_network_option | ||
| */ | ||
| public function test_update_option_with_false_pre_filter_adds_missing_option( $option ) { | ||
| // Filter the old option value to `false`. | ||
| add_filter( 'pre_option_foo', '__return_false' ); | ||
| add_filter( 'pre_site_option_foo', '__return_false' ); | ||
|
Comment on lines
+722
to
+723
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these use the pattern used in other tests here?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't feel strongly about this, but I think having both filters potentially helps uncover future weirdness in behavior more reliably than only having one of them set. Alternatively, we duplicate this test and have both versions covered (one test with both filters, the other with only the one appropriate filter). |
||
|
|
||
| /* | ||
| * When the network option is equal to the filtered version, update option will bail early. | ||
| * Otherwise, The pre-filter will make the old option `false`, which is equal to the | ||
| * default value. This causes an add_network_option() to be triggered. | ||
| */ | ||
|
Comment on lines
+725
to
+729
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this comment is accurate, as this was copy/pasta from the update_option tests. |
||
| if ( false === $option ) { | ||
| $this->assertFalse( update_network_option( null, 'foo', $option ) ); | ||
| } else { | ||
| $this->assertTrue( update_network_option( null, 'foo', $option ) ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Tests that a non-existent option is never added when the pre-filter is not 'false'. | ||
| * | ||
| * @ticket 59360 | ||
| * @dataProvider data_option_values | ||
| * | ||
| * @covers ::update_network_option | ||
| */ | ||
| public function test_update_option_with_truthy_pre_filter_does_not_add_missing_option( $option ) { | ||
| // Filter the old option value to `true`. | ||
| add_filter( 'pre_option_foo', '__return_true' ); | ||
| add_filter( 'pre_site_option_foo', '__return_true' ); | ||
|
|
||
| $this->assertFalse( update_network_option( null, 'foo', $option ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that an existing option is updated even when its pre filter returns the same value. | ||
| * | ||
| * @ticket 59360 | ||
| * @dataProvider data_option_values | ||
| * | ||
| * @covers ::update_network_option | ||
| */ | ||
| public function test_update_option_with_false_pre_filter_updates_option( $option ) { | ||
| // Add the option with a value that is different than any updated. | ||
| add_network_option( null, 'foo', 'bar' ); | ||
|
|
||
| // Force a return value of false. | ||
| add_filter( 'pre_option_foo', '__return_false' ); | ||
| add_filter( 'pre_site_option_foo', '__return_false' ); | ||
|
|
||
| // This should succeed, since the pre-filtered option will be treated as the default. | ||
| $this->assertTrue( update_network_option( null, 'foo', $option ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that an existing option is updated even when its pre filter returns the same value. | ||
| * | ||
| * @ticket 59360 | ||
| * @dataProvider data_option_values | ||
| * | ||
| * @covers ::update_network_option | ||
| */ | ||
| public function test_update_option_with_true_pre_filter_updates_option( $option ) { | ||
| // Add the option with a value that is different than any updated. | ||
| update_network_option( null, 'foo', 'bar' ); | ||
|
|
||
| // Force a return value of true. | ||
| add_filter( 'pre_option_foo', '__return_true' ); | ||
| add_filter( 'pre_site_option_foo', '__return_true' ); | ||
|
|
||
| /* | ||
| * If the option will result in the same DB value, the option should not | ||
| * be updated. Otherwise, the option should be updated regardless of the prefilter. | ||
| */ | ||
| if ( _is_equal_database_value( $option, true ) ) { | ||
| $this->assertFalse( update_network_option( null, 'foo', $option ) ); | ||
| } else { | ||
| $this->assertTrue( update_network_option( null, 'foo', $option ) ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Tests that a non-existent option is added even when its pre filter returns a value. | ||
| * | ||
| * @ticket 59360 | ||
| * | ||
| * @covers ::update_network_option | ||
| */ | ||
| public function test_update_network_option_with_pre_filter_adds_missing_option() { | ||
| public function test_update_network_option_with_pre_filter_does_not_missing_option() { | ||
| $hook_name = is_multisite() ? 'pre_site_option_foo' : 'pre_option_foo'; | ||
|
|
||
| // Force a return value of integer 0. | ||
|
|
@@ -706,7 +814,7 @@ public function test_update_network_option_with_pre_filter_adds_missing_option() | |
| * This should succeed, since the 'foo' option does not exist in the database. | ||
| * The default value is false, so it differs from 0. | ||
|
Comment on lines
814
to
815
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update inline comment |
||
| */ | ||
| $this->assertTrue( update_network_option( null, 'foo', 0 ) ); | ||
| $this->assertFalse( update_network_option( null, 'foo', 0 ) ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -716,7 +824,7 @@ public function test_update_network_option_with_pre_filter_adds_missing_option() | |
| * | ||
| * @covers ::update_network_option | ||
| */ | ||
| public function test_update_network_option_with_pre_filter_updates_option_with_different_value() { | ||
| public function test_update_network_option_with_pre_filter_does_not_option_with_different_value() { | ||
| $hook_name = is_multisite() ? 'pre_site_option_foo' : 'pre_option_foo'; | ||
|
|
||
| // Add the option with a value of 1 to the database. | ||
|
|
@@ -729,7 +837,7 @@ public function test_update_network_option_with_pre_filter_updates_option_with_d | |
| * This should succeed, since the 'foo' option has a value of 1 in the database. | ||
| * Therefore it differs from 0 and should be updated. | ||
|
Comment on lines
837
to
838
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update inline comment |
||
| */ | ||
| $this->assertTrue( update_network_option( null, 'foo', 0 ) ); | ||
| $this->assertFalse( update_network_option( null, 'foo', 0 ) ); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't verify but this will introduce error in Yoast. Per https://core.trac.wordpress.org/ticket/59360#comment:35