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

(-)a/C4/AuthoritiesMarc.pm (-2 / +2 lines)
Lines 26-32 use C4::AuthoritiesMarc::MARC21; Link Here
26
use C4::AuthoritiesMarc::UNIMARC;
26
use C4::AuthoritiesMarc::UNIMARC;
27
use C4::Charset;
27
use C4::Charset;
28
use C4::Log;
28
use C4::Log;
29
use Koha::Authority;
29
use Koha::DataObject::Authority;
30
30
31
use vars qw($VERSION @ISA @EXPORT);
31
use vars qw($VERSION @ISA @EXPORT);
32
32
Lines 849-855 Returns MARC::Record of the authority passed in parameter. Link Here
849
849
850
sub GetAuthority {
850
sub GetAuthority {
851
    my ($authid)=@_;
851
    my ($authid)=@_;
852
    my $authority = Koha::Authority->get_from_authid($authid);
852
    my $authority = Koha::DataObject::Authority->get_from_authid($authid);
853
    return ($authority->record);
853
    return ($authority->record);
854
}
854
}
855
855
(-)a/Koha/Authority.pm (-9 / +8 lines)
Lines 1-4 Link Here
1
package Koha::Authority;
1
package Koha::DataObject::Authority;
2
2
3
# Copyright 2012 C & P Bibliography Services
3
# Copyright 2012 C & P Bibliography Services
4
#
4
#
Lines 19-25 package Koha::Authority; Link Here
19
19
20
=head1 NAME
20
=head1 NAME
21
21
22
Koha::Authority - class to encapsulate authority records in Koha
22
Koha::DataObject::Authority - class to encapsulate authority records in Koha
23
23
24
=head1 SYNOPSIS
24
=head1 SYNOPSIS
25
25
Lines 31-38 Authority data. Link Here
31
31
32
=cut
32
=cut
33
33
34
use strict;
34
use Modern::Perl;
35
use warnings;
36
use C4::Context;
35
use C4::Context;
37
use MARC::Record;
36
use MARC::Record;
38
use MARC::File::XML;
37
use MARC::File::XML;
Lines 44-52 __PACKAGE__->mk_accessors(qw( authid authtype record marcflavour )); Link Here
44
43
45
=head2 new
44
=head2 new
46
45
47
    my $auth = Koha::Authority->new($record);
46
    my $auth = Koha::DataObject::Authority->new($record);
48
47
49
Create a new Koha::Authority object based on the provided record.
48
Create a new Koha::DataObject::Authority object based on the provided record.
50
49
51
=cut
50
=cut
52
sub new {
51
sub new {
Lines 61-69 sub new { Link Here
61
60
62
=head2 get_from_authid
61
=head2 get_from_authid
63
62
64
    my $auth = Koha::Authority->get_from_authid($authid);
63
    my $auth = Koha::DataObject::Authority->get_from_authid($authid);
65
64
66
Create the Koha::Authority object associated with the provided authid.
65
Create the Koha::DataObject::Authority object associated with the provided authid.
67
66
68
=cut
67
=cut
69
sub get_from_authid {
68
sub get_from_authid {
Lines 77-83 sub get_from_authid { Link Here
77
    my ($authtypecode, $marcxml) = $sth->fetchrow;
76
    my ($authtypecode, $marcxml) = $sth->fetchrow;
78
    my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
77
    my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
79
        (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
78
        (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
80
    return undef if ($@);
79
    return if ($@);
81
    $record->encoding('UTF-8');
80
    $record->encoding('UTF-8');
82
81
83
    my $self = $class->SUPER::new( { authid => $authid,
82
    my $self = $class->SUPER::new( { authid => $authid,
(-)a/Koha/Filter/MARC/EmbedSeeFromHeadings.pm (-5 / +4 lines)
Lines 30-39 Filter to embed see from headings into MARC records. Link Here
30
30
31
=cut
31
=cut
32
32
33
use strict;
33
use Modern::Perl;
34
use warnings;
35
use Carp;
34
use Carp;
36
use Koha::Authority;
35
use Koha::DataObject::Authority;
37
36
38
use base qw(Koha::RecordProcessor::Base);
37
use base qw(Koha::RecordProcessor::Base);
39
our $NAME = 'EmbedSeeFromHeadings';
38
our $NAME = 'EmbedSeeFromHeadings';
Lines 54-60 sub filter { Link Here
54
    my $record = shift;
53
    my $record = shift;
55
    my $newrecord;
54
    my $newrecord;
56
55
57
    return undef unless defined $record;
56
    return unless defined $record;
58
57
59
    if (ref $record eq 'ARRAY') {
58
    if (ref $record eq 'ARRAY') {
60
        my @recarray;
59
        my @recarray;
Lines 78-84 sub _processrecord { Link Here
78
77
79
        next unless $authid;
78
        next unless $authid;
80
79
81
        my $authority = Koha::Authority->get_from_authid($authid);
80
        my $authority = Koha::DataObject::Authority->get_from_authid($authid);
82
        next unless $authority;
81
        next unless $authority;
83
        my $auth_marc = $authority->record;
82
        my $auth_marc = $authority->record;
84
        my @seefrom = $auth_marc->field('4..');
83
        my @seefrom = $auth_marc->field('4..');
(-)a/Koha/Filter/MARC/Null.pm (-2 / +1 lines)
Lines 31-38 RecordProcessor. Link Here
31
31
32
=cut
32
=cut
33
33
34
use strict;
34
use Modern::Perl;
35
use warnings;
36
use Carp;
35
use Carp;
37
36
38
use base qw(Koha::RecordProcessor::Base);
37
use base qw(Koha::RecordProcessor::Base);
(-)a/Koha/RecordProcessor.pm (-2 / +1 lines)
Lines 57-64 clone it I<prior> to passing it off to the RecordProcessor. Link Here
57
57
58
=cut
58
=cut
59
59
60
use strict;
60
use Modern::Perl;
61
use warnings;
62
use Module::Load::Conditional qw(can_load);
61
use Module::Load::Conditional qw(can_load);
63
use Module::Pluggable::Object;
62
use Module::Pluggable::Object;
64
63
(-)a/Koha/RecordProcessor/Base.pm (-2 / +1 lines)
Lines 58-65 clone it I<prior> to passing it off to the RecordProcessor. Link Here
58
58
59
=cut
59
=cut
60
60
61
use strict;
61
use Modern::Perl;
62
use warnings;
63
62
64
use base qw(Class::Accessor);
63
use base qw(Class::Accessor);
65
64
(-)a/t/RecordProcessor_EmbedSeeFromHeadings.t (+121 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2012 C & P Bibliography Services
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 2 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
use DBI;
22
use File::Spec;
23
use MARC::Record;
24
use MARC::File::XML;
25
26
use Test::More tests => 9;
27
use Test::MockModule;
28
29
use C4::Context;
30
31
BEGIN {
32
    use_ok('Koha::RecordProcessor');
33
}
34
35
# Mock the call to create a database handler
36
my $module = new Test::MockModule('C4::Context');
37
$module->mock('_new_dbh', sub {
38
    my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
39
    || die "Cannot create handle: $DBI::errstr\n";
40
    return $dbh
41
});
42
$module->mock('preference', sub {
43
    return 'MARC21' if (pop @_ eq 'marcflavour');
44
    return 0;
45
});
46
47
48
# Mock data
49
my $auth = MARC::Record->new;
50
$auth->add_fields(
51
    [ '001', '1234' ],
52
    [ '150', ' ', ' ', a => 'Cooking' ],
53
    [ '450', ' ', ' ', a => 'Cookery' ],
54
);
55
56
my $mockauthority = [
57
    ['authtypecode','marcxml'],
58
    ['TOPIC', $auth->as_xml_record() ],
59
    ];
60
61
my $dbh = C4::Context->dbh(); # since we mocked _new_dbh we get a mock handle
62
63
my $bib = MARC::Record->new;
64
$bib->add_fields(
65
    [ '245', '0', '4', a => 'The Ifrane cookbook' ],
66
    [ '650', ' ', ' ', a => 'Cooking', 9 => '1234' ]
67
    );
68
69
my $resultbib = MARC::Record->new;
70
$resultbib->add_fields(
71
    [ '245', '0', '4', a => 'The Ifrane cookbook' ],
72
    [ '650', ' ', ' ', a => 'Cooking', 9 => '1234' ],
73
    [ '650', 'z', ' ', a => 'Cookery' ]
74
    );
75
76
my $processor = Koha::RecordProcessor->new( { filters => ( 'EmbedSeeFromHeadings' ) } );
77
is(ref($processor), 'Koha::RecordProcessor', 'Created record processor');
78
is(ref($processor->filters->[0]), 'Koha::Filter::MARC::EmbedSeeFromHeadings', 'Loaded EmbedSeeFromHeadings filter');
79
80
$dbh->{mock_add_resultset} = $mockauthority;
81
82
my $result = $processor->process(undef);
83
84
is($result, undef, "Don't flip out when asked to process nothing");
85
86
$result = $processor->process($bib);
87
88
my $history = $dbh->{mock_all_history};
89
is(scalar(@{$history}), 1, 'Correct number of statements executed') ;
90
$dbh->{mock_clear_history} = 1;
91
92
is_deeply($result, $resultbib, 'Inserted see-from heading to record');
93
94
my @bibs;
95
$bib = MARC::Record->new;
96
$bib->add_fields(
97
    [ '245', '0', '4', a => 'The Ifrane cookbook' ],
98
    [ '650', ' ', ' ', a => 'Cooking', 9 => '1234' ]
99
    );
100
push @bibs, $bib;
101
$bib = MARC::Record->new;
102
$bib->add_fields(
103
    [ '245', '0', '2', a => 'A story of great tragedy, and fish' ],
104
    [ '650', ' ', ' ', a => 'Fish', 9 => '5432' ]
105
    );
106
push @bibs, $bib;
107
my @resultbibs;
108
push @resultbibs, $resultbib;
109
push @resultbibs, $bib;
110
111
112
$dbh->{mock_add_resultset} = $mockauthority;
113
114
$result = $processor->process(\@bibs);
115
116
is(scalar(@$result), 2, 'Processed two records');
117
118
$history = $dbh->{mock_all_history};
119
is(scalar(@{$history}), 2, 'Correct number of statements executed');
120
121
is_deeply($result, \@resultbibs, 'Handled both records correctly');
(-)a/t/db_dependent/Koha_Authority.t (-8 / +7 lines)
Lines 17-30 Link Here
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
19
20
use strict;
20
use Modern::Perl;
21
use warnings;
22
21
23
use C4::Context;
22
use C4::Context;
24
use Test::More;
23
use Test::More;
25
24
26
BEGIN {
25
BEGIN {
27
        use_ok('Koha::Authority');
26
        use_ok('Koha::DataObject::Authority');
28
}
27
}
29
28
30
my $record = MARC::Record->new;
29
my $record = MARC::Record->new;
Lines 34-42 $record->add_fields( Link Here
34
        [ '150', ' ', ' ', a => 'Cooking' ],
33
        [ '150', ' ', ' ', a => 'Cooking' ],
35
        [ '450', ' ', ' ', a => 'Cookery' ],
34
        [ '450', ' ', ' ', a => 'Cookery' ],
36
        );
35
        );
37
my $authority = Koha::Authority->new($record);
36
my $authority = Koha::DataObject::Authority->new($record);
38
37
39
is(ref($authority), 'Koha::Authority', 'Created valid Koha::Authority object');
38
is(ref($authority), 'Koha::DataObject::Authority', 'Created valid Koha::DataObject::Authority object');
40
39
41
is_deeply($authority->record, $record, 'Saved record');
40
is_deeply($authority->record, $record, 'Saved record');
42
41
Lines 51-65 SKIP: Link Here
51
        $authid = $row->{'authid'};
50
        $authid = $row->{'authid'};
52
    }
51
    }
53
    skip 'No authorities', 3 unless $authid;
52
    skip 'No authorities', 3 unless $authid;
54
    $authority = Koha::Authority->get_from_authid($authid);
53
    $authority = Koha::DataObject::Authority->get_from_authid($authid);
55
54
56
    is(ref($authority), 'Koha::Authority', 'Retrieved valid Koha::Authority object');
55
    is(ref($authority), 'Koha::DataObject::Authority', 'Retrieved valid Koha::DataObject::Authority object');
57
56
58
    is($authority->authid, $authid, 'Object authid is correct');
57
    is($authority->authid, $authid, 'Object authid is correct');
59
58
60
    is($authority->record->field('001')->data(), $authid, 'Retrieved correct record');
59
    is($authority->record->field('001')->data(), $authid, 'Retrieved correct record');
61
60
62
    $authority = Koha::Authority->get_from_authid('alphabetsoup');
61
    $authority = Koha::DataObject::Authority->get_from_authid('alphabetsoup');
63
    is($authority, undef, 'No invalid record is retrieved');
62
    is($authority, undef, 'No invalid record is retrieved');
64
}
63
}
65
64
(-)a/t/db_dependent/RecordProcessor_EmbedSeeFromHeadings.t (-67 lines)
Lines 1-66 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2012 C & P Bibliography Services
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 2 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use strict;
21
use warnings;
22
use File::Spec;
23
use MARC::Record;
24
use Koha::Authority;
25
26
use Test::More;
27
use Test::MockModule;
28
29
BEGIN {
30
        use_ok('Koha::RecordProcessor');
31
}
32
33
my $module = new Test::MockModule('MARC::Record');
34
$module->mock('new_from_xml', sub {
35
    my $record = MARC::Record->new;
36
37
    $record->add_fields(
38
        [ '001', '1234' ],
39
        [ '150', ' ', ' ', a => 'Cooking' ],
40
        [ '450', ' ', ' ', a => 'Cookery' ],
41
        );
42
43
    return $record;
44
});
45
46
my $bib = MARC::Record->new;
47
$bib->add_fields(
48
    [ '245', '0', '4', a => 'The Ifrane cookbook' ],
49
    [ '650', ' ', ' ', a => 'Cooking', 9 => '1234' ]
50
    );
51
52
my $resultbib = MARC::Record->new;
53
$resultbib->add_fields(
54
    [ '245', '0', '4', a => 'The Ifrane cookbook' ],
55
    [ '650', ' ', ' ', a => 'Cooking', 9 => '1234' ],
56
    [ '650', 'z', ' ', a => 'Cookery' ]
57
    );
58
59
my $processor = Koha::RecordProcessor->new( { filters => ( 'EmbedSeeFromHeadings' ) } );
60
is(ref($processor), 'Koha::RecordProcessor', 'Created record processor');
61
62
my $result = $processor->process($bib);
63
64
is_deeply($result, $resultbib, 'Inserted see-from heading to record');
65
66
done_testing();
67
- 

Return to bug 7417