Made the path tokenization deterministic
Since the tokens are replaced in a first-discovered first-replaced order, we may accidentally create tokenized paths like '{{ABSPATH}}/test' instead of the desired '{{WP_CONTENT_DIR}}test'. By ordering them according to specificity however, we ensure that we tokenize as much of the path as possible.
This commit is contained in:
parent
4aab99614a
commit
33e81654a9
|
@ -159,6 +159,25 @@ function wc_update_order( $args ) {
|
|||
* @return string The tokenized path.
|
||||
*/
|
||||
function wc_tokenize_path( $path, $path_tokens ) {
|
||||
// Order most to least specific so that the token can encompass as much of the path as possible.
|
||||
uasort(
|
||||
$path_tokens,
|
||||
function ( $a, $b ) {
|
||||
$a = strlen( $a );
|
||||
$b = strlen( $b );
|
||||
|
||||
if ( $a > $b ) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ( $b > $a ) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
);
|
||||
|
||||
foreach ( $path_tokens as $token => $token_path ) {
|
||||
if ( 0 !== strpos( $path, $token_path ) ) {
|
||||
continue;
|
||||
|
|
|
@ -283,7 +283,7 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
|
|||
public function test_wc_get_log_file_path() {
|
||||
$log_dir = trailingslashit( WC_LOG_DIR );
|
||||
$hash_name = sanitize_file_name( wp_hash( 'unit-tests' ) );
|
||||
$date_suffix = date( 'Y-m-d', time() );
|
||||
$date_suffix = date( 'Y-m-d', time() ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
|
||||
|
||||
$this->assertEquals( $log_dir . 'unit-tests-' . $date_suffix . '-' . $hash_name . '.log', wc_get_log_file_path( 'unit-tests' ) );
|
||||
}
|
||||
|
@ -573,66 +573,66 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
|
|||
* Tests the wc_tokenize_path function.
|
||||
*/
|
||||
public function test_wc_tokenize_path() {
|
||||
$path = wc_tokenize_path( ABSPATH . '/test', array() );
|
||||
$this->assertEquals( ABSPATH . '/test', $path );
|
||||
$path = wc_tokenize_path( ABSPATH . 'test', array() );
|
||||
$this->assertEquals( ABSPATH . 'test', $path );
|
||||
|
||||
$path = wc_tokenize_path(
|
||||
ABSPATH . '/test',
|
||||
ABSPATH . 'test',
|
||||
array(
|
||||
'ABSPATH' => ABSPATH,
|
||||
)
|
||||
);
|
||||
$this->assertEquals( '{{ABSPATH}}/test', $path );
|
||||
$this->assertEquals( '{{ABSPATH}}test', $path );
|
||||
|
||||
$path = wc_tokenize_path(
|
||||
ABSPATH . '/test',
|
||||
ABSPATH . 'test',
|
||||
array(
|
||||
'WP_CONTENT_DIR' => WP_CONTENT_DIR,
|
||||
)
|
||||
);
|
||||
$this->assertEquals( ABSPATH . '/test', $path );
|
||||
$this->assertEquals( ABSPATH . 'test', $path );
|
||||
|
||||
$path = wc_tokenize_path(
|
||||
WP_CONTENT_DIR . '/test',
|
||||
WP_CONTENT_DIR . 'test',
|
||||
array(
|
||||
'WP_CONTENT_DIR' => WP_CONTENT_DIR,
|
||||
'ABSPATH' => ABSPATH,
|
||||
'WP_CONTENT_DIR' => WP_CONTENT_DIR,
|
||||
)
|
||||
);
|
||||
$this->assertEquals( '{{WP_CONTENT_DIR}}/test', $path );
|
||||
$this->assertEquals( '{{WP_CONTENT_DIR}}test', $path );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the wc_untokenize_path function.
|
||||
*/
|
||||
public function test_wc_untokenize_path() {
|
||||
$path = wc_untokenize_path( '{{ABSPATH}}/test', array() );
|
||||
$this->assertEquals( '{{ABSPATH}}/test', $path );
|
||||
$path = wc_untokenize_path( '{{ABSPATH}}test', array() );
|
||||
$this->assertEquals( '{{ABSPATH}}test', $path );
|
||||
|
||||
$path = wc_untokenize_path(
|
||||
'{{ABSPATH}}/test',
|
||||
'{{ABSPATH}}test',
|
||||
array(
|
||||
'ABSPATH' => ABSPATH,
|
||||
)
|
||||
);
|
||||
$this->assertEquals( ABSPATH . '/test', $path );
|
||||
$this->assertEquals( ABSPATH . 'test', $path );
|
||||
|
||||
$path = wc_untokenize_path(
|
||||
'{{ABSPATH}}/test',
|
||||
'{{ABSPATH}}test',
|
||||
array(
|
||||
'WP_CONTENT_DIR' => WP_CONTENT_DIR,
|
||||
)
|
||||
);
|
||||
$this->assertEquals( '{{ABSPATH}}/test', $path );
|
||||
$this->assertEquals( '{{ABSPATH}}test', $path );
|
||||
|
||||
$path = wc_untokenize_path(
|
||||
'{{WP_CONTENT_DIR}}/test',
|
||||
'{{WP_CONTENT_DIR}}test',
|
||||
array(
|
||||
'WP_CONTENT_DIR' => WP_CONTENT_DIR,
|
||||
'ABSPATH' => ABSPATH,
|
||||
)
|
||||
);
|
||||
$this->assertEquals( WP_CONTENT_DIR . '/test', $path );
|
||||
$this->assertEquals( WP_CONTENT_DIR . 'test', $path );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -682,7 +682,7 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
|
|||
ob_start();
|
||||
try {
|
||||
wc_get_template( 'global/wrapper-start.php' );
|
||||
} catch ( \Exception $exception ) {
|
||||
} catch ( \Exception $exception ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
|
||||
// Since the file doesn't really exist this is going to throw an exception (which is fine for our test).
|
||||
}
|
||||
ob_end_clean();
|
||||
|
@ -1044,7 +1044,9 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
|
|||
$this->assertInstanceOf( 'WC_Customer', $this->wc->customer );
|
||||
$this->assertInstanceOf( 'WC_Session', $this->wc->session );
|
||||
|
||||
$this->wc->cart = $this->wc->customer = $this->wc->session = null;
|
||||
$this->wc->cart = null;
|
||||
$this->wc->customer = null;
|
||||
$this->wc->session = null;
|
||||
$this->assertNull( $this->wc->cart );
|
||||
$this->assertNull( $this->wc->customer );
|
||||
$this->assertNull( $this->wc->session );
|
||||
|
|
Loading…
Reference in New Issue