Skip to content

Commit

Permalink
Fix actual tests for pre-WP 6.3.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixarntz committed Nov 5, 2023
1 parent 7f6907f commit f593f9b
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions tests/phpunit/tests/Plugin_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function test_fast_smooth_scroll_enqueue_scripts_with_debug_polyfill_para

$polyfill_enqueued = wp_script_is( 'fast-smooth-scroll-scroll-behavior-polyfill', 'enqueued' );
$loader_enqueued = wp_script_is( 'fast-smooth-scroll-polyfills', 'enqueued' );
$polyfill_inline_script = wp_scripts()->get_inline_script_data( 'fast-smooth-scroll-scroll-behavior-polyfill', 'before' );
$polyfill_inline_script = $this->get_inline_script_data( 'fast-smooth-scroll-scroll-behavior-polyfill', 'before' );

// Restore original `$wp_scripts`.
$wp_scripts = $orig_wp_scripts;
Expand All @@ -139,7 +139,7 @@ public function test_fast_smooth_scroll_enqueue_scripts_with_debug_polyfill_para

$polyfill_enqueued = wp_script_is( 'fast-smooth-scroll-scroll-behavior-polyfill', 'enqueued' );
$loader_enqueued = wp_script_is( 'fast-smooth-scroll-polyfills', 'enqueued' );
$polyfill_inline_script = wp_scripts()->get_inline_script_data( 'fast-smooth-scroll-scroll-behavior-polyfill', 'before' );
$polyfill_inline_script = $this->get_inline_script_data( 'fast-smooth-scroll-scroll-behavior-polyfill', 'before' );

// Restore original `$wp_scripts`.
$wp_scripts = $orig_wp_scripts;
Expand All @@ -149,4 +149,25 @@ public function test_fast_smooth_scroll_enqueue_scripts_with_debug_polyfill_para
$this->assertFalse( $loader_enqueued );
$this->assertSame( 'document.documentElement.style.scrollBehavior = "auto";', $polyfill_inline_script );
}

/**
* Workaround (almost polyfill) for `WP_Scripts::get_inline_script_data()` (introduced in WordPress 6.3).
*
* @param string $handle Name of the script to get data for.
* @param string $position Optional. Whether to add the inline script before the handle or after. Default 'after'.
* @return string Inline script, which may be empty string.
*/
private function get_inline_script_data( $handle, $position = 'after' ) {
$wp_scripts = wp_scripts();

if ( method_exists( $wp_scripts, 'get_inline_script_data' ) ) {
return $wp_scripts->get_inline_script_data( $handle, $position );
}

$data = $wp_scripts->get_data( $handle, $position );
if ( empty( $data ) || ! is_array( $data ) ) {
return '';
}
return trim( implode( "\n", $data ), "\n" );
}
}

0 comments on commit f593f9b

Please sign in to comment.