Skip to content

Commit 9dcef48

Browse files
westonruterclaude
andcommitted
Use MockAction for content_url test, drop spy from the others
Replace the bespoke by-ref-closure spy (not used elsewhere in tests/phpunit/) with the idiomatic MockAction helper. Keep the spy only on test_unparseable_content_url_returns_false, where the early return is the only thing distinguishing the new branch from the fallback path tail-end load_script_translations(false, ...) call. The other two tests do not need a spy: removing the early return for unparseable src triggers the "Automatic conversion of false to array" deprecation, and reverting the is_string guard triggers a TypeError in str_ends_with() -- both surface as test failures already. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent f3e294e commit 9dcef48

1 file changed

Lines changed: 9 additions & 44 deletions

File tree

tests/phpunit/tests/l10n/loadScriptTextdomain.php

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -173,28 +173,6 @@ public function test_does_not_throw_deprecation_notice_for_rtrim_with_default_pa
173173
$this->assertSame( $expected, load_script_textdomain( $handle ) );
174174
}
175175

176-
/**
177-
* Records every `$file` value passed to `load_script_translations()`
178-
* so individual tests can assert which code path produced the result.
179-
*
180-
* @return list<string|false> Reference to the array updated by the spy filter.
181-
*/
182-
private function &spy_load_script_translations_files(): array {
183-
/** @var list<string|false> $files_seen */
184-
$files_seen = array();
185-
add_filter(
186-
'pre_load_script_translations',
187-
static function ( $translations, $file ) use ( &$files_seen ) {
188-
assert( is_string( $file ) || false === $file );
189-
$files_seen[] = $file;
190-
return $translations;
191-
},
192-
10,
193-
2
194-
);
195-
return $files_seen;
196-
}
197-
198176
/**
199177
* Tests that an unparseable script source URL short-circuits to
200178
* `load_script_translations( false, ... )` instead of falling through
@@ -208,26 +186,22 @@ public function test_unparseable_src_returns_false(): void {
208186

209187
$this->assertFalse( wp_parse_url( $src ), 'Test prerequisite failed: the test src should be unparseable.' );
210188

211-
$files_seen = &$this->spy_load_script_translations_files();
212-
213189
wp_enqueue_script( $handle, $src, array(), null );
214190

215191
$this->assertFalse( load_script_textdomain( $handle, 'default', DIR_TESTDATA . '/languages' ) );
216-
$this->assertSame(
217-
array(
218-
DIR_TESTDATA . '/languages/en_US-' . $handle . '.json',
219-
false,
220-
),
221-
$files_seen,
222-
'Expected the unparseable $src branch to short-circuit before any relative-path lookup.'
223-
);
224192
}
225193

226194
/**
227195
* Tests that an unparseable `content_url()` return value short-circuits
228196
* to `load_script_translations( false, ... )` instead of computing
229197
* `$relative` from a corrupted parsed-URL array.
230198
*
199+
* The `MockAction` spy on `pre_load_script_translations` is necessary
200+
* here because the function's tail end also calls `load_script_translations( false, ... )`,
201+
* so a regression that bypasses the early return would still return false
202+
* via the fallback path. Asserting on the recorded `$file` arguments pins
203+
* the test to the intended branch.
204+
*
231205
* @ticket 65015
232206
*/
233207
public function test_unparseable_content_url_returns_false(): void {
@@ -241,7 +215,8 @@ static function () {
241215
}
242216
);
243217

244-
$files_seen = &$this->spy_load_script_translations_files();
218+
$mock = new MockAction();
219+
add_filter( 'pre_load_script_translations', array( $mock, 'filter' ), 10, 4 );
245220

246221
wp_enqueue_script( $handle, $src, array(), null );
247222

@@ -251,7 +226,7 @@ static function () {
251226
DIR_TESTDATA . '/languages/en_US-' . $handle . '.json',
252227
false,
253228
),
254-
$files_seen,
229+
array_column( $mock->get_args(), 1 ),
255230
'Expected the unparseable content_url branch to short-circuit before any relative-path lookup.'
256231
);
257232
}
@@ -270,19 +245,9 @@ public function test_non_string_relative_path_filter_returns_false(): void {
270245

271246
add_filter( 'load_script_textdomain_relative_path', '__return_null' );
272247

273-
$files_seen = &$this->spy_load_script_translations_files();
274-
275248
wp_enqueue_script( $handle, $src, array(), null );
276249

277250
$this->assertFalse( load_script_textdomain( $handle, 'default', DIR_TESTDATA . '/languages' ) );
278-
$this->assertSame(
279-
array(
280-
DIR_TESTDATA . '/languages/en_US-' . $handle . '.json',
281-
false,
282-
),
283-
$files_seen,
284-
'Expected the non-string $relative branch to short-circuit before md5 path computation.'
285-
);
286251
}
287252

288253
/**

0 commit comments

Comments
 (0)