From 31995cbcd108f494cbbe46e1ef1174409c430d2e Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Mon, 14 Apr 2014 08:03:34 -0400 Subject: [PATCH] Bug 11630: (QA Followup) Move code to subroutine, add unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Patch behaves as expected. Signed-off-by: Marc Véron Signed-off-by: Jonathan Druart Signed-off-by: Galen Charlton --- C4/Circulation.pm | 106 ++++++++++-------- .../Circulation/AgeRestrictionMarkers.t | 17 +++ 2 files changed, 76 insertions(+), 47 deletions(-) create mode 100644 t/db_dependent/Circulation/AgeRestrictionMarkers.t diff --git a/C4/Circulation.pm b/C4/Circulation.pm index cddab0a85c..91a5814de5 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -972,63 +972,37 @@ sub CanBookBeIssued { } } } - # - # CHECK AGE RESTRICTION - # + ## CHECK AGE RESTRICTION # get $marker from preferences. Could be something like "FSK|PEGI|Alter|Age:" - my $markers = C4::Context->preference('AgeRestrictionMarker' ); - my $bibvalues = $biblioitem->{'agerestriction'}; - if (($markers)&&($bibvalues)) - { - # Split $bibvalues to something like FSK 16 or PEGI 6 - my @values = split ' ', uc($bibvalues); - - # Search first occurence of one of the markers - my @markers = split /\|/, uc($markers); - my $index = 0; - my $restrictionyear = 0; - for my $value (@values) { - $index ++; - for my $marker (@markers) { - $marker =~ s/^\s+//; #remove leading spaces - $marker =~ s/\s+$//; #remove trailing spaces - if ($marker eq $value) { - if ($index <= $#values) { - $restrictionyear += $values[$index]; - } - last; - } elsif ($value =~ /^\Q$marker\E(\d+)$/) { - # Perhaps it is something like "K16" (as in Finland) - $restrictionyear += $1; - last; - } + my $markers = C4::Context->preference('AgeRestrictionMarker'); + my $bibvalues = $biblioitem->{'agerestriction'}; + my $restriction_age = 0; + + $restriction_age = GetAgeRestriction( $biblioitem->{'agerestriction'} ); + + if ( $restriction_age > 0 ) { + if ( $borrower->{'dateofbirth'} ) { + my @alloweddate = split /-/, $borrower->{'dateofbirth'}; + $alloweddate[0] += $restriction_age; + + #Prevent runime eror on leap year (invalid date) + if ( ( $alloweddate[1] == 2 ) && ( $alloweddate[2] == 29 ) ) { + $alloweddate[2] = 28; } - last if ($restrictionyear > 0); - } - if ($restrictionyear > 0) { - if ( $borrower->{'dateofbirth'} ) { - my @alloweddate = split /-/,$borrower->{'dateofbirth'} ; - $alloweddate[0] += $restrictionyear; - #Prevent runime eror on leap year (invalid date) - if (($alloweddate[1] == 2) && ($alloweddate[2] == 29)) { - $alloweddate[2] = 28; + if ( Date_to_Days(Today) < Date_to_Days(@alloweddate) - 1 ) { + if ( C4::Context->preference('AgeRestrictionOverride') ) { + $needsconfirmation{AGE_RESTRICTION} = "$bibvalues"; } - - if ( Date_to_Days(Today) < Date_to_Days(@alloweddate) -1 ) { - if (C4::Context->preference('AgeRestrictionOverride' )) { - $needsconfirmation{AGE_RESTRICTION} = "$bibvalues"; - } - else { - $issuingimpossible{AGE_RESTRICTION} = "$bibvalues"; - } + else { + $issuingimpossible{AGE_RESTRICTION} = "$bibvalues"; } } } } -## check for high holds decreasing loan period + ## check for high holds decreasing loan period my $decrease_loan = C4::Context->preference('decreaseLoanHighHolds'); if ( $decrease_loan && $decrease_loan == 1 ) { my ( $reserved, $num, $duration, $returndate ) = @@ -3588,6 +3562,44 @@ sub IsItemIssued { return $sth->fetchrow; } +sub GetAgeRestriction { + my ($record_restrictions) = @_; + my $markers = C4::Context->preference('AgeRestrictionMarker'); + + # Split $record_restrictions to something like FSK 16 or PEGI 6 + my @values = split ' ', uc($record_restrictions); + return unless @values; + + # Search first occurence of one of the markers + my @markers = split /\|/, uc($markers); + return unless @markers; + + my $index = 0; + my $restriction_year = 0; + for my $value (@values) { + $index++; + for my $marker (@markers) { + $marker =~ s/^\s+//; #remove leading spaces + $marker =~ s/\s+$//; #remove trailing spaces + if ( $marker eq $value ) { + if ( $index <= $#values ) { + $restriction_year += $values[$index]; + } + last; + } + elsif ( $value =~ /^\Q$marker\E(\d+)$/ ) { + + # Perhaps it is something like "K16" (as in Finland) + $restriction_year += $1; + last; + } + } + last if ( $restriction_year > 0 ); + } + + return $restriction_year; +} + 1; __END__ diff --git a/t/db_dependent/Circulation/AgeRestrictionMarkers.t b/t/db_dependent/Circulation/AgeRestrictionMarkers.t new file mode 100644 index 0000000000..c06776a1dd --- /dev/null +++ b/t/db_dependent/Circulation/AgeRestrictionMarkers.t @@ -0,0 +1,17 @@ +use Modern::Perl; +use Test::More tests => 4; + +use C4::Context; +use C4::Circulation; + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +C4::Context->set_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' ); + +is ( C4::Circulation::GetAgeRestriction('FSK 16'), '16', 'FSK 16 returns 16' ); +is ( C4::Circulation::GetAgeRestriction('PEGI 16'), '16', 'PEGI 16 returns 16' ); +is ( C4::Circulation::GetAgeRestriction('Age 16'), '16', 'Age 16 returns 16' ); +is ( C4::Circulation::GetAgeRestriction('K16'), '16', 'K16 returns 16' ); + -- 2.39.5