Skip to content

Commit 46e9d18

Browse files
Abilities API: cover a custom registered setting in the core/settings tests
Register a setting with show_in_abilities and assert it is exposed in the ability's input enum, output schema, and execute output.
1 parent a28c976 commit 46e9d18

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

tests/phpunit/tests/abilities-api/wpRegisterCoreSettingsAbility.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ public static function set_up_before_class(): void {
2525

2626
register_initial_settings();
2727

28+
// A non-core setting flagged for the Abilities API, to verify that any registered
29+
// setting (not just the core ones) is exposed by the ability.
30+
register_setting(
31+
'general',
32+
'core_settings_ability_test_option',
33+
array(
34+
'type' => 'integer',
35+
'label' => 'Custom Ability Setting',
36+
'description' => 'A custom setting exposed through the Abilities API.',
37+
'show_in_abilities' => true,
38+
'default' => 42,
39+
)
40+
);
41+
2842
// Temporarily remove the unhook functions so we can register core abilities.
2943
remove_action( 'wp_abilities_api_categories_init', '_unhook_core_ability_categories_registration', 1 );
3044
remove_action( 'wp_abilities_api_init', '_unhook_core_abilities_registration', 1 );
@@ -51,6 +65,8 @@ public static function tear_down_after_class(): void {
5165
wp_unregister_ability_category( $ability_category->get_slug() );
5266
}
5367

68+
unregister_setting( 'general', 'core_settings_ability_test_option' );
69+
5470
parent::tear_down_after_class();
5571
}
5672

@@ -180,4 +196,26 @@ public function test_core_settings_requires_manage_options(): void {
180196
$this->assertWPError( $result );
181197
$this->assertSame( 'ability_invalid_permissions', $result->get_error_code() );
182198
}
199+
200+
/**
201+
* A setting registered with `show_in_abilities` (for example by a plugin) is exposed by the ability.
202+
*
203+
* @ticket 64146
204+
*/
205+
public function test_core_settings_exposes_a_custom_registered_setting(): void {
206+
$ability = wp_get_ability( 'core/settings' );
207+
208+
// Present in both the input `settings` enum and the output schema built at registration.
209+
$settings_branch = $ability->get_input_schema()['oneOf'][2];
210+
$this->assertContains( 'core_settings_ability_test_option', $settings_branch['properties']['settings']['items']['enum'] );
211+
$this->assertArrayHasKey( 'core_settings_ability_test_option', $ability->get_output_schema()['properties'] );
212+
213+
// And returned, correctly typed, by execute.
214+
$this->become_admin();
215+
update_option( 'core_settings_ability_test_option', 7 );
216+
217+
$result = $ability->execute( array( 'settings' => array( 'core_settings_ability_test_option' ) ) );
218+
219+
$this->assertSame( array( 'core_settings_ability_test_option' => 7 ), $result );
220+
}
183221
}

0 commit comments

Comments
 (0)