From 9baac5074de6ab7701162534560c579ebc61c9a7 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 9 Aug 2019 10:47:17 +0000 Subject: [PATCH] Bug 23324: (QA follow-up) Use existing NormalizeISBN function Signed-off-by: Marcel de Rooy Signed-off-by: Martin Renvoize (cherry picked from commit 5104d1eae67db94bebb05b2e9f1817685649b224) Signed-off-by: Fridolin Somers (cherry picked from commit a924e1e0507812222614e0ed9030b6977babbdf4) Signed-off-by: Lucas Gass --- C4/Koha.pm | 10 ++++++++-- Koha/Util/Normalize.pm | 14 ++++++++------ t/Koha.t | 4 +++- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/C4/Koha.pm b/C4/Koha.pm index a15080998a..2493467fff 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -836,7 +836,9 @@ sub _isbn_cleanup { to be of the specified format. If the string cannot be validated as an isbn, - it returns nothing. + it returns nothing unless return_invalid param is passed. + + #FIXME This routine (and others?) should be moved to Koha::Util::Normalize =cut @@ -846,6 +848,7 @@ sub NormalizeISBN { my $string = $params->{isbn}; my $strip_hyphens = $params->{strip_hyphens}; my $format = $params->{format}; + my $return_invalid = $params->{return_invalid}; return unless $string; @@ -854,7 +857,7 @@ sub NormalizeISBN { if ( $isbn && $isbn->is_valid() ) { if ( $format eq 'ISBN-10' ) { - $isbn = $isbn->as_isbn10(); + $isbn = $isbn->as_isbn10(); } elsif ( $format eq 'ISBN-13' ) { $isbn = $isbn->as_isbn13(); @@ -867,8 +870,11 @@ sub NormalizeISBN { $string = $isbn->as_string(); } + return $string; + } elsif ( $return_invalid ) { return $string; } + } =head2 GetVariationsOfISBN diff --git a/Koha/Util/Normalize.pm b/Koha/Util/Normalize.pm index efe4839d4e..7fe4179506 100644 --- a/Koha/Util/Normalize.pm +++ b/Koha/Util/Normalize.pm @@ -112,12 +112,14 @@ sub ISBN { my ( $string ) = @_; return if !defined( $string ); - my $isbn = Business::ISBN->new($string); - if (defined $isbn && $isbn->is_valid) { - $string = $isbn->as_isbn13->as_string([]); - } - - return $string; + my $isbn = C4::Koha::NormalizeISBN({ + isbn => $string, + format => 'ISBN-13', + strip_hyphens => 1, + return_invalid => 1, + }); + + return $isbn; } 1; diff --git a/t/Koha.t b/t/Koha.t index b8d4346757..9f794d4a8c 100755 --- a/t/Koha.t +++ b/t/Koha.t @@ -25,7 +25,7 @@ use Module::Load::Conditional qw/check_install/; BEGIN { if ( check_install( module => 'Test::DBIx::Class' ) ) { - plan tests => 38; + plan tests => 39; } else { plan skip_all => "Need Test::DBIx::Class" } @@ -81,6 +81,8 @@ eval { $isbn = C4::Koha::NormalizeISBN({ isbn => '0788893777 (2 DVD 45th ed)', format => 'ISBN-10', strip_hyphens => 1 }); }; ok($@ eq '', 'NormalizeISBN does not throw exception when parsing invalid ISBN (bug 12243)'); +$isbn = C4::Koha::NormalizeISBN({ isbn => '0788893777 (2 DVD 45th ed)', format => 'ISBN-10', strip_hyphens => 1, return_invalid =>1 }); +is($isbn, '0788893777 (2 DVD 45th ed)', 'NormalizeISBN returns original string when converting to ISBN10 an ISBN starting with 979 (bug 13167)'); eval { $isbn = C4::Koha::NormalizeISBN({ isbn => '979-10-90085-00-8', format => 'ISBN-10', strip_hyphens => 1 }); -- 2.39.5