View | Details | Raw Unified | Return to bug 34828
Collapse All | Expand All

(-)a/Koha/Biblio.pm (-2 / +18 lines)
Lines 33-38 use base qw(Koha::Object); Link Here
33
use Koha::Acquisition::Orders;
33
use Koha::Acquisition::Orders;
34
use Koha::ArticleRequests;
34
use Koha::ArticleRequests;
35
use Koha::Biblio::Metadatas;
35
use Koha::Biblio::Metadatas;
36
use Koha::Biblio::Metadata::Extractor;
36
use Koha::Biblio::ItemGroups;
37
use Koha::Biblio::ItemGroups;
37
use Koha::Biblioitems;
38
use Koha::Biblioitems;
38
use Koha::Cache::Memory::Lite;
39
use Koha::Cache::Memory::Lite;
Lines 1280-1285 sub public_read_list { Link Here
1280
    ];
1281
    ];
1281
}
1282
}
1282
1283
1284
=head3 metadata_extractor
1285
1286
    my $extractor = $biblio->metadata_extractor
1287
1288
Return a Koha::Biblio::Metadata::Extractor object to use to extract data from the metadata (ie. MARC record for now)
1289
1290
=cut
1291
1292
sub metadata_extractor {
1293
    my ($self) = @_;
1294
1295
    $self->{metadata_extractor} ||= Koha::Biblio::Metadata::Extractor->new( { biblio => $self } );
1296
1297
    return $self->{metadata_extractor};
1298
}
1299
1283
=head3 normalized_upc
1300
=head3 normalized_upc
1284
1301
1285
    my $normalized_upc = $biblio->normalized_upc
1302
    my $normalized_upc = $biblio->normalized_upc
Lines 1290-1297 Normalizes and returns the UPC value found in the MARC record. Link Here
1290
1307
1291
sub normalized_upc {
1308
sub normalized_upc {
1292
    my ($self) = @_;
1309
    my ($self) = @_;
1293
    my $marc_record = $self->metadata->record;
1310
    return $self->metadata_extractor->get_normalized_upc;
1294
    return C4::Koha::GetNormalizedUPC($marc_record);
1295
}
1311
}
1296
1312
1297
=head3 normalized_oclc
1313
=head3 normalized_oclc
(-)a/Koha/Biblio/Metadata/Extractor.pm (-56 / +8 lines)
Lines 1-6 Link Here
1
package Koha::Biblio::Metadata::Extractor;
1
package Koha::Biblio::Metadata::Extractor;
2
2
3
# Copyright ByWater Solutions 2023
3
# Copyright Koha Development Team 2023
4
#
4
#
5
# This file is part of Koha.
5
# This file is part of Koha.
6
#
6
#
Lines 26-31 Koha::Biblio::Metadata::Extractor - Extract specific metadata from MARC::Record Link Here
26
use Modern::Perl;
26
use Modern::Perl;
27
27
28
use Koha::Exceptions;
28
use Koha::Exceptions;
29
use Koha::Biblio::Metadata::Extractor::MARC;
29
30
30
=head1 API
31
=head1 API
31
32
Lines 33-106 use Koha::Exceptions; Link Here
33
34
34
=head3 new
35
=head3 new
35
36
36
    my $extractor = Koha::Biblio::Metadata::Extractor->new;
37
    my $extractor = Koha::Biblio::Metadata::Extractor->new({ biblio => $biblio });
37
38
38
Constructor for the I<Koha::Biblio::Metadata::Extractor> class.
39
Constructor for the I<Koha::Biblio::Metadata::Extractor> class.
39
40
40
=cut
41
=cut
41
42
42
sub new {
43
sub new {
43
    my ($class) = @_;
44
    my ( $class, $params ) = @_;
44
    my $self = { extractors => {} };
45
45
46
    return
46
    # We only support MARC for now, no need to complexify here
47
        bless $self,
47
    return Koha::Biblio::Metadata::Extractor::MARC->new($params);
48
        $class;
49
}
50
51
=head2 get_normalized_upc
52
53
    my $normalized_upc = $extractor->get_normalized_upc( { record => $record, schema => $schema } );
54
55
Returns the normalized UPC for the passed I<$record>.
56
57
=cut
58
59
sub get_normalized_upc {
60
    my ( $self, $params ) = @_;
61
62
    Koha::Exceptions::MissingParameter->throw( parameter => 'record' )
63
        unless $params->{record};
64
65
    return $self->get_extractor( { schema => $params->{schema} } )->get_normalized_upc( $params->{record} );
66
}
67
68
=head2 Internal methods
69
70
=head3 get_extractor
71
72
    my $extractor = $self->get_extractor( { schema => $schema } );
73
74
Returns the cached extractor for the specified I<$schema>.
75
76
=cut
77
78
sub get_extractor {
79
    my ( $self, $params ) = @_;
80
81
    my $schema = $params->{schema};
82
83
    Koha::Exceptions::MissingParameter->throw( parameter => 'schema' )
84
        unless $schema;
85
86
    my $valid_schemas = { 'MARC21' => 1, 'UNIMARC' => 1 };
87
88
    Koha::Exceptions::WrongParameter->throw( name => 'schema', value => $schema )
89
        unless $valid_schemas->{$schema};
90
91
    unless ( $self->{extractors}->{$schema} ) {
92
        my $extractor_class = "Koha::Biblio::Metadata::Extractor::MARC::$schema";
93
        eval "require $extractor_class";
94
        $self->{extractors}->{$schema} = $extractor_class->new;
95
    }
96
97
    return $self->{extractors}->{$schema};
98
}
48
}
99
49
100
=head1 AUTHOR
50
=head1 AUTHOR
101
51
102
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
52
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
103
53
54
Jonathan Druart, E<lt>jonathan.druart@bugs.koha-community.orgE<gt>
55
104
=cut
56
=cut
105
57
106
1;
58
1;
(-)a/Koha/Biblio/Metadata/Extractor/MARC.pm (+106 lines)
Line 0 Link Here
1
package Koha::Biblio::Metadata::Extractor::MARC;
2
3
# Copyright Koha Development Team 2023
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
=head1 NAME
21
22
Koha::Biblio::Metadata::Extractor::MARC - Extract specific metadata from MARC::Record objects
23
24
=cut
25
26
use Modern::Perl;
27
28
use Koha::Exceptions;
29
30
use Koha::Biblio::Metadata::Extractor::MARC::MARC21;
31
use Koha::Biblio::Metadata::Extractor::MARC::UNIMARC;
32
33
=head1 API
34
35
=head2 Class methods
36
37
=head3 new
38
39
    my $extractor = Koha::Biblio::Metadata::Extractor::MARC->new({ biblio => $biblio });
40
41
Constructor for the I<Koha::Biblio::Metadata::Extractor::MARC> class.
42
43
=cut
44
45
sub new {
46
    my ( $class, $params ) = @_;
47
48
    Koha::Exceptions::MissingParameter->throw( parameter => 'metadata' )
49
        unless $params->{metadata} || $params->{biblio};
50
51
    #my $metadata = $biblio->metadata;
52
    #my $schema   = $metadata->schema;
53
    # Get the schema from the pref so that we do not fetch the biblio_metadata
54
    my $schema = C4::Context->preference('marcflavour');
55
56
    my $valid_schemas = { 'MARC21' => 1, 'UNIMARC' => 1 };
57
    Koha::Exceptions::WrongParameter->throw( name => 'schema', value => $schema )
58
        unless $valid_schemas->{$schema};
59
60
    my $sub_class = "Koha::Biblio::Metadata::Extractor::MARC::$schema";
61
    return $sub_class->new($params);
62
}
63
64
=head3 metadata
65
66
    my $metadata = $marc_extractor->metadata;
67
68
Return a MARC record.
69
70
=cut
71
72
sub metadata {
73
    my ($self) = @_;
74
    if ( $self->{biblio} ) {
75
        $self->{metadata} ||= $self->{biblio}->metadata->record;
76
    }
77
    return $self->{metadata};
78
}
79
80
=head3 _normalize_string
81
82
    my $normalized_string = $self->_normalize_string($string);
83
84
Returns a normalized string (remove dashes)
85
86
=cut
87
88
sub _normalize_string {
89
    my ( $self, $string ) = @_;
90
    ( my $normalized_string ) = $string =~ /([\d-]*[X]*)/;
91
    $normalized_string =~ s/-//g;
92
93
    return $normalized_string;
94
}
95
96
=head1 AUTHOR
97
98
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
99
100
Jonathan Druart, E<lt>jonathan.druart@bugs.koha-community.orgE<gt>
101
102
=cut
103
104
1;
105
106
__END__
(-)a/Koha/Biblio/Metadata/Extractor/MARC/MARC21.pm (-20 / +14 lines)
Lines 1-6 Link Here
1
package Koha::Biblio::Metadata::Extractor::MARC::MARC21;
1
package Koha::Biblio::Metadata::Extractor::MARC::MARC21;
2
2
3
# Copyright ByWater Solutions 2023
3
# Copyright Koha Development Team 2023
4
#
4
#
5
# This file is part of Koha.
5
# This file is part of Koha.
6
#
6
#
Lines 19-30 package Koha::Biblio::Metadata::Extractor::MARC::MARC21; Link Here
19
19
20
=head1 NAME
20
=head1 NAME
21
21
22
Koha::Biblio::Metadata::Extractor - Extract specific metadata from MARC::Record objects
22
Koha::Biblio::Metadata::Extractor::MARC::MARC21 - Extract specific metadata from MARC21 MARC::Record objects
23
23
24
=cut
24
=cut
25
25
26
use Modern::Perl;
26
use Modern::Perl;
27
27
28
use base qw(Koha::Biblio::Metadata::Extractor::MARC);
29
28
use Koha::Exceptions;
30
use Koha::Exceptions;
29
31
30
=head1 API
32
=head1 API
Lines 35-91 use Koha::Exceptions; Link Here
35
37
36
    my $extractor = Koha::Biblio::Metadata::Extractor::MARC::MARC21->new;
38
    my $extractor = Koha::Biblio::Metadata::Extractor::MARC::MARC21->new;
37
39
38
Constructor for the I<Koha::Biblio::Metadata::Extractor> class.
40
Constructor for the I<Koha::Biblio::Metadata::Extractor::MARC::MARC21> class.
39
41
40
=cut
42
=cut
41
43
42
sub new {
44
sub new {
43
    my ($class) = @_;
45
    my ( $class, $params ) = @_;
44
    my $self = {};
45
46
46
    return
47
    return
47
        bless $self,
48
        bless $params,
48
        $class;
49
        $class;
49
}
50
}
50
51
51
=head2 get_normalized_upc
52
=head2 get_normalized_upc
52
53
53
    my $normalized_upc = $extractor->get_normalized_upc( $record );
54
    my $normalized_upc = $extractor->get_normalized_upc();
54
55
55
Returns a stringthe COinS (a span) which can be included in a biblio record
56
Returns a normalized UPC.
56
57
57
=cut
58
=cut
58
59
59
sub get_normalized_upc {
60
sub get_normalized_upc {
60
    my ( $self, $record ) = @_;
61
    my ($self) = @_;
61
62
    Koha::Exceptions::MissingParameter->throw( parameter => 'record' )
63
        unless $record;
64
65
    Koha::Exceptions::WrongParameter->throw( name => 'record', type => ref($record) )
66
        unless ref($record) eq 'MARC::Record';
67
62
63
    my $record = $self->metadata;
68
    my @fields = $record->field('024');
64
    my @fields = $record->field('024');
69
    foreach my $field (@fields) {
65
    foreach my $field (@fields) {
70
66
71
        my $indicator = $field->indicator(1);
67
        my $indicator = $field->indicator(1);
72
        my $upc       = $field->subfield('a');
73
68
74
        ( my $normalized_upc ) = $upc =~ /([\d-]*[X]*)/;
69
        my $normalized_upc = $self->_normalize_string( $field->subfield('a') );
75
        $normalized_upc =~ s/-//g;
76
70
77
        if ( $normalized_upc && $indicator eq "1" ) {
71
        if ( $normalized_upc && $indicator eq "1" ) {
78
            return $normalized_upc;
72
            return $normalized_upc;
79
        }
73
        }
80
    }
74
    }
81
82
    return;
83
}
75
}
84
76
85
=head1 AUTHOR
77
=head1 AUTHOR
86
78
87
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
79
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
88
80
81
Jonathan Druart, E<lt>jonathan.druart@bugs.koha-community.orgE<gt>
82
89
=cut
83
=cut
90
84
91
1;
85
1;
(-)a/Koha/Biblio/Metadata/Extractor/MARC/UNIMARC.pm (-18 / +13 lines)
Lines 1-6 Link Here
1
package Koha::Biblio::Metadata::Extractor::MARC::UNIMARC;
1
package Koha::Biblio::Metadata::Extractor::MARC::UNIMARC;
2
2
3
# Copyright ByWater Solutions 2023
3
# Copyright Koha Development Team 2023
4
#
4
#
5
# This file is part of Koha.
5
# This file is part of Koha.
6
#
6
#
Lines 19-30 package Koha::Biblio::Metadata::Extractor::MARC::UNIMARC; Link Here
19
19
20
=head1 NAME
20
=head1 NAME
21
21
22
Koha::Biblio::Metadata::Extractor - Extract specific metadata from MARC::Record objects
22
Koha::Biblio::Metadata::Extractor::MARC::UNIMARC - Extract specific metadata from UNIMARC MARC::Record objects
23
23
24
=cut
24
=cut
25
25
26
use Modern::Perl;
26
use Modern::Perl;
27
27
28
use base qw(Koha::Biblio::Metadata::Extractor::MARC);
29
28
use Koha::Exceptions;
30
use Koha::Exceptions;
29
31
30
=head1 API
32
=head1 API
Lines 40-77 Constructor for the I<Koha::Biblio::Metadata::Extractor::MARC::UNIMARC> class. Link Here
40
=cut
42
=cut
41
43
42
sub new {
44
sub new {
43
    my ($class) = @_;
45
    my ( $class, $params ) = @_;
44
    my $self = {};
45
46
46
    return
47
    return
47
        bless $self,
48
        bless $params,
48
        $class;
49
        $class;
49
}
50
}
50
51
51
=head2 get_normalized_upc
52
=head2 get_normalized_upc
52
53
53
    my $normalized_upc = $extractor->get_normalized_upc( $record );
54
    my $normalized_upc = $extractor->get_normalized_upc();
54
55
55
Returns the normalized UPC for the passed I<$record>.
56
Returns a normalized UPC.
56
57
57
=cut
58
=cut
58
59
59
sub get_normalized_upc {
60
sub get_normalized_upc {
60
    my ( $self, $record ) = @_;
61
    my ($self) = @_;
61
62
    Koha::Exceptions::MissingParameter->throw( parameter => 'record' )
63
        unless $record;
64
65
    Koha::Exceptions::WrongParameter->throw( name => 'record', type => ref($record) )
66
        unless ref($record) eq 'MARC::Record';
67
62
63
    my $record = $self->metadata;
68
    my @fields = $record->field('072');
64
    my @fields = $record->field('072');
69
    foreach my $field (@fields) {
65
    foreach my $field (@fields) {
70
66
71
        my $upc = $field->subfield('a');
67
        my $normalized_upc = $self->_normalize_string( $field->subfield('a') );
72
73
        ( my $normalized_upc ) = $upc =~ /([\d-]*[X]*)/;
74
        $normalized_upc =~ s/-//g;
75
68
76
        if ($normalized_upc) {
69
        if ($normalized_upc) {
77
            return $normalized_upc;
70
            return $normalized_upc;
Lines 83-88 sub get_normalized_upc { Link Here
83
76
84
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
77
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
85
78
79
Jonathan Druart, E<lt>jonathan.druart@bugs.koha-community.orgE<gt>
80
86
=cut
81
=cut
87
82
88
1;
83
1;
(-)a/t/Koha/MetadataExtractor.t (-102 lines)
Lines 1-102 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::Biblio::Metadata::Extractor;
29
30
subtest 'new() tests' => sub {
31
32
    plan tests => 1;
33
34
    my $extractor = Koha::Biblio::Metadata::Extractor->new;
35
    is( ref($extractor), 'Koha::Biblio::Metadata::Extractor' );
36
};
37
38
subtest 'get_extractor() tests' => sub {
39
40
    plan tests => 8;
41
42
    my $extractor = Koha::Biblio::Metadata::Extractor->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::Biblio::Metadata::Extractor::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::Biblio::Metadata::Extractor->new;
77
78
    my $record = MARC::Record->new();
79
80
    my $mock_marc21 = Test::MockModule->new('Koha::Biblio::Metadata::Extractor::MARC::MARC21');
81
    $mock_marc21->mock( 'get_normalized_upc', sub { return 'MARC21' } );
82
83
    my $mock_unimarc = Test::MockModule->new('Koha::Biblio::Metadata::Extractor::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
};
(-)a/t/Koha/MetadataExtractor/MARC/MARC21.t (-20 / +27 lines)
Lines 22-64 use Modern::Perl; Link Here
22
use Test::More tests => 2;
22
use Test::More tests => 2;
23
use Test::Exception;
23
use Test::Exception;
24
24
25
use MARC::Record;
25
use t::lib::TestBuilder;
26
use t::lib::Mocks;
26
27
27
use Koha::Biblio::Metadata::Extractor::MARC::MARC21;
28
use Koha::Biblio::Metadata::Extractor;
29
30
my $schema  = Koha::Database->schema;
31
my $builder = t::lib::TestBuilder->new;
32
33
t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
28
34
29
subtest 'new() tests' => sub {
35
subtest 'new() tests' => sub {
30
36
31
    plan tests => 1;
37
    plan tests => 3;
38
39
    $schema->storage->txn_begin;
40
41
    throws_ok { Koha::Biblio::Metadata::Extractor->new; }
42
    'Koha::Exceptions::MissingParameter',
43
        'Exception if no parameter';
44
45
    my $biblio = $builder->build_sample_biblio;
46
47
    my $extractor = Koha::Biblio::Metadata::Extractor->new( { biblio => $biblio } );
48
    is( ref($extractor), 'Koha::Biblio::Metadata::Extractor::MARC::MARC21' );
32
49
33
    my $extractor = Koha::Biblio::Metadata::Extractor::MARC::MARC21->new;
50
    my $record = $biblio->metadata->record;
51
    $extractor = Koha::Biblio::Metadata::Extractor->new( { metadata => $record } );
34
    is( ref($extractor), 'Koha::Biblio::Metadata::Extractor::MARC::MARC21' );
52
    is( ref($extractor), 'Koha::Biblio::Metadata::Extractor::MARC::MARC21' );
35
};
53
};
36
54
37
subtest 'get_normalized_upc() tests' => sub {
55
subtest 'get_normalized_upc() tests' => sub {
38
56
39
    plan tests => 6;
57
    plan tests => 2;
40
41
    my $extractor = Koha::Biblio::Metadata::Extractor::MARC::MARC21->new;
42
58
43
    my $record = MARC::Record->new();
59
    my $record = MARC::Record->new();
44
    $record->append_fields( MARC::Field->new( '024', '1', ' ', a => "9-123345345X" ) );
60
    $record->append_fields( MARC::Field->new( '024', '1', ' ', a => "9-123345345X" ) );
45
61
46
    is( $extractor->get_normalized_upc($record), '9123345345X' );
62
    my $extractor = Koha::Biblio::Metadata::Extractor->new( { metadata => $record } );
63
    is( $extractor->get_normalized_upc, '9123345345X' );
47
64
48
    $record = MARC::Record->new();
65
    $record = MARC::Record->new();
49
    $record->append_fields( MARC::Field->new( '024', ' ', ' ', a => "9-123345345X" ) );
66
    $record->append_fields( MARC::Field->new( '024', ' ', ' ', a => "9-123345345X" ) );
50
67
51
    is( $extractor->get_normalized_upc($record), undef );
68
    $extractor = Koha::Biblio::Metadata::Extractor->new( { metadata => $record } );
52
69
    is( $extractor->get_normalized_upc, "" );
53
    throws_ok { $extractor->get_normalized_upc() }
54
    'Koha::Exceptions::MissingParameter',
55
        'Exception if no parameter';
56
57
    like( "$@", qr{A required parameter is missing' with parameter => record} );
58
59
    throws_ok { $extractor->get_normalized_upc("Some string") }
60
    'Koha::Exceptions::WrongParameter',
61
        'Exception if no parameter';
62
70
63
    like( "$@", qr{Parameter has wrong value or type} );
64
};
71
};
(-)a/t/Koha/MetadataExtractor/MARC/UNIMARC.t (-18 / +30 lines)
Lines 22-64 use Modern::Perl; Link Here
22
use Test::More tests => 2;
22
use Test::More tests => 2;
23
use Test::Exception;
23
use Test::Exception;
24
24
25
use MARC::Record;
25
use t::lib::TestBuilder;
26
use t::lib::Mocks;
26
27
27
use Koha::Biblio::Metadata::Extractor::MARC::UNIMARC;
28
use Koha::Biblio::Metadata::Extractor;
29
30
my $schema  = Koha::Database->schema;
31
my $builder = t::lib::TestBuilder->new;
32
33
t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
28
34
29
subtest 'new() tests' => sub {
35
subtest 'new() tests' => sub {
30
36
31
    plan tests => 1;
37
    plan tests => 3;
38
39
    $schema->storage->txn_begin;
40
41
    throws_ok { Koha::Biblio::Metadata::Extractor->new; }
42
    'Koha::Exceptions::MissingParameter',
43
        'Exception if no parameter';
44
45
    my $biblio = $builder->build_sample_biblio;
46
47
    my $extractor = Koha::Biblio::Metadata::Extractor->new( { biblio => $biblio } );
48
    is( ref($extractor), 'Koha::Biblio::Metadata::Extractor::MARC::UNIMARC' );
32
49
33
    my $extractor = Koha::Biblio::Metadata::Extractor::MARC::UNIMARC->new;
50
    my $record = $biblio->metadata->record;
51
    $extractor = Koha::Biblio::Metadata::Extractor->new( { metadata => $record } );
34
    is( ref($extractor), 'Koha::Biblio::Metadata::Extractor::MARC::UNIMARC' );
52
    is( ref($extractor), 'Koha::Biblio::Metadata::Extractor::MARC::UNIMARC' );
53
54
    $schema->storage->txn_rollback;
55
35
};
56
};
36
57
37
subtest 'get_normalized_upc() tests' => sub {
58
subtest 'get_normalized_upc() tests' => sub {
38
59
39
    plan tests => 6;
60
    plan tests => 2;
40
61
41
    my $extractor = Koha::Biblio::Metadata::Extractor::MARC::UNIMARC->new;
62
    $schema->storage->txn_begin;
42
63
43
    my $record = MARC::Record->new();
64
    my $record = MARC::Record->new();
44
    $record->append_fields( MARC::Field->new( '072', '1', ' ', a => "9-123345345X" ) );
65
    $record->append_fields( MARC::Field->new( '072', '1', ' ', a => "9-123345345X" ) );
45
66
46
    is( $extractor->get_normalized_upc($record), '9123345345X' );
67
    my $extractor = Koha::Biblio::Metadata::Extractor->new( { metadata => $record } );
68
    is( $extractor->get_normalized_upc, '9123345345X' );
47
69
48
    $record = MARC::Record->new();
70
    $record = MARC::Record->new();
49
    $record->append_fields( MARC::Field->new( '072', ' ', ' ', a => "9-123345345X" ) );
71
    $record->append_fields( MARC::Field->new( '072', ' ', ' ', a => "9-123345345X" ) );
50
72
51
    is( $extractor->get_normalized_upc($record), '9123345345X', 'Indicator has no effect' );
73
    is( $extractor->get_normalized_upc($record), '9123345345X', 'Indicator has no effect' );
52
74
53
    throws_ok { $extractor->get_normalized_upc() }
75
    $schema->storage->txn_rollback;
54
    'Koha::Exceptions::MissingParameter',
55
        'Exception if no parameter';
56
57
    like( "$@", qr{A required parameter is missing' with parameter => record} );
58
59
    throws_ok { $extractor->get_normalized_upc("Some string") }
60
    'Koha::Exceptions::WrongParameter',
61
        'Exception if no parameter';
62
76
63
    like( "$@", qr{Parameter has wrong value or type} );
64
};
77
};
65
- 

Return to bug 34828