Commit d52cad76464 for woocommerce
commit d52cad76464010aca15d5110ebc9a610711c89fc
Author: AnnaMag <AnnaMag@users.noreply.github.com>
Date: Fri Nov 20 14:26:17 2026 +0100
Agent readiness: encode the settings views safely in their data attributes (#71193)
* Agent readiness: encode the settings views safely in their data attributes
esc_attr() keeps an entity such as " that is already in a page or product
name, so after the browser decodes the attribute it could end a JSON string in
the AI shopping or AI tools view. wp_json_encode() now hex-encodes &, quotes,
< and >, so names stay names.
* Agent readiness: render only http, https and relative links from the views
The AI shopping and readiness views now drop a link whose scheme is anything
else, so a javascript: or data: URL in their data never reaches an href.
* Agent readiness: assert entity encoding on a label the Overview shows
The store name never reaches that view. A check label with no ReadinessCopy
wording does, so the test reads it back from the rendered attribute.
view_json() now documents its return value.
* Agent readiness: disable rejected links and lowercase their scheme
A button whose URL is not http or https stays disabled and does not record a click.
safeHref writes the scheme in lowercase, because PHP's HTTP stream wrapper skips TLS
for a mixed-case scheme such as HTTPS://. The settings test reads data-view with the
HTML API instead of a regular expression.
* Agent readiness: show decoded policy page titles
A stored title such as Shipping & Returns was shown with the entity.
Dropdowns decode it the same way product names are decoded. safeHref keeps
a trimmed link as written, and a rejected checklist link is disabled.
* Agent readiness: assert the decoded page title literally
The test rebuilt the expected dropdown label with the same decode as the
page, so a wrong rule could pass in both places. It now expects the title
the merchant sees.
diff --git a/plugins/woocommerce/changelog/fix-ai-shopping-data-view-json b/plugins/woocommerce/changelog/fix-ai-shopping-data-view-json
new file mode 100644
index 00000000000..2706d777842
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-ai-shopping-data-view-json
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+Comment: Encode the AI shopping and AI tools views safely in their data attributes, behind the agent_readiness flag.
+
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/ai-shopping.tsx b/plugins/woocommerce/client/admin/client/sales-readiness/ai-shopping.tsx
index f388f93c831..46399dc0ebe 100644
--- a/plugins/woocommerce/client/admin/client/sales-readiness/ai-shopping.tsx
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/ai-shopping.tsx
@@ -22,6 +22,7 @@ import { Button, Link, Notice, Stack, Text } from '@wordpress/ui';
/**
* Internal dependencies
*/
+import { safeHref } from './safe-href';
import './sales-readiness.scss';
import './ai-shopping.scss';
import type { ShoppingView } from './types';
@@ -58,7 +59,9 @@ export const SettingsBreadcrumbs = ( {
<li>
<Text
variant="body-lg"
- render={ <Link tone="neutral" href={ parent.href } /> }
+ render={
+ <Link tone="neutral" href={ safeHref( parent.href ) } />
+ }
>
{ parent.text }
</Text>
@@ -258,7 +261,10 @@ export default function AiShopping( { view }: { view: ShoppingView } ) {
description: (
<>
{ ucp.description }{ ' ' }
- <Link href={ ucp.learn_more.href } openInNewTab>
+ <Link
+ href={ safeHref( ucp.learn_more.href ) }
+ openInNewTab
+ >
{ ucp.learn_more.text }
</Link>
</>
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/components/check-row.tsx b/plugins/woocommerce/client/admin/client/sales-readiness/components/check-row.tsx
index 475f98d6c38..3a02a82f55c 100644
--- a/plugins/woocommerce/client/admin/client/sales-readiness/components/check-row.tsx
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/components/check-row.tsx
@@ -11,6 +11,7 @@ import type { ReactNode } from 'react';
/**
* Internal dependencies
*/
+import { safeHref } from '../safe-href';
import type { CheckRow as CheckRowData, ViewBadge, ViewLink } from '../types';
import { trackReadiness, type Surface } from '../tracks';
@@ -23,12 +24,13 @@ export const RowLink = ( {
}: {
link: ViewLink;
onClick?: () => void;
-} ) =>
- link.href ? (
+} ) => {
+ const href = safeHref( link.href );
+ return href ? (
<Button
variant="secondary"
size="compact"
- href={ link.href }
+ href={ href }
onClick={ onClick }
>
{ link.text }
@@ -38,6 +40,7 @@ export const RowLink = ( {
{ link.text }
</Text>
);
+};
// The swatch icon, a dashed circle, from the WordPress icons package from version 18 on.
// This package pins an older version that doesn't have it.
@@ -155,7 +158,7 @@ const TextWithLink = ( {
return (
<>
{ text.slice( 0, at ) }
- <Link href={ link.href } openInNewTab>
+ <Link href={ safeHref( link.href ) } openInNewTab>
{ link.text }
</Link>
{ text.slice( at + link.text.length ) }
@@ -225,7 +228,9 @@ export const CheckRow = ( {
row.seen_at.text
),
{
- link: <Link href={ row.seen_at.href } />,
+ link: (
+ <Link href={ safeHref( row.seen_at.href ) } />
+ ),
}
) }
</Text>
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/components/connection-url.tsx b/plugins/woocommerce/client/admin/client/sales-readiness/components/connection-url.tsx
index 18c8bf652c2..e06fc186f1d 100644
--- a/plugins/woocommerce/client/admin/client/sales-readiness/components/connection-url.tsx
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/components/connection-url.tsx
@@ -9,6 +9,7 @@ import { Button, InputControl, Link } from '@wordpress/ui';
/**
* Internal dependencies
*/
+import { safeHref } from '../safe-href';
import type { ShoppingView } from '../types';
/**
@@ -51,7 +52,7 @@ export const ConnectionUrl = ( {
{ copied ? connection.copied : connection.copy }
</Button>
</div>
- <Link href={ connection.test.href } openInNewTab>
+ <Link href={ safeHref( connection.test.href ) } openInNewTab>
{ connection.test.text }
</Link>
</div>
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/components/fix-first.tsx b/plugins/woocommerce/client/admin/client/sales-readiness/components/fix-first.tsx
index 904f9ff865a..44dcd505c74 100644
--- a/plugins/woocommerce/client/admin/client/sales-readiness/components/fix-first.tsx
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/components/fix-first.tsx
@@ -6,6 +6,7 @@ import { Badge, Card, Link, Text } from '@wordpress/ui';
/**
* Internal dependencies
*/
+import { safeHref } from '../safe-href';
import type { FixFirst as FixFirstData } from '../types';
import { trackReadiness, type Surface } from '../tracks';
import { Row, RowLink } from './check-row';
@@ -117,7 +118,7 @@ export const FixFirstCard = ( { fixFirst }: { fixFirst: FixFirstData } ) => (
{ fixFirst.link && (
<>
{ ' ' }
- <Link href={ fixFirst.link.href }>
+ <Link href={ safeHref( fixFirst.link.href ) }>
{ fixFirst.link.text }
</Link>
</>
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/components/product-visibility.tsx b/plugins/woocommerce/client/admin/client/sales-readiness/components/product-visibility.tsx
index 2e653270eeb..cd0fddd321f 100644
--- a/plugins/woocommerce/client/admin/client/sales-readiness/components/product-visibility.tsx
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/components/product-visibility.tsx
@@ -10,6 +10,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
+import { safeHref } from '../safe-href';
import type { ShoppingView } from '../types';
/**
@@ -67,7 +68,7 @@ export const ProductVisibility = ( {
{ count.count }
</Text>
<Text variant="body-sm">
- <Link href={ count.link.href }>
+ <Link href={ safeHref( count.link.href ) }>
{ count.link.text }
</Link>
</Text>
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/components/shopping-checklist.tsx b/plugins/woocommerce/client/admin/client/sales-readiness/components/shopping-checklist.tsx
index c8c6bd09bd3..3a58da37ef7 100644
--- a/plugins/woocommerce/client/admin/client/sales-readiness/components/shopping-checklist.tsx
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/components/shopping-checklist.tsx
@@ -10,22 +10,29 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
+import { safeHref } from '../safe-href';
import type { ShoppingView } from '../types';
import { Row } from './check-row';
type Checklist = ShoppingView[ 'checklist' ];
/**
- * Props that make a @wordpress/ui Button a link: an anchor that keeps the link role. This
- * version of the package has no ButtonLink.
+ * Props that make a @wordpress/ui Button a link, or that disable it when the URL is rejected.
+ * This version of the package has no ButtonLink.
*/
-export const asLink = ( href: string ) => ( {
- nativeButton: false,
- role: 'link',
- // The Button renders the text into it.
- // eslint-disable-next-line jsx-a11y/anchor-has-content
- render: <a href={ href } />,
-} );
+export const asLink = ( href: string ) => {
+ const safe = safeHref( href );
+ if ( ! safe ) {
+ return { disabled: true };
+ }
+ return {
+ nativeButton: false as const,
+ role: 'link' as const,
+ // The Button renders the text into it.
+ // eslint-disable-next-line jsx-a11y/anchor-has-content
+ render: <a href={ safe } />,
+ };
+};
/**
* The open setup steps, required ones first, each with a button to where to fix it, then the
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/overview.tsx b/plugins/woocommerce/client/admin/client/sales-readiness/overview.tsx
index 81ebf21ff53..448f605e1d8 100644
--- a/plugins/woocommerce/client/admin/client/sales-readiness/overview.tsx
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/overview.tsx
@@ -8,6 +8,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
+import { safeHref } from './safe-href';
import './sales-readiness.scss';
import type { ReadinessView } from './types';
import { useReadinessView } from './use-readiness-view';
@@ -50,6 +51,18 @@ export default function SalesReadinessOverview( {
);
}
const { shopping, view_all: viewAll } = view.overview;
+ // A rejected URL is not a link: the button stays, disabled, and does not record a click.
+ const linkProps = ( href: string | undefined, area: string ) =>
+ href
+ ? {
+ href,
+ onClick: () =>
+ trackReadiness( 'link_clicked', {
+ surface: 'overview',
+ area,
+ } ),
+ }
+ : { disabled: true };
return (
<div className="wc-sales-readiness wc-sales-readiness--overview">
@@ -78,13 +91,10 @@ export default function SalesReadinessOverview( {
<Button
variant="secondary"
size="compact"
- href={ shopping.button.href }
- onClick={ () =>
- trackReadiness( 'link_clicked', {
- surface: 'overview',
- area: 'shopping',
- } )
- }
+ { ...linkProps(
+ safeHref( shopping.button.href ),
+ 'shopping'
+ ) }
>
{ shopping.button.text }
</Button>
@@ -151,13 +161,10 @@ export default function SalesReadinessOverview( {
<Button
variant="tertiary"
size="compact"
- href={ viewAll.href }
- onClick={ () =>
- trackReadiness( 'link_clicked', {
- surface: 'overview',
- area: 'view_all',
- } )
- }
+ { ...linkProps(
+ safeHref( viewAll.href ),
+ 'view_all'
+ ) }
>
{ viewAll.text }
</Button>
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/safe-href.ts b/plugins/woocommerce/client/admin/client/sales-readiness/safe-href.ts
new file mode 100644
index 00000000000..685e2297226
--- /dev/null
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/safe-href.ts
@@ -0,0 +1,21 @@
+/**
+ * A link from the view data, or undefined when its scheme isn't http or https. Relative links
+ * stay, so a `javascript:` or `data:` URL in stored data never reaches an href. The URL parser
+ * lowercases the scheme, so HTTPS:// is kept as it was written.
+ *
+ * @param href Link from the view.
+ */
+export const safeHref = ( href: string | null | undefined ) => {
+ const trimmed = href?.trim();
+ if ( ! trimmed ) {
+ return undefined;
+ }
+ try {
+ const { protocol } = new URL( trimmed, window.location.href );
+ return protocol === 'http:' || protocol === 'https:'
+ ? trimmed
+ : undefined;
+ } catch {
+ return undefined;
+ }
+};
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/test/as-link.test.tsx b/plugins/woocommerce/client/admin/client/sales-readiness/test/as-link.test.tsx
new file mode 100644
index 00000000000..9ebfb466ee2
--- /dev/null
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/test/as-link.test.tsx
@@ -0,0 +1,25 @@
+/**
+ * Internal dependencies
+ */
+import { asLink } from '../components/shopping-checklist';
+
+describe( 'asLink', () => {
+ it( 'disables the button when the URL is rejected', () => {
+ expect( asLink( 'javascript:alert(1)' ) ).toEqual( { disabled: true } );
+ expect( asLink( ' JavaScript:alert(1)' ) ).toEqual( {
+ disabled: true,
+ } );
+ } );
+
+ it( 'keeps an http link as an anchor', () => {
+ const props = asLink( 'https://example.com/policy' );
+ expect( props ).toMatchObject( {
+ nativeButton: false,
+ role: 'link',
+ } );
+ if ( ! ( 'render' in props ) || ! props.render ) {
+ throw new Error( 'expected a link' );
+ }
+ expect( props.render.props.href ).toBe( 'https://example.com/policy' );
+ } );
+} );
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/test/safe-href.test.ts b/plugins/woocommerce/client/admin/client/sales-readiness/test/safe-href.test.ts
new file mode 100644
index 00000000000..12303b63751
--- /dev/null
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/test/safe-href.test.ts
@@ -0,0 +1,40 @@
+/**
+ * Internal dependencies
+ */
+import { safeHref } from '../safe-href';
+
+describe( 'safeHref', () => {
+ it( 'keeps http, https and relative links', () => {
+ expect( safeHref( 'https://ucpplayground.com/' ) ).toBe(
+ 'https://ucpplayground.com/'
+ );
+ expect( safeHref( ' https://ucpplayground.com/ ' ) ).toBe(
+ 'https://ucpplayground.com/'
+ );
+ expect( safeHref( 'http://example.com/wp-admin/' ) ).toBe(
+ 'http://example.com/wp-admin/'
+ );
+ expect( safeHref( 'admin.php?page=wc-settings' ) ).toBe(
+ 'admin.php?page=wc-settings'
+ );
+ expect( safeHref( 'admin.php?foo=a:b' ) ).toBe( 'admin.php?foo=a:b' );
+ expect( safeHref( '/wp-admin/plugins.php' ) ).toBe(
+ '/wp-admin/plugins.php'
+ );
+ expect( safeHref( 'HTTPS://ucpplayground.com/Path' ) ).toBe(
+ 'HTTPS://ucpplayground.com/Path'
+ );
+ expect( safeHref( 'Http://example.com/' ) ).toBe(
+ 'Http://example.com/'
+ );
+ } );
+
+ it( 'drops links with any other scheme, and empty ones', () => {
+ expect( safeHref( 'javascript:alert(1)' ) ).toBeUndefined();
+ expect( safeHref( ' JavaScript:alert(1)' ) ).toBeUndefined();
+ expect( safeHref( 'data:text/html,x' ) ).toBeUndefined();
+ expect( safeHref( 'FTPS://files.example/a' ) ).toBeUndefined();
+ expect( safeHref( '' ) ).toBeUndefined();
+ expect( safeHref( null ) ).toBeUndefined();
+ } );
+} );
diff --git a/plugins/woocommerce/src/Internal/AgentReadiness/AgenticCommerceSettingsPage.php b/plugins/woocommerce/src/Internal/AgentReadiness/AgenticCommerceSettingsPage.php
index 2e4dbcc2c0a..2086de36bd3 100644
--- a/plugins/woocommerce/src/Internal/AgentReadiness/AgenticCommerceSettingsPage.php
+++ b/plugins/woocommerce/src/Internal/AgentReadiness/AgenticCommerceSettingsPage.php
@@ -97,6 +97,16 @@ class AgenticCommerceSettingsPage extends \WC_Settings_Page {
return array();
}
+ /**
+ * A page title for a dropdown. Tags are removed and entities decoded, so a stored title shows as the merchant wrote it.
+ *
+ * @param \WP_Post $page Page.
+ * @return string
+ */
+ private static function page_title( \WP_Post $page ): string {
+ return html_entity_decode( wp_strip_all_tags( get_the_title( $page ) ), ENT_QUOTES, 'UTF-8' );
+ }
+
/**
* The store policy fields, on AI shopping. The privacy policy page is a WordPress setting, so it is
* listed, and saved, only for users who can manage privacy options.
@@ -113,7 +123,7 @@ class AgenticCommerceSettingsPage extends \WC_Settings_Page {
)
);
foreach ( is_array( $published ) ? $published : array() as $page ) {
- $pages[ (string) $page->ID ] = $page->post_title;
+ $pages[ (string) $page->ID ] = self::page_title( $page );
}
$page_field = static function ( string $option, string $title ) use ( $pages, $store_pages ): array {
$options = $pages;
@@ -123,7 +133,7 @@ class AgenticCommerceSettingsPage extends \WC_Settings_Page {
$status = get_post_status_object( $stored->post_status );
$state = in_array( $stored->ID, $store_pages, true ) ? __( 'Store page', 'woocommerce' ) : ( $status ? $status->label : $stored->post_status );
/* translators: 1: page title, 2: page status, such as Draft, or "Store page". */
- $options[ (string) $stored->ID ] = sprintf( __( '%1$s (%2$s)', 'woocommerce' ), $stored->post_title, $state );
+ $options[ (string) $stored->ID ] = sprintf( __( '%1$s (%2$s)', 'woocommerce' ), self::page_title( $stored ), $state );
}
return array(
'title' => $title,
@@ -170,7 +180,7 @@ class AgenticCommerceSettingsPage extends \WC_Settings_Page {
$GLOBALS['hide_save_button'] = true;
printf(
'<div class="wc-agentic-commerce"><div id="wc-agent-readiness-overview" class="wc-agent-readiness-overview" data-view="%s"></div></div>',
- esc_attr( (string) wp_json_encode( $this->readiness_page->view() ) )
+ esc_attr( self::view_json( $this->readiness_page->view() ) )
);
return;
}
@@ -181,10 +191,22 @@ class AgenticCommerceSettingsPage extends \WC_Settings_Page {
$GLOBALS['hide_save_button'] = true;
printf(
'<div id="wc-ai-shopping" class="wc-ai-shopping-root wc-settings-prevent-change-event" data-view="%s"></div>',
- esc_attr( (string) wp_json_encode( $this->shopping_view() ) )
+ esc_attr( self::view_json( $this->shopping_view() ) )
);
}
+ /**
+ * A view as JSON for a data attribute. The hex flags encode &, quotes, < and > inside its strings, so
+ * an entity already in a value, such as ", which esc_attr() leaves as it is, can't decode to a
+ * quote that ends a JSON string.
+ *
+ * @param array<string, mixed> $view View data.
+ * @return string JSON for the attribute.
+ */
+ private static function view_json( array $view ): string {
+ return (string) wp_json_encode( $view, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT );
+ }
+
/**
* Everything AI shopping shows, as data for its React view: the switch, the coming soon notice,
* Product visibility, the Setup checklist, the store policy fields with their pages, and the
diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/AgenticCommerceSettingsTest.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/AgenticCommerceSettingsTest.php
index 0233cc4bbe6..f97d2d97a2f 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/AgenticCommerceSettingsTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/AgenticCommerceSettingsTest.php
@@ -1359,9 +1359,7 @@ class AgenticCommerceSettingsTest extends WC_Unit_Test_Case {
$html = $this->render_page( '' );
$this->assertTrue( $this->save_hidden, 'Nothing on the Overview saves' );
$this->assertStringContainsString( '<div id="wc-agent-readiness-overview"', $html, 'The Overview is React, mounted by the settings-embed script' );
- $this->assertMatchesRegularExpression( '#data-view="([^"]+)"#', $html );
- preg_match( '#data-view="([^"]+)"#', $html, $match );
- $this->assertSame( 'Set up AI shopping', json_decode( html_entity_decode( $match[1] ), true )['overview']['shopping']['button']['text'], 'The view comes with the page, so the cards draw without a request' );
+ $this->assertSame( 'Set up AI shopping', json_decode( $this->data_view( $html, 'wc-agent-readiness-overview' ), true )['overview']['shopping']['button']['text'], 'The view comes with the page, so the cards draw without a request' );
$this->assertStringNotContainsString( 'No llms.txt file', $html );
$view = $this->view();
@@ -2194,6 +2192,54 @@ class AgenticCommerceSettingsTest extends WC_Unit_Test_Case {
}
}
+ /**
+ * @testdox A page title is shown with its entities decoded, and an entity in a check label stays inside the JSON.
+ */
+ public function test_page_title_with_entities_stays_inside_the_view(): void {
+ $title = 'Returns "}, "z": {"a": " & <b> <i>';
+ $id = self::factory()->post->create(
+ array(
+ 'post_type' => 'page',
+ 'post_status' => 'publish',
+ 'post_title' => $title,
+ )
+ );
+ $stored = get_post( $id )->post_title;
+ $this->assertStringContainsString( '"', $stored, 'The title keeps its entities, as one saved without unfiltered_html can' );
+
+ $view = $this->shopping();
+ $labels = array_merge( ...array_map( static fn( array $field ): array => array_column( $field['options'], 'label' ), $view['policies']['fields'] ) );
+ $titles = array_values( array_filter( $labels, static fn( string $label ): bool => str_contains( $label, 'Returns' ) ) );
+ $this->assertNotEmpty( $titles, 'The page is listed in the policy dropdowns' );
+ foreach ( $titles as $label ) {
+ $this->assertSame( 'Returns "}, "z": {"a": " & <b>', $label, 'The dropdown shows the title with its entities decoded' );
+ }
+
+ // ReadinessCopy has no wording for this check, so the stored label is what the Overview shows.
+ $label = 'Blue "}, & Co <i>';
+ update_option(
+ AgentReadiness::REPORT_OPTION,
+ array(
+ 'generated_at' => gmdate( 'c' ),
+ 'checks' => array(
+ array(
+ 'id' => 'entity_in_label',
+ 'count' => 1,
+ 'label' => $label,
+ ),
+ ),
+ ),
+ false
+ );
+ $html = $this->render_page( '' );
+ $this->assertStringNotContainsString( '&', $html, 'No & from the view is left for the browser to decode' );
+ $overview = json_decode( $this->data_view( $html, 'wc-agent-readiness-overview' ), true );
+ $this->assertIsArray( $overview, 'The Overview view still reads as JSON' );
+ $products = current( array_filter( $overview['sections'], static fn( array $section ): bool => 'products' === $section['id'] ) );
+ $this->assertIsArray( $products, 'The Overview lists product checks' );
+ $this->assertContains( $label, array_column( $products['rows'], 'title' ), 'A check label with entities stays a label in the Overview' );
+ }
+
/**
* @testdox A stored policy page that no longer exists starts on "Choose a page", as the select did, and no policy field means no pages to list.
*/
@@ -2383,14 +2429,44 @@ class AgenticCommerceSettingsTest extends WC_Unit_Test_Case {
* @return array<string, mixed>
*/
private function shopping( bool $ucp_on = false ): array {
- $html = $this->render_page( AgenticCommerceSettings::SHOPPING_SECTION, $ucp_on );
- $this->assertMatchesRegularExpression( '#<div id="wc-ai-shopping" class="wc-ai-shopping-root wc-settings-prevent-change-event" data-view="([^"]+)"></div>#', $html );
- preg_match( '#data-view="([^"]+)"#', $html, $match );
- $view = json_decode( html_entity_decode( $match[1], ENT_QUOTES ), true );
+ $html = $this->render_page( AgenticCommerceSettings::SHOPPING_SECTION, $ucp_on );
+ $processor = new \WP_HTML_Tag_Processor( $html );
+ $this->assertTrue(
+ $processor->next_tag(
+ array(
+ 'tag_name' => 'div',
+ 'class_name' => 'wc-ai-shopping-root',
+ )
+ ),
+ 'AI shopping renders its view root'
+ );
+ $this->assertSame( 'wc-ai-shopping', $processor->get_attribute( 'id' ) );
+ $this->assertStringContainsString( 'wc-settings-prevent-change-event', (string) $processor->get_attribute( 'class' ) );
+ $view = json_decode( (string) $processor->get_attribute( 'data-view' ), true );
$this->assertIsArray( $view );
return $view;
}
+ /**
+ * The data-view attribute on a rendered root, decoded the way the browser reads it.
+ *
+ * @param string $html Rendered markup.
+ * @param string $root_class Class of the root div.
+ */
+ private function data_view( string $html, string $root_class ): string {
+ $processor = new \WP_HTML_Tag_Processor( $html );
+ $this->assertTrue(
+ $processor->next_tag(
+ array(
+ 'tag_name' => 'div',
+ 'class_name' => $root_class,
+ )
+ ),
+ 'The page renders its view root'
+ );
+ return (string) $processor->get_attribute( 'data-view' );
+ }
+
/**
* AI shopping's data as text, to look for wording anywhere in it.
*/