Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Nov 4, 2024
1 parent 6b65e1d commit 48c213d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
Text Domain: custom-internationalized-plugin
*/

load_plugin_textdomain( 'custom-internationalized-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
function custom_i18n_load_textdomain() {
load_plugin_textdomain( 'custom-internationalized-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}

add_action( 'init', 'custom_i18n_load_textdomain' );

function custom_i18n_plugin_test() {
return __( 'This is a dummy plugin', 'custom-internationalized-plugin' );
Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/l10n/loadTextdomainJustInTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,36 @@ public function test_get_locale_is_called_only_once_per_textdomain_with_custom_l
$this->assertFalse( is_textdomain_loaded( $textdomain ) );
$this->assertSame( 1, $filter->get_call_count() );
}

/**
* @ticket 44937
* @ticket 62337
*
* @covers ::load_plugin_textdomain
* @covers ::is_textdomain_loaded
* @covers WP_Textdomain_Registry::set_custom_path
*/
public function test_plugin_translation_should_be_translated_when_calling_load_plugin_textdomain_too_late() {
require_once DIR_TESTDATA . '/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php';

add_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );

$is_textdomain_loaded_before = is_textdomain_loaded( 'custom-internationalized-plugin' );
$output_before = custom_i18n_plugin_test();

$is_textdomain_loaded_middle = is_textdomain_loaded( 'custom-internationalized-plugin' );

custom_i18n_load_textdomain();

$output_after = custom_i18n_plugin_test();
$is_textdomain_loaded_after = is_textdomain_loaded( 'custom-internationalized-plugin' );

remove_filter( 'locale', array( $this, 'filter_set_locale_to_german' ) );

$this->assertFalse( $is_textdomain_loaded_before );
$this->assertFalse( $is_textdomain_loaded_middle );
$this->assertSame( 'This is a dummy plugin', $output_before );
$this->assertSame( 'Das ist ein Dummy Plugin', $output_after );
$this->assertTrue( $is_textdomain_loaded_after );
}
}
2 changes: 2 additions & 0 deletions tests/phpunit/tests/l10n/wpLocaleSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ public function test_switch_reloads_plugin_translations_outside_wp_lang_dir() {

require_once DIR_TESTDATA . '/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php';

custom_i18n_load_textdomain();

$actual = custom_i18n_plugin_test();

switch_to_locale( 'es_ES' );
Expand Down

0 comments on commit 48c213d

Please sign in to comment.