|
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'); |