| | 37 | function test_default_filter() { |
| | 38 | $return_foo = function ( $value ) { |
| | 39 | return 'foo'; |
| | 40 | }; |
| | 41 | |
| | 42 | $random = rand_str(); |
| | 43 | |
| | 44 | $this->assertFalse( get_option( 'doesnotexist' ) ); |
| | 45 | |
| | 46 | // Default filter overrides $default arg. |
| | 47 | add_filter( 'default_option_doesnotexist', $return_foo ); |
| | 48 | $this->assertEquals( 'foo', get_option( 'doesnotexist', 'bar' ) ); |
| | 49 | |
| | 50 | // Remove the filter and the $default arg is honored. |
| | 51 | remove_filter( 'default_option_doesnotexist', $return_foo ); |
| | 52 | $this->assertEquals( 'bar', get_option( 'doesnotexist', 'bar' ) ); |
| | 53 | |
| | 54 | // Once the option exists, the $default arg and the default filter are ignored. |
| | 55 | add_option( 'doesnotexist', $random ); |
| | 56 | $this->assertEquals( $random, get_option( 'doesnotexist', 'foo' ) ); |
| | 57 | add_filter( 'default_option_doesnotexist', $return_foo ); |
| | 58 | $this->assertEquals( $random, get_option( 'doesnotexist', 'foo' ) ); |
| | 59 | remove_filter( 'default_option_doesnotexist', $return_foo ); |
| | 60 | |
| | 61 | // Cleanup |
| | 62 | $this->assertTrue( delete_option( 'doesnotexist' ) ); |
| | 63 | $this->assertFalse( get_option( 'doesnotexist' ) ); |
| | 64 | } |
| | 65 | |
| | 114 | function test_default_filter() { |
| | 115 | $return_foo = function ( $value ) { |
| | 116 | return 'foo'; |
| | 117 | }; |
| | 118 | |
| | 119 | $random = rand_str(); |
| | 120 | |
| | 121 | $this->assertFalse( get_site_option( 'doesnotexist' ) ); |
| | 122 | |
| | 123 | // Default filter overrides $default arg. |
| | 124 | add_filter( 'default_site_option_doesnotexist', $return_foo ); |
| | 125 | $this->assertEquals( 'foo', get_site_option( 'doesnotexist', 'bar' ) ); |
| | 126 | |
| | 127 | // Remove the filter and the $default arg is honored. |
| | 128 | remove_filter( 'default_site_option_doesnotexist', $return_foo ); |
| | 129 | $this->assertEquals( 'bar', get_site_option( 'doesnotexist', 'bar' ) ); |
| | 130 | |
| | 131 | // Once the option exists, the $default arg and the default filter are ignored. |
| | 132 | add_site_option( 'doesnotexist', $random ); |
| | 133 | $this->assertEquals( $random, get_site_option( 'doesnotexist', 'foo' ) ); |
| | 134 | add_filter( 'default_site_option_doesnotexist', $return_foo ); |
| | 135 | $this->assertEquals( $random, get_site_option( 'doesnotexist', 'foo' ) ); |
| | 136 | remove_filter( 'default_site_option_doesnotexist', $return_foo ); |
| | 137 | |
| | 138 | // Cleanup |
| | 139 | $this->assertTrue( delete_site_option( 'doesnotexist' ) ); |
| | 140 | $this->assertFalse( get_site_option( 'doesnotexist' ) ); |
| | 141 | } |
| | 142 | |