Bug 11630: (QA Followup) Move code to subroutine, add unit tests
authorKyle M Hall <kyle@bywatersolutions.com>
Mon, 14 Apr 2014 12:03:34 +0000 (08:03 -0400)
committerGalen Charlton <gmc@esilibrary.com>
Wed, 16 Apr 2014 14:55:08 +0000 (14:55 +0000)
Patch behaves as expected.

Signed-off-by: Marc VĂ©ron <veron@veron.ch>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
C4/Circulation.pm
t/db_dependent/Circulation/AgeRestrictionMarkers.t [new file with mode: 0644]

index cddab0a..91a5814 100644 (file)
@@ -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 (file)
index 0000000..c06776a
--- /dev/null
@@ -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' );
+