| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Tests\Integration\QuickEdit\Services { |
| 4 |
|
| 5 |
use Extendify\QuickEdit\Services\TranslatedContext; |
| 6 |
use WP_UnitTestCase; |
| 7 |
|
| 8 |
/** |
| 9 |
* Characterizes TranslatedContext::detect() — the server-side check for |
| 10 |
* whether the current front-end render is showing a non-default-language |
| 11 |
* translation, across the three supported multilingual plugins: |
| 12 |
* |
| 13 |
* - TranslatePress: trp_settings['default-language'] vs the $TRP_LANGUAGE |
| 14 |
* global the plugin resolves per request. |
| 15 |
* - WPML: the wpml_current_language / wpml_default_language filters. |
| 16 |
* - Polylang: pll_current_language() / pll_default_language(). |
| 17 |
* |
| 18 |
* WPML is filter-driven and Polylang is exercised through global-namespace |
| 19 |
* function stubs (defined below) so neither plugin needs to be installed. |
| 20 |
*/ |
| 21 |
class TranslatedContextTest extends WP_UnitTestCase |
| 22 |
{ |
| 23 |
public function tearDown(): void |
| 24 |
{ |
| 25 |
remove_all_filters('wpml_current_language'); |
| 26 |
remove_all_filters('wpml_default_language'); |
| 27 |
delete_option('trp_settings'); |
| 28 |
unset( |
| 29 |
$GLOBALS['TRP_LANGUAGE'], |
| 30 |
$GLOBALS['__qe_test_pll_current'], |
| 31 |
$GLOBALS['__qe_test_pll_default'] |
| 32 |
); |
| 33 |
parent::tearDown(); |
| 34 |
} |
| 35 |
|
| 36 |
public function test_no_multilingual_plugin_reports_not_translated() |
| 37 |
{ |
| 38 |
$result = TranslatedContext::detect(); |
| 39 |
|
| 40 |
$this->assertFalse($result['isTranslated']); |
| 41 |
$this->assertNull($result['plugin']); |
| 42 |
} |
| 43 |
|
| 44 |
public function test_translatepress_translated_when_current_differs_from_default() |
| 45 |
{ |
| 46 |
update_option('trp_settings', ['default-language' => 'en_US']); |
| 47 |
$GLOBALS['TRP_LANGUAGE'] = 'es_ES'; |
| 48 |
|
| 49 |
$result = TranslatedContext::detect(); |
| 50 |
|
| 51 |
$this->assertTrue($result['isTranslated']); |
| 52 |
$this->assertSame('translatepress', $result['plugin']); |
| 53 |
} |
| 54 |
|
| 55 |
public function test_translatepress_not_translated_on_default_language() |
| 56 |
{ |
| 57 |
update_option('trp_settings', ['default-language' => 'en_US']); |
| 58 |
$GLOBALS['TRP_LANGUAGE'] = 'en_US'; |
| 59 |
|
| 60 |
$result = TranslatedContext::detect(); |
| 61 |
|
| 62 |
$this->assertFalse($result['isTranslated']); |
| 63 |
$this->assertNull($result['plugin']); |
| 64 |
} |
| 65 |
|
| 66 |
public function test_wpml_translated_when_current_differs_from_default() |
| 67 |
{ |
| 68 |
add_filter('wpml_current_language', function () { |
| 69 |
return 'es'; |
| 70 |
}); |
| 71 |
add_filter('wpml_default_language', function () { |
| 72 |
return 'en'; |
| 73 |
}); |
| 74 |
|
| 75 |
$result = TranslatedContext::detect(); |
| 76 |
|
| 77 |
$this->assertTrue($result['isTranslated']); |
| 78 |
$this->assertSame('wpml', $result['plugin']); |
| 79 |
} |
| 80 |
|
| 81 |
public function test_wpml_not_translated_on_default_language() |
| 82 |
{ |
| 83 |
add_filter('wpml_current_language', function () { |
| 84 |
return 'en'; |
| 85 |
}); |
| 86 |
add_filter('wpml_default_language', function () { |
| 87 |
return 'en'; |
| 88 |
}); |
| 89 |
|
| 90 |
$result = TranslatedContext::detect(); |
| 91 |
|
| 92 |
$this->assertFalse($result['isTranslated']); |
| 93 |
$this->assertNull($result['plugin']); |
| 94 |
} |
| 95 |
|
| 96 |
public function test_polylang_translated_when_current_differs_from_default() |
| 97 |
{ |
| 98 |
$GLOBALS['__qe_test_pll_current'] = 'es'; |
| 99 |
$GLOBALS['__qe_test_pll_default'] = 'en'; |
| 100 |
|
| 101 |
$result = TranslatedContext::detect(); |
| 102 |
|
| 103 |
$this->assertTrue($result['isTranslated']); |
| 104 |
$this->assertSame('polylang', $result['plugin']); |
| 105 |
} |
| 106 |
|
| 107 |
public function test_polylang_not_translated_on_default_language() |
| 108 |
{ |
| 109 |
$GLOBALS['__qe_test_pll_current'] = 'en'; |
| 110 |
$GLOBALS['__qe_test_pll_default'] = 'en'; |
| 111 |
|
| 112 |
$result = TranslatedContext::detect(); |
| 113 |
|
| 114 |
$this->assertFalse($result['isTranslated']); |
| 115 |
$this->assertNull($result['plugin']); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
namespace { |
| 121 |
|
| 122 |
// Polylang ships pll_current_language() / pll_default_language() as global |
| 123 |
// functions. Stub them when the plugin is absent so TranslatedContext's |
| 124 |
// Polylang branch can run under test; the return values are driven by the |
| 125 |
// globals each test sets, and an unset global reads as "no language" (false). |
| 126 |
if (!function_exists('pll_current_language')) { |
| 127 |
function pll_current_language($field = 'slug') |
| 128 |
{ |
| 129 |
return isset($GLOBALS['__qe_test_pll_current']) ? $GLOBALS['__qe_test_pll_current'] : false; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if (!function_exists('pll_default_language')) { |
| 134 |
function pll_default_language($field = 'slug') |
| 135 |
{ |
| 136 |
return isset($GLOBALS['__qe_test_pll_default']) ? $GLOBALS['__qe_test_pll_default'] : false; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|