From 15cc8b3828e03e3a75f1fe6dcd8ef672d5b07c79 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 17 Nov 2023 13:52:43 +0100 Subject: [PATCH] Add tests --- .../class-wp-textdomain-registry.php | 7 +- .../tests/l10n/wpTextdomainRegistry.php | 82 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-textdomain-registry.php b/src/wp-includes/class-wp-textdomain-registry.php index 04b22a97b71da..2936f3b9a81f0 100644 --- a/src/wp-includes/class-wp-textdomain-registry.php +++ b/src/wp-includes/class-wp-textdomain-registry.php @@ -48,9 +48,10 @@ class WP_Textdomain_Registry { protected $custom_paths = array(); /** - * Unused. Holds a cached list of available .mo files to improve performance. + * Holds a cached list of available .mo files to improve performance. * * @since 6.1.0 + * @since 6.5.0 This property is no longer used. * * @var array * @@ -182,11 +183,15 @@ public function get_language_files_from_path( $path ) { return $mo_files; } + $cache_key = 'cached_mo_files_' . md5( $path ); $mo_files = wp_cache_get( $cache_key, 'translations' ); if ( false === $mo_files ) { $mo_files = glob( $path . '*.mo' ); + if ( false === $mo_files ) { + $mo_files = array(); + } wp_cache_set( $cache_key, $mo_files, 'translations' ); } diff --git a/tests/phpunit/tests/l10n/wpTextdomainRegistry.php b/tests/phpunit/tests/l10n/wpTextdomainRegistry.php index 0c852f334ae60..c333eb927227d 100644 --- a/tests/phpunit/tests/l10n/wpTextdomainRegistry.php +++ b/tests/phpunit/tests/l10n/wpTextdomainRegistry.php @@ -18,6 +18,15 @@ public function set_up() { $this->instance = new WP_Textdomain_Registry(); } + public function tear_down() { + wp_cache_delete( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . '/foobar/' ), 'translations' ); + wp_cache_delete( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . '/plugins/' ), 'translations' ); + wp_cache_delete( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . '/themes/' ), 'translations' ); + wp_cache_delete( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) ), 'translations' ); + + parent::tear_down(); + } + /** * @covers ::has * @covers ::get @@ -39,6 +48,10 @@ public function test_set_custom_path() { $this->instance->get( 'foo', 'de_DE' ), 'Custom path for textdomain not returned' ); + $this->assertNotFalse( + wp_cache_get( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . 'bar/' ), 'translations' ), + 'List of files in custom path not cached' + ); } /** @@ -67,6 +80,75 @@ public function test_set_populates_cache() { ); } + /** + * @covers ::get_language_files_from_path + */ + public function test_get_language_files_from_path_caches_results() { + $this->instance->get_language_files_from_path( trailingslashit( WP_LANG_DIR ) . '/foobar/' ); + $this->instance->get_language_files_from_path( trailingslashit( WP_LANG_DIR ) . '/plugins/' ); + $this->instance->get_language_files_from_path( trailingslashit( WP_LANG_DIR ) . '/themes/' ); + $this->instance->get_language_files_from_path( trailingslashit( WP_LANG_DIR ) ); + + $this->assertNotFalse( wp_cache_get( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . '/plugins/' ), 'translations' ) ); + $this->assertNotFalse( wp_cache_get( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . '/themes/' ), 'translations' ) ); + $this->assertNotFalse( wp_cache_get( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . '/foobar/' ), 'translations' ) ); + $this->assertNotFalse( wp_cache_get( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) ), 'translations' ) ); + } + + /** + * @covers ::get_language_files_from_path + */ + public function test_get_language_files_from_path_short_circuit() { + add_filter( 'pre_get_language_files_from_path', '__return_empty_array' ); + $result = $this->instance->get_language_files_from_path( trailingslashit( WP_LANG_DIR ) . '/plugins/' ); + remove_filter( 'pre_get_language_files_from_path', '__return_empty_array' ); + + $cache = wp_cache_get( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . '/plugins/' ), 'translations' ); + + $this->assertEmpty( $result ); + $this->assertFalse( $cache ); + } + + /** + * @covers ::invalidate_mo_files_cache + */ + public function test_invalidate_mo_files_cache() { + $this->instance->get_language_files_from_path( trailingslashit( WP_LANG_DIR ) . '/plugins/' ); + $this->instance->get_language_files_from_path( trailingslashit( WP_LANG_DIR ) . '/themes/' ); + $this->instance->get_language_files_from_path( trailingslashit( WP_LANG_DIR ) ); + + $this->instance->invalidate_mo_files_cache( + null, + array( + 'type' => 'translation', + 'translations' => array( + (object) array( + 'type' => 'plugin', + 'slug' => 'internationalized-plugin', + 'language' => 'de_DE', + 'version' => '99.9.9', + ), + (object) array( + 'type' => 'theme', + 'slug' => 'internationalized-theme', + 'language' => 'de_DE', + 'version' => '99.9.9', + ), + (object) array( + 'type' => 'core', + 'slug' => 'default', + 'language' => 'es_ES', + 'version' => '99.9.9', + ), + ), + ) + ); + + $this->assertFalse( wp_cache_get( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . '/plugins/' ), 'translations' ) ); + $this->assertFalse( wp_cache_get( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . '/themes/' ), 'translations' ) ); + $this->assertFalse( wp_cache_get( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) ), 'translations' ) ); + } + public function data_domains_locales() { return array( 'Non-existent plugin' => array(