33namespace Tests \Uploads ;
44
55use BookStack \Exceptions \HttpFetchException ;
6- use BookStack \Uploads \HttpFetcher ;
76use BookStack \Uploads \UserAvatars ;
87use BookStack \Users \Models \User ;
8+ use GuzzleHttp \Exception \ConnectException ;
9+ use GuzzleHttp \Psr7 \Request ;
10+ use GuzzleHttp \Psr7 \Response ;
911use Tests \TestCase ;
1012
1113class AvatarTest extends TestCase
@@ -22,34 +24,26 @@ protected function createUserRequest($user): User
2224 return User::query ()->where ('email ' , '= ' , $ user ->email )->first ();
2325 }
2426
25- protected function assertImageFetchFrom (string $ url )
26- {
27- $ http = $ this ->mock (HttpFetcher::class);
28-
29- $ http ->shouldReceive ('fetch ' )
30- ->once ()->with ($ url )
31- ->andReturn ($ this ->files ->pngImageData ());
32- }
33-
34- protected function deleteUserImage (User $ user )
27+ protected function deleteUserImage (User $ user ): void
3528 {
3629 $ this ->files ->deleteAtRelativePath ($ user ->avatar ->path );
3730 }
3831
3932 public function test_gravatar_fetched_on_user_create ()
4033 {
41- config ()->set ([
42- 'services.disable_services ' => false ,
43- ]);
34+ $ requests = $ this ->mockHttpClient ([new Response (200 , ['Content-Type ' => 'image/png ' ], $ this ->files ->pngImageData ())]);
35+ config ()->set (['services.disable_services ' => false ]);
4436 $ user = User::factory ()->make ();
45- $ this ->assertImageFetchFrom ('https://www.gravatar.com/avatar/ ' . md5 (strtolower ($ user ->email )) . '?s=500&d=identicon ' );
4637
4738 $ user = $ this ->createUserRequest ($ user );
4839 $ this ->assertDatabaseHas ('images ' , [
4940 'type ' => 'user ' ,
5041 'created_by ' => $ user ->id ,
5142 ]);
5243 $ this ->deleteUserImage ($ user );
44+
45+ $ expectedUri = 'https://www.gravatar.com/avatar/ ' . md5 (strtolower ($ user ->email )) . '?s=500&d=identicon ' ;
46+ $ this ->assertEquals ($ expectedUri , $ requests ->latestRequest ()->getUri ());
5347 }
5448
5549 public function test_custom_url_used_if_set ()
@@ -61,24 +55,22 @@ public function test_custom_url_used_if_set()
6155
6256 $ user = User::factory ()->make ();
6357 $ url = 'https://example.com/ ' . urlencode (strtolower ($ user ->email )) . '/ ' . md5 (strtolower ($ user ->email )) . '/500 ' ;
64- $ this ->assertImageFetchFrom ( $ url );
58+ $ requests = $ this ->mockHttpClient ([ new Response ( 200 , [ ' Content-Type ' => ' image/png ' ], $ this -> files -> pngImageData ())] );
6559
6660 $ user = $ this ->createUserRequest ($ user );
61+ $ this ->assertEquals ($ url , $ requests ->latestRequest ()->getUri ());
6762 $ this ->deleteUserImage ($ user );
6863 }
6964
7065 public function test_avatar_not_fetched_if_no_custom_url_and_services_disabled ()
7166 {
72- config ()->set ([
73- 'services.disable_services ' => true ,
74- ]);
75-
67+ config ()->set (['services.disable_services ' => true ]);
7668 $ user = User::factory ()->make ();
77-
78- $ http = $ this ->mock (HttpFetcher::class);
79- $ http ->shouldNotReceive ('fetch ' );
69+ $ requests = $ this ->mockHttpClient ([new Response ()]);
8070
8171 $ this ->createUserRequest ($ user );
72+
73+ $ this ->assertEquals (0 , $ requests ->requestCount ());
8274 }
8375
8476 public function test_avatar_not_fetched_if_avatar_url_option_set_to_false ()
@@ -89,21 +81,18 @@ public function test_avatar_not_fetched_if_avatar_url_option_set_to_false()
8981 ]);
9082
9183 $ user = User::factory ()->make ();
92-
93- $ http = $ this ->mock (HttpFetcher::class);
94- $ http ->shouldNotReceive ('fetch ' );
84+ $ requests = $ this ->mockHttpClient ([new Response ()]);
9585
9686 $ this ->createUserRequest ($ user );
87+
88+ $ this ->assertEquals (0 , $ requests ->requestCount ());
9789 }
9890
9991 public function test_no_failure_but_error_logged_on_failed_avatar_fetch ()
10092 {
101- config ()->set ([
102- 'services.disable_services ' => false ,
103- ]);
93+ config ()->set (['services.disable_services ' => false ]);
10494
105- $ http = $ this ->mock (HttpFetcher::class);
106- $ http ->shouldReceive ('fetch ' )->andThrow (new HttpFetchException ());
95+ $ this ->mockHttpClient ([new ConnectException ('Failed to connect ' , new Request ('GET ' , '' ))]);
10796
10897 $ logger = $ this ->withTestLogger ();
10998
@@ -122,17 +111,16 @@ public function test_exception_message_on_failed_fetch()
122111
123112 $ user = User::factory ()->make ();
124113 $ avatar = app ()->make (UserAvatars::class);
125- $ url = 'http_malformed_url/ ' . urlencode (strtolower ($ user ->email )) . '/ ' . md5 (strtolower ($ user ->email )) . '/500 ' ;
126114 $ logger = $ this ->withTestLogger ();
115+ $ this ->mockHttpClient ([new ConnectException ('Could not resolve host http_malformed_url ' , new Request ('GET ' , '' ))]);
127116
128117 $ avatar ->fetchAndAssignToUser ($ user );
129118
119+ $ url = 'http_malformed_url/ ' . urlencode (strtolower ($ user ->email )) . '/ ' . md5 (strtolower ($ user ->email )) . '/500 ' ;
130120 $ this ->assertTrue ($ logger ->hasError ('Failed to save user avatar image ' ));
131121 $ exception = $ logger ->getRecords ()[0 ]['context ' ]['exception ' ];
132- $ this ->assertEquals (new HttpFetchException (
133- 'Cannot get image from ' . $ url ,
134- 6 ,
135- (new HttpFetchException ('Could not resolve host: http_malformed_url ' , 6 ))
136- ), $ exception );
122+ $ this ->assertInstanceOf (HttpFetchException::class, $ exception );
123+ $ this ->assertEquals ('Cannot get image from ' . $ url , $ exception ->getMessage ());
124+ $ this ->assertEquals ('Could not resolve host http_malformed_url ' , $ exception ->getPrevious ()->getMessage ());
137125 }
138126}
0 commit comments