Skip to content

Commit

Permalink
Add Integrations tests to 100%.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Nov 3, 2024
1 parent 25c5f8c commit 5f9837b
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 4 deletions.
131 changes: 131 additions & 0 deletions .tests/php/unit/Settings/IntegrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*/
Expand Down Expand Up @@ -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 );
}
}
6 changes: 2 additions & 4 deletions src/php/Settings/Integrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 ) {
Expand Down

0 comments on commit 5f9837b

Please sign in to comment.