diff options
author | Matthew Somerville <matthew@mysociety.org> | 2020-06-15 19:16:39 +0100 |
---|---|---|
committer | M Somerville <matthew-github@dracos.co.uk> | 2020-09-02 14:52:54 +0100 |
commit | 764a6b7f8056d55aeba4656cba279b27a60d6b1a (patch) | |
tree | c0363cb8a6fa49e4e7302a926df155ff6501089a | |
parent | 36f64da7723794e5b3faf674821fcfe4d422ca8b (diff) |
[Bexley] Factor out BH code, fix offline test.
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Bexley.pm | 37 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/UK.pm | 43 | ||||
-rw-r--r-- | t/cobrand/bexley.t | 10 |
3 files changed, 49 insertions, 41 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Bexley.pm b/perllib/FixMyStreet/Cobrand/Bexley.pm index e29856f31..3f52c42b0 100644 --- a/perllib/FixMyStreet/Cobrand/Bexley.pm +++ b/perllib/FixMyStreet/Cobrand/Bexley.pm @@ -3,10 +3,6 @@ use parent 'FixMyStreet::Cobrand::Whitelabel'; use strict; use warnings; -use Encode; -use JSON::MaybeXS; -use LWP::Simple qw($ua); -use Path::Tiny; use Time::Piece; sub council_area_id { 2494 } @@ -221,39 +217,8 @@ sub _is_out_of_hours { return 1 if $time->hour > 16 || ($time->hour == 16 && $time->min >= 45); return 1 if $time->hour < 8; return 1 if $time->wday == 1 || $time->wday == 7; - return 1 if _is_bank_holiday(); + return 1 if FixMyStreet::Cobrand::UK::is_public_holiday(); return 0; } -sub _is_bank_holiday { - my $json = _get_bank_holiday_json(); - my $today = localtime->date; - for my $event (@{$json->{'england-and-wales'}{events}}) { - if ($event->{date} eq $today) { - return 1; - } - } -} - -sub _get_bank_holiday_json { - my $file = 'bank-holidays.json'; - my $cache_file = path(FixMyStreet->path_to("../data/$file")); - my $js; - if (-s $cache_file && -M $cache_file <= 7 && !FixMyStreet->config('STAGING_SITE')) { - # uncoverable statement - $js = $cache_file->slurp_utf8; - } else { - $ua->timeout(5); - $js = LWP::Simple::get("https://www.gov.uk/$file"); - # uncoverable branch false - $js = decode_utf8($js) if !utf8::is_utf8($js); - if ($js && !FixMyStreet->config('STAGING_SITE')) { - # uncoverable statement - $cache_file->spew_utf8($js); - } - } - $js = JSON->new->decode($js) if $js; - return $js; -} - 1; diff --git a/perllib/FixMyStreet/Cobrand/UK.pm b/perllib/FixMyStreet/Cobrand/UK.pm index 8d464ca7b..3ede055b9 100644 --- a/perllib/FixMyStreet/Cobrand/UK.pm +++ b/perllib/FixMyStreet/Cobrand/UK.pm @@ -2,8 +2,11 @@ package FixMyStreet::Cobrand::UK; use base 'FixMyStreet::Cobrand::Default'; use strict; +use Encode; use JSON::MaybeXS; use LWP::UserAgent; +use Path::Tiny; +use Time::Piece; use mySociety::MaPit; use mySociety::VotingArea; use Utils; @@ -449,4 +452,44 @@ sub check_recaptcha { unless $res->{success}; } +sub is_public_holiday { + my %args = @_; + $args{date} ||= localtime; + $args{date} = $args{date}->date; + $args{nation} ||= 'england-and-wales'; + my $json = _get_bank_holiday_json(); + for my $event (@{$json->{$args{nation}}{events}}) { + if ($event->{date} eq $args{date}) { + return 1; + } + } +} + +sub _get_bank_holiday_json { + my $file = 'bank-holidays.json'; + my $cache_file = path(FixMyStreet->path_to("../data/$file")); + my $js; + if (-s $cache_file && -M $cache_file <= 7 && !FixMyStreet->config('STAGING_SITE')) { + # uncoverable statement + $js = $cache_file->slurp_utf8; + } else { + $js = _fetch_url("https://www.gov.uk/$file"); + # uncoverable branch false + $js = decode_utf8($js) if !utf8::is_utf8($js); + if ($js && !FixMyStreet->config('STAGING_SITE')) { + # uncoverable statement + $cache_file->spew_utf8($js); + } + } + $js = JSON->new->decode($js) if $js; + return $js; +} + +sub _fetch_url { + my $url = shift; + my $ua = LWP::UserAgent->new; + $ua->timeout(5); + $ua->get($url)->content; +} + 1; diff --git a/t/cobrand/bexley.t b/t/cobrand/bexley.t index 91e30ff50..8ce6c45f2 100644 --- a/t/cobrand/bexley.t +++ b/t/cobrand/bexley.t @@ -256,9 +256,10 @@ subtest 'geocoder' => sub { ] }; }; -my $bex = Test::MockModule->new('FixMyStreet::Cobrand::Bexley'); -$bex->mock('get', sub { - return <<EOF +subtest 'out of hours' => sub { + my $lwp = Test::MockModule->new('LWP::UserAgent'); + $lwp->mock('get', sub { + HTTP::Response->new(200, 'OK', [], <<EOF); { "england-and-wales": { "events": [ @@ -267,9 +268,8 @@ $bex->mock('get', sub { } } EOF -}); + }); -subtest 'out of hours' => sub { my $cobrand = FixMyStreet::Cobrand::Bexley->new; set_fixed_time('2019-10-16T12:00:00Z'); is $cobrand->_is_out_of_hours(), 0, 'not out of hours in the day'; |