@@ -7,30 +7,41 @@ class UserPreferencePatchTest < GttTest
77 @pref = User . find_by_login ( 'dlopper' ) . pref
88 end
99
10+ teardown do
11+ Setting . plugin_redmine_gtt = Setting . plugin_redmine_gtt . merge (
12+ 'distance_unit' => 'm'
13+ )
14+ end
15+
1016 test 'preference is off by default' do
1117 assert_not @pref . gtt_watch_nearby?
12- assert_nil @pref . gtt_watch_radius_km
18+ assert_nil @pref . gtt_watch_radius_m
1319 end
1420
1521 test 'accessors persist through the serialized others hash' do
1622 @pref . gtt_watch_nearby = '1'
17- @pref . gtt_watch_radius = '25 '
23+ @pref . gtt_watch_radius = '25000 '
1824 assert @pref . save
1925
2026 pref = User . find_by_login ( 'dlopper' ) . pref
2127 assert pref . gtt_watch_nearby?
22- assert_equal 25 , pref . gtt_watch_radius_km
28+ assert_equal 25_000 , pref . gtt_watch_radius_m
2329 end
2430
25- test 'safe_attributes mass-assignment works for the new keys' do
31+ test 'safe_attributes mass-assignment works for the form keys' do
2632 @pref . safe_attributes = {
27- 'gtt_watch_nearby' => '1' , 'gtt_watch_radius ' => '10'
33+ 'gtt_watch_nearby' => '1' , 'gtt_watch_radius_in_unit ' => '10'
2834 }
2935 assert @pref . save
3036
3137 pref = User . find_by_login ( 'dlopper' ) . pref
3238 assert pref . gtt_watch_nearby?
33- assert_equal 10 , pref . gtt_watch_radius_km
39+ assert_equal 10 , pref . gtt_watch_radius_m
40+ end
41+
42+ test 'the raw radius is not mass-assignable' do
43+ @pref . safe_attributes = { 'gtt_watch_radius' => '123' }
44+ assert_nil @pref . gtt_watch_radius_m
3445 end
3546
3647 test 'gtt_watch_nearby? treats anything but "1" as off' do
@@ -40,16 +51,47 @@ class UserPreferencePatchTest < GttTest
4051 end
4152 end
4253
43- test 'gtt_watch_radius_km rejects blank, non-numeric and non-positive values' do
44- [ nil , '' , 'abc' , '0' , '-5' , '2.5' ] . each do |value |
54+ test 'gtt_watch_radius_m rejects blank, non-numeric and non-positive values' do
55+ [ nil , '' , 'abc' , '0' , '-5' ] . each do |value |
4556 @pref . gtt_watch_radius = value
46- assert_nil @pref . gtt_watch_radius_km , "expected #{ value . inspect } to be nil"
57+ assert_nil @pref . gtt_watch_radius_m , "expected #{ value . inspect } to be nil"
4758 end
4859 end
4960
50- test 'gtt_watch_radius_km caps the radius server-side' do
51- @pref . gtt_watch_radius = '999999'
52- assert_equal RedmineGtt ::Patches ::UserPreferencePatch ::NEARBY_WATCH_MAX_RADIUS_KM ,
53- @pref . gtt_watch_radius_km
61+ test 'gtt_watch_radius_m caps the radius server-side' do
62+ @pref . gtt_watch_radius = '99999999'
63+ assert_equal RedmineGtt ::Patches ::UserPreferencePatch ::NEARBY_WATCH_MAX_RADIUS_M ,
64+ @pref . gtt_watch_radius_m
65+ end
66+
67+ test 'the form attribute converts through the configured display unit' do
68+ Setting . plugin_redmine_gtt = Setting . plugin_redmine_gtt . merge (
69+ 'distance_unit' => 'km'
70+ )
71+ @pref . gtt_watch_radius_in_unit = '25'
72+ assert_equal 25_000 , @pref . gtt_watch_radius_m
73+ assert_equal 25 , @pref . gtt_watch_radius_in_unit
74+
75+ @pref . gtt_watch_radius_in_unit = '0.5'
76+ assert_equal 500 , @pref . gtt_watch_radius_m
77+ assert_equal 0.5 , @pref . gtt_watch_radius_in_unit
78+ end
79+
80+ test 'a read-save round trip does not drift the stored meters' do
81+ Setting . plugin_redmine_gtt = Setting . plugin_redmine_gtt . merge (
82+ 'distance_unit' => 'mi'
83+ )
84+ @pref . gtt_watch_radius = '500' # meters, not a round mile value
85+ displayed = @pref . gtt_watch_radius_in_unit
86+ @pref . gtt_watch_radius_in_unit = displayed . to_s
87+ assert_equal 500 , @pref . gtt_watch_radius_m
88+ end
89+
90+ test 'the form attribute keeps garbage rejected and blank clearing' do
91+ @pref . gtt_watch_radius_in_unit = 'abc'
92+ assert_nil @pref . gtt_watch_radius_m
93+
94+ @pref . gtt_watch_radius_in_unit = ' '
95+ assert_nil @pref . gtt_watch_radius
5496 end
5597end
0 commit comments