| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group option |
| 5 | */ |
| 6 | class Tests_Option_Registration extends WP_UnitTestCase { |
| 7 | public function test_register() { |
| 8 | register_setting( 'test_group', 'test_option' ); |
| 9 | |
| 10 | $registered = get_registered_settings(); |
| 11 | $this->assertArrayHasKey( 'test_option', $registered ); |
| 12 | |
| 13 | $args = $registered['test_option']; |
| 14 | $this->assertEquals( 'test_group', $args['group'] ); |
| 15 | |
| 16 | // Check defaults. |
| 17 | $this->assertEquals( 'string', $args['type'] ); |
| 18 | $this->assertEquals( false, $args['show_in_rest'] ); |
| 19 | $this->assertEquals( '', $args['description'] ); |
| 20 | } |
| 21 | |
| 22 | public function test_register_with_callback() { |
| 23 | register_setting( 'test_group', 'test_option', array( $this, 'filter_registered_setting' ) ); |
| 24 | |
| 25 | $filtered = apply_filters( 'sanitize_option_test_option', 'smart', 'test_option', 'smart' ); |
| 26 | $this->assertEquals( 'S-M-R-T', $filtered ); |
| 27 | } |
| 28 | |
| 29 | public function test_register_with_array() { |
| 30 | register_setting( 'test_group', 'test_option', array( |
| 31 | 'sanitize_callback' => array( $this, 'filter_registered_setting' ), |
| 32 | )); |
| 33 | |
| 34 | $filtered = apply_filters( 'sanitize_option_test_option', 'smart', 'test_option', 'smart' ); |
| 35 | $this->assertEquals( 'S-M-R-T', $filtered ); |
| 36 | } |
| 37 | |
| 38 | public function filter_registered_setting() { |
| 39 | return 'S-M-R-T'; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @expectedDeprecated register_setting |
| 44 | */ |
| 45 | public function test_register_deprecated_group_misc() { |
| 46 | register_setting( 'misc', 'test_option' ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @expectedDeprecated register_setting |
| 51 | */ |
| 52 | public function test_register_deprecated_group_privacy() { |
| 53 | register_setting( 'privacy', 'test_option' ); |
| 54 | } |
| 55 | } |