Skip to content

Commit 7057417

Browse files
committed
Tests: Add end-to-end coverage for script module translation printing.
Add two integration tests verifying the full translation flow: - test_print_script_module_translations_outputs_set_locale_data covers the happy path, asserting that the inline script contains the expected module ID, the wp.i18n.setLocaleData() call, and the translated string. - test_print_script_module_translations_includes_dependencies covers translations for dependency modules (not just directly enqueued ones). Both tests use the pre_load_script_translations filter to provide mock translation data inline, so no fixture JSON files are needed. See #65015.
1 parent e23d4f2 commit 7057417

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

tests/phpunit/tests/script-modules/wpScriptModules.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,4 +2677,100 @@ public function test_print_script_module_translations_outputs_nothing_for_non_en
26772677

26782678
$this->assertEmpty( $output );
26792679
}
2680+
2681+
/**
2682+
* Tests that print_script_module_translations() outputs an inline script
2683+
* calling wp.i18n.setLocaleData() when translations are available.
2684+
*
2685+
* @ticket 65015
2686+
*
2687+
* @covers WP_Script_Modules::print_script_module_translations
2688+
* @covers ::load_script_module_textdomain
2689+
*/
2690+
public function test_print_script_module_translations_outputs_set_locale_data() {
2691+
$this->script_modules->register( 'test-module', '/wp-includes/js/test-module.js' );
2692+
$this->script_modules->enqueue( 'test-module' );
2693+
$this->script_modules->set_translations( 'test-module', 'default' );
2694+
2695+
// Provide test translations via the pre_load_script_translations filter
2696+
// so no fixture files are needed.
2697+
add_filter(
2698+
'pre_load_script_translations',
2699+
static function ( $translations, $file, $handle ) {
2700+
if ( 'test-module' !== $handle ) {
2701+
return $translations;
2702+
}
2703+
return wp_json_encode(
2704+
array(
2705+
'domain' => 'messages',
2706+
'locale_data' => array(
2707+
'messages' => array(
2708+
'' => array(
2709+
'domain' => 'messages',
2710+
'lang' => 'es',
2711+
),
2712+
'Hello' => array( 'Hola' ),
2713+
),
2714+
),
2715+
)
2716+
);
2717+
},
2718+
10,
2719+
3
2720+
);
2721+
2722+
ob_start();
2723+
$this->script_modules->print_script_module_translations();
2724+
$output = ob_get_clean();
2725+
2726+
$this->assertStringContainsString( 'test-module-js-module-translations', $output, 'Translation inline script should be printed with the expected ID.' );
2727+
$this->assertStringContainsString( 'wp.i18n.setLocaleData', $output, 'Output should call wp.i18n.setLocaleData().' );
2728+
$this->assertStringContainsString( 'Hola', $output, 'Output should contain the translated string.' );
2729+
}
2730+
2731+
/**
2732+
* Tests that print_script_module_translations() also outputs translations
2733+
* for dependencies of enqueued modules (not just directly enqueued ones).
2734+
*
2735+
* @ticket 65015
2736+
*
2737+
* @covers WP_Script_Modules::print_script_module_translations
2738+
*/
2739+
public function test_print_script_module_translations_includes_dependencies() {
2740+
$this->script_modules->register( 'dep-module', '/wp-includes/js/dep-module.js' );
2741+
$this->script_modules->register( 'main-module', '/wp-includes/js/main-module.js', array( 'dep-module' ) );
2742+
$this->script_modules->enqueue( 'main-module' );
2743+
$this->script_modules->set_translations( 'dep-module', 'default' );
2744+
2745+
add_filter(
2746+
'pre_load_script_translations',
2747+
static function ( $translations, $file, $handle ) {
2748+
if ( 'dep-module' !== $handle ) {
2749+
return $translations;
2750+
}
2751+
return wp_json_encode(
2752+
array(
2753+
'locale_data' => array(
2754+
'messages' => array(
2755+
'' => array(
2756+
'domain' => 'messages',
2757+
'lang' => 'es',
2758+
),
2759+
'World' => array( 'Mundo' ),
2760+
),
2761+
),
2762+
)
2763+
);
2764+
},
2765+
10,
2766+
3
2767+
);
2768+
2769+
ob_start();
2770+
$this->script_modules->print_script_module_translations();
2771+
$output = ob_get_clean();
2772+
2773+
$this->assertStringContainsString( 'dep-module-js-module-translations', $output, 'Dependency module translations should be printed.' );
2774+
$this->assertStringContainsString( 'Mundo', $output, 'Output should contain the dependency translation.' );
2775+
}
26802776
}

0 commit comments

Comments
 (0)