Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Nov 17, 2023
1 parent 2f0a5cb commit 15cc8b3
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/wp-includes/class-wp-textdomain-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -182,11 +183,15 @@ public function get_language_files_from_path( $path ) {
return $mo_files;
}

Check failure on line 185 in src/wp-includes/class-wp-textdomain-registry.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Functions must not contain multiple empty lines in a row; found 2 empty lines

$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' );
}

Expand Down
82 changes: 82 additions & 0 deletions tests/phpunit/tests/l10n/wpTextdomainRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
);
}

/**
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 15cc8b3

Please sign in to comment.