Skip to content

Commit 260081f

Browse files
committed
fix tests
1 parent 38c0b0b commit 260081f

File tree

4 files changed

+129
-46
lines changed

4 files changed

+129
-46
lines changed

projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-site-settings-endpoint.php

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
'jetpack_waf_share_data' => '(bool) Whether the WAF should share basic data with Jetpack',
140140
'jetpack_waf_share_debug_data' => '(bool) Whether the WAF should share debug data with Jetpack',
141141
'jetpack_waf_automatic_rules_last_updated_timestamp' => '(int) Timestamp of the last time the automatic rules were updated',
142-
'mcp_settings' => '(string) Whether MCP Settings is enabled and list of enabled abilities',
142+
'mcp_settings' => '(array) Whether MCP Settings is enabled and list of enabled abilities',
143143
),
144144

145145
'response_format' => array(
@@ -420,12 +420,11 @@ public function get_settings_response() {
420420
);
421421
}
422422
}
423+
$mcp_abilities = apply_filters( 'jetpack_mcp_abilities', $mcp_abilities );
423424

424-
$mcp_settings = wp_json_encode(
425-
array(
426-
'mcp_enabled' => ! empty( $mcp_settings['mcp_enabled'] ?? true ),
427-
'mcp_abilities' => $mcp_abilities,
428-
)
425+
$mcp_settings = array(
426+
'mcp_enabled' => ! empty( $mcp_settings['mcp_enabled'] ?? true ),
427+
'mcp_abilities' => $mcp_abilities,
429428
);
430429

431430
$response[ $key ] = array(
@@ -911,7 +910,7 @@ function ( &$value ) {
911910
}
912911
);
913912

914-
$old_subscription_options = get_option( 'subscription_options' );
913+
$old_subscription_options = get_option( 'subscription_options', array() );
915914
$new_subscription_options = array_merge( $old_subscription_options, $filtered_value );
916915

917916
if ( update_option( $key, $new_subscription_options ) ) {
@@ -1230,12 +1229,7 @@ function ( &$value ) {
12301229
break;
12311230

12321231
case 'mcp_settings':
1233-
if ( ! is_string( $value ) ) {
1234-
break;
1235-
}
1236-
1237-
$new_mcp_settings = json_decode( $value, true );
1238-
if ( ! is_array( $new_mcp_settings ) ) {
1232+
if ( ! is_array( $value ) ) {
12391233
break;
12401234
}
12411235

@@ -1248,12 +1242,12 @@ function ( &$value ) {
12481242
}
12491243

12501244
$filtered_value = array(
1251-
'mcp_enabled' => ! empty( $new_mcp_settings['mcp_enabled'] ),
1245+
'mcp_enabled' => ! empty( $value['mcp_enabled'] ),
12521246
'mcp_abilities' => array(),
12531247
);
12541248

1255-
if ( ! empty( $new_mcp_settings['mcp_abilities'] ) && is_array( $new_mcp_settings['mcp_abilities'] ) ) {
1256-
foreach ( $new_mcp_settings['mcp_abilities'] as $ability_name => $ability ) {
1249+
if ( ! empty( $value['mcp_abilities'] ) && is_array( $value['mcp_abilities'] ) ) {
1250+
foreach ( $value['mcp_abilities'] as $ability_name => $ability ) {
12571251
// Validate ability name exists in registered abilities
12581252
if ( ! in_array( $ability_name, $valid_abilities, true ) ) {
12591253
continue;
@@ -1269,7 +1263,30 @@ function ( &$value ) {
12691263
}
12701264

12711265
if ( update_option( $key, $filtered_value ) ) {
1272-
$updated[ $key ] = $filtered_value;
1266+
$response_abilities = array();
1267+
1268+
if ( function_exists( 'wp_get_abilities' ) ) {
1269+
$abilities = wp_get_abilities();
1270+
foreach ( $abilities as $ability ) {
1271+
$ability_name = $ability->get_name();
1272+
if ( ! empty( $ability_name ) ) {
1273+
$is_enabled = $filtered_value['mcp_abilities'][ $ability_name ] ?? false;
1274+
$response_abilities[ $ability_name ] = array(
1275+
'label' => $ability->get_label(),
1276+
'description' => $ability->get_description(),
1277+
'enabled' => $is_enabled,
1278+
);
1279+
}
1280+
}
1281+
}
1282+
1283+
$response_abilities = apply_filters( 'jetpack_mcp_abilities', $response_abilities );
1284+
1285+
// Return enriched structure for API consistency
1286+
$updated[ $key ] = array(
1287+
'mcp_enabled' => $filtered_value['mcp_enabled'],
1288+
'mcp_abilities' => $response_abilities,
1289+
);
12731290
}
12741291
break;
12751292

projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-site-settings-v1-4-endpoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
'jetpack_comment_form_color_scheme' => '(string) The color scheme for the comment form',
154154
'is_fully_managed_agency_site' => '(bool) Whether the site is a fully managed agency site',
155155
'wpcom_hide_action_bar' => '(bool) Whether to hide the Action bar',
156-
'mcp_settings' => '(string) Whether MCP Settings is enabled and list of enabled abilities',
156+
'mcp_settings' => '(array) Whether MCP Settings is enabled and list of enabled abilities',
157157
),
158158

159159
'response_format' => array(

projects/plugins/jetpack/tests/php.multisite.11.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
<testsuite name="json-api">
4040
<file>php/json-api/Jetpack_Json_Api_Endpoints_Test.php</file>
4141
<file>php/json-api/Jetpack_Json_Api_Plugins_Endpoints_Test.php</file>
42-
</testsuite>
42+
<file>php/json-api/WPCOM_JSON_API_Site_Settings_V1_4_Endpoint_Test.php</file>
43+
</testsuite>
4344
<testsuite name="infinite-scroll">
4445
<directory suffix="Test.php">php/modules/infinite-scroll</directory>
4546
</testsuite>

projects/plugins/jetpack/tests/php/json-api/WPCOM_JSON_API_Site_Settings_V1_4_Endpoint_Test.php

Lines changed: 92 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ public function set_up() {
7171
parent::set_up();
7272

7373
WPCOM_JSON_API::init()->token_details = array( 'blog_id' => $blog_id );
74+
75+
// Mock MCP abilities for testing
76+
add_filter(
77+
'jetpack_mcp_abilities',
78+
function () {
79+
return array(
80+
'wpcom-mcp/posts-search' => array(
81+
'label' => 'Posts Search',
82+
'description' => 'Search posts',
83+
'enabled' => true,
84+
),
85+
'wpcom-mcp/user-sites' => array(
86+
'label' => 'User Sites',
87+
'description' => 'Access user sites',
88+
'enabled' => false,
89+
),
90+
);
91+
}
92+
);
93+
}
94+
95+
public function tear_down() {
96+
// Remove the filter to avoid affecting other tests
97+
remove_all_filters( 'jetpack_mcp_abilities' );
98+
parent::tear_down();
7499
}
75100

76101
/**
@@ -272,7 +297,7 @@ public function make_post_request( $setting ) {
272297
'page_on_front' => '(string) The page ID of the page to use as the site\'s homepage. It will apply only if \'show_on_front\' is set to \'page\'.',
273298
'page_for_posts' => '(string) The page ID of the page to use as the site\'s posts page. It will apply only if \'show_on_front\' is set to \'page\'.',
274299
'subscription_options' => '(array) Array of two options used in subscription email templates: \'invitation\' and \'comment_follow\' strings.',
275-
'mcp_settings' => '(string) Whether MCP Settings is enabled and list of enabled abilities',
300+
'mcp_settings' => '(array) Whether MCP Settings is enabled and list of enabled abilities',
276301
),
277302

278303
'response_format' => array(
@@ -308,7 +333,18 @@ public static function setting_default_key_values() {
308333
'mcp_settings',
309334
array(
310335
'mcp_enabled' => true,
311-
'mcp_abilities' => array(),
336+
'mcp_abilities' => array(
337+
'wpcom-mcp/posts-search' => array(
338+
'label' => 'Posts Search',
339+
'description' => 'Search posts',
340+
'enabled' => true,
341+
),
342+
'wpcom-mcp/user-sites' => array(
343+
'label' => 'User Sites',
344+
'description' => 'Access user sites',
345+
'enabled' => false,
346+
),
347+
),
312348
),
313349
),
314350
);
@@ -334,8 +370,16 @@ public static function setting_value_pairs_get_request() {
334370
array(
335371
'mcp_enabled' => true,
336372
'mcp_abilities' => array(
337-
'wpcom-mcp/posts-search' => true,
338-
'wpcom-mcp/user-sites' => false,
373+
'wpcom-mcp/posts-search' => array(
374+
'label' => 'Posts Search',
375+
'description' => 'Search posts',
376+
'enabled' => true,
377+
),
378+
'wpcom-mcp/user-sites' => array(
379+
'label' => 'User Sites',
380+
'description' => 'Access user sites',
381+
'enabled' => false,
382+
),
339383
),
340384
),
341385
),
@@ -373,43 +417,64 @@ public static function setting_value_pairs_post_request() {
373417
),
374418
),
375419
// Add MCP settings POST tests
376-
'mcp_settings valid json' => array(
420+
'mcp_settings valid params' => array(
377421
'mcp_settings',
378-
wp_json_encode(
379-
array(
380-
'mcp_enabled' => true,
381-
'mcp_abilities' => array(
382-
'wpcom-mcp/posts-search' => array( 'enabled' => true ),
383-
'wpcom-mcp/user-sites' => array( 'enabled' => false ),
384-
),
385-
)
422+
array( // This should be the actual input structure
423+
'mcp_enabled' => true,
424+
'mcp_abilities' => array(
425+
'wpcom-mcp/posts-search' => array( 'enabled' => true ),
426+
'wpcom-mcp/user-sites' => array( 'enabled' => false ),
427+
),
386428
),
387-
array(
429+
array( // Expected output
388430
'mcp_enabled' => true,
389431
'mcp_abilities' => array(
390-
'wpcom-mcp/posts-search' => true,
391-
'wpcom-mcp/user-sites' => false,
432+
'wpcom-mcp/posts-search' => array(
433+
'label' => 'Posts Search',
434+
'description' => 'Search posts',
435+
'enabled' => true,
436+
),
437+
'wpcom-mcp/user-sites' => array(
438+
'label' => 'User Sites',
439+
'description' => 'Access user sites',
440+
'enabled' => false,
441+
),
392442
),
393443
),
394444
),
395445
'mcp_settings disabled' => array(
396446
'mcp_settings',
397-
wp_json_encode(
398-
array(
399-
'mcp_enabled' => false,
400-
'mcp_abilities' => array(),
401-
)
447+
array(
448+
'mcp_enabled' => false,
449+
'mcp_abilities' => array(
450+
'wpcom-mcp/posts-search' => array(
451+
'label' => 'Posts Search',
452+
'description' => 'Search posts',
453+
'enabled' => true,
454+
),
455+
'wpcom-mcp/user-sites' => array(
456+
'label' => 'User Sites',
457+
'description' => 'Access user sites',
458+
'enabled' => false,
459+
),
460+
),
402461
),
403462
array(
404463
'mcp_enabled' => false,
405-
'mcp_abilities' => array(),
464+
'mcp_abilities' => array(
465+
'wpcom-mcp/posts-search' => array(
466+
'label' => 'Posts Search',
467+
'description' => 'Search posts',
468+
'enabled' => true,
469+
),
470+
'wpcom-mcp/user-sites' => array(
471+
'label' => 'User Sites',
472+
'description' => 'Access user sites',
473+
'enabled' => false,
474+
),
475+
),
406476
),
407477
),
408-
'mcp_settings malformed json' => array(
409-
'mcp_settings',
410-
'invalid json string',
411-
false, // Should fail validation
412-
),
413478
);
414479
}
415480
}

0 commit comments

Comments
 (0)