Skip to content

Commit

Permalink
Add missing test case and @cover annotations for improved code covera…
Browse files Browse the repository at this point in the history
…ge in web-worker-offloading plugin
  • Loading branch information
Sarthak Jaiswal authored and Sarthak Jaiswal committed Feb 14, 2025
1 parent d94977f commit 1c60b11
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 10 deletions.
2 changes: 1 addition & 1 deletion plugins/web-worker-offloading/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function plwwo_get_configuration(): array {
);

if ( WP_DEBUG && SCRIPT_DEBUG ) {
$config['debug'] = true;
$config['debug'] = true;// @codeCoverageIgnore
}

/**
Expand Down
4 changes: 2 additions & 2 deletions plugins/web-worker-offloading/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ function plwwo_register_default_scripts( WP_Scripts $scripts ): void {
// The source code for partytown.js is built from <https://github.com/BuilderIO/partytown/blob/b292a14047a0c12ca05ba97df1833935d42fdb66/src/lib/main/snippet.ts>.
// See webpack config in the WordPress/performance repo: <https://github.com/WordPress/performance/blob/282a068f3eb2575d37aeb9034e894e7140fcddca/webpack.config.js#L84-L130>.
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
$partytown_js_path = '/build/debug/partytown.js';
$partytown_js_path = '/build/debug/partytown.js';// @codeCoverageIgnore
} else {
$partytown_js_path = '/build/partytown.js';
}

$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.
if ( false === $partytown_js ) {
return;
return;// @codeCoverageIgnore
}

$scripts->add(
Expand Down
2 changes: 1 addition & 1 deletion plugins/web-worker-offloading/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// @codeCoverageIgnoreEnd

// Define the constant.
if ( defined( 'WEB_WORKER_OFFLOADING_VERSION' ) ) {
Expand Down Expand Up @@ -49,3 +48,4 @@
require_once __DIR__ . '/helper.php';
require_once __DIR__ . '/hooks.php';
require_once __DIR__ . '/third-party.php';
// @codeCoverageIgnoreEnd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function set_up(): void {
parent::set_up();
$this->reset_wp_dependencies();
add_theme_support( 'html5', array( 'script' ) );
require_once __DIR__ . '/../third-party/woocommerce.php';
require_once __DIR__ . '/../third-party/seo-by-rank-math.php';
}

/**
Expand Down Expand Up @@ -190,7 +192,7 @@ public static function data_update_script_types(): array {
*
* @covers ::plwwo_update_script_type
* @covers ::plwwo_filter_print_scripts_array
* @cogers ::plwwo_filter_inline_script_attributes
* @covers ::plwwo_filter_inline_script_attributes
*
* @dataProvider data_update_script_types
*
Expand Down Expand Up @@ -324,4 +326,38 @@ public function test_plwwo_render_generator_meta_tag(): void {
private function reset_wp_dependencies(): void {
$GLOBALS['wp_scripts'] = null;
}

/**
* Test the function that marks scripts for offloading.
*
* @covers ::plwwo_mark_scripts_for_offloading
*/
public function test_plwwo_mark_scripts_for_offloading(): void {
// Enqueue a script.
wp_enqueue_script( 'test-script', 'https://example.com/test-script.js', array(), '1.0.0', true );
plwwo_mark_scripts_for_offloading( array( 'test-script' ) );

$scripts = wp_scripts();
$scripts->do_items();

// Check that the 'worker' data has been added to the script.
$this->assertTrue( wp_scripts()->get_data( 'test-script', 'worker' ) );
}

/**
* Test the function that loads third party plugin integrations.
*
* @covers ::plwwo_load_third_party_integrations
*/
public function test_plwwo_load_third_party_integrations(): void {
if ( ! defined( 'GOOGLESITEKIT_VERSION' ) ) {
define( 'GOOGLESITEKIT_VERSION', '1.0.0' );
}
do_action( 'plugins_loaded' );

// Check that the integrations have been loaded.
$this->assertTrue( function_exists( 'plwwo_google_site_kit_configure' ) );
$this->assertTrue( function_exists( 'plwwo_rank_math_configure' ) );
$this->assertTrue( function_exists( 'plwwo_woocommerce_configure' ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Test cases for google-site-kit.php in Web Worker Offloading.
*
* @package web-worker-offloading
*/
class Test_Google_Site_Kit extends WP_UnitTestCase {

/**
* Test the function that configures WWO for Google Site Kit.
*
* @covers ::plwwo_google_site_kit_configure
*/
public function test_plwwo_google_site_kit_configure(): void {
$configuration = array();
$expected_configuration = array(
'globalFns' => array( 'gtag', 'wp_has_consent' ),
'forward' => array( 'dataLayer.push', 'gtag' ),
'mainWindowAccessors' => array(
'_googlesitekitConsentCategoryMap',
'_googlesitekitConsents',
'wp_consent_type',
'wp_fallback_consent_type',
'wp_has_consent',
'waitfor_consent_hook',
),
);

$result = plwwo_google_site_kit_configure( $configuration );

$this->assertEquals( $expected_configuration, $result );
}

/**
* Test the function that filters inline script attributes.
*
* @covers ::plwwo_google_site_kit_filter_inline_script_attributes
*/
public function test_plwwo_google_site_kit_filter_inline_script_attributes(): void {
$attributes = array( 'id' => 'google_gtagjs-js-consent-mode-data-layer' );
$expected_attributes = array(
'id' => 'google_gtagjs-js-consent-mode-data-layer',
'type' => 'text/partytown',
);

$result = plwwo_google_site_kit_filter_inline_script_attributes( $attributes );

$this->assertEquals( $expected_attributes, $result );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Test cases for seo-by-rank-math.php in Web Worker Offloading.
*
* @package web-worker-offloading
*/

class Test_Seo_By_Rank_Math extends WP_UnitTestCase {

/**
* Test the function that configures WWO for Rank Math SEO.
*/
public function test_plwwo_rank_math_configure(): void {
$configuration = array();
$expected_configuration = array(
'globalFns' => array( 'gtag' ),
'forward' => array( 'dataLayer.push', 'gtag' ),
);

$result = plwwo_rank_math_configure( $configuration );

$this->assertEquals( $expected_configuration, $result );
}

/**
* Test the function that filters script attributes.
*
* @covers ::plwwo_rank_math_filter_script_attributes
*/
public function test_plwwo_rank_math_filter_script_attributes(): void {
$attributes = array( 'id' => 'google_gtagjs' );
$expected_attributes = array(
'id' => 'google_gtagjs',
'type' => 'text/partytown',
);

$result = plwwo_rank_math_filter_script_attributes( $attributes );

$this->assertEquals( $expected_attributes, $result );
}

/**
* Test the function that filters inline script attributes.
*
* @covers ::plwwo_rank_math_filter_inline_script_attributes
*/
public function test_plwwo_rank_math_filter_inline_script_attributes(): void {
$attributes = array( 'id' => 'google_gtagjs-inline' );
$expected_attributes = array(
'id' => 'google_gtagjs-inline',
'type' => 'text/partytown',
);

$result = plwwo_rank_math_filter_inline_script_attributes( $attributes );

$this->assertEquals( $expected_attributes, $result );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Test cases for woocommerce.php in Web Worker Offloading.
*
* @package web-worker-offloading
*/

class Test_WooCommerce extends WP_UnitTestCase {

/**
* Test the function that configures WWO for WooCommerce.
*/
public function test_plwwo_woocommerce_configure(): void {
$configuration = array();
$expected_configuration = array(
'globalFns' => array( 'gtag' ),
'forward' => array( 'dataLayer.push', 'gtag' ),
);

$result = plwwo_woocommerce_configure( $configuration );

$this->assertEquals( $expected_configuration, $result );
}
}
5 changes: 3 additions & 2 deletions plugins/web-worker-offloading/third-party/google-site-kit.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function plwwo_google_site_kit_configure( $configuration ): array {

return $configuration;
}
// @codeCoverageIgnoreStart
add_filter( 'plwwo_configuration', 'plwwo_google_site_kit_configure' );

plwwo_mark_scripts_for_offloading(
Expand All @@ -51,7 +52,7 @@ function plwwo_google_site_kit_configure( $configuration ): array {
'googlesitekit-consent-mode',
)
);

// @codeCoverageIgnoreEnd
/**
* Filters inline script attributes to offload Google Site Kit's GTag script tag to Partytown.
*
Expand All @@ -70,4 +71,4 @@ function plwwo_google_site_kit_filter_inline_script_attributes( $attributes ) {
return $attributes;
}

add_filter( 'wp_inline_script_attributes', 'plwwo_google_site_kit_filter_inline_script_attributes' );
add_filter( 'wp_inline_script_attributes', 'plwwo_google_site_kit_filter_inline_script_attributes' );// @codeCoverageIgnore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function plwwo_rank_math_configure( $configuration ): array {
$configuration['forward'][] = 'gtag';
return $configuration;
}
add_filter( 'plwwo_configuration', 'plwwo_rank_math_configure' );
add_filter( 'plwwo_configuration', 'plwwo_rank_math_configure' );// @codeCoverageIgnore

/*
* Note: The following integration is not targeting the \RankMath\Analytics\GTag::enqueue_gtag_js() code which is only
Expand Down Expand Up @@ -60,7 +60,7 @@ function plwwo_rank_math_filter_script_attributes( $attributes ) {
return $attributes;
}

add_filter( 'wp_script_attributes', 'plwwo_rank_math_filter_script_attributes' );
add_filter( 'wp_script_attributes', 'plwwo_rank_math_filter_script_attributes' );// @codeCoverageIgnore

/**
* Filters inline script attributes to offload Rank Math's GTag script tag to Partytown.
Expand All @@ -80,4 +80,4 @@ function plwwo_rank_math_filter_inline_script_attributes( $attributes ) {
return $attributes;
}

add_filter( 'wp_inline_script_attributes', 'plwwo_rank_math_filter_inline_script_attributes' );
add_filter( 'wp_inline_script_attributes', 'plwwo_rank_math_filter_inline_script_attributes' );// @codeCoverageIgnore
2 changes: 2 additions & 0 deletions plugins/web-worker-offloading/third-party/woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function plwwo_woocommerce_configure( $configuration ): array {

return $configuration;
}
// @codeCoverageIgnoreStart
add_filter( 'plwwo_configuration', 'plwwo_woocommerce_configure' );

plwwo_mark_scripts_for_offloading(
Expand All @@ -42,3 +43,4 @@ function plwwo_woocommerce_configure( $configuration ): array {
'woocommerce-google-analytics-integration-gtag',
)
);
// @codeCoverageIgnoreEnd

0 comments on commit 1c60b11

Please sign in to comment.