Last active
August 2, 2016 18:12
-
-
Save GaryJones/c8259da3a4501fd0648f19beddce0249 to your computer and use it in GitHub Desktop.
Test that plugin called `load_plugin_textdomain()` correctly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace GAA\CorpManager; | |
use WP_UnitTestCase; | |
class PluginTest extends WP_UnitTestCase { | |
private $domain; | |
private $mofile; | |
/** | |
* @covers GAA\CorpManager\Plugin::load_plugin_textdomain | |
*/ | |
public function test_load_plugin_textdomain() { | |
// Plugin-specific values. | |
$text_domain = 'corp-manager'; | |
$languages_dir = 'languages/'; | |
$load_callable = [ $GLOBALS[ 'corp_manager_plugin' ], 'load_plugin_textdomain' ]; | |
$locale = $this->_locale_cb(); | |
// Check the plugin method that loads the text domain is hooked into the right filter. | |
$this->assertNotFalse( has_action( 'plugins_loaded', $load_callable ) ); | |
// Change the locale to Spanish. | |
add_filter( 'locale', [ $this, '_locale_cb' ] ); | |
// Hook in to this action, so we can extract what the domain and mofile path will be the next time | |
// a text domain is loaded. | |
add_action( 'override_load_textdomain', array( $this, '_override_load_textdomain_cb' ), 10, 3 ); | |
// Call the textdomain loading function. | |
call_user_func( $load_callable ); | |
// Remove the hack as we don't care about any future textdomain loading calls. | |
remove_action( 'override_load_textdomain', array( $this, '_override_load_textdomain_cb' ), 10, 3 ); | |
// Check that the loaded domain name is what we think it should be. | |
$this->assertSame( $text_domain, $this->domain ); | |
// Check the path to the loaded mofile is correct. | |
// Since `load_plugin_textdomain()` will prefix the relative path with the path to the test directory path | |
// i.e. wordpress-develop/wp-content/plugins, regardless of where your plugin is located, then we just | |
// check the string contains the bit we know it should have. | |
$this->assertStringEndsWith( "/{$text_domain}/{$languages_dir}{$text_domain}-{$locale}.mo", $this->mofile ); | |
// Revert the locale change. | |
remove_filter( 'locale', [ $this, '_locale_cb' ] ); | |
} | |
public function _override_load_textdomain_cb( $override, $domain, $mofile ) { | |
$this->domain = $domain; | |
$this->mofile = $mofile; | |
return $override; // or simply true? | |
} | |
public function _locale_cb() { | |
return 'es_ES'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Huge hat-tip to @swissspidy, as most of the code is his!