Skip to content

Commit fa69d62

Browse files
committed
[WOOTAX-294] Make the asset file path overridable for tests
Use the WCSERVICES_PLUGIN_DIST_DIR constant and extract the asset path into a protected method, so tests can point the lookup at a temp directory instead of writing fixtures into the real dist directory.
1 parent 0efd855 commit fa69d62

3 files changed

Lines changed: 70 additions & 33 deletions

File tree

src/Integrations/WooCommerceBlocksIntegration.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ public function register_scripts() {
8585
}
8686
}
8787

88+
/**
89+
* Get the filesystem path of a script's asset file.
90+
*
91+
* @param string $handle Script handle.
92+
*
93+
* @return string
94+
*/
95+
protected function get_script_asset_path( string $handle ): string {
96+
return WCSERVICES_PLUGIN_DIST_DIR . $handle . '.asset.php';
97+
}
98+
8899
/**
89100
* Register a script for the integration.
90101
*
@@ -94,12 +105,14 @@ protected function register_script( string $handle ) {
94105
$plugin_version = Utils::get_wcservices_version();
95106
$script_name = "$handle-$plugin_version.js";
96107
$script_url = Utils::get_enqueue_base_url() . $script_name;
97-
$script_asset_path = Utils::get_plugin_path() . 'dist/' . $handle . '.asset.php';
108+
$script_asset_path = $this->get_script_asset_path( $handle );
98109
$script_asset = file_exists( $script_asset_path )
99110
? require $script_asset_path : array(); // nosemgrep: audit.php.lang.security.file.inclusion-arg --- This is a safe file inclusion.
100111

101-
// The defaults cover the globals the script reads at load time:
102-
// window.wp.plugins, window.wp.element, window.wp.data and window.wc.blocksCheckout.
112+
// The webpack build does not emit asset files, so the defaults below are
113+
// the live dependency list; keep them in sync with the globals the script
114+
// reads at load time: window.wp.plugins, window.wp.element, window.wp.data
115+
// and window.wc.blocksCheckout.
103116
$script_dependencies = $script_asset['dependencies'] ?? array( 'wp-plugins', 'wp-element', 'wp-data', 'wc-blocks-checkout' );
104117

105118
wp_register_script(
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Automattic\WCServices\Integrations\WooCommerceBlocksIntegration;
4+
5+
/**
6+
* Test double that points asset file lookups at a temp directory,
7+
* so tests never touch the plugin's real dist/ directory.
8+
*/
9+
class WCS_Test_Blocks_Integration extends WooCommerceBlocksIntegration {
10+
11+
/**
12+
* Directory the asset file lookup should use.
13+
*
14+
* @var string
15+
*/
16+
public $asset_dir;
17+
18+
/**
19+
* Get the filesystem path of a script's asset file.
20+
*
21+
* @param string $handle Script handle.
22+
*
23+
* @return string
24+
*/
25+
protected function get_script_asset_path( string $handle ): string {
26+
return $this->asset_dir . $handle . '.asset.php';
27+
}
28+
}

tests/php/test-class-woocommerce-blocks-integration.php

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

3-
use Automattic\WCServices\Integrations\WooCommerceBlocksIntegration;
4-
use Automattic\WCServices\Utils;
3+
require_once __DIR__ . '/class-wcs-test-blocks-integration.php';
54

65
/**
76
* Unit tests for WooCommerceBlocksIntegration.
@@ -11,44 +10,49 @@ class WP_Test_WooCommerce_Blocks_Integration extends WC_Unit_Test_Case {
1110
const HANDLE = 'woocommerce-services-store-notices';
1211

1312
/**
14-
* Path of the asset file created by a test, if any.
13+
* Temp directory used for asset files, removed on tear down.
1514
*
16-
* @var string|null
15+
* @var string
1716
*/
18-
private $asset_file;
17+
private $asset_dir;
1918

2019
/**
21-
* Whether the dist directory was created by a test.
22-
*
23-
* @var bool
20+
* Create a fresh temp asset directory.
2421
*/
25-
private $created_dist_dir = false;
22+
public function set_up() {
23+
parent::set_up();
24+
25+
$this->asset_dir = trailingslashit( get_temp_dir() ) . 'wcs-test-assets-' . uniqid() . '/';
26+
wp_mkdir_p( $this->asset_dir );
27+
}
2628

2729
/**
28-
* Deregister the script and remove any test artifacts.
30+
* Deregister the script and remove the temp asset directory.
2931
*/
3032
public function tear_down() {
3133
wp_deregister_script( self::HANDLE );
3234

33-
if ( $this->asset_file && file_exists( $this->asset_file ) ) {
34-
wp_delete_file( $this->asset_file );
35-
$this->asset_file = null;
35+
$asset_file = $this->asset_dir . self::HANDLE . '.asset.php';
36+
if ( file_exists( $asset_file ) ) {
37+
wp_delete_file( $asset_file );
3638
}
37-
if ( $this->created_dist_dir ) {
38-
rmdir( Utils::get_plugin_path() . 'dist' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir --- Test cleanup of a directory the test created.
39-
$this->created_dist_dir = false;
39+
if ( is_dir( $this->asset_dir ) ) {
40+
rmdir( $this->asset_dir ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir --- Test cleanup of a temp directory the test created.
4041
}
4142

4243
parent::tear_down();
4344
}
4445

4546
/**
46-
* Build an integration instance like the plugin loader does.
47+
* Build an integration instance whose asset lookup uses the temp directory.
4748
*
48-
* @return WooCommerceBlocksIntegration
49+
* @return WCS_Test_Blocks_Integration
4950
*/
5051
private function new_integration() {
51-
return new WooCommerceBlocksIntegration( 'https://example.com/wp-content/plugins/woocommerce-services/dist/' );
52+
$integration = new WCS_Test_Blocks_Integration( 'https://example.com/wp-content/plugins/woocommerce-services/dist/' );
53+
$integration->asset_dir = $this->asset_dir;
54+
55+
return $integration;
5256
}
5357

5458
/**
@@ -68,19 +72,11 @@ public function test_store_notices_script_registers_with_default_dependencies()
6872
}
6973

7074
/**
71-
* With an asset file present in dist/, its dependencies must win over the defaults.
75+
* With an asset file present, its dependencies must win over the defaults.
7276
*/
7377
public function test_store_notices_script_uses_asset_file_dependencies_when_present() {
74-
$dist_dir = Utils::get_plugin_path() . 'dist/';
75-
76-
if ( ! is_dir( $dist_dir ) ) {
77-
wp_mkdir_p( $dist_dir );
78-
$this->created_dist_dir = true;
79-
}
80-
81-
$this->asset_file = $dist_dir . self::HANDLE . '.asset.php';
82-
file_put_contents( // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents --- Test fixture written to a location the test cleans up.
83-
$this->asset_file,
78+
file_put_contents( // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents --- Test fixture written to a temp directory the test cleans up.
79+
$this->asset_dir . self::HANDLE . '.asset.php',
8480
'<?php return array( "dependencies" => array( "wp-element" ), "version" => "test" );'
8581
);
8682

0 commit comments

Comments
 (0)