@@ -565,4 +565,101 @@ public function testAuthenticateInvalidChecker()
565565
566566 $ form ->authenticate ($ request );
567567 }
568+
569+ /**
570+ * Test that FormAuthenticator uses default Password identifier when none is provided.
571+ *
572+ * @return void
573+ */
574+ public function testDefaultPasswordIdentifier ()
575+ {
576+ // Create an empty IdentifierCollection (simulating no explicit identifier configuration)
577+ $ identifiers = new IdentifierCollection ();
578+
579+ $ request = ServerRequestFactory::fromGlobals (
580+ ['REQUEST_URI ' => '/testpath ' ],
581+ [],
582+ ['username ' => 'mariano ' , 'password ' => 'password ' ],
583+ );
584+
585+ // FormAuthenticator should automatically configure a Password identifier
586+ $ form = new FormAuthenticator ($ identifiers );
587+ $ result = $ form ->authenticate ($ request );
588+
589+ $ this ->assertInstanceOf (Result::class, $ result );
590+ $ this ->assertSame (Result::SUCCESS , $ result ->getStatus ());
591+
592+ // Verify the identifier collection now has the Password identifier
593+ $ identifier = $ form ->getIdentifier ();
594+ $ this ->assertInstanceOf (IdentifierCollection::class, $ identifier );
595+ $ this ->assertFalse ($ identifier ->isEmpty ());
596+ }
597+
598+ /**
599+ * Test that FormAuthenticator respects explicitly configured identifier.
600+ *
601+ * @return void
602+ */
603+ public function testExplicitIdentifierNotOverridden ()
604+ {
605+ // Create an IdentifierCollection with a specific identifier
606+ $ identifiers = new IdentifierCollection ([
607+ 'Password ' => [
608+ 'className ' => 'Authentication.Password ' ,
609+ 'fields ' => [
610+ 'username ' => 'email ' ,
611+ 'password ' => 'password ' ,
612+ ],
613+ ],
614+ ]);
615+
616+ ServerRequestFactory::fromGlobals (
617+ ['REQUEST_URI ' => '/testpath ' ],
618+ [],
619+ ['email ' => 'mariano@example.com ' , 'password ' => 'password ' ],
620+ );
621+
622+ // FormAuthenticator should use the provided identifier
623+ $ form = new FormAuthenticator ($ identifiers );
624+
625+ // The identifier should remain as configured
626+ $ identifier = $ form ->getIdentifier ();
627+ $ this ->assertInstanceOf (IdentifierCollection::class, $ identifier );
628+ $ this ->assertFalse ($ identifier ->isEmpty ());
629+ $ this ->assertSame ($ identifiers , $ identifier , 'Identifier collection should be the same. ' );
630+ $ this ->assertSame ($ identifiers ->get ('Password ' ), $ identifier ->get ('Password ' ), 'Identifier should be the same. ' );
631+ }
632+
633+ /**
634+ * Test that default identifier inherits fields configuration from authenticator.
635+ *
636+ * @return void
637+ */
638+ public function testDefaultIdentifierInheritsFieldsConfig ()
639+ {
640+ // Create an empty IdentifierCollection
641+ $ identifiers = new IdentifierCollection ();
642+
643+ // Configure authenticator with custom fields mapping
644+ $ config = [
645+ 'fields ' => [
646+ 'username ' => 'user_name ' ,
647+ 'password ' => 'pass_word ' ,
648+ ],
649+ ];
650+
651+ // FormAuthenticator should create default identifier with inherited fields
652+ $ form = new FormAuthenticator ($ identifiers , $ config );
653+
654+ // Verify the identifier was created with the correct configuration
655+ $ identifier = $ form ->getIdentifier ();
656+ $ this ->assertInstanceOf (IdentifierCollection::class, $ identifier );
657+ $ this ->assertFalse ($ identifier ->isEmpty ());
658+
659+ // Verify the fields are properly configured
660+ // We can't directly access the internal configuration, but we can verify
661+ // the FormAuthenticator has the expected configuration
662+ $ this ->assertEquals ('user_name ' , $ form ->getConfig ('fields.username ' ));
663+ $ this ->assertEquals ('pass_word ' , $ form ->getConfig ('fields.password ' ));
664+ }
568665}
0 commit comments