From 1aae76c0eacc508c40363320b3d8902732e9fdff Mon Sep 17 00:00:00 2001 From: Christoph Daum Date: Wed, 3 Jun 2026 21:50:51 +0200 Subject: [PATCH 1/2] feat: make msls_get_switcher() $attr optional Give $attr a default of array() so the documented template tag can be called with no arguments. The body already coerced any non-array value to array(), so $attr was effectively optional; the missing default made msls_get_switcher() throw an ArgumentCountError on the natural no-arg call. The parameter stays untyped to preserve the is_array() coercion, which keeps the [sc_msls] shortcode working (WordPress passes '' when a shortcode has no attributes). Closes #643 Co-Authored-By: Claude --- includes/api.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/includes/api.php b/includes/api.php index b230259b..0a6b312f 100644 --- a/includes/api.php +++ b/includes/api.php @@ -10,10 +10,11 @@ * Get the output for using the links to the translations in your code * * @package Msls - * @param mixed $attr + * @since 3.0.0 The $attr parameter is optional and defaults to an empty array. + * @param mixed $attr Optional. Attributes forwarded to the switcher output. Default empty array. * @return string */ -function msls_get_switcher( $attr ): string { +function msls_get_switcher( $attr = array() ): string { $arr = is_array( $attr ) ? $attr : array(); $obj = apply_filters( 'msls_get_output', null ); From 318ecdb7693654e068952a6b719f0965e36da344 Mon Sep 17 00:00:00 2001 From: Christoph Daum Date: Wed, 3 Jun 2026 21:50:51 +0200 Subject: [PATCH 2/2] test: cover msls_get_switcher() argument handling Add an isolated test for includes/api.php covering the no-arg call, an explicit attribute array, and coercion of a non-array value (the '' the [sc_msls] shortcode passes). Runs in a separate process so loading api.php does not collide with the Brain Monkey function stubs used elsewhere in the suite. Co-Authored-By: Claude --- tests/phpunit/TestApi.php | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/phpunit/TestApi.php diff --git a/tests/phpunit/TestApi.php b/tests/phpunit/TestApi.php new file mode 100644 index 00000000..44d4cf39 --- /dev/null +++ b/tests/phpunit/TestApi.php @@ -0,0 +1,45 @@ +once()->with( 'msls_get_output', null )->andReturn( null ); + + $this->assertSame( '', msls_get_switcher() ); + } + + public function test_msls_get_switcher_with_array(): void { + $attr = array( 'before_item' => '
  • ' ); + + $output = \Mockery::mock(); + $output->shouldReceive( 'set_tags' )->once()->with( $attr )->andReturn( 'switcher' ); + + Functions\expect( 'apply_filters' )->once()->with( 'msls_get_output', null )->andReturn( $output ); + + $this->assertSame( 'switcher', msls_get_switcher( $attr ) ); + } + + public function test_msls_get_switcher_coerces_non_array_to_empty_array(): void { + $output = \Mockery::mock(); + $output->shouldReceive( 'set_tags' )->once()->with( array() )->andReturn( 'switcher' ); + + Functions\expect( 'apply_filters' )->once()->with( 'msls_get_output', null )->andReturn( $output ); + + // The [sc_msls] shortcode passes '' when used without attributes. + $this->assertSame( 'switcher', msls_get_switcher( '' ) ); + } +}