Skip to content

Commit 1b63aec

Browse files
committed
WP/I18n: add tests for namespaced names
I'm adding three different tests for fully qualified global function calls to cover all the global functions that are referenced directly in the `I18nSniff::process_matched_token()` method.
1 parent 89512f5 commit 1b63aec

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

WordPress/Tests/WP/I18nUnitTest.1.inc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ _n( 'I have %d cat.', 'I have %d cats.', $number, 'my-slug' ); // OK.
4747
_n( 'I have %d cat.', 'I have %d cats.', $number, "illegal $string" ); // Bad.
4848
_n( 'I have %d cat.', 'I have %d cats.', $number, SOMETHING ); // Bad.
4949

50-
_n_noop( 'I have %d cat.', 'I have %d cats.' ); // Bad, no text domain.
50+
_N_NOOP( 'I have %d cat.', 'I have %d cats.' ); // Bad, no text domain.
5151
_n_noop( 'I have %d cat.', 'I have %d cats.', 'my-slug' ); // OK.
5252
_n_noop( 'I have %d cat.', 'I have %d cats.', "illegal $string" ); // Bad.
5353
_n_noop( 'I have %d cat.', 'I have %d cats.', SOMETHING ); // Bad.
@@ -75,7 +75,7 @@ __( 'foo', 'my-slug', 'too-many-args' ); // Bad.
7575
_x( 'string', 'context', 'my-slug', 'too-many-args' ); // Bad.
7676
_n( 'I have %d cat.', 'I have %d cats.', $number, 'my-slug', 'too-many-args' ); // Bad.
7777
_n_noop( 'I have %d cat.', 'I have %d cats.', 'my-slug', 'too-many-args' ); // Bad.
78-
_nx_noop( 'I have %d cat.', 'I have %d cats.', 'Not really.', 'my-slug', 'too-many-args' ); // Bad.
78+
\_Nx_Noop( 'I have %d cat.', 'I have %d cats.', 'Not really.', 'my-slug', 'too-many-args' ); // Bad.
7979

8080
// Make sure that multi-line string literals are accepted.
8181
_nx( 'I have
@@ -317,4 +317,15 @@ esc_html_e( 'foo', '' ); // Bad: text-domain can not be empty.
317317
// PHP 8.0+: safeguard handling of newly introduced placeholders.
318318
__( 'There are %1$h monkeys in the %H', 'my-slug' ); // Bad: multiple arguments should be numbered.
319319

320+
/*
321+
* Safeguard correct handling of all types of namespaced function calls.
322+
*/
323+
\_( 'foo', 'my-slug' ); // Bad.
324+
\translate( 'foo', 'my-slug' ); // Bad.
325+
\translate_with_gettext_context( 'foo', 'bar', 'my-slug' ); // Bad.
326+
MyNamespace\__( 'foo', 'my-slug' ); // Ok.
327+
\MyNamespace\_e( 'foo', 'my-slug' ); // Ok.
328+
namespace\esc_html_e( 'foo', '' ); // The sniff should start flagging this once it can resolve relative namespaces.
329+
namespace\Sub\translate( 'foo', 'my-slug' ); // Ok.
330+
320331
// phpcs:enable WordPress.WP.I18n.MissingTranslatorsComment

WordPress/Tests/WP/I18nUnitTest.1.inc.fixed

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ _n( 'I have %d cat.', 'I have %d cats.', $number, 'my-slug' ); // OK.
4747
_n( 'I have %d cat.', 'I have %d cats.', $number, "illegal $string" ); // Bad.
4848
_n( 'I have %d cat.', 'I have %d cats.', $number, SOMETHING ); // Bad.
4949

50-
_n_noop( 'I have %d cat.', 'I have %d cats.' ); // Bad, no text domain.
50+
_N_NOOP( 'I have %d cat.', 'I have %d cats.' ); // Bad, no text domain.
5151
_n_noop( 'I have %d cat.', 'I have %d cats.', 'my-slug' ); // OK.
5252
_n_noop( 'I have %d cat.', 'I have %d cats.', "illegal $string" ); // Bad.
5353
_n_noop( 'I have %d cat.', 'I have %d cats.', SOMETHING ); // Bad.
@@ -75,7 +75,7 @@ __( 'foo', 'my-slug', 'too-many-args' ); // Bad.
7575
_x( 'string', 'context', 'my-slug', 'too-many-args' ); // Bad.
7676
_n( 'I have %d cat.', 'I have %d cats.', $number, 'my-slug', 'too-many-args' ); // Bad.
7777
_n_noop( 'I have %d cat.', 'I have %d cats.', 'my-slug', 'too-many-args' ); // Bad.
78-
_nx_noop( 'I have %d cat.', 'I have %d cats.', 'Not really.', 'my-slug', 'too-many-args' ); // Bad.
78+
\_Nx_Noop( 'I have %d cat.', 'I have %d cats.', 'Not really.', 'my-slug', 'too-many-args' ); // Bad.
7979

8080
// Make sure that multi-line string literals are accepted.
8181
_nx( 'I have
@@ -317,4 +317,15 @@ esc_html_e( 'foo', '' ); // Bad: text-domain can not be empty.
317317
// PHP 8.0+: safeguard handling of newly introduced placeholders.
318318
__( 'There are %1$h monkeys in the %H', 'my-slug' ); // Bad: multiple arguments should be numbered.
319319

320+
/*
321+
* Safeguard correct handling of all types of namespaced function calls.
322+
*/
323+
\_( 'foo', 'my-slug' ); // Bad.
324+
\translate( 'foo', 'my-slug' ); // Bad.
325+
\translate_with_gettext_context( 'foo', 'bar', 'my-slug' ); // Bad.
326+
MyNamespace\__( 'foo', 'my-slug' ); // Ok.
327+
\MyNamespace\_e( 'foo', 'my-slug' ); // Ok.
328+
namespace\esc_html_e( 'foo', '' ); // The sniff should start flagging this once it can resolve relative namespaces.
329+
namespace\Sub\translate( 'foo', 'my-slug' ); // Ok.
330+
320331
// phpcs:enable WordPress.WP.I18n.MissingTranslatorsComment

WordPress/Tests/WP/I18nUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public function getErrorList( $testFile = '' ) {
148148
311 => 1,
149149
315 => 1,
150150
318 => 1,
151+
323 => 1,
151152
);
152153

153154
case 'I18nUnitTest.2.inc':
@@ -217,6 +218,8 @@ public function getWarningList( $testFile = '' ) {
217218
300 => 1,
218219
301 => 1,
219220
302 => 1,
221+
324 => 1,
222+
325 => 1,
220223
);
221224

222225
case 'I18nUnitTest.2.inc':

0 commit comments

Comments
 (0)