]> git.openstreetmap.org Git - rails.git/blob - test/helpers/user_helper_test.rb
Localisation updates from https://translatewiki.net.
[rails.git] / test / helpers / user_helper_test.rb
1 require "test_helper"
2
3 class UserHelperTest < ActionView::TestCase
4   include ERB::Util
5
6   def test_user_image
7     user = create(:user)
8     gravatar_user = create(:user, :image_use_gravatar => true)
9
10     image = user_image(user)
11     assert_match %r{^<img class="user_image border border-grey" .* src="/images/avatar_large.png" />$}, image
12
13     image = user_image(user, :class => "foo")
14     assert_match %r{^<img class="foo" .* src="/images/avatar_large.png" />$}, image
15     image = user_image(gravatar_user)
16     assert_match %r{^<img class="user_image border border-grey" .* src="http://www.gravatar.com/avatar/.*" />$}, image
17
18     image = user_image(gravatar_user, :class => "foo")
19     assert_match %r{^<img class="foo" .* src="http://www.gravatar.com/avatar/.*" />$}, image
20   end
21
22   def test_user_thumbnail
23     user = create(:user)
24     gravatar_user = create(:user, :image_use_gravatar => true)
25
26     image = user_thumbnail(user)
27     assert_match %r{^<img class="user_thumbnail border border-grey" .* src="/images/avatar_small.png" />$}, image
28
29     image = user_thumbnail(user, :class => "foo")
30     assert_match %r{^<img class="foo" .* src="/images/avatar_small.png" />$}, image
31
32     image = user_thumbnail(gravatar_user)
33     assert_match %r{^<img class="user_thumbnail border border-grey" .* src="http://www.gravatar.com/avatar/.*" />$}, image
34
35     image = user_thumbnail(gravatar_user, :class => "foo")
36     assert_match %r{^<img class="foo" .* src="http://www.gravatar.com/avatar/.*" />$}, image
37   end
38
39   def test_user_thumbnail_tiny
40     user = create(:user)
41     gravatar_user = create(:user, :image_use_gravatar => true)
42
43     image = user_thumbnail_tiny(user)
44     assert_match %r{^<img class="user_thumbnail_tiny border border-grey" .* src="/images/avatar_small.png" />$}, image
45
46     image = user_thumbnail_tiny(user, :class => "foo")
47     assert_match %r{^<img class="foo" .* src="/images/avatar_small.png" />$}, image
48
49     image = user_thumbnail_tiny(gravatar_user)
50     assert_match %r{^<img class="user_thumbnail_tiny border border-grey" .* src="http://www.gravatar.com/avatar/.*" />$}, image
51
52     image = user_thumbnail_tiny(gravatar_user, :class => "foo")
53     assert_match %r{^<img class="foo" .* src="http://www.gravatar.com/avatar/.*" />$}, image
54   end
55
56   def test_user_image_url
57     user = create(:user)
58     user.avatar.attach(:io => File.open("test/gpx/fixtures/a.gif"), :filename => "a.gif")
59     gravatar_user = create(:user, :image_use_gravatar => true)
60
61     url = user_image_url(user)
62     assert_match %r{^http://test.host/rails/active_storage/representations/redirect/[^/]+/[^/]+/a.gif$}, url
63
64     url = user_image_url(gravatar_user)
65     assert_match %r{^http://www.gravatar.com/avatar/}, url
66   end
67
68   def test_user_image_sizes_default_image
69     user = create(:user)
70
71     image = user_image(user)
72     assert_match %r{^<img .* width="100" height="100" .* />$}, image
73
74     thumbnail = user_thumbnail(user)
75     assert_match %r{^<img .* width="50" height="50" .* />$}, thumbnail
76   end
77
78   def test_user_image_sizes_avatar
79     user = create(:user)
80     user.avatar.attach(:io => File.open("test/gpx/fixtures/a.gif"), :filename => "a.gif")
81
82     # first time access, no width or height is found
83     image = user_image(user)
84     assert_no_match %r{^<img .* width="100" height="100" .* />$}, image
85
86     thumbnail = user_thumbnail(user)
87     assert_no_match %r{^<img .* width="50" height="50" .* />$}, thumbnail
88
89     # Small hacks to simulate what happens when the images have been fetched at least once before
90     variant = user.avatar.variant(:resize_to_limit => [100, 100])
91     variant.processed.send(:record).image.blob.analyze
92     variant = user.avatar.variant(:resize_to_limit => [50, 50])
93     variant.processed.send(:record).image.blob.analyze
94
95     image = user_image(user)
96     assert_match %r{^<img .* width="100" height="100" .* />$}, image
97
98     thumbnail = user_thumbnail(user)
99     assert_match %r{^<img .* width="50" height="50" .* />$}, thumbnail
100   end
101
102   def test_user_image_sizes_gravatar
103     user = create(:user, :image_use_gravatar => true)
104
105     image = user_image(user)
106     assert_match %r{^<img .* width="100" height="100" .* />$}, image
107
108     thumbnail = user_thumbnail(user)
109     assert_match %r{^<img .* width="50" height="50" .* />$}, thumbnail
110   end
111
112   def test_openid_logo
113     logo = openid_logo
114     assert_match %r{^<img .* class="openid_logo" src="/images/openid_small.png" />$}, logo
115   end
116
117   def test_auth_button
118     button = auth_button("google", "google")
119     assert_equal("<a class=\"auth_button\" title=\"Login with Google\" rel=\"nofollow\" data-method=\"post\" href=\"/auth/google\"><img alt=\"Login with a Google OpenID\" class=\"rounded-lg\" src=\"/images/google.svg\" /></a>", button)
120   end
121
122   private
123
124   def request
125     controller.request
126   end
127 end