diff --git a/projects/packages/premium-analytics/changelog/update-pa-widget-dashboard-backend b/projects/packages/premium-analytics/changelog/update-pa-widget-dashboard-backend new file mode 100644 index 000000000000..c73c26c0ff5d --- /dev/null +++ b/projects/packages/premium-analytics/changelog/update-pa-widget-dashboard-backend @@ -0,0 +1,4 @@ +Significance: patch +Type: added + +Dashboard widgets: carry declarative actions and icon references from the widget manifest to the registry and the widget-modules REST record. diff --git a/projects/packages/premium-analytics/src/class-widget-type.php b/projects/packages/premium-analytics/src/class-widget-type.php index 5c98791a8521..f3b9fd591c01 100644 --- a/projects/packages/premium-analytics/src/class-widget-type.php +++ b/projects/packages/premium-analytics/src/class-widget-type.php @@ -112,6 +112,28 @@ class Widget_Type { */ public $help = null; + /** + * Registered icon name (`collection/icon-name`), resolved on the client + * through the application's icon resolver. + * + * Null when the widget did not declare the field. + * + * @var string|null + */ + public $icon = null; + + /** + * Declarative actions the widget exposes. Each entry carries `id`, + * `label`, `href`, and optional `download`/`openInNewTab`/`icon`/ + * `relevance`. Labels are translated at registration time using the + * widget's text domain. + * + * Null when the widget did not declare the field. + * + * @var array|null + */ + public $actions = null; + /** * Alternative terms used to match the widget type when searching, * e.g. "calendar" for an events widget. Translated at registration diff --git a/projects/packages/premium-analytics/src/widget-i18n.json b/projects/packages/premium-analytics/src/widget-i18n.json index 859fa3ef5dc8..a5ef9b522f6a 100644 --- a/projects/packages/premium-analytics/src/widget-i18n.json +++ b/projects/packages/premium-analytics/src/widget-i18n.json @@ -5,5 +5,6 @@ "content": "widget help content", "links": [ { "label": "widget help link label" } ] }, + "actions": [ { "label": "widget action label" } ], "keywords": [ "widget keyword" ] } diff --git a/projects/packages/premium-analytics/src/widget-modules.php b/projects/packages/premium-analytics/src/widget-modules.php index aab1344e9cbf..a384dd5be796 100644 --- a/projects/packages/premium-analytics/src/widget-modules.php +++ b/projects/packages/premium-analytics/src/widget-modules.php @@ -77,6 +77,8 @@ function get_widget_modules_response() { 'title' => $widget_type->title, 'description' => $widget_type->description, 'help' => $widget_type->help, + 'icon' => $widget_type->icon, + 'actions' => $widget_type->actions, 'keywords' => $widget_type->keywords, ); } diff --git a/projects/packages/premium-analytics/src/widget-types.php b/projects/packages/premium-analytics/src/widget-types.php index 84e69bca4b42..00463a410e32 100644 --- a/projects/packages/premium-analytics/src/widget-types.php +++ b/projects/packages/premium-analytics/src/widget-types.php @@ -5,7 +5,8 @@ * Copies the wp-build manifest (`jpa_get_registered_widget_modules()`) into the * in-memory Widget_Type_Registry, so the plugin queries the registry instead * of re-parsing the manifest. On the way in, user-facing metadata strings are - * translated (per the widget-i18n.json schema) and the `help` note sanitized. + * translated (per the widget-i18n.json schema) and the `help`, `icon`, and + * `actions` fields sanitized. * * This is the problem-agnostic "core" layer (a PA-namespaced copy of the * experimental Gutenberg API): it exposes the hooks a consumer uses to scope @@ -58,10 +59,11 @@ function get_widget_metadata_i18n_schema() { /** * Translates a widget's user-facing metadata strings. * - * Runs `title`, `description`, `help`, and `keywords` through the widget - * i18n schema, leaving every other key untouched. Unlike the upstream copy, - * a widget with no `textdomain` falls back to the package text domain - * instead of skipping translation: every bundled widget shares it. + * Runs `title`, `description`, `help`, `actions`, and `keywords` through + * the widget i18n schema, leaving every other key untouched. Unlike the + * upstream copy, a widget with no `textdomain` falls back to the package + * text domain instead of skipping translation: every bundled widget + * shares it. * * @param array $widget Widget data from the build manifest. * @return array Widget data with its translatable strings localized. @@ -70,7 +72,7 @@ function translate_widget_metadata( $widget ) { $textdomain = ! empty( $widget['textdomain'] ) ? $widget['textdomain'] : 'jetpack-premium-analytics-pkg'; $i18n_schema = get_widget_metadata_i18n_schema(); - foreach ( array( 'title', 'description', 'help', 'keywords' ) as $field ) { + foreach ( array( 'title', 'description', 'help', 'actions', 'keywords' ) as $field ) { if ( isset( $widget[ $field ] ) && isset( $i18n_schema->$field ) ) { $widget[ $field ] = translate_settings_using_i18n_schema( $i18n_schema->$field, $widget[ $field ], $textdomain ); } @@ -125,6 +127,148 @@ function sanitize_widget_help( $help ) { return $sanitized; } +/** + * Resolves an action href to the form `esc_url_raw()` can judge. + * + * Absolute, scheme-relative, root-relative, and single-segment admin `.php` + * hrefs pass through unchanged. Returns '' for path traversal and for any + * other relative href, so `esc_url_raw()` cannot invent `http://filename`. + * Unlike upstream, widget-local files are never resolved: `widgets/` does + * not ship with the package, only its build output does. + * + * @param string $href Action href. + * @return string The href to escape, or ''. + */ +function resolve_widget_action_href( $href ) { + if ( ! is_string( $href ) || '' === $href ) { + return ''; + } + + // Absolute, scheme-relative, or schemed, including URLs with `..` in the path. + if ( preg_match( '#^([a-z][a-z0-9+.-]*:)?//#i', $href ) || str_contains( $href, ':' ) ) { + return $href; + } + + // Root-relative paths (e.g. /wp-admin/..., /report.csv). + if ( str_starts_with( $href, '/' ) ) { + return $href; + } + + if ( str_contains( $href, '..' ) ) { + return ''; + } + + $path_only = preg_split( '/[?#]/', $href, 2 )[0]; + if ( str_ends_with( strtolower( $path_only ), '.php' ) ) { + // Single-segment admin entry points stay as-is. Deeper relative + // paths would come out of `esc_url_raw()` as `http://` URLs. + return str_contains( $path_only, '/' ) ? '' : $href; + } + + return ''; +} + +/** + * Sanitizes widget actions to `id` / `label` / `href` (via `esc_url_raw()`), + * plus optional `download` / `openInNewTab` / `icon` / `relevance`. Drops + * incomplete or unsafe entries and reports dropped hrefs through + * `_doing_it_wrong()`; a malformed `icon` or `relevance` drops the key, never + * the action, and a `download` name that sanitizes to nothing becomes `true`. + * + * @param mixed $actions Actions from the build manifest. + * @return array|null Sanitized actions, or null when none survive. + */ +function sanitize_widget_actions( $actions ) { + if ( ! is_array( $actions ) ) { + return null; + } + + $sanitized = array(); + foreach ( $actions as $action ) { + if ( + ! is_array( $action ) || + ! isset( $action['id'] ) || + ! isset( $action['label'] ) || + ! isset( $action['href'] ) || + ! is_string( $action['id'] ) || + ! is_string( $action['label'] ) || + ! is_string( $action['href'] ) || + '' === $action['id'] || + '' === $action['label'] || + '' === $action['href'] + ) { + continue; + } + + $href = esc_url_raw( resolve_widget_action_href( $action['href'] ) ); + if ( ! $href ) { + $message = sprintf( + /* translators: 1: Widget action id. 2: Declared action href. */ + __( 'Dropped widget action "%1$s": href "%2$s" is not an allowed URL.', 'jetpack-premium-analytics-pkg' ), + $action['id'], + $action['href'] + ); + // One line: tools/replace-next-version-tag.sh only rewrites the token in a single-line call. + _doing_it_wrong( __FUNCTION__, esc_html( $message ), 'jetpack-premium-analytics-$$next-version$$' ); + continue; + } + + $entry = array( + 'id' => $action['id'], + 'label' => $action['label'], + 'href' => $href, + ); + + if ( isset( $action['download'] ) ) { + if ( is_bool( $action['download'] ) ) { + $entry['download'] = $action['download']; + } else { + // A name that sanitizes to nothing keeps the download under the original name; only `false` navigates. + $filename = sanitize_file_name( (string) $action['download'] ); + $entry['download'] = '' !== $filename ? $filename : true; + } + } + + if ( isset( $action['openInNewTab'] ) ) { + $entry['openInNewTab'] = (bool) $action['openInNewTab']; + } + + if ( isset( $action['icon'] ) ) { + $icon = sanitize_widget_icon( $action['icon'] ); + if ( $icon ) { + $entry['icon'] = $icon; + } + } + + if ( isset( $action['relevance'] ) && in_array( $action['relevance'], array( 'high', 'medium', 'low' ), true ) ) { + $entry['relevance'] = $action['relevance']; + } + + $sanitized[] = $entry; + } + + return $sanitized ? $sanitized : null; +} + +/** + * Constrains a widget icon reference to a registered icon name + * (`collection/icon-name`); anything else drops silently to no icon. + * + * @param mixed $icon Icon reference from the build manifest. + * @return string|null The icon name, or null when the shape does not match. + */ +function sanitize_widget_icon( $icon ) { + if ( ! is_string( $icon ) || '' === $icon ) { + return null; + } + + if ( ! preg_match( '#^[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?/[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?$#', $icon ) ) { + return null; + } + + return $icon; +} + /** * Hydrates the widget type registry from the build manifest. * @@ -173,6 +317,8 @@ function register_widget_types() { 'title' => $widget['title'] ?? null, 'description' => $widget['description'] ?? null, 'help' => sanitize_widget_help( $widget['help'] ?? null ), + 'icon' => sanitize_widget_icon( $widget['icon'] ?? null ), + 'actions' => sanitize_widget_actions( $widget['actions'] ?? null ), 'keywords' => $widget['keywords'] ?? null, ) ); diff --git a/projects/packages/premium-analytics/tests/php/Analytics_Test.php b/projects/packages/premium-analytics/tests/php/Analytics_Test.php index 82c6926ba7fd..12590f3ca1ff 100644 --- a/projects/packages/premium-analytics/tests/php/Analytics_Test.php +++ b/projects/packages/premium-analytics/tests/php/Analytics_Test.php @@ -15,6 +15,8 @@ use PHPUnit\Framework\Attributes\RunInSeparateProcess; use PHPUnit\Framework\TestCase; +require_once __DIR__ . '/traits/trait-widget-manifest-fixture.php'; + /** * Tests for the Analytics class. Also covers ensure_widget_registry_ready(): only * test_rest_request_still_serves_the_widget_manifest exercises it, and php-code-coverage @@ -26,6 +28,7 @@ #[CoversClass( Analytics::class )] #[CoversFunction( 'Automattic\\Jetpack\\PremiumAnalytics\\ensure_widget_registry_ready' )] class Analytics_Test extends TestCase { + use Widget_Manifest_Fixture_Trait; const MENU_SLUG = 'jetpack-premium-analytics-wp-admin'; const MENU_HOOKNAME = 'toplevel_page_' . self::MENU_SLUG; @@ -430,24 +433,6 @@ public function test_rest_request_still_serves_the_widget_manifest() { } } - /** - * Point the manifest require at the fixture manifest. - * - * @return string - */ - public function use_fixture_widget_manifest() { - return __DIR__ . '/fixtures/build-entry/widgets.php'; - } - - /** - * Point the manifest at a missing file. - * - * @return string - */ - public function use_absent_widget_manifest() { - return __DIR__ . '/fixtures/build-entry/no-such-widgets.php'; - } - /** * The wp-admin integrated dashboard slug in admin is recognized as a dashboard request. */ diff --git a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php index 47f6c18706ac..955be268223f 100644 --- a/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php +++ b/projects/packages/premium-analytics/tests/php/Widget_Metadata_Test.php @@ -14,20 +14,28 @@ require_once __DIR__ . '/../../src/widget-types.php'; require_once __DIR__ . '/../../src/widget-modules.php'; require_once __DIR__ . '/fixtures/widget-modules-manifest.php'; +require_once __DIR__ . '/traits/trait-widget-manifest-fixture.php'; /** * @covers ::Automattic\Jetpack\PremiumAnalytics\register_widget_types * @covers ::Automattic\Jetpack\PremiumAnalytics\translate_widget_metadata * @covers ::Automattic\Jetpack\PremiumAnalytics\sanitize_widget_help + * @covers ::Automattic\Jetpack\PremiumAnalytics\sanitize_widget_icon + * @covers ::Automattic\Jetpack\PremiumAnalytics\sanitize_widget_actions + * @covers ::Automattic\Jetpack\PremiumAnalytics\resolve_widget_action_href * @covers ::Automattic\Jetpack\PremiumAnalytics\get_widget_metadata_i18n_schema * @covers ::Automattic\Jetpack\PremiumAnalytics\get_widget_modules_response */ #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\register_widget_types' )] #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\translate_widget_metadata' )] #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\sanitize_widget_help' )] +#[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\sanitize_widget_icon' )] +#[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\sanitize_widget_actions' )] +#[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\resolve_widget_action_href' )] #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\get_widget_metadata_i18n_schema' )] #[CoversFunction( 'Automattic\Jetpack\PremiumAnalytics\get_widget_modules_response' )] class Widget_Metadata_Test extends BaseTestCase { + use Widget_Manifest_Fixture_Trait; /** * Without a declared textdomain, strings are translated under the package @@ -81,6 +89,36 @@ public function test_translate_widget_metadata_honors_declared_textdomain() { $this->assertContains( array( 'Hello world', 'widget title', 'my-widget-pack' ), $calls, 'The declared textdomain is used for translation.' ); } + /** + * Action labels are translated with the action label context; the other + * action keys pass through untouched. + */ + public function test_translate_widget_metadata_translates_action_labels() { + $calls = array(); + $callback = static function ( $translation, $text, $context, $domain ) use ( &$calls ) { + $calls[] = array( $text, $context, $domain ); + return $translation; + }; + add_filter( 'gettext_with_context', $callback, 10, 4 ); + + $widget = translate_widget_metadata( + array( + 'actions' => array( + array( + 'id' => 'export', + 'label' => 'Export CSV', + 'href' => 'https://example.com/export.csv', + ), + ), + ) + ); + + remove_filter( 'gettext_with_context', $callback ); + + $this->assertContains( array( 'Export CSV', 'widget action label', 'jetpack-premium-analytics-pkg' ), $calls, 'Each action label is translated under the package domain.' ); + $this->assertSame( 'https://example.com/export.csv', $widget['actions'][0]['href'], 'Non-translatable action keys pass through untouched.' ); + } + /** * Hydration registers manifest candidates with metadata translated, * the help note sanitized, and every field mapped onto the type. @@ -104,6 +142,18 @@ public function test_register_widget_types_hydrates_metadata_from_manifest() { ), ), ), + 'icon' => 'core/chart-bar', + 'actions' => array( + array( + 'id' => 'export', + 'label' => 'Export CSV', + 'href' => 'https://example.com/export.csv', + 'download' => true, + 'icon' => 'core/download', + 'relevance' => 'high', + 'extra' => 'dropped', + ), + ), 'keywords' => array( 'sentinel' ), ), ); @@ -134,6 +184,21 @@ public function test_register_widget_types_hydrates_metadata_from_manifest() { $widget_type->help, 'The help note is sanitized during hydration.' ); + $this->assertSame( 'core/chart-bar', $widget_type->icon, 'The icon reference is mapped.' ); + $this->assertSame( + array( + array( + 'id' => 'export', + 'label' => 'Export CSV', + 'href' => 'https://example.com/export.csv', + 'download' => true, + 'icon' => 'core/download', + 'relevance' => 'high', + ), + ), + $widget_type->actions, + 'The actions are sanitized during hydration.' + ); } finally { Widget_Type_Registry::get_instance()->unregister( 'test/hydration-sentinel' ); unset( $GLOBALS['jpa_test_widget_manifest'] ); @@ -247,6 +312,285 @@ public function test_sanitize_widget_help_omits_links_when_none_survive() { ); } + /** + * Only `collection/icon-name` references survive; anything else drops to null. + */ + public function test_sanitize_widget_icon_accepts_only_registered_icon_names() { + $this->assertSame( 'core/chart-bar', sanitize_widget_icon( 'core/chart-bar' ), 'A registered icon name passes through.' ); + $this->assertSame( 'jpa/site_logo-2', sanitize_widget_icon( 'jpa/site_logo-2' ), 'Digits, hyphens and underscores are allowed inside a segment.' ); + $this->assertNull( sanitize_widget_icon( null ), 'Null stays null.' ); + $this->assertNull( sanitize_widget_icon( '' ), 'An empty string drops.' ); + $this->assertNull( sanitize_widget_icon( 'chart-bar' ), 'A bare name without a collection drops.' ); + $this->assertNull( sanitize_widget_icon( 'Core/Plus' ), 'Uppercase letters drop.' ); + $this->assertNull( sanitize_widget_icon( 'core/-plus' ), 'A segment cannot start with a separator.' ); + $this->assertNull( sanitize_widget_icon( 'core/plus/extra' ), 'Only two segments are allowed.' ); + $this->assertNull( sanitize_widget_icon( array( 'core/plus' ) ), 'A non-string reference drops.' ); + } + + /** + * Entries missing a non-empty string id, label or href are dropped; when + * none survive the result is null. + */ + public function test_sanitize_widget_actions_drops_incomplete_entries() { + $this->assertNull( sanitize_widget_actions( null ), 'Null input stays null.' ); + $this->assertNull( sanitize_widget_actions( 'export' ), 'A non-array input is dropped.' ); + $this->assertNull( + sanitize_widget_actions( + array( + 'not-an-action', + array( + 'label' => 'No id', + 'href' => 'https://example.com', + ), + array( + 'id' => 'no-label', + 'href' => 'https://example.com', + ), + array( + 'id' => 'no-href', + 'label' => 'No href', + ), + array( + 'id' => '', + 'label' => 'Empty id', + 'href' => 'https://example.com', + ), + array( + 'id' => 42, + 'label' => 'Numeric id', + 'href' => 'https://example.com', + ), + ) + ), + 'Incomplete entries are dropped and an empty result becomes null.' + ); + } + + /** + * Hrefs go through resolve_widget_action_href() and esc_url_raw(): absolute, + * scheme-relative, root-relative and single-segment admin `.php` hrefs pass; + * everything else drops the action with a _doing_it_wrong() notice. + */ + public function test_sanitize_widget_actions_keeps_only_allowed_hrefs() { + $notices = array(); + $callback = static function ( $function_name ) use ( &$notices ) { + $notices[] = $function_name; + }; + add_action( 'doing_it_wrong_run', $callback ); + add_filter( 'doing_it_wrong_trigger_error', '__return_false' ); + + $actions = sanitize_widget_actions( + array( + array( + 'id' => 'absolute', + 'label' => 'Absolute', + 'href' => 'https://example.com/report.csv?range=7d', + ), + array( + 'id' => 'scheme-relative', + 'label' => 'Scheme relative', + 'href' => '//example.com/report.csv', + ), + array( + 'id' => 'root', + 'label' => 'Root relative', + 'href' => '/wp-admin/export.php?type=csv', + ), + array( + 'id' => 'admin', + 'label' => 'Admin entry point', + 'href' => 'export.php?type=csv', + ), + array( + 'id' => 'script', + 'label' => 'Script', + 'href' => 'javascript:alert(1)', + ), + array( + 'id' => 'traversal', + 'label' => 'Traversal', + 'href' => '../secret.csv', + ), + array( + 'id' => 'nested-php', + 'label' => 'Nested PHP', + 'href' => 'sub/dir.php', + ), + array( + 'id' => 'local-file', + 'label' => 'Local file', + 'href' => 'report.csv', + ), + ) + ); + + remove_filter( 'doing_it_wrong_trigger_error', '__return_false' ); + remove_action( 'doing_it_wrong_run', $callback ); + + $this->assertSame( + array( + array( + 'id' => 'absolute', + 'label' => 'Absolute', + 'href' => 'https://example.com/report.csv?range=7d', + ), + array( + 'id' => 'scheme-relative', + 'label' => 'Scheme relative', + 'href' => '//example.com/report.csv', + ), + array( + 'id' => 'root', + 'label' => 'Root relative', + 'href' => '/wp-admin/export.php?type=csv', + ), + array( + 'id' => 'admin', + 'label' => 'Admin entry point', + 'href' => 'export.php?type=csv', + ), + ), + $actions, + 'Only absolute, scheme-relative, root-relative and single-segment .php hrefs survive, unchanged.' + ); + $this->assertSame( array_fill( 0, 4, __NAMESPACE__ . '\\sanitize_widget_actions' ), $notices, 'Each dropped href is reported through _doing_it_wrong().' ); + } + + /** + * Optional fields are normalized: `download` keeps booleans and sanitizes + * filenames, `openInNewTab` casts to bool, and a malformed `icon` or + * `relevance` drops the key but keeps the action. Unknown keys never ride along. + */ + public function test_sanitize_widget_actions_normalizes_optional_fields() { + $this->assertSame( + array( + array( + 'id' => 'download-bool', + 'label' => 'Download', + 'href' => 'https://example.com/report.csv', + 'download' => true, + 'openInNewTab' => true, + 'icon' => 'core/download', + 'relevance' => 'high', + ), + array( + 'id' => 'download-name', + 'label' => 'Download as', + 'href' => 'https://example.com/report.csv', + 'download' => 'My-Report.csv', + ), + ), + sanitize_widget_actions( + array( + array( + 'id' => 'download-bool', + 'label' => 'Download', + 'href' => 'https://example.com/report.csv', + 'download' => true, + 'openInNewTab' => 1, + 'icon' => 'core/download', + 'relevance' => 'high', + 'extra' => 'dropped', + ), + array( + 'id' => 'download-name', + 'label' => 'Download as', + 'href' => 'https://example.com/report.csv', + 'download' => 'My Report.csv', + 'icon' => 'not a reference', + 'relevance' => 'urgent', + ), + ) + ), + 'Optional fields are normalized and unknown keys dropped.' + ); + } + + /** + * Every download value but `false` keeps the download: a usable filename + * survives sanitization, anything else falls back to the original name. + */ + public function test_sanitize_widget_actions_keeps_download_intent() { + $href = 'https://example.com/export.csv'; + + $actions = sanitize_widget_actions( + array( + array( + 'id' => 'empty', + 'label' => 'Empty string', + 'href' => $href, + 'download' => '', + ), + array( + 'id' => 'zero', + 'label' => 'Zero', + 'href' => $href, + 'download' => '0', + ), + array( + 'id' => 'unusable', + 'label' => 'Unusable name', + 'href' => $href, + 'download' => '???', + ), + array( + 'id' => 'flag', + 'label' => 'Boolean true', + 'href' => $href, + 'download' => true, + ), + array( + 'id' => 'off', + 'label' => 'Boolean false', + 'href' => $href, + 'download' => false, + ), + ) + ); + + $this->assertSame( array( true, '0', true, true, false ), array_column( $actions, 'download' ), 'Only false means navigation; an unusable name downloads under the original one.' ); + } + + /** + * The three relevance tiers survive; any other value drops the key and + * keeps the action. + */ + public function test_sanitize_widget_actions_keeps_every_relevance_tier() { + $href = 'https://example.com/report'; + + $actions = sanitize_widget_actions( + array( + array( + 'id' => 'high', + 'label' => 'High', + 'href' => $href, + 'relevance' => 'high', + ), + array( + 'id' => 'medium', + 'label' => 'Medium', + 'href' => $href, + 'relevance' => 'medium', + ), + array( + 'id' => 'low', + 'label' => 'Low', + 'href' => $href, + 'relevance' => 'low', + ), + array( + 'id' => 'unknown', + 'label' => 'Unknown', + 'href' => $href, + 'relevance' => 'urgent', + ), + ) + ); + + $this->assertSame( array( 'high', 'medium', 'low' ), array_column( $actions, 'relevance' ), 'Every tier survives in order.' ); + $this->assertSame( array( 'high', 'medium', 'low', 'unknown' ), array_column( $actions, 'id' ), 'An unknown tier keeps the action.' ); + } + /** * A registered widget type's metadata reaches the widget-modules REST * record intact. @@ -263,13 +607,27 @@ public function test_widget_modules_record_carries_metadata() { 'title' => 'Sentinel', 'description' => 'Metadata carrier.', 'help' => array( 'content' => 'Helpful.' ), + 'icon' => 'core/chart-bar', + 'actions' => array( + array( + 'id' => 'export', + 'label' => 'Export', + 'href' => 'https://example.com/export.csv', + ), + ), 'keywords' => array( 'sentinel' ), ) ); - $records = get_widget_modules_response()->get_data(); + add_filter( 'jetpack_premium_analytics_widgets_manifest_path', array( $this, 'use_fixture_widget_manifest' ) ); - $registry->unregister( 'test/metadata-sentinel' ); + $records = array(); + try { + $records = get_widget_modules_response()->get_data(); + } finally { + remove_filter( 'jetpack_premium_analytics_widgets_manifest_path', array( $this, 'use_fixture_widget_manifest' ) ); + $registry->unregister( 'test/metadata-sentinel' ); + } $record = null; foreach ( $records as $candidate ) { @@ -283,6 +641,18 @@ public function test_widget_modules_record_carries_metadata() { $this->assertSame( 'Sentinel', $record['title'], 'The title reaches the record.' ); $this->assertSame( 'Metadata carrier.', $record['description'], 'The description reaches the record.' ); $this->assertSame( array( 'content' => 'Helpful.' ), $record['help'], 'The help note reaches the record.' ); + $this->assertSame( 'core/chart-bar', $record['icon'], 'The icon reference reaches the record.' ); + $this->assertSame( + array( + array( + 'id' => 'export', + 'label' => 'Export', + 'href' => 'https://example.com/export.csv', + ), + ), + $record['actions'], + 'The actions reach the record.' + ); $this->assertSame( array( 'sentinel' ), $record['keywords'], 'The keywords reach the record.' ); } } diff --git a/projects/packages/premium-analytics/tests/php/Widget_Modules_Test.php b/projects/packages/premium-analytics/tests/php/Widget_Modules_Test.php index 3c39af7a1aae..4c7516d259f9 100644 --- a/projects/packages/premium-analytics/tests/php/Widget_Modules_Test.php +++ b/projects/packages/premium-analytics/tests/php/Widget_Modules_Test.php @@ -11,11 +11,13 @@ use WP_REST_Server; require_once __DIR__ . '/../../src/widget-modules.php'; +require_once __DIR__ . '/traits/trait-widget-manifest-fixture.php'; /** * Tests for Premium Analytics widget module discovery. */ class Widget_Modules_Test extends TestCase { + use Widget_Manifest_Fixture_Trait; const ROUTE = '/wpcom/v2/widget-modules'; const LEGACY_ROUTE = '/jetpack/v4/widget-modules'; @@ -73,7 +75,12 @@ public function test_widget_modules_route_is_gated_on_the_dashboard_capability() * the process has touched the registry yet. */ public function test_response_hydrates_the_registry_on_first_use() { - $response = get_widget_modules_response(); + add_filter( 'jetpack_premium_analytics_widgets_manifest_path', array( $this, 'use_fixture_widget_manifest' ) ); + try { + $response = get_widget_modules_response(); + } finally { + remove_filter( 'jetpack_premium_analytics_widgets_manifest_path', array( $this, 'use_fixture_widget_manifest' ) ); + } $this->assertInstanceOf( \WP_REST_Response::class, $response ); $this->assertIsArray( $response->get_data() ); diff --git a/projects/packages/premium-analytics/tests/php/traits/trait-widget-manifest-fixture.php b/projects/packages/premium-analytics/tests/php/traits/trait-widget-manifest-fixture.php new file mode 100644 index 000000000000..15c5f1ed178d --- /dev/null +++ b/projects/packages/premium-analytics/tests/php/traits/trait-widget-manifest-fixture.php @@ -0,0 +1,35 @@ + & { + relevance?: string; +}; + /** * Runtime-only fields a widget declares in `widget.ts` (its default export): * the ones that cannot live in JSON because they hold a React element or @@ -65,6 +74,17 @@ export function createStoryWidgetType( ...( manifest.category ? { category: manifest.category } : {} ), ...( manifest.keywords ? { keywords: manifest.keywords } : {} ), ...( manifest.help ? { help: manifest.help } : {} ), + ...( manifest.actions + ? { + // Mirrors useWidgetTypes: an icon reference only reaches the + // host once resolved, and the story has no resolver. + actions: manifest.actions.map( action => ( { + ...action, + icon: undefined, + relevance: action.relevance as WidgetRelevance | undefined, + } ) ), + } + : {} ), ...( manifest.presentation ? { presentation: manifest.presentation as WidgetType[ 'presentation' ],