From 5ce14cfa4d7792346e96a3895df73e3510c936ea Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Wed, 9 Jun 2021 14:17:56 +0100 Subject: [PATCH 1/2] Ensure that short duration blockss are shown as '0 hours' Passing `:count => nil` means that i18n.t will return the hash of translations, so use a default value of 0 when checking for hours to prevent this. Refs #3210 --- app/helpers/user_blocks_helper.rb | 2 +- test/helpers/user_blocks_helper_test.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/helpers/user_blocks_helper.rb b/app/helpers/user_blocks_helper.rb index 73425edec..0528140f4 100644 --- a/app/helpers/user_blocks_helper.rb +++ b/app/helpers/user_blocks_helper.rb @@ -28,7 +28,7 @@ module UserBlocksHelper def block_duration_in_words(duration) parts = ActiveSupport::Duration.build(duration).parts if duration < 1.day - t("user_blocks.helper.block_duration.hours", :count => parts[:hours]) + t("user_blocks.helper.block_duration.hours", :count => parts.fetch(:hours, 0)) elsif duration < 1.week t("user_blocks.helper.block_duration.days", :count => parts[:days]) elsif duration < 1.month diff --git a/test/helpers/user_blocks_helper_test.rb b/test/helpers/user_blocks_helper_test.rb index 16f83ec89..7d0b664fe 100644 --- a/test/helpers/user_blocks_helper_test.rb +++ b/test/helpers/user_blocks_helper_test.rb @@ -13,4 +13,19 @@ class UserBlocksHelperTest < ActionView::TestCase block = create(:user_block, :ends_at => Time.now.getutc + 1.hour) assert_match %r{^Ends in about 1 hour\.$}, block_status(block) end + + def test_block_duration_in_words + words = block_duration_in_words(364.days) + assert_equal "11 months", words + + words = block_duration_in_words(24.hours) + assert_equal "1 day", words + + # Ensure that nil hours is not passed to i18n.t + words = block_duration_in_words(10.minutes) + assert_equal "0 hours", words + + words = block_duration_in_words(0) + assert_equal "0 hours", words + end end From 71be43588658a84dbf4d424a4e0084f38eaab9bf Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Wed, 9 Jun 2021 15:14:14 +0100 Subject: [PATCH 2/2] Fix case when user block durations are slightly negative The output from ActiveSupport::Duration is wildly unhelpful in those cases Fixes #3210 --- app/helpers/user_blocks_helper.rb | 2 ++ test/helpers/user_blocks_helper_test.rb | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/app/helpers/user_blocks_helper.rb b/app/helpers/user_blocks_helper.rb index 0528140f4..95b6cb600 100644 --- a/app/helpers/user_blocks_helper.rb +++ b/app/helpers/user_blocks_helper.rb @@ -26,6 +26,8 @@ module UserBlocksHelper end def block_duration_in_words(duration) + # Ensure the requested duration isn't negative, even by a millisecond + duration = 0 if duration.negative? parts = ActiveSupport::Duration.build(duration).parts if duration < 1.day t("user_blocks.helper.block_duration.hours", :count => parts.fetch(:hours, 0)) diff --git a/test/helpers/user_blocks_helper_test.rb b/test/helpers/user_blocks_helper_test.rb index 7d0b664fe..c4afa6c83 100644 --- a/test/helpers/user_blocks_helper_test.rb +++ b/test/helpers/user_blocks_helper_test.rb @@ -27,5 +27,10 @@ class UserBlocksHelperTest < ActionView::TestCase words = block_duration_in_words(0) assert_equal "0 hours", words + + # Ensure that (slightly) negative durations don't mess everything up + # This can happen on zero hour blocks when ends_at is a millisecond before created_at + words = block_duration_in_words(-0.001) + assert_equal "0 hours", words end end