|
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Copyright 2023 Koha Development team |
| 4 |
# |
| 5 |
# This file is part of Koha |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 3; |
| 23 |
use Test::Exception; |
| 24 |
use Test::MockModule; |
| 25 |
|
| 26 |
use MARC::Record; |
| 27 |
|
| 28 |
use Koha::MetadataExtractor; |
| 29 |
|
| 30 |
subtest 'new() tests' => sub { |
| 31 |
|
| 32 |
plan tests => 1; |
| 33 |
|
| 34 |
my $extractor = Koha::MetadataExtractor->new; |
| 35 |
is( ref($extractor), 'Koha::MetadataExtractor' ); |
| 36 |
}; |
| 37 |
|
| 38 |
subtest 'get_extractor() tests' => sub { |
| 39 |
|
| 40 |
plan tests => 8; |
| 41 |
|
| 42 |
my $extractor = Koha::MetadataExtractor->new; |
| 43 |
|
| 44 |
foreach my $schema (qw{ MARC21 UNIMARC }) { |
| 45 |
my $specific_extractor = $extractor->get_extractor( { schema => $schema } ); |
| 46 |
is( |
| 47 |
ref($specific_extractor), "Koha::MetadataExtractor::MARC::$schema", |
| 48 |
"Returns the right extractor library for schema ($schema)" |
| 49 |
); |
| 50 |
ok( exists $extractor->{extractors}->{$schema}, "Extractor for $schema cached" ); |
| 51 |
} |
| 52 |
|
| 53 |
throws_ok { $extractor->get_extractor } |
| 54 |
'Koha::Exceptions::MissingParameter', |
| 55 |
'Exception if no schema parameter'; |
| 56 |
|
| 57 |
like( |
| 58 |
"$@", qr{A required parameter is missing' with parameter => schema}, |
| 59 |
'Exception correctly details missing parameter' |
| 60 |
); |
| 61 |
|
| 62 |
throws_ok { $extractor->get_extractor( { schema => 'POTATO' } ) } |
| 63 |
'Koha::Exceptions::WrongParameter', |
| 64 |
'Exception if the passed schema is not supported'; |
| 65 |
|
| 66 |
like( |
| 67 |
"$@", qr{'Parameter has wrong value or type' with name => schema, value => POTATO}, |
| 68 |
'Exception correctly details incorrect parameter value' |
| 69 |
); |
| 70 |
}; |
| 71 |
|
| 72 |
subtest 'get_normalized_upc() tests' => sub { |
| 73 |
|
| 74 |
plan tests => 6; |
| 75 |
|
| 76 |
my $extractor = Koha::MetadataExtractor->new; |
| 77 |
|
| 78 |
my $record = MARC::Record->new(); |
| 79 |
|
| 80 |
my $mock_marc21 = Test::MockModule->new('Koha::MetadataExtractor::MARC::MARC21'); |
| 81 |
$mock_marc21->mock( 'get_normalized_upc', sub { return 'MARC21' } ); |
| 82 |
|
| 83 |
my $mock_unimarc = Test::MockModule->new('Koha::MetadataExtractor::MARC::UNIMARC'); |
| 84 |
$mock_unimarc->mock( 'get_normalized_upc', sub { return 'UNIMARC' } ); |
| 85 |
|
| 86 |
foreach my $schema (qw{ MARC21 UNIMARC }) { |
| 87 |
is( |
| 88 |
$extractor->get_normalized_upc( { record => $record, schema => $schema } ), $schema, |
| 89 |
"Library for handling $schema called" |
| 90 |
); |
| 91 |
ok( exists $extractor->{extractors}->{$schema}, "Extractor for $schema cached" ); |
| 92 |
} |
| 93 |
|
| 94 |
throws_ok { $extractor->get_normalized_upc() } |
| 95 |
'Koha::Exceptions::MissingParameter', |
| 96 |
'Exception if no record parameter'; |
| 97 |
|
| 98 |
like( |
| 99 |
"$@", qr{A required parameter is missing' with parameter => record}, |
| 100 |
'Exception correctly details missing parameter' |
| 101 |
); |
| 102 |
}; |