From 5f9837bde19e7b4a5db16bce516f81fb7847205c Mon Sep 17 00:00:00 2001 From: kagg-design Date: Sun, 3 Nov 2024 22:42:03 +0200 Subject: [PATCH] Add Integrations tests to 100%. --- .tests/php/unit/Settings/IntegrationsTest.php | 131 ++++++++++++++++++ src/php/Settings/Integrations.php | 6 +- 2 files changed, 133 insertions(+), 4 deletions(-) diff --git a/.tests/php/unit/Settings/IntegrationsTest.php b/.tests/php/unit/Settings/IntegrationsTest.php index 5267c474..e84d633c 100644 --- a/.tests/php/unit/Settings/IntegrationsTest.php +++ b/.tests/php/unit/Settings/IntegrationsTest.php @@ -32,6 +32,17 @@ */ class IntegrationsTest extends HCaptchaTestCase { + /** + * Tear down test. + * + * @return void + */ + public function tearDown(): void { + unset( $GLOBALS['wp_filter'] ); + + parent::tearDown(); + } + /** * Test page_title(). */ @@ -1305,4 +1316,124 @@ public function test_json_data(): void { self::assertSame( $expected, $subject->$method( $message ) ); } + + /** + * Test remove_action_regex(). + * + * @return void + */ + public function test_remove_action_regex(): void { + global $wp_filter; + + for ( $i = 1; $i <= 6; $i++ ) { + $action[ $i ] = [ + 'name' . $i => [ + 'function' => [ 'SomeClass' . $i, 'some_method' . $i ], + 'accepted_args' => 1, + ], + ]; + } + + $init_callbacks = (object) [ + 'callbacks' => [ + 10 => array_merge( $action[1], $action[2] ), + 20 => $action[3], + ], + ]; + $ast_callbacks = (object) [ + 'callbacks' => [ + 0 => $action[4], + 5 => array_merge( $action[5], $action[6] ), + ], + ]; + + $callback_pattern = '/^Avada/'; + + $subject = Mockery::mock( Integrations::class )->makePartial(); + + $subject->shouldAllowMockingProtectedMethods(); + + foreach ( $init_callbacks->callbacks as $priority => $actions ) { + foreach ( $actions as $action ) { + $subject->shouldReceive( 'maybe_remove_action_regex' ) + ->with( $callback_pattern, 'init', $action, $priority )->once(); + } + } + + foreach ( $ast_callbacks->callbacks as $priority => $actions ) { + foreach ( $actions as $action ) { + $subject->shouldReceive( 'maybe_remove_action_regex' ) + ->with( $callback_pattern, 'after_switch_theme', $action, $priority )->once(); + } + } + + WP_Mock::userFunction( 'current_action' )->andReturn( 'init' ); + + // No actions. + $subject->remove_action_regex( $callback_pattern ); + + // No callbacks for 'init'. + // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited + $wp_filter['init'] = []; + + $subject->remove_action_regex( $callback_pattern ); + + // Three callback for 'init'. + // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited + $wp_filter['init'] = $init_callbacks; + + $subject->remove_action_regex( $callback_pattern ); + + // Three callback for 'after_switch_theme'. + // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited + $wp_filter['after_switch_theme'] = $ast_callbacks; + + $subject->remove_action_regex( $callback_pattern, 'after_switch_theme' ); + // phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited + } + + /** + * Test maybe_remove_action_regex(). + * + * @return void + */ + public function test_maybe_remove_action_regex(): void { + $callback_pattern = '/^Avada/'; + $hook_name = 'after_switch_theme'; + $action = []; + $priority = 10; + + $subject = Mockery::mock( Integrations::class )->makePartial(); + + $subject->shouldAllowMockingProtectedMethods(); + + // Callback is closure. + $action['function'] = static function () { + return true; + }; + + $subject->maybe_remove_action_regex( $callback_pattern, $hook_name, $action, $priority ); + + // Callback is array. Class is an object. + $action['function'] = [ $this, 'some_method' ]; + + $subject->maybe_remove_action_regex( $callback_pattern, $hook_name, $action, $priority ); + + // Callback is array. Class is a string. + $action['function'] = [ 'SomeClass', 'some_method' ]; + + $subject->maybe_remove_action_regex( $callback_pattern, $hook_name, $action, $priority ); + + // Callback is a string. + $action['function'] = 'some_function'; + + $subject->maybe_remove_action_regex( $callback_pattern, $hook_name, $action, $priority ); + + // Callback is matched class and method. + $action['function'] = [ 'AvadaClass', 'some_method' ]; + + WP_Mock::userFunction( 'remove_action' )->with( $hook_name, $action['function'], $priority )->once(); + + $subject->maybe_remove_action_regex( $callback_pattern, $hook_name, $action, $priority ); + } } diff --git a/src/php/Settings/Integrations.php b/src/php/Settings/Integrations.php index 76880ab9..91b23253 100644 --- a/src/php/Settings/Integrations.php +++ b/src/php/Settings/Integrations.php @@ -1372,10 +1372,9 @@ protected function get_default_theme(): string { * @param string $callback_pattern Callback pattern to match. A regex matching to SomeNameSpace\SomeClass::some_method. * @param string $hook_name Action name. * - * @noinspection PhpSameParameterValueInspection + * @return void */ protected function remove_action_regex( string $callback_pattern, string $hook_name = '' ): void { - global $wp_filter; $hook_name = $hook_name ?: current_action(); @@ -1399,8 +1398,7 @@ protected function remove_action_regex( string $callback_pattern, string $hook_n * * @return void */ - private function maybe_remove_action_regex( string $callback_pattern, string $hook_name, array $action, int $priority ): void { - + protected function maybe_remove_action_regex( string $callback_pattern, string $hook_name, array $action, int $priority ): void { $callback = $action['function'] ?? ''; if ( $callback instanceof Closure ) {