Skip to content

Commit 8050a1b

Browse files
authored
Merge pull request #1871 from sarthak-19/add/code_coverage_web_worker
Improve Test Coverage for Web Worker Offloading Plugin
2 parents 5693cc4 + 17020f8 commit 8050a1b

File tree

10 files changed

+327
-136
lines changed

10 files changed

+327
-136
lines changed

plugins/web-worker-offloading/helper.php

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function plwwo_get_configuration(): array {
2929
);
3030

3131
if ( WP_DEBUG && SCRIPT_DEBUG ) {
32-
$config['debug'] = true;
32+
$config['debug'] = true;// @codeCoverageIgnore
3333
}
3434

3535
/**
@@ -72,3 +72,129 @@ function plwwo_get_configuration(): array {
7272
*/
7373
return (array) apply_filters( 'plwwo_configuration', $config );
7474
}
75+
76+
/**
77+
* Registers defaults scripts for Web Worker Offloading.
78+
*
79+
* @since 0.1.0
80+
* @access private
81+
*
82+
* @param WP_Scripts $scripts WP_Scripts instance.
83+
*/
84+
function plwwo_register_default_scripts( WP_Scripts $scripts ): void {
85+
// The source code for partytown.js is built from <https://github.com/BuilderIO/partytown/blob/b292a14047a0c12ca05ba97df1833935d42fdb66/src/lib/main/snippet.ts>.
86+
// See webpack config in the WordPress/performance repo: <https://github.com/WordPress/performance/blob/282a068f3eb2575d37aeb9034e894e7140fcddca/webpack.config.js#L84-L130>.
87+
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
88+
$partytown_js_path = '/build/debug/partytown.js';// @codeCoverageIgnore
89+
} else {
90+
$partytown_js_path = '/build/partytown.js';
91+
}
92+
93+
$partytown_js = file_get_contents( __DIR__ . $partytown_js_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request.
94+
if ( false === $partytown_js ) {
95+
return;// @codeCoverageIgnore
96+
}
97+
98+
$scripts->add(
99+
'web-worker-offloading',
100+
'',
101+
array(),
102+
WEB_WORKER_OFFLOADING_VERSION,
103+
array( 'in_footer' => false )
104+
);
105+
106+
$scripts->add_inline_script(
107+
'web-worker-offloading',
108+
sprintf(
109+
'window.partytown = {...(window.partytown || {}), ...%s};',
110+
wp_json_encode( plwwo_get_configuration() )
111+
),
112+
'before'
113+
);
114+
115+
$scripts->add_inline_script( 'web-worker-offloading', $partytown_js );
116+
}
117+
118+
/**
119+
* Prepends web-worker-offloading to the list of scripts to print if one of the queued scripts is offloaded to a worker.
120+
*
121+
* @since 0.1.0
122+
* @access private
123+
*
124+
* @param string[]|mixed $script_handles An array of enqueued script dependency handles.
125+
* @return string[] Script handles.
126+
*/
127+
function plwwo_filter_print_scripts_array( $script_handles ): array {
128+
$scripts = wp_scripts();
129+
foreach ( (array) $script_handles as $handle ) {
130+
if ( true === (bool) $scripts->get_data( $handle, 'worker' ) ) {
131+
$scripts->set_group( 'web-worker-offloading', false, 0 ); // Try to print in the head.
132+
array_unshift( $script_handles, 'web-worker-offloading' );
133+
break;
134+
}
135+
}
136+
return $script_handles;
137+
}
138+
139+
/**
140+
* Updates script type for handles having `web-worker-offloading` as dependency.
141+
*
142+
* @since 0.1.0
143+
* @access private
144+
*
145+
* @param string|mixed $tag Script tag.
146+
* @param string $handle Script handle.
147+
* @return string|mixed Script tag with type="text/partytown" for eligible scripts.
148+
*/
149+
function plwwo_update_script_type( $tag, string $handle ) {
150+
if (
151+
is_string( $tag )
152+
&&
153+
(bool) wp_scripts()->get_data( $handle, 'worker' )
154+
) {
155+
$html_processor = new WP_HTML_Tag_Processor( $tag );
156+
while ( $html_processor->next_tag( array( 'tag_name' => 'SCRIPT' ) ) ) {
157+
if ( $html_processor->get_attribute( 'id' ) === "{$handle}-js" ) {
158+
$html_processor->set_attribute( 'type', 'text/partytown' );
159+
$tag = $html_processor->get_updated_html();
160+
break;
161+
}
162+
}
163+
}
164+
return $tag;
165+
}
166+
167+
/**
168+
* Filters inline script attributes to offload to a worker if the script has been opted-in.
169+
*
170+
* @since 0.1.0
171+
* @access private
172+
*
173+
* @param array<string, mixed>|mixed $attributes Attributes.
174+
* @return array<string, mixed> Attributes.
175+
*/
176+
function plwwo_filter_inline_script_attributes( $attributes ): array {
177+
$attributes = (array) $attributes;
178+
if (
179+
isset( $attributes['id'] )
180+
&&
181+
1 === preg_match( '/^(?P<handle>.+)-js-(?:before|after)$/', $attributes['id'], $matches )
182+
&&
183+
(bool) wp_scripts()->get_data( $matches['handle'], 'worker' )
184+
) {
185+
$attributes['type'] = 'text/partytown';
186+
}
187+
return $attributes;
188+
}
189+
190+
/**
191+
* Displays the HTML generator meta tag for the Web Worker Offloading plugin.
192+
*
193+
* See {@see 'wp_head'}.
194+
*
195+
* @since 0.1.1
196+
*/
197+
function plwwo_render_generator_meta_tag(): void {
198+
// Use the plugin slug as it is immutable.
199+
echo '<meta name="generator" content="web-worker-offloading ' . esc_attr( WEB_WORKER_OFFLOADING_VERSION ) . '">' . "\n";
200+
}

plugins/web-worker-offloading/hooks.php

Lines changed: 1 addition & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -10,135 +10,9 @@
1010
if ( ! defined( 'ABSPATH' ) ) {
1111
exit; // Exit if accessed directly.
1212
}
13-
// @codeCoverageIgnoreEnd
14-
15-
/**
16-
* Registers defaults scripts for Web Worker Offloading.
17-
*
18-
* @since 0.1.0
19-
* @access private
20-
*
21-
* @param WP_Scripts $scripts WP_Scripts instance.
22-
*/
23-
function plwwo_register_default_scripts( WP_Scripts $scripts ): void {
24-
// The source code for partytown.js is built from <https://github.com/BuilderIO/partytown/blob/b292a14047a0c12ca05ba97df1833935d42fdb66/src/lib/main/snippet.ts>.
25-
// See webpack config in the WordPress/performance repo: <https://github.com/WordPress/performance/blob/282a068f3eb2575d37aeb9034e894e7140fcddca/webpack.config.js#L84-L130>.
26-
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
27-
$partytown_js_path = '/build/debug/partytown.js';
28-
} else {
29-
$partytown_js_path = '/build/partytown.js';
30-
}
31-
32-
$partytown_js = file_get_contents( __DIR__ . $partytown_js_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request.
33-
if ( false === $partytown_js ) {
34-
return;
35-
}
36-
37-
$scripts->add(
38-
'web-worker-offloading',
39-
'',
40-
array(),
41-
WEB_WORKER_OFFLOADING_VERSION,
42-
array( 'in_footer' => false )
43-
);
44-
45-
$scripts->add_inline_script(
46-
'web-worker-offloading',
47-
sprintf(
48-
'window.partytown = {...(window.partytown || {}), ...%s};',
49-
wp_json_encode( plwwo_get_configuration() )
50-
),
51-
'before'
52-
);
53-
54-
$scripts->add_inline_script( 'web-worker-offloading', $partytown_js );
55-
}
5613
add_action( 'wp_default_scripts', 'plwwo_register_default_scripts' );
57-
58-
/**
59-
* Prepends web-worker-offloading to the list of scripts to print if one of the queued scripts is offloaded to a worker.
60-
*
61-
* @since 0.1.0
62-
* @access private
63-
*
64-
* @param string[]|mixed $script_handles An array of enqueued script dependency handles.
65-
* @return string[] Script handles.
66-
*/
67-
function plwwo_filter_print_scripts_array( $script_handles ): array {
68-
$scripts = wp_scripts();
69-
foreach ( (array) $script_handles as $handle ) {
70-
if ( true === (bool) $scripts->get_data( $handle, 'worker' ) ) {
71-
$scripts->set_group( 'web-worker-offloading', false, 0 ); // Try to print in the head.
72-
array_unshift( $script_handles, 'web-worker-offloading' );
73-
break;
74-
}
75-
}
76-
return $script_handles;
77-
}
7814
add_filter( 'print_scripts_array', 'plwwo_filter_print_scripts_array', PHP_INT_MAX );
79-
80-
/**
81-
* Updates script type for handles having `web-worker-offloading` as dependency.
82-
*
83-
* @since 0.1.0
84-
* @access private
85-
*
86-
* @param string|mixed $tag Script tag.
87-
* @param string $handle Script handle.
88-
* @return string|mixed Script tag with type="text/partytown" for eligible scripts.
89-
*/
90-
function plwwo_update_script_type( $tag, string $handle ) {
91-
if (
92-
is_string( $tag )
93-
&&
94-
(bool) wp_scripts()->get_data( $handle, 'worker' )
95-
) {
96-
$html_processor = new WP_HTML_Tag_Processor( $tag );
97-
while ( $html_processor->next_tag( array( 'tag_name' => 'SCRIPT' ) ) ) {
98-
if ( $html_processor->get_attribute( 'id' ) === "{$handle}-js" ) {
99-
$html_processor->set_attribute( 'type', 'text/partytown' );
100-
$tag = $html_processor->get_updated_html();
101-
break;
102-
}
103-
}
104-
}
105-
return $tag;
106-
}
10715
add_filter( 'script_loader_tag', 'plwwo_update_script_type', 10, 2 );
108-
109-
/**
110-
* Filters inline script attributes to offload to a worker if the script has been opted-in.
111-
*
112-
* @since 0.1.0
113-
* @access private
114-
*
115-
* @param array<string, mixed>|mixed $attributes Attributes.
116-
* @return array<string, mixed> Attributes.
117-
*/
118-
function plwwo_filter_inline_script_attributes( $attributes ): array {
119-
$attributes = (array) $attributes;
120-
if (
121-
isset( $attributes['id'] )
122-
&&
123-
1 === preg_match( '/^(?P<handle>.+)-js-(?:before|after)$/', $attributes['id'], $matches )
124-
&&
125-
(bool) wp_scripts()->get_data( $matches['handle'], 'worker' )
126-
) {
127-
$attributes['type'] = 'text/partytown';
128-
}
129-
return $attributes;
130-
}
13116
add_filter( 'wp_inline_script_attributes', 'plwwo_filter_inline_script_attributes' );
132-
133-
/**
134-
* Displays the HTML generator meta tag for the Web Worker Offloading plugin.
135-
*
136-
* See {@see 'wp_head'}.
137-
*
138-
* @since 0.1.1
139-
*/
140-
function plwwo_render_generator_meta_tag(): void {
141-
// Use the plugin slug as it is immutable.
142-
echo '<meta name="generator" content="web-worker-offloading ' . esc_attr( WEB_WORKER_OFFLOADING_VERSION ) . '">' . "\n";
143-
}
14417
add_action( 'wp_head', 'plwwo_render_generator_meta_tag' );
18+
// @codeCoverageIgnoreEnd

plugins/web-worker-offloading/load.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
if ( ! defined( 'ABSPATH' ) ) {
2020
exit; // Exit if accessed directly.
2121
}
22-
// @codeCoverageIgnoreEnd
2322

2423
// Define the constant.
2524
if ( defined( 'WEB_WORKER_OFFLOADING_VERSION' ) ) {
@@ -49,3 +48,4 @@
4948
require_once __DIR__ . '/helper.php';
5049
require_once __DIR__ . '/hooks.php';
5150
require_once __DIR__ . '/third-party.php';
51+
// @codeCoverageIgnoreEnd

plugins/web-worker-offloading/tests/test-web-worker-offloading.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public static function data_update_script_types(): array {
190190
*
191191
* @covers ::plwwo_update_script_type
192192
* @covers ::plwwo_filter_print_scripts_array
193-
* @cogers ::plwwo_filter_inline_script_attributes
193+
* @covers ::plwwo_filter_inline_script_attributes
194194
*
195195
* @dataProvider data_update_script_types
196196
*
@@ -324,4 +324,38 @@ public function test_plwwo_render_generator_meta_tag(): void {
324324
private function reset_wp_dependencies(): void {
325325
$GLOBALS['wp_scripts'] = null;
326326
}
327+
328+
/**
329+
* Test the function that marks scripts for offloading.
330+
*
331+
* @covers ::plwwo_mark_scripts_for_offloading
332+
*/
333+
public function test_plwwo_mark_scripts_for_offloading(): void {
334+
// Enqueue a script.
335+
wp_enqueue_script( 'test-script', 'https://example.com/test-script.js', array(), '1.0.0', true );
336+
plwwo_mark_scripts_for_offloading( array( 'test-script' ) );
337+
338+
$html = get_echo( array( wp_scripts(), 'do_items' ) );
339+
340+
// Check that the 'worker' data has been added to the script.
341+
$this->assertTrue( wp_scripts()->get_data( 'test-script', 'worker' ) );
342+
343+
$this->assertStringContainsString( 'Partytown', $html );
344+
}
345+
346+
/**
347+
* Test the function that loads third party plugin integrations.
348+
*
349+
* @covers ::plwwo_load_third_party_integrations
350+
*/
351+
public function test_plwwo_load_third_party_integrations(): void {
352+
plwwo_load_third_party_integrations();
353+
354+
// Note: Calling the above function won't actually impact these since none of the plugins are active.
355+
// In order to test these being true it would depend on either actually loading those plugins,
356+
// or defining constants/classes in separate processes.
357+
$this->assertFalse( function_exists( 'plwwo_google_site_kit_configure' ) );
358+
$this->assertFalse( function_exists( 'plwwo_rank_math_configure' ) );
359+
$this->assertFalse( function_exists( 'plwwo_woocommerce_configure' ) );
360+
}
327361
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Test cases for google-site-kit.php in Web Worker Offloading.
4+
*
5+
* @package web-worker-offloading
6+
*/
7+
class Test_Google_Site_Kit extends WP_UnitTestCase {
8+
/**
9+
* Runs the routine before each test is executed.
10+
*/
11+
public function set_up(): void {
12+
parent::set_up();
13+
require_once __DIR__ . '/../../third-party/google-site-kit.php';
14+
}
15+
16+
/**
17+
* Test the function that configures WWO for Google Site Kit.
18+
*
19+
* @covers ::plwwo_google_site_kit_configure
20+
*/
21+
public function test_plwwo_google_site_kit_configure(): void {
22+
$configuration = array();
23+
$expected_configuration = array(
24+
'globalFns' => array( 'gtag', 'wp_has_consent' ),
25+
'forward' => array( 'dataLayer.push', 'gtag' ),
26+
'mainWindowAccessors' => array(
27+
'_googlesitekitConsentCategoryMap',
28+
'_googlesitekitConsents',
29+
'wp_consent_type',
30+
'wp_fallback_consent_type',
31+
'wp_has_consent',
32+
'waitfor_consent_hook',
33+
),
34+
);
35+
36+
$result = plwwo_google_site_kit_configure( $configuration );
37+
38+
$this->assertEquals( $expected_configuration, $result );
39+
}
40+
41+
/**
42+
* Test the function that filters inline script attributes.
43+
*
44+
* @covers ::plwwo_google_site_kit_filter_inline_script_attributes
45+
*/
46+
public function test_plwwo_google_site_kit_filter_inline_script_attributes(): void {
47+
$attributes = array( 'id' => 'google_gtagjs-js-consent-mode-data-layer' );
48+
$expected_attributes = array(
49+
'id' => 'google_gtagjs-js-consent-mode-data-layer',
50+
'type' => 'text/partytown',
51+
);
52+
53+
$result = plwwo_google_site_kit_filter_inline_script_attributes( $attributes );
54+
55+
$this->assertEquals( $expected_attributes, $result );
56+
}
57+
}

0 commit comments

Comments
 (0)