1 # frozen_string_literal: true
5 class BannerHelperTest < ActionView::TestCase
6 def test_banner_without_locales
7 banners = { :global => make_banner("global") }
9 stub_const(:BANNERS, banners) do
10 I18n.with_locale(:en) do
11 result = active_banners
12 assert_includes result.keys, :global
15 I18n.with_locale(:"zh-TW") do
16 result = active_banners
17 assert_includes result.keys, :global
22 def test_banner_with_single_locale
23 banners = { :german => make_banner("german", :locales => %w[de]) }
25 stub_const(:BANNERS, banners) do
26 I18n.with_locale(:de) do
27 assert_includes active_banners.keys, :german
30 I18n.with_locale(:en) do
31 assert_empty active_banners
36 def test_banner_with_multiple_locales
37 banners = { :regional => make_banner("regional", :locales => %w[it zh-TW]) }
39 stub_const(:BANNERS, banners) do
40 I18n.with_locale(:it) do
41 assert_includes active_banners.keys, :regional
44 I18n.with_locale(:"zh-TW") do
45 assert_includes active_banners.keys, :regional
48 I18n.with_locale(:fr) do
49 assert_empty active_banners
54 def test_multiple_banners_single_locale
56 :german => make_banner("german_only", :locales => %w[de]),
57 :chinese => make_banner("chinese_only", :locales => %w[zh-TW])
60 stub_const(:BANNERS, banners) do
61 I18n.with_locale(:it) do
62 assert_empty active_banners
65 I18n.with_locale(:de) do
66 assert_includes active_banners.keys, :german
67 assert_not_includes active_banners.keys, :chinese
70 I18n.with_locale(:"zh-TW") do
71 assert_includes active_banners.keys, :chinese
72 assert_not_includes active_banners.keys, :german
77 def test_mixed_banner_locales
79 :global => make_banner("global"),
80 :italian => make_banner("italian_only", :locales => %w[it])
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
90 I18n.with_locale(:en) do
91 result = active_banners
92 assert_includes result.keys, :global
93 assert_not_includes result.keys, :italian
98 def test_banner_with_single_country
99 banners = { :germany => make_banner("germany", :countries => %w[DE]) }
101 stub_const(:BANNERS, banners) do
102 # User country unknown
103 params.delete(:country)
104 assert_empty active_banners
106 # User country is in banner list
107 params[:country] = "DE"
108 assert_includes active_banners.keys, :germany
110 # User country is NOT in banner list
111 params[:country] = "US"
112 assert_empty active_banners
116 def test_banner_with_multiple_countries
117 banners = { :itde => make_banner("itde", :countries => %w[IT DE]) }
119 stub_const(:BANNERS, banners) do
120 # User country unknown
121 params.delete(:country)
122 assert_empty active_banners
124 # User country is in banner list
125 params[:country] = "IT"
126 assert_includes active_banners.keys, :itde
128 params[:country] = "DE"
129 assert_includes active_banners.keys, :itde
131 # User country is NOT in banner list
132 params[:country] = "US"
133 assert_empty active_banners
137 def test_multiple_banners_single_country
139 :germany => make_banner("germany_only", :countries => %w[DE]),
140 :italy => make_banner("italy_only", :countries => %w[IT])
143 stub_const(:BANNERS, banners) do
144 # User country unknown
145 params.delete(:country)
146 assert_empty active_banners
148 # User country is NOT in banner list
149 params[:country] = "US"
150 assert_empty active_banners
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
157 params[:country] = "IT"
158 assert_includes active_banners.keys, :italy
159 assert_not_includes active_banners.keys, :germany
163 def test_mixed_banners_country_filtering
165 :global => make_banner("global"),
166 :italy => make_banner("italy_only", :countries => %w[IT])
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
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
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
192 def make_banner(id, locales: nil, countries: nil)
195 :alt => "Test Banner",
196 :link => "https://example.com",
197 :img => "banners/test.png",
198 :enddate => "2099-jan-01",
200 :countries => countries
204 def stub_const(name, value)
205 old = Object.const_get(name)
206 Object.send(:remove_const, name)
207 Object.const_set(name, value)
210 Object.send(:remove_const, name)
211 Object.const_set(name, old)