]> git.openstreetmap.org Git - rails.git/blob - test/helpers/banner_helper_test.rb
Merge pull request #6923 from tyrasd/iD-2.39.3
[rails.git] / test / helpers / banner_helper_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class BannerHelperTest < ActionView::TestCase
6   def test_banner_without_locales
7     banners = { :global => make_banner("global") }
8
9     stub_const(:BANNERS, banners) do
10       I18n.with_locale(:en) do
11         result = active_banners
12         assert_includes result.keys, :global
13       end
14
15       I18n.with_locale(:"zh-TW") do
16         result = active_banners
17         assert_includes result.keys, :global
18       end
19     end
20   end
21
22   def test_banner_with_single_locale
23     banners = { :german => make_banner("german", :locales => %w[de]) }
24
25     stub_const(:BANNERS, banners) do
26       I18n.with_locale(:de) do
27         assert_includes active_banners.keys, :german
28       end
29
30       I18n.with_locale(:en) do
31         assert_empty active_banners
32       end
33     end
34   end
35
36   def test_banner_with_multiple_locales
37     banners = { :regional => make_banner("regional", :locales => %w[it zh-TW]) }
38
39     stub_const(:BANNERS, banners) do
40       I18n.with_locale(:it) do
41         assert_includes active_banners.keys, :regional
42       end
43
44       I18n.with_locale(:"zh-TW") do
45         assert_includes active_banners.keys, :regional
46       end
47
48       I18n.with_locale(:fr) do
49         assert_empty active_banners
50       end
51     end
52   end
53
54   def test_multiple_banners_single_locale
55     banners = {
56       :german => make_banner("german_only", :locales => %w[de]),
57       :chinese => make_banner("chinese_only", :locales => %w[zh-TW])
58     }
59
60     stub_const(:BANNERS, banners) do
61       I18n.with_locale(:it) do
62         assert_empty active_banners
63       end
64
65       I18n.with_locale(:de) do
66         assert_includes active_banners.keys, :german
67         assert_not_includes active_banners.keys, :chinese
68       end
69
70       I18n.with_locale(:"zh-TW") do
71         assert_includes active_banners.keys, :chinese
72         assert_not_includes active_banners.keys, :german
73       end
74     end
75   end
76
77   def test_mixed_banner_locales
78     banners = {
79       :global => make_banner("global"),
80       :italian => make_banner("italian_only", :locales => %w[it])
81     }
82
83     stub_const(:BANNERS, banners) do
84       I18n.with_locale(:it) do
85         result = active_banners
86         assert_includes result.keys, :global
87         assert_includes result.keys, :italian
88       end
89
90       I18n.with_locale(:en) do
91         result = active_banners
92         assert_includes result.keys, :global
93         assert_not_includes result.keys, :italian
94       end
95     end
96   end
97
98   def test_banner_with_single_country
99     banners = { :germany => make_banner("germany", :countries => %w[DE]) }
100
101     stub_const(:BANNERS, banners) do
102       # User country unknown
103       params.delete(:country)
104       assert_empty active_banners
105
106       # User country is in banner list
107       params[:country] = "DE"
108       assert_includes active_banners.keys, :germany
109
110       # User country is NOT in banner list
111       params[:country] = "US"
112       assert_empty active_banners
113     end
114   end
115
116   def test_banner_with_multiple_countries
117     banners = { :itde => make_banner("itde", :countries => %w[IT DE]) }
118
119     stub_const(:BANNERS, banners) do
120       # User country unknown
121       params.delete(:country)
122       assert_empty active_banners
123
124       # User country is in banner list
125       params[:country] = "IT"
126       assert_includes active_banners.keys, :itde
127
128       params[:country] = "DE"
129       assert_includes active_banners.keys, :itde
130
131       # User country is NOT in banner list
132       params[:country] = "US"
133       assert_empty active_banners
134     end
135   end
136
137   def test_multiple_banners_single_country
138     banners = {
139       :germany => make_banner("germany_only", :countries => %w[DE]),
140       :italy => make_banner("italy_only", :countries => %w[IT])
141     }
142
143     stub_const(:BANNERS, banners) do
144       # User country unknown
145       params.delete(:country)
146       assert_empty active_banners
147
148       # User country is NOT in banner list
149       params[:country] = "US"
150       assert_empty active_banners
151
152       # User country is in banner list
153       params[:country] = "DE"
154       assert_includes active_banners.keys, :germany
155       assert_not_includes active_banners.keys, :italy
156
157       params[:country] = "IT"
158       assert_includes active_banners.keys, :italy
159       assert_not_includes active_banners.keys, :germany
160     end
161   end
162
163   def test_mixed_banners_country_filtering
164     banners = {
165       :global => make_banner("global"),
166       :italy => make_banner("italy_only", :countries => %w[IT])
167     }
168
169     stub_const(:BANNERS, banners) do
170       # User country unknown
171       params.delete(:country)
172       result = active_banners
173       assert_includes result.keys, :global
174       assert_not_includes result.keys, :italy
175
176       # User country is in local banner list
177       params[:country] = "IT"
178       result = active_banners
179       assert_includes result.keys, :global
180       assert_includes result.keys, :italy
181
182       # User country is NOT in local banner list
183       params[:country] = "US"
184       result = active_banners
185       assert_includes result.keys, :global
186       assert_not_includes result.keys, :italy
187     end
188   end
189
190   private
191
192   def make_banner(id, locales: nil, countries: nil)
193     {
194       :id => id,
195       :alt => "Test Banner",
196       :link => "https://example.com",
197       :img => "banners/test.png",
198       :enddate => "2099-jan-01",
199       :locales => locales,
200       :countries => countries
201     }.compact
202   end
203
204   def stub_const(name, value)
205     old = Object.const_get(name)
206     Object.send(:remove_const, name)
207     Object.const_set(name, value)
208     yield
209   ensure
210     Object.send(:remove_const, name)
211     Object.const_set(name, old)
212   end
213 end