3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 3;
22 use t::lib::TestBuilder;
25 use C4::Biblio qw( ModBiblio ModBiblioMarc );
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
32 $schema->storage->txn_begin;
34 subtest "Check MARC field length calculation" => sub {
37 t::lib::Mocks->mock_preference( 'marcflavour', 'MARC21' );
39 my $biblio = t::lib::TestBuilder->new->build_sample_biblio;
40 my $record = MARC::Record->new;
41 $record->append_fields(
42 MARC::Field->new( '100', '', '', a => 'My title' ),
45 is( $record->leader, ' 'x24, 'No leader lengths' );
46 C4::Biblio::ModBiblioMarc( $record, $biblio->biblionumber );
47 my $savedrec = $biblio->metadata->record;
48 like( substr($savedrec->leader,0,5), qr/^\d{5}$/, 'Record length found' );
49 like( substr($savedrec->leader,12,5), qr/^\d{5}$/, 'Base address found' );
52 subtest "StripWhitespaceChars tests" => sub {
55 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
56 t::lib::Mocks::mock_preference('StripWhitespaceChars', 0);
58 my $biblio = t::lib::TestBuilder->new->build_sample_biblio;
59 my $record = MARC::Record->new;
60 $record->append_fields(
61 MARC::Field->new( '003', "abcdefg\n" ),
62 MARC::Field->new( '245', '', '', a => " My\ntitle\n" ),
65 my $title = $record->title;
66 is( $title, " My\ntitle\n", 'Title has whitespace characters' );
68 C4::Biblio::ModBiblioMarc( $record, $biblio->biblionumber );
69 $biblio = Koha::Biblios->find( $biblio->biblionumber );
70 my $savedrec = $biblio->metadata->record;
71 my $savedtitle = $savedrec->title;
72 is( $savedtitle, " My\ntitle\n", "Title still has whitespace characters because StripWhitespaceChars is disabled" );
74 t::lib::Mocks::mock_preference('StripWhitespaceChars', 1);
76 C4::Biblio::ModBiblioMarc( $record, $biblio->biblionumber );
77 $biblio = Koha::Biblios->find( $biblio->biblionumber );
78 my $amendedrec = $biblio->metadata->record;
79 my $amendedtitle = $amendedrec->title;
80 is( $amendedtitle, "My title", "Whitespace characters removed from title because StripWhitespaceChars is enabled" );
82 my $f003 = $record->field('003')->data;
83 is( $f003, "abcdefg\n", "Whitespace characters are not stripped from control fields" );
86 $schema->storage->txn_rollback;
88 subtest "record_source_id parameter tests" => sub {
92 $schema->storage->txn_begin;
94 my $biblio = $builder->build_sample_biblio;
95 my $source = $builder->build_object( { class => 'Koha::RecordSources' } );
97 my $metadata = $biblio->metadata;
99 is( $metadata->record_source_id, undef, 'Record source not set for biblio' );
101 C4::Biblio::ModBiblioMarc( $metadata->record, $biblio->id, { record_source_id => undef } );
102 $metadata->discard_changes;
104 is( $metadata->record_source_id, undef, 'Record source not set for biblio' );
106 C4::Biblio::ModBiblioMarc( $metadata->record, $biblio->id, { record_source_id => $source->id } );
107 $metadata->discard_changes;
109 is( $metadata->record_source_id, $source->id, 'Record source set for biblio' );
111 C4::Biblio::ModBiblioMarc( $metadata->record, $biblio->id );
112 $metadata->discard_changes;
114 is( $metadata->record_source_id, $source->id, 'Record source param not passed, no change' );
116 C4::Biblio::ModBiblioMarc( $metadata->record, $biblio->id, { record_source_id => undef } );
117 $metadata->discard_changes;
119 is( $metadata->record_source_id, undef, 'Record source passed but undef, unset for biblio' );
121 $schema->storage->txn_rollback;