@@ -811,6 +811,61 @@ public function test_show_password_reset_error() {
811
811
$ this ->assertStringContainsString ( 'check your email for instructions on regaining access ' , $ contents );
812
812
}
813
813
814
+ /**
815
+ * @covers Two_Factor_Core::enable_provider_for_user()
816
+ * @covers Two_Factor_Core::disable_provider_for_user()
817
+ */
818
+ public function test_enable_disable_provider_for_user () {
819
+ $ user = self ::factory ()->user ->create_and_get ();
820
+ $ enabled_providers = Two_Factor_Core::get_enabled_providers_for_user ( $ user ->ID );
821
+ $ this ->assertEmpty ( $ enabled_providers );
822
+
823
+ // Disabling one that's already disabled should succeed.
824
+ $ totp_disabled = Two_Factor_Core::disable_provider_for_user ( $ user ->ID , 'Two_Factor_Totp ' );
825
+ $ this ->assertTrue ( $ totp_disabled );
826
+
827
+ // Disabling one that doesn't exist should fail.
828
+ $ nonexistent_enabled = Two_Factor_Core::enable_provider_for_user ( $ user ->ID , 'Nonexistent_Provider ' );
829
+ $ enabled_providers = Two_Factor_Core::get_enabled_providers_for_user ( $ user ->ID );
830
+ $ this ->assertFalse ( $ nonexistent_enabled );
831
+ $ this ->assertEmpty ( $ enabled_providers );
832
+ $ this ->assertNull ( Two_Factor_Core::get_primary_provider_for_user ( $ user ->ID ) );
833
+
834
+ // Enabling a valid one should succeed. The first one that's enabled and configured should be the default primary.
835
+ $ totp = Two_Factor_Totp::get_instance ();
836
+ $ totp ->set_user_totp_key ( $ user ->ID , 'foo ' );
837
+ $ totp_enabled = Two_Factor_Core::enable_provider_for_user ( $ user ->ID , 'Two_Factor_Totp ' );
838
+ $ enabled_providers = Two_Factor_Core::get_enabled_providers_for_user ( $ user ->ID );
839
+ $ this ->assertTrue ( $ totp_enabled );
840
+ $ this ->assertSame ( array ( 'Two_Factor_Totp ' ), $ enabled_providers );
841
+ $ this ->assertSame ( 'Two_Factor_Totp ' , Two_Factor_Core::get_primary_provider_for_user ( $ user ->ID )->get_key () );
842
+
843
+ // Enabling one that's already enabled should succeed.
844
+ $ totp_enabled = Two_Factor_Core::enable_provider_for_user ( $ user ->ID , 'Two_Factor_Totp ' );
845
+ $ this ->assertTrue ( $ totp_enabled );
846
+
847
+ // Enabling another should succeed, and not change the primary.
848
+ $ dummy_enabled = Two_Factor_Core::enable_provider_for_user ( $ user ->ID , 'Two_Factor_Dummy ' );
849
+ $ enabled_providers = Two_Factor_Core::get_enabled_providers_for_user ( $ user ->ID );
850
+ $ this ->assertTrue ( $ dummy_enabled );
851
+ $ this ->assertSame ( array ( 'Two_Factor_Totp ' , 'Two_Factor_Dummy ' ), $ enabled_providers );
852
+ $ this ->assertSame ( 'Two_Factor_Totp ' , Two_Factor_Core::get_primary_provider_for_user ( $ user ->ID )->get_key () );
853
+
854
+ // Disabling one that doesn't exist should fail.
855
+ $ nonexistent_disabled = Two_Factor_Core::disable_provider_for_user ( $ user ->ID , 'Nonexistent_Provider ' );
856
+ $ enabled_providers = Two_Factor_Core::get_enabled_providers_for_user ( $ user ->ID );
857
+ $ this ->assertFalse ( $ nonexistent_disabled );
858
+ $ this ->assertSame ( array ( 'Two_Factor_Totp ' , 'Two_Factor_Dummy ' ), $ enabled_providers );
859
+ $ this ->assertSame ( 'Two_Factor_Totp ' , Two_Factor_Core::get_primary_provider_for_user ( $ user ->ID )->get_key () );
860
+
861
+ // Disabling one that's enabled should succeed, and change the primary to the next available one.
862
+ $ totp_disabled = Two_Factor_Core::disable_provider_for_user ( $ user ->ID , 'Two_Factor_Totp ' );
863
+ $ enabled_providers = Two_Factor_Core::get_enabled_providers_for_user ( $ user ->ID );
864
+ $ this ->assertTrue ( $ totp_disabled ); //todo enable and fix
865
+ $ this ->assertSame ( array ( 1 => 'Two_Factor_Dummy ' ), $ enabled_providers );
866
+ $ this ->assertSame ( 'Two_Factor_Dummy ' , Two_Factor_Core::get_primary_provider_for_user ( $ user ->ID )->get_key () );
867
+ }
868
+
814
869
/**
815
870
* Ensure that when a user enables two factor, that they are able to continue to change settings.
816
871
*
0 commit comments