From 34bc943d5dab9f76d1abfee4a26f4cc50b8df19a Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sun, 8 Feb 2026 23:19:39 +0100 Subject: [PATCH 1/7] Remove failing unit test --- .../unit/tests/Shopping/WooCommerce_Query.php | 87 ------------------- 1 file changed, 87 deletions(-) diff --git a/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php b/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php index 08dfa79ce604..8b702751e982 100644 --- a/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php +++ b/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php @@ -60,93 +60,6 @@ public function set_up(): void { $this->instance = new \Google\Web_Stories\Shopping\WooCommerce_Query( $woocommerce ); } - /** - * @covers ::get_search - */ - public function test_products_image(): void { - $this->markTestSkipped( 'Currently failing, needs investigation' ); - - Monkey\Functions\stubs( - [ - 'wc_get_products' => static function () { - $object = new \stdClass(); - $products = [ - Mockery::mock( - \WC_Product::class, - [ - 'get_id' => 1, - 'get_title' => '', - 'get_price' => 0, - 'get_average_rating' => 0.0, - 'get_rating_count' => 0, - 'get_permalink' => '', - 'get_short_description' => '', - 'get_image_id' => 50, - 'get_gallery_image_ids' => [ - 51, - 59, - 60, - ], - ] - ), - - Mockery::mock( - \WC_Product::class, - [ - 'get_id' => 2, - 'get_title' => '', - 'get_price' => 0, - 'get_average_rating' => 0.0, - 'get_rating_count' => 0, - 'get_permalink' => '', - 'get_short_description' => '', - 'get_image_id' => null, - 'get_gallery_image_ids' => [], - ] - ), - - Mockery::mock( - \WC_Product::class, - [ - 'get_id' => 3, - 'get_title' => '', - 'get_price' => 0, - 'get_average_rating' => 0.0, - 'get_rating_count' => 0, - 'get_permalink' => '', - 'get_short_description' => '', - 'get_image_id' => null, - 'get_gallery_image_ids' => [ - 72, - null, - 76, - ], - ] - ), - ]; - $object->products = $products; - $object->max_num_pages = 1; - return $object; - }, - 'wp_get_attachment_image_url' => static function ( $id ) { - if ( ! $id ) { - // id was passed as null to simulate missing post / attachment - return false; - } - - return \sprintf( 'http://example.com/%s', $id ); - }, - ] - ); - - $results = $this->instance->get_search( 'hoodie' ); - $this->assertIsArray( $results ); - $this->assertEquals( 'http://example.com/50', $results['products'][0]->get_images()[0]['url'] ); - $this->assertEquals( 'http://example.com/60', $results['products'][0]->get_images()[3]['url'] ); - $this->assertEquals( 'http://example.com/72', $results['products'][2]->get_images()[1]['url'] ); - $this->assertCount( 0, $results['products'][1]->get_images() ); - } - /** * @covers ::get_product_image_ids */ From 6b7eb4b30cb67cef62e7ac90ea8eac98b23466ee Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 9 Feb 2026 11:36:12 +0100 Subject: [PATCH 2/7] Remove more tests --- .../unit/tests/Shopping/WooCommerce_Query.php | 56 ------------------- 1 file changed, 56 deletions(-) diff --git a/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php b/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php index 8b702751e982..0e1355ed3473 100644 --- a/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php +++ b/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php @@ -60,62 +60,6 @@ public function set_up(): void { $this->instance = new \Google\Web_Stories\Shopping\WooCommerce_Query( $woocommerce ); } - /** - * @covers ::get_product_image_ids - */ - public function test_get_product_image_ids(): void { - $product = Mockery::mock( - \WC_Product::class, - [ - 'get_id' => 1, - 'get_title' => '', - 'get_price' => 0, - 'get_average_rating' => 0.0, - 'get_rating_count' => 0, - 'get_permalink' => '', - 'get_short_description' => '', - 'get_image_id' => 50, - 'get_gallery_image_ids' => [ - 51, - 59, - ], - ] - ); - - $ids = $this->call_private_method( [ $this->instance, 'get_product_image_ids' ], [ $product ] ); - - $this->assertEquals( [ 50, 51, 59 ], $ids ); - } - - /** - * @covers ::get_product_image_ids - */ - public function test_get_product_image_ids_invalid(): void { - $product = Mockery::mock( - \WC_Product::class, - [ - 'get_id' => 1, - 'get_title' => '', - 'get_price' => 0, - 'get_average_rating' => 0.0, - 'get_rating_count' => 0, - 'get_permalink' => '', - 'get_short_description' => '', - 'get_image_id' => null, - 'get_gallery_image_ids' => [ - null, - 27, - ], - ] - ); - - $ids = $this->call_private_method( [ $this->instance, 'get_product_image_ids' ], [ $product ] ); - - $this->assertIsArray( $ids ); - $this->assertCount( 1, $ids ); - $this->assertContains( 27, $ids ); - } - /** * @covers ::get_product_image */ From 213e45e9f76a6352a5a95ab112990d2d4f364182 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 9 Feb 2026 11:36:21 +0100 Subject: [PATCH 3/7] Register tracking script earlier --- includes/Tracking.php | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/includes/Tracking.php b/includes/Tracking.php index f71ed5fa8957..9b3a7b9943ec 100644 --- a/includes/Tracking.php +++ b/includes/Tracking.php @@ -151,14 +151,7 @@ public function register(): void { false ); - if ( ! $this->context->is_story_editor() && 'web-story' !== $this->context->get_screen_post_type() ) { - return; - } - - wp_add_inline_script( - self::SCRIPT_HANDLE, - 'window.webStoriesTrackingSettings = ' . wp_json_encode( $this->get_settings() ) . ';' - ); + add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); } /** @@ -169,7 +162,36 @@ public function register(): void { * @return string Registration action to use. */ public static function get_registration_action(): string { - return 'admin_head'; + return 'init'; + } + + /** + * Get the action priority to use for registering the service. + * + * @since 1.43.0 + * + * @return int Registration action priority to use. + */ + public static function get_registration_action_priority(): int { + return 1; + } + + /** + * Enqueues tracking scripts + * + * @since 1.43.0 + * + * @param string $hook_suffix The current admin page. + */ + public function enqueue_assets( string $hook_suffix ): void { + if ( ! $this->context->is_story_editor() && 'web-story' !== $this->context->get_screen_post_type() ) { + return; + } + + wp_add_inline_script( + self::SCRIPT_HANDLE, + 'window.webStoriesTrackingSettings = ' . wp_json_encode( $this->get_settings() ) . ';' + ); } /** From 427dd1639070df28a0217e6e4ae2becefa634072 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 9 Feb 2026 11:36:46 +0100 Subject: [PATCH 4/7] Lint fix --- tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php b/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php index 0e1355ed3473..be2297eb6798 100644 --- a/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php +++ b/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php @@ -24,7 +24,6 @@ use Google\Web_Stories\Integrations\WooCommerce; use Google\Web_Stories\Tests\Shared\Private_Access; use Google\Web_Stories\Tests\Unit\TestCase; -use Mockery; /** * @coversDefaultClass \Google\Web_Stories\Shopping\WooCommerce_Query From c12bce9063ccf5d8665262f69f3cf0b59ea24272 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 9 Feb 2026 11:52:54 +0100 Subject: [PATCH 5/7] Outright remove test file --- .../unit/tests/Shopping/WooCommerce_Query.php | 84 ------------------- 1 file changed, 84 deletions(-) delete mode 100644 tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php diff --git a/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php b/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php deleted file mode 100644 index be2297eb6798..000000000000 --- a/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php +++ /dev/null @@ -1,84 +0,0 @@ -stubEscapeFunctions(); - - Monkey\Functions\stubs( - [ - '_prime_post_caches', - 'get_post_meta', - 'wp_strip_all_tags', - 'get_woocommerce_currency' => static fn() => 'USD', - ] - ); - - $woocommerce = $this->createMock( WooCommerce::class ); - $woocommerce->method( 'get_plugin_status' )->willReturn( - [ - 'installed' => true, - 'active' => true, - 'canManage' => true, - 'link' => 'https://example.com', - ] - ); - $this->instance = new \Google\Web_Stories\Shopping\WooCommerce_Query( $woocommerce ); - } - - /** - * @covers ::get_product_image - */ - public function test_get_product_image(): void { - Monkey\Functions\stubs( - [ - 'wp_get_attachment_image_url' => static fn( $id ) => \sprintf( 'http://example.com/%s', $id ), - 'get_post_meta' => static fn() => 'image alt', - - ] - ); - - $results = $this->call_private_method( [ $this->instance, 'get_product_image' ], [ 2 ] ); - - $this->assertEquals( - [ - 'url' => 'http://example.com/2', - 'alt' => 'image alt', - ], - $results - ); - } -} From 4d49359f408e6e1e0577360b62ced02637ab7ff0 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 9 Feb 2026 13:26:52 +0100 Subject: [PATCH 6/7] Revert change --- .../unit/tests/Shopping/WooCommerce_Query.php | 228 ++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php diff --git a/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php b/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php new file mode 100644 index 000000000000..08dfa79ce604 --- /dev/null +++ b/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php @@ -0,0 +1,228 @@ +stubEscapeFunctions(); + + Monkey\Functions\stubs( + [ + '_prime_post_caches', + 'get_post_meta', + 'wp_strip_all_tags', + 'get_woocommerce_currency' => static fn() => 'USD', + ] + ); + + $woocommerce = $this->createMock( WooCommerce::class ); + $woocommerce->method( 'get_plugin_status' )->willReturn( + [ + 'installed' => true, + 'active' => true, + 'canManage' => true, + 'link' => 'https://example.com', + ] + ); + $this->instance = new \Google\Web_Stories\Shopping\WooCommerce_Query( $woocommerce ); + } + + /** + * @covers ::get_search + */ + public function test_products_image(): void { + $this->markTestSkipped( 'Currently failing, needs investigation' ); + + Monkey\Functions\stubs( + [ + 'wc_get_products' => static function () { + $object = new \stdClass(); + $products = [ + Mockery::mock( + \WC_Product::class, + [ + 'get_id' => 1, + 'get_title' => '', + 'get_price' => 0, + 'get_average_rating' => 0.0, + 'get_rating_count' => 0, + 'get_permalink' => '', + 'get_short_description' => '', + 'get_image_id' => 50, + 'get_gallery_image_ids' => [ + 51, + 59, + 60, + ], + ] + ), + + Mockery::mock( + \WC_Product::class, + [ + 'get_id' => 2, + 'get_title' => '', + 'get_price' => 0, + 'get_average_rating' => 0.0, + 'get_rating_count' => 0, + 'get_permalink' => '', + 'get_short_description' => '', + 'get_image_id' => null, + 'get_gallery_image_ids' => [], + ] + ), + + Mockery::mock( + \WC_Product::class, + [ + 'get_id' => 3, + 'get_title' => '', + 'get_price' => 0, + 'get_average_rating' => 0.0, + 'get_rating_count' => 0, + 'get_permalink' => '', + 'get_short_description' => '', + 'get_image_id' => null, + 'get_gallery_image_ids' => [ + 72, + null, + 76, + ], + ] + ), + ]; + $object->products = $products; + $object->max_num_pages = 1; + return $object; + }, + 'wp_get_attachment_image_url' => static function ( $id ) { + if ( ! $id ) { + // id was passed as null to simulate missing post / attachment + return false; + } + + return \sprintf( 'http://example.com/%s', $id ); + }, + ] + ); + + $results = $this->instance->get_search( 'hoodie' ); + $this->assertIsArray( $results ); + $this->assertEquals( 'http://example.com/50', $results['products'][0]->get_images()[0]['url'] ); + $this->assertEquals( 'http://example.com/60', $results['products'][0]->get_images()[3]['url'] ); + $this->assertEquals( 'http://example.com/72', $results['products'][2]->get_images()[1]['url'] ); + $this->assertCount( 0, $results['products'][1]->get_images() ); + } + + /** + * @covers ::get_product_image_ids + */ + public function test_get_product_image_ids(): void { + $product = Mockery::mock( + \WC_Product::class, + [ + 'get_id' => 1, + 'get_title' => '', + 'get_price' => 0, + 'get_average_rating' => 0.0, + 'get_rating_count' => 0, + 'get_permalink' => '', + 'get_short_description' => '', + 'get_image_id' => 50, + 'get_gallery_image_ids' => [ + 51, + 59, + ], + ] + ); + + $ids = $this->call_private_method( [ $this->instance, 'get_product_image_ids' ], [ $product ] ); + + $this->assertEquals( [ 50, 51, 59 ], $ids ); + } + + /** + * @covers ::get_product_image_ids + */ + public function test_get_product_image_ids_invalid(): void { + $product = Mockery::mock( + \WC_Product::class, + [ + 'get_id' => 1, + 'get_title' => '', + 'get_price' => 0, + 'get_average_rating' => 0.0, + 'get_rating_count' => 0, + 'get_permalink' => '', + 'get_short_description' => '', + 'get_image_id' => null, + 'get_gallery_image_ids' => [ + null, + 27, + ], + ] + ); + + $ids = $this->call_private_method( [ $this->instance, 'get_product_image_ids' ], [ $product ] ); + + $this->assertIsArray( $ids ); + $this->assertCount( 1, $ids ); + $this->assertContains( 27, $ids ); + } + + /** + * @covers ::get_product_image + */ + public function test_get_product_image(): void { + Monkey\Functions\stubs( + [ + 'wp_get_attachment_image_url' => static fn( $id ) => \sprintf( 'http://example.com/%s', $id ), + 'get_post_meta' => static fn() => 'image alt', + + ] + ); + + $results = $this->call_private_method( [ $this->instance, 'get_product_image' ], [ 2 ] ); + + $this->assertEquals( + [ + 'url' => 'http://example.com/2', + 'alt' => 'image alt', + ], + $results + ); + } +} From 594f93110437769d20ffc41d7758b5e7745cbfdd Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 9 Feb 2026 13:30:30 +0100 Subject: [PATCH 7/7] Revert "Temporarily skip failing PHP test" This reverts commit d54accd8ea8c04825b581fc9c3e586df594874d0. --- tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php b/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php index 08dfa79ce604..61c074af814c 100644 --- a/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php +++ b/tests/phpunit/unit/tests/Shopping/WooCommerce_Query.php @@ -64,8 +64,6 @@ public function set_up(): void { * @covers ::get_search */ public function test_products_image(): void { - $this->markTestSkipped( 'Currently failing, needs investigation' ); - Monkey\Functions\stubs( [ 'wc_get_products' => static function () {