File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -74,10 +74,11 @@ public function recordLoginAttempt(
7474 */
7575 public function previousLogin (User $ user ): ?Login
7676 {
77- return $ this ->where ('success ' , 1 )
77+ $ previous = $ this ->where ('success ' , 1 )
7878 ->where ('user_id ' , $ user ->id )
79- ->orderBy ('id ' , 'desc ' )->get ()
80- ->getRow (1 , Login::class);
79+ ->orderBy ('id ' , 'desc ' )
80+ ->findAll (1 , 1 );
81+ return ($ previous ) ? $ previous [0 ] : NULL ;
8182 }
8283
8384 /**
Original file line number Diff line number Diff line change @@ -114,6 +114,41 @@ public function testLastLogin(): void
114114 $ this ->assertInstanceOf (Time::class, $ last ->date );
115115 }
116116
117+ public function testPreviousLogin (): void
118+ {
119+ fake (
120+ UserIdentityModel::class,
121+ ['user_id ' => $ this ->user ->id , 'type ' => Session::ID_TYPE_EMAIL_PASSWORD , 'secret ' => 'foo@example.com ' ]
122+ );
123+
124+ // No logins found.
125+ $ this ->assertNull ($ this ->user ->previousLogin ());
126+
127+ fake (
128+ LoginModel::class,
129+ ['id_type ' => 'email ' , 'identifier ' => $ this ->user ->email , 'user_id ' => $ this ->user ->id ]
130+ );
131+ $ login2 = fake (
132+ LoginModel::class,
133+ ['id_type ' => 'email ' , 'identifier ' => $ this ->user ->email , 'user_id ' => $ this ->user ->id ]
134+ );
135+ fake (
136+ LoginModel::class,
137+ [
138+ 'id_type ' => 'email ' ,
139+ 'identifier ' => $ this ->user ->email ,
140+ 'user_id ' => $ this ->user ->id ,
141+ 'success ' => false ,
142+ ]
143+ );
144+
145+ $ previous = $ this ->user ->previousLogin ();
146+
147+ $ this ->assertInstanceOf (Login::class, $ previous ); // @phpstan-ignore-line
148+ $ this ->assertSame ($ login2 ->id , $ previous ->id );
149+ $ this ->assertInstanceOf (Time::class, $ previous ->date );
150+ }
151+
117152 /**
118153 * @see https://github.com/codeigniter4/shield/issues/103
119154 */
You can’t perform that action at this time.
0 commit comments