| 1 |
<?php |
| 2 |
/** |
| 3 |
* Unit tests for the PluginClass class. |
| 4 |
* |
| 5 |
* @package Custom_404_Pro |
| 6 |
*/ |
| 7 |
|
| 8 |
use PHPUnit\Framework\TestCase; |
| 9 |
|
| 10 |
/** |
| 11 |
* PluginClass test case. |
| 12 |
*/ |
| 13 |
class PluginClassTest extends TestCase { |
| 14 |
|
| 15 |
/** |
| 16 |
* Resets action and textdomain stubs before each test. |
| 17 |
*/ |
| 18 |
protected function setUp(): void { |
| 19 |
parent::setUp(); |
| 20 |
$GLOBALS['_test_actions'] = array(); |
| 21 |
$GLOBALS['_load_plugin_textdomain_calls'] = array(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Asserts that load_textdomain is a public method. |
| 26 |
*/ |
| 27 |
public function test_load_textdomain_is_public_method() { |
| 28 |
$ref = new ReflectionClass( PluginClass::class ); |
| 29 |
$method = $ref->getMethod( 'load_textdomain' ); |
| 30 |
$this->assertTrue( $method->isPublic(), 'load_textdomain must be public so the plugins_loaded hook can call it' ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Asserts that plugins_loaded action is registered with load_textdomain on construction. |
| 35 |
*/ |
| 36 |
public function test_plugins_loaded_action_is_registered_on_construction() { |
| 37 |
new PluginClass(); |
| 38 |
|
| 39 |
$this->assertArrayHasKey( 'plugins_loaded', $GLOBALS['_test_actions'], 'plugins_loaded action must be registered' ); |
| 40 |
|
| 41 |
$found = false; |
| 42 |
foreach ( $GLOBALS['_test_actions']['plugins_loaded'] as $entry ) { |
| 43 |
$cb = $entry[0]; |
| 44 |
if ( is_array( $cb ) && $cb[0] instanceof PluginClass && 'load_textdomain' === $cb[1] ) { |
| 45 |
$found = true; |
| 46 |
break; |
| 47 |
} |
| 48 |
} |
| 49 |
$this->assertTrue( $found, 'plugins_loaded must be hooked to PluginClass::load_textdomain' ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Asserts that load_textdomain calls load_plugin_textdomain with the correct text domain. |
| 54 |
*/ |
| 55 |
public function test_load_textdomain_uses_correct_text_domain() { |
| 56 |
$plugin = new PluginClass(); |
| 57 |
$plugin->load_textdomain(); |
| 58 |
|
| 59 |
$this->assertNotEmpty( $GLOBALS['_load_plugin_textdomain_calls'], 'load_plugin_textdomain must be called' ); |
| 60 |
$this->assertSame( |
| 61 |
'custom-404-pro', |
| 62 |
$GLOBALS['_load_plugin_textdomain_calls'][0]['domain'], |
| 63 |
'Text domain must be "custom-404-pro"' |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Asserts that load_textdomain passes a languages path ending in /languages. |
| 69 |
*/ |
| 70 |
public function test_load_textdomain_passes_languages_path() { |
| 71 |
$plugin = new PluginClass(); |
| 72 |
$plugin->load_textdomain(); |
| 73 |
|
| 74 |
$rel_path = $GLOBALS['_load_plugin_textdomain_calls'][0]['plugin_rel_path']; |
| 75 |
$this->assertStringEndsWith( '/languages', $rel_path, 'plugin_rel_path must end with /languages' ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Asserts that load_textdomain can be called more than once without error. |
| 80 |
*/ |
| 81 |
public function test_load_textdomain_is_idempotent() { |
| 82 |
$plugin = new PluginClass(); |
| 83 |
$plugin->load_textdomain(); |
| 84 |
$plugin->load_textdomain(); |
| 85 |
|
| 86 |
$this->assertCount( 2, $GLOBALS['_load_plugin_textdomain_calls'] ); |
| 87 |
} |
| 88 |
} |
| 89 |
|