From f275d7c207959a6e451feae945c8d27c6d441115 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Mon, 2 Dec 2024 14:42:24 +0200 Subject: [PATCH] Add a unit test that disabled providers are still deleted during uninstall --- tests/class-two-factor-core.php | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/class-two-factor-core.php b/tests/class-two-factor-core.php index 5f6a15a2..a2bcaa57 100644 --- a/tests/class-two-factor-core.php +++ b/tests/class-two-factor-core.php @@ -1581,4 +1581,41 @@ public function test_uninstall_removes_user_meta() { 'Provider was disabled due to uninstall' ); } + + /** + * Plugin uninstall removes all user meta even for disabled providers. + * + * @covers Two_Factor_Core::uninstall + */ + public function test_uninstall_removes_disabled_provider_user_meta() { + $user = self::factory()->user->create_and_get(); + + // Enable a provider for the user. + Two_Factor_Core::enable_provider_for_user( $user->ID, 'Two_Factor_Totp' ); + + $totp_provider = Two_Factor_Totp::get_instance(); + + $totp_provider->set_user_totp_key( $user->ID, 'some_key' ); + + $this->assertEquals( 'some_key', $totp_provider->get_user_totp_key( $user->ID ), 'TOTP secret was set for user' ); + + add_filter( + 'two_factor_providers', + function( $providers ) { + return array_diff_key( $providers, array( 'Two_Factor_Totp' => null ) ); + } + ); + + $this->assertNotContains( + 'Two_Factor_Totp', + Two_Factor_Core::get_enabled_providers_for_user( $user->ID ), + 'TOTP provider is disabled for everyone via filter' + ); + + Two_Factor_Core::uninstall(); + + $this->assertEmpty( $totp_provider->get_user_totp_key( $user->ID ), 'TOTP secret was deleted during uninstall' ); + + remove_all_filters( 'two_factor_providers' ); + } }