Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRESS10-42 Fix/transients filters #109

Merged
merged 11 commits into from
Dec 5, 2024
35 changes: 31 additions & 4 deletions includes/Helpers/Transient.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,25 @@ public static function get( string $key ) {
}

/**
* @var array{value:mixed, expires_at:int} $data The saved value and the Unix time it expires at.
* Implement the filters as used in {@see get_transient()}.
*/
$pre = apply_filters( "pre_transient_{$key}", false, $key );
if ( false !== $pre ) {
return $pre;
}

/**
* The saved value and the Unix time it expires at.
*
* @var array{value:mixed, expires_at:int} $data
*/
$data = \get_option( $key );
if ( is_array( $data ) && isset( $data['expires_at'], $data['value'] ) ) {
if ( $data['expires_at'] > time() ) {
return $data['value'];
/**
* Implement the filters as used in {@see get_transient()}.
*/
return apply_filters( "transient_{$key}", $data['value'], $key );
GiuseppeArcifa marked this conversation as resolved.
Show resolved Hide resolved
} else {
\delete_option( $key );
}
Expand All @@ -65,11 +78,25 @@ public static function set( string $key, $value, int $expires_in = 3600 ): bool
return \set_transient( $key, $value, $expires_in );
}

/**
* Implement the filters as used in {@see set_transient()}.
*/
$value = apply_filters( "pre_set_transient_{$key}", $value, $expires_in, $key );
$expires_in = apply_filters( "expiration_of_transient_{$key}", $expires_in, $value, $key );

$data = array(
'value' => $value,
'expires_at' => $expires_in + time(),
);
return \update_option( $key, $data, false );

$result = \update_option( $key, $data, false );

if ( $result ) {
do_action( "set_transient_{$key}", $value, $expires_in, $key );
do_action( 'setted_transient', $key, $value, $expires_in );
}

return $result;
}

/**
Expand Down Expand Up @@ -100,7 +127,7 @@ public static function delete( $key ): bool {
*/
public function __call( $name, $arguments ) {
if ( ! method_exists( __CLASS__, $name ) ) {
throw new \BadMethodCallException( "Method $name does not exist" );
throw new \BadMethodCallException( 'Method ' . esc_html( $name ) . ' does not exist' );
}
return self::$name( ...$arguments );
}
Expand Down
7 changes: 0 additions & 7 deletions tests/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* I was having trouble with {@see WP_Mock::expectAction()} so mocked it here. Because we're using PHP 7.1, we're
* not on the latest version of WP_Mock, which requires 7.4.
*/
function do_action(...$args) {}
function apply_filters(...$args) { return $args[1]; }

/**
* Resets mocks between each test case so the mocks in one do not unintentionally help pass another test.
*/
Expand Down
111 changes: 105 additions & 6 deletions tests/phpunit/includes/Helpers/TransientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NewfoldLabs\WP\Module\Helpers;

use NewfoldLabs\WP\Module\Data\Helpers\Transient;
use WP_Mock;
use WP_Mock\Tools\TestCase;

/**
Expand Down Expand Up @@ -72,6 +73,20 @@ public function test_set_transient_use_options(): void {
\WP_Mock::userFunction( 'set_transient' )
->never();

WP_Mock::expectFilter(
"pre_set_transient_{$test_transient_name}",
'value',
999,
$test_transient_name
);

WP_Mock::expectFilter(
"expiration_of_transient_{$test_transient_name}",
999,
'value',
$test_transient_name,
);

\WP_Mock::userFunction( 'update_option' )
->once()
->with(
Expand All @@ -81,6 +96,20 @@ public function test_set_transient_use_options(): void {
)
->andReturnTrue();

WP_Mock::expectAction(
"set_transient_{$test_transient_name}",
'value',
999,
$test_transient_name
);

WP_Mock::expectAction(
'setted_transient',
$test_transient_name,
'value',
999
);

Transient::set( $test_transient_name, 'value', 999 );

$this->assertConditionsMet();
Expand Down Expand Up @@ -124,6 +153,12 @@ public function test_get_transient_use_options(): void {
\WP_Mock::userFunction( 'get_transient' )
->never();

WP_Mock::expectFilter(
"pre_transient_{$test_transient_name}",
false,
$test_transient_name
);

\WP_Mock::userFunction( 'get_option' )
->once()
->with( $test_transient_name, )
Expand All @@ -134,6 +169,12 @@ public function test_get_transient_use_options(): void {
)
);

WP_Mock::expectFilter(
"transient_{$test_transient_name}",
'value',
$test_transient_name
);

$result = Transient::get( $test_transient_name );

$this->assertEquals( 'value', $result );
Expand All @@ -153,6 +194,12 @@ public function test_get_transient_use_options_expired(): void {
\WP_Mock::userFunction( 'get_transient' )
->never();

WP_Mock::expectFilter(
"pre_transient_{$test_transient_name}",
false,
$test_transient_name
);

\WP_Mock::userFunction( 'get_option' )
->once()
->with( $test_transient_name, )
Expand Down Expand Up @@ -205,22 +252,74 @@ public function test_should_use_transients_bluehost_cloud(): void {
$test_transient_name = uniqid( __FUNCTION__ );

\WP_Mock::userFunction( 'get_dropins' )
->once()
->andReturn( array( 'object-cache.php' => array() ) );
->once()
->andReturn( array( 'object-cache.php' => array() ) );

\WP_Mock::userFunction( 'set_transient' )
->once()
->with( $test_transient_name, 'value', 999 )
->andReturnTrue();
->once()
->with( $test_transient_name, 'value', 999 )
->andReturnTrue();

\WP_Mock::userFunction( 'update_option' )
->never();
->never();

\NewfoldLabs\WP\Context\setContext( 'platform', 'atomic' );

Transient::set( $test_transient_name, 'value', 999 );

$this->assertConditionsMet();
}

/**
* {@see \WP_Mock\Functions::$wp_mocked_fuctions} array is not being reset between tests. The code seems to
* have been refactored in WP_Mock's newer versions but we are stuck using PHP 7.3 for now.
*
* @runInSeparateProcess
*
* @covers ::set
*/
public function test_set_transient_filters_are_called(): void {

$test_transient_name = uniqid( __FUNCTION__ );

WP_Mock::userFunction( 'get_dropins' )
->once()
->andReturn( array( 'object-cache.php' => array() ) );

WP_Mock::expectFilter(
"pre_set_transient_{$test_transient_name}",
'value',
999,
$test_transient_name
);

WP_Mock::expectFilter(
"expiration_of_transient_{$test_transient_name}",
999,
'value',
$test_transient_name,
);

\WP_Mock::userFunction( 'update_option' )
->once()
->andReturn( true );

WP_Mock::expectAction(
"set_transient_{$test_transient_name}",
'value',
999,
$test_transient_name
);

WP_Mock::expectAction(
'setted_transient',
$test_transient_name,
'value',
999
);

Transient::set( $test_transient_name, 'value', 999 );

$this->assertConditionsMet();
}
}
41 changes: 36 additions & 5 deletions tests/phpunit/includes/HiiveConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NewfoldLabs\WP\Module\Data;

use Mockery;
use NewfoldLabs\Container\Container;
use NewfoldLabs\WP\Module\Data\Listeners\Plugin;
use WP_Mock;
use WP_Mock\Tools\TestCase;
Expand All @@ -16,6 +17,8 @@ class HiiveConnectionTest extends TestCase {
public function setUp(): void {
parent::setUp();

forceWpMockStrictModeOn();

WP_Mock::passthruFunction( '__' );
WP_Mock::passthruFunction( 'sanitize_title' );

Expand All @@ -39,7 +42,11 @@ function () {
* @covers ::get_core_data
*/
public function test_plugin_sends_boxname_to_hiive(): void {
// WP_Mock::expectAction('newfold_container_set');
/**
* Unable to get this working.
*/
forceWpMockStrictModeOff();
// WP_Mock::expectAction( 'newfold_container_set', \WP_Mock\Functions::type( Container::class ) );

$plugin = Mockery::mock( Plugin::class );
$plugin->brand = 'bluehost';
Expand All @@ -48,11 +55,11 @@ public function test_plugin_sends_boxname_to_hiive(): void {
container()->set( 'plugin', $plugin );

WP_Mock::userFunction( 'get_option' )->once()->with( 'newfold_cache_level', 2 )->andReturn( 2 );
WP_Mock::userFunction( 'get_option' )->once()->with( 'newfold_cloudflare_enabled', false )->andReturn( false );
WP_Mock::userFunction( 'get_option' )->once()->with( 'newfold_cloudflare_enabled', false )->andReturnFalse();
WP_Mock::userFunction( 'get_option' )->once()->with( 'admin_email' )->andReturn( '[email protected]' );
WP_Mock::userFunction( 'get_site_url' )->once()->withNoArgs()->andReturn( 'http://example.com' );

// WP_Mock::expectFilter('newfold_wp_data_module_core_data_filter');
// WP_Mock::expectFilter( 'newfold_wp_data_module_core_data_filter', \WP_Mock\Functions::type( 'array' ) );

global $wpdb;
$wpdb = Mockery::mock();
Expand All @@ -64,11 +71,16 @@ public function test_plugin_sends_boxname_to_hiive(): void {

self::assertArrayHasKey( 'hostname', $result );
}

/**
* @covers ::get_core_data
*/
public function test_plugin_sends_server_path_to_hiive(): void {
// WP_Mock::expectAction('newfold_container_set');
/**
* Unable to get this working.
*/
forceWpMockStrictModeOff();
// WP_Mock::expectAction( 'newfold_container_set', \WP_Mock\Functions::type( Container::class ) );

$plugin = Mockery::mock( Plugin::class );
$plugin->brand = 'bluehost';
Expand All @@ -81,7 +93,7 @@ public function test_plugin_sends_server_path_to_hiive(): void {
WP_Mock::userFunction( 'get_option' )->once()->with( 'admin_email' )->andReturn( '[email protected]' );
WP_Mock::userFunction( 'get_site_url' )->once()->withNoArgs()->andReturn( 'http://example.com' );

// WP_Mock::expectFilter('newfold_wp_data_module_core_data_filter');
// WP_Mock::expectFilter( 'newfold_wp_data_module_core_data_filter', \WP_Mock\Functions::type( 'array' ) );

global $wpdb;
$wpdb = Mockery::mock();
Expand Down Expand Up @@ -118,6 +130,12 @@ public function test_hiive_request_returns_wperror_when_no_auth_token(): void {
->once()
->andReturnNull();

/**
* Unable to get this working.
*/
forceWpMockStrictModeOff();
// WP_Mock::expectAction('wp_error_added', \WP_Mock\Functions::type( 'string' ),\WP_Mock\Functions::type( 'string' ),\WP_Mock\Functions::type( 'string' ), \WP_Mock\Functions::type( 'WP_Error' ) );

$result = $sut->hiive_request( '/sites/v2/events' );

self::assertInstanceOf( \WP_Error::class, $result );
Expand Down Expand Up @@ -347,6 +365,12 @@ function ( string $constant_name ) use ( $temp_dir ) {
->twice()
->with( 'http_headers_useragent', array( $sut, 'add_plugin_name_version_to_user_agent' ) );

/**
* Unable to get this working.
*/
forceWpMockStrictModeOff();
// WP_Mock::expectAction('wp_error_added', \WP_Mock\Functions::type( 'string' ),\WP_Mock\Functions::type( 'string' ),\WP_Mock\Functions::type( 'string' ), \WP_Mock\Functions::type( 'WP_Error' ) );

$sut->notify( array( $event ) );

$this->assertConditionsMet();
Expand All @@ -358,6 +382,7 @@ function ( string $constant_name ) use ( $temp_dir ) {
* @covers ::get_auth_token
*/
public function test_fails_to_reconnect() {

$sut = Mockery::mock( HiiveConnection::class )->makePartial();

WP_Mock::expectFilterAdded( 'http_headers_useragent', array( $sut, 'add_plugin_name_version_to_user_agent' ), 10, 2 );
Expand Down Expand Up @@ -386,6 +411,12 @@ public function test_fails_to_reconnect() {
->once()
->andReturnFalse();

/**
* Unable to get this working.
*/
forceWpMockStrictModeOff();
// WP_Mock::expectAction('wp_error_added', 'hiive_connection', 'This site is not connected to the hiive.', '', \WP_Mock\Functions::type( 'WP_Error' ) );

$sut->expects( 'reconnect' )->once()->andReturnFalse();

$result = $sut->hiive_request( 'sites/v2/events' );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/includes/Listeners/CronTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function test_cron_job_main_function(): void {
WP_Mock::userFunction('get_mu_plugins')
->once()->andReturn(array());

// WP_Mock::expectFilter('newfold_wp_data_module_cron_data_filter');
WP_Mock::expectFilter( 'newfold_wp_data_module_cron_data_filter', array( 'plugins' => array() ) );

$sut->shouldReceive('push')
->once()
Expand Down
Loading