mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
I18N: Change how WP_Textdomain_Registry stores the default languages path.
`WP_Textdomain_Registry` was introduced in [53874] to store text domains and their language directory paths, addressing issues with just-in-time loading of textdomains when using locale switching and when using`load_*_textdomain()` functions. Said change has inadvertently caused a performance regression exactly when using`load_*_textdomain()`, which still often is the case, where the cached information was not further used or even overridden. This change addresses that issue by storing the default languages paths in a separate way, while at the same time making `WP_Textdomain_Registry` easier to maintain and adding new tests to catch future regressions. Props flixos90, spacedmonkey, ocean90, SergeyBiryukov, costdev. Fixes #39210. git-svn-id: https://develop.svn.wordpress.org/trunk@54669 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -28,7 +28,7 @@ class Tests_L10n_LoadTextdomain extends WP_UnitTestCase {
|
||||
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
|
||||
global $wp_textdomain_registry;
|
||||
|
||||
$wp_textdomain_registry->reset();
|
||||
$wp_textdomain_registry = new WP_Textdomain_Registry();
|
||||
}
|
||||
|
||||
public function tear_down() {
|
||||
@@ -36,7 +36,7 @@ class Tests_L10n_LoadTextdomain extends WP_UnitTestCase {
|
||||
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
|
||||
global $wp_textdomain_registry;
|
||||
|
||||
$wp_textdomain_registry->reset();
|
||||
$wp_textdomain_registry = new WP_Textdomain_Registry();
|
||||
|
||||
parent::tear_down();
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ class Tests_L10n_LoadTextdomainJustInTime extends WP_UnitTestCase {
|
||||
protected $orig_theme_dir;
|
||||
protected $theme_root;
|
||||
protected static $user_id;
|
||||
private $locale_count;
|
||||
|
||||
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
|
||||
self::$user_id = $factory->user->create(
|
||||
@@ -24,7 +23,6 @@ class Tests_L10n_LoadTextdomainJustInTime extends WP_UnitTestCase {
|
||||
|
||||
$this->theme_root = DIR_TESTDATA . '/themedir1';
|
||||
$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
|
||||
$this->locale_count = 0;
|
||||
|
||||
// /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
|
||||
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
|
||||
@@ -37,7 +35,7 @@ class Tests_L10n_LoadTextdomainJustInTime extends WP_UnitTestCase {
|
||||
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
|
||||
global $wp_textdomain_registry;
|
||||
|
||||
$wp_textdomain_registry->reset();
|
||||
$wp_textdomain_registry = new WP_Textdomain_Registry();
|
||||
}
|
||||
|
||||
public function tear_down() {
|
||||
@@ -48,7 +46,7 @@ class Tests_L10n_LoadTextdomainJustInTime extends WP_UnitTestCase {
|
||||
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
|
||||
global $wp_textdomain_registry;
|
||||
|
||||
$wp_textdomain_registry->reset();
|
||||
$wp_textdomain_registry = new WP_Textdomain_Registry();
|
||||
|
||||
parent::tear_down();
|
||||
}
|
||||
@@ -262,7 +260,8 @@ class Tests_L10n_LoadTextdomainJustInTime extends WP_UnitTestCase {
|
||||
public function test_get_locale_is_called_only_once_per_textdomain() {
|
||||
$textdomain = 'foo-bar-baz';
|
||||
|
||||
add_filter( 'locale', array( $this, '_filter_locale_count' ) );
|
||||
$filter = new MockAction();
|
||||
add_filter( 'locale', array( $filter, 'filter' ) );
|
||||
|
||||
__( 'Foo', $textdomain );
|
||||
__( 'Bar', $textdomain );
|
||||
@@ -270,15 +269,31 @@ class Tests_L10n_LoadTextdomainJustInTime extends WP_UnitTestCase {
|
||||
__( 'Foo Bar', $textdomain );
|
||||
__( 'Foo Bar Baz', $textdomain );
|
||||
|
||||
remove_filter( 'locale', array( $this, '_filter_locale_count' ) );
|
||||
$this->assertFalse( is_textdomain_loaded( $textdomain ) );
|
||||
$this->assertSame( 1, $filter->get_call_count() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 37997
|
||||
* @ticket 39210
|
||||
*
|
||||
* @covers ::_load_textdomain_just_in_time
|
||||
*/
|
||||
public function test_get_locale_is_called_only_once_per_textdomain_with_custom_lang_dir() {
|
||||
load_plugin_textdomain( 'custom-internationalized-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
|
||||
|
||||
$textdomain = 'custom-internationalized-plugin';
|
||||
|
||||
$filter = new MockAction();
|
||||
add_filter( 'locale', array( $filter, 'filter' ) );
|
||||
|
||||
__( 'Foo', $textdomain );
|
||||
__( 'Bar', $textdomain );
|
||||
__( 'Baz', $textdomain );
|
||||
__( 'Foo Bar', $textdomain );
|
||||
__( 'Foo Bar Baz', $textdomain );
|
||||
|
||||
$this->assertFalse( is_textdomain_loaded( $textdomain ) );
|
||||
$this->assertSame( 1, $this->locale_count );
|
||||
}
|
||||
|
||||
public function _filter_locale_count( $locale ) {
|
||||
++$this->locale_count;
|
||||
|
||||
return $locale;
|
||||
$this->assertSame( 1, $filter->get_call_count() );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
|
||||
global $wp_textdomain_registry;
|
||||
|
||||
$wp_textdomain_registry->reset();
|
||||
$wp_textdomain_registry = new WP_Textdomain_Registry();
|
||||
}
|
||||
|
||||
public function tear_down() {
|
||||
@@ -36,7 +36,7 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
|
||||
global $wp_textdomain_registry;
|
||||
|
||||
$wp_textdomain_registry->reset();
|
||||
$wp_textdomain_registry = new WP_Textdomain_Registry();
|
||||
|
||||
parent::tear_down();
|
||||
}
|
||||
@@ -478,11 +478,12 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
|
||||
require_once DIR_TESTDATA . '/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php';
|
||||
|
||||
$registry_value = $wp_textdomain_registry->get( 'custom-internationalized-plugin', determine_locale() );
|
||||
|
||||
$actual = custom_i18n_plugin_test();
|
||||
|
||||
switch_to_locale( 'es_ES' );
|
||||
|
||||
$registry_value = $wp_textdomain_registry->get( 'custom-internationalized-plugin', determine_locale() );
|
||||
|
||||
switch_to_locale( 'de_DE' );
|
||||
|
||||
$actual_de_de = custom_i18n_plugin_test();
|
||||
@@ -517,11 +518,12 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
|
||||
|
||||
require_once get_stylesheet_directory() . '/functions.php';
|
||||
|
||||
$registry_value = $wp_textdomain_registry->get( 'custom-internationalized-theme', determine_locale() );
|
||||
|
||||
$actual = custom_i18n_theme_test();
|
||||
|
||||
switch_to_locale( 'es_ES' );
|
||||
|
||||
$registry_value = $wp_textdomain_registry->get( 'custom-internationalized-theme', determine_locale() );
|
||||
|
||||
switch_to_locale( 'de_DE' );
|
||||
|
||||
$actual_de_de = custom_i18n_theme_test();
|
||||
|
||||
150
tests/phpunit/tests/l10n/wpTextdomainRegistry.php
Normal file
150
tests/phpunit/tests/l10n/wpTextdomainRegistry.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group l10n
|
||||
* @group i18n
|
||||
*
|
||||
* @coversDefaultClass WP_Textdomain_Registry
|
||||
*/
|
||||
class Tests_L10n_wpTextdomainRegistry extends WP_UnitTestCase {
|
||||
/**
|
||||
* @var WP_Textdomain_Registry
|
||||
*/
|
||||
protected $instance;
|
||||
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
|
||||
$this->instance = new WP_Textdomain_Registry();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::has
|
||||
* @covers ::get
|
||||
* @covers ::set_custom_path
|
||||
*/
|
||||
public function test_set_custom_path() {
|
||||
$reflection = new ReflectionClass( $this->instance );
|
||||
$reflection_property = $reflection->getProperty( 'cached_mo_files' );
|
||||
$reflection_property->setAccessible( true );
|
||||
|
||||
$this->assertNull(
|
||||
$reflection_property->getValue( $this->instance ),
|
||||
'Cache not empty by default'
|
||||
);
|
||||
|
||||
$this->instance->set_custom_path( 'foo', WP_LANG_DIR . '/bar' );
|
||||
|
||||
$this->assertTrue(
|
||||
$this->instance->has( 'foo' ),
|
||||
'Incorrect availability status for textdomain with custom path'
|
||||
);
|
||||
$this->assertFalse(
|
||||
$this->instance->get( 'foo', 'en_US' ),
|
||||
'Should not return custom path for textdomain and en_US locale'
|
||||
);
|
||||
$this->assertSame(
|
||||
WP_LANG_DIR . '/bar/',
|
||||
$this->instance->get( 'foo', 'de_DE' ),
|
||||
'Custom path for textdomain not returned'
|
||||
);
|
||||
$this->assertArrayHasKey(
|
||||
WP_LANG_DIR . '/bar',
|
||||
$reflection_property->getValue( $this->instance ),
|
||||
'Custom path missing from cache'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::get
|
||||
* @dataProvider data_domains_locales
|
||||
*/
|
||||
public function test_get( $domain, $locale, $expected ) {
|
||||
$reflection = new ReflectionClass( $this->instance );
|
||||
$reflection_property = $reflection->getProperty( 'cached_mo_files' );
|
||||
$reflection_property->setAccessible( true );
|
||||
|
||||
$actual = $this->instance->get( $domain, $locale );
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$actual,
|
||||
'Expected languages directory path not matching actual one'
|
||||
);
|
||||
|
||||
$this->assertArrayHasKey(
|
||||
WP_LANG_DIR . '/plugins',
|
||||
$reflection_property->getValue( $this->instance ),
|
||||
'Default plugins path missing from cache'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::get_path_from_lang_dir
|
||||
*/
|
||||
public function test_get_does_not_check_themes_directory_for_plugin() {
|
||||
$reflection = new ReflectionClass( $this->instance );
|
||||
$reflection_property = $reflection->getProperty( 'cached_mo_files' );
|
||||
$reflection_property->setAccessible( true );
|
||||
|
||||
$this->instance->get( 'internationalized-plugin', 'de_DE' );
|
||||
|
||||
$this->assertArrayHasKey(
|
||||
WP_LANG_DIR . '/plugins',
|
||||
$reflection_property->getValue( $this->instance ),
|
||||
'Default plugins path missing from cache'
|
||||
);
|
||||
$this->assertArrayNotHasKey(
|
||||
WP_LANG_DIR . '/themes',
|
||||
$reflection_property->getValue( $this->instance ),
|
||||
'Default themes path should not be in cache'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::set
|
||||
* @covers ::get
|
||||
*/
|
||||
public function test_set_populates_cache() {
|
||||
$this->instance->set( 'foo-plugin', 'de_DE', '/foo/bar' );
|
||||
|
||||
$this->assertSame(
|
||||
'/foo/bar/',
|
||||
$this->instance->get( 'foo-plugin', 'de_DE' )
|
||||
);
|
||||
}
|
||||
|
||||
public function data_domains_locales() {
|
||||
return array(
|
||||
'Non-existent plugin' => array(
|
||||
'unknown-plugin',
|
||||
'en_US',
|
||||
false,
|
||||
),
|
||||
'Non-existent plugin with de_DE' => array(
|
||||
'unknown-plugin',
|
||||
'de_DE',
|
||||
false,
|
||||
),
|
||||
'Available de_DE translations' => array(
|
||||
'internationalized-plugin',
|
||||
'de_DE',
|
||||
WP_LANG_DIR . '/plugins/',
|
||||
),
|
||||
'Available es_ES translations' => array(
|
||||
'internationalized-plugin',
|
||||
'es_ES',
|
||||
WP_LANG_DIR . '/plugins/',
|
||||
),
|
||||
'Unavailable fr_FR translations' => array(
|
||||
'internationalized-plugin',
|
||||
'fr_FR',
|
||||
false,
|
||||
),
|
||||
'Unavailable en_US translations' => array(
|
||||
'internationalized-plugin',
|
||||
'en_US',
|
||||
false,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user