Skip to content

Commit

Permalink
manage user with factory
Browse files Browse the repository at this point in the history
  • Loading branch information
remyperona committed Oct 3, 2024
1 parent 0c24ae4 commit 5e5bc82
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace WP_Rocket\Tests\Integration\inc\Engine\Common\PerformanceHints\Frontend\Subscriber;

use Brain\Monkey\Functions;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Tests\Integration\FilesystemTestCase;

/**
Expand All @@ -13,10 +12,9 @@
*/
class Test_MaybeApplyOptimizations extends FilesystemTestCase {
protected $path_to_test_data = '/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php';

protected $config;

private $cached_user = false;
private $user_id = 0;

public static function set_up_before_class() {
parent::set_up_before_class();
Expand Down Expand Up @@ -45,6 +43,10 @@ public function tear_down() {
remove_filter( 'pre_get_rocket_option_cache_logged_user', [ $this, 'get_cache_user' ] );
$this->restoreWpHook( 'rocket_buffer' );

if ( $this->user_id > 0 ) {
wp_delete_user( $this->user_id );
}

parent::tear_down();
}

Expand All @@ -55,7 +57,6 @@ public function testShouldReturnAsExpected( $config, $expected ) {
$this->config = $config;
$this->cached_user = $config['user_cache_enabled'] ?? false;


if ( isset( $config['query_string'] ) ) {
$_GET[ $config['query_string'] ] = 1;
}
Expand All @@ -72,9 +73,13 @@ public function testShouldReturnAsExpected( $config, $expected ) {
}

Functions\when( 'wp_create_nonce' )->justReturn( '96ac96b69e' );
Functions\when( 'is_user_logged_in' )->justReturn( $config['is_logged_in'] ?? false );

// Override cache_logged_user option for this specific scenario
if ( isset( $config['is_logged_in'] ) ) {
$this->user_id = self::factory()->user->create( [ 'role' => 'editor' ] );
wp_set_current_user( $this->user_id );
}

// Override cache_logged_user option for this specific scenario.
add_filter( 'pre_get_rocket_option_cache_logged_user', [ $this, 'get_cache_user' ] );

$this->assertSame(
Expand Down

0 comments on commit 5e5bc82

Please sign in to comment.