@@ -25,31 +25,39 @@ public function testUnauthenticatedUserCannotCreateTestimonial()
2525 ]);
2626 }
2727
28- public function testAuthenticatedUserCanCreateTestimonial ()
29- {
30- // Create a user with a known password
31- $ user = User::factory ()->create (['password ' => bcrypt ('password ' )]);
32-
33- // Get a JWT token
34- $ token = JWTAuth::attempt (['email ' => $ user ->email , 'password ' => 'password ' ]);
35-
36- // Make an authenticated request
37- $ response = $ this ->postJson ('/api/v1/testimonials ' , [
38- 'content ' => 'This is a testimonial. ' ,
39- ], [
40- 'Authorization ' => 'Bearer ' . $ token ,
41- ]);
42-
43- $ response ->assertStatus (201 );
44- $ response ->assertJson ([
45- 'status ' => 'success ' ,
46- 'message ' => 'Testimonial created successfully ' ,
47- 'data ' => [
48- 'name ' => $ user ->name ,
49- 'content ' => 'This is a testimonial. ' ,
50- ],
51- ]);
52- }
28+ public function testAuthenticatedUserCanCreateTestimonialWithAnonymousName ()
29+ {
30+ // Create a user with a known password
31+ $ user = User::factory ()->create (['password ' => bcrypt ('password ' )]);
32+
33+ // Get a JWT token
34+ $ token = JWTAuth::attempt (['email ' => $ user ->email , 'password ' => 'password ' ]);
35+
36+ // Make an authenticated request without a name
37+ $ response = $ this ->postJson ('/api/v1/testimonials ' , [
38+ 'content ' => 'This is a testimonial without a name. ' ,
39+ ], [
40+ 'Authorization ' => 'Bearer ' . $ token ,
41+ ]);
42+
43+ $ response ->assertStatus (201 );
44+ $ response ->assertJson ([
45+ 'status ' => 'success ' ,
46+ 'message ' => 'Testimonial created successfully ' ,
47+ 'data ' => [
48+ 'name ' => 'Anonymous User ' , // Expecting the fallback
49+ 'content ' => 'This is a testimonial without a name. ' ,
50+ 'user_id ' => $ user ->id ,
51+ ],
52+ ]);
53+
54+ // Verify the testimonial exists in the database
55+ $ this ->assertDatabaseHas ('testimonials ' , [
56+ 'user_id ' => $ user ->id ,
57+ 'name ' => 'Anonymous User ' ,
58+ 'content ' => 'This is a testimonial without a name. ' ,
59+ ]);
60+ }
5361
5462 public function testValidationErrorsAreReturnedForMissingData ()
5563 {
0 commit comments