Bug 34032: (QA follow-up) Tidy code
[koha.git] / t / db_dependent / Biblio / ModBiblioMarc.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 3;
21 use t::lib::Mocks;
22 use t::lib::TestBuilder;
23 use MARC::Record;
24
25 use C4::Biblio qw( ModBiblio ModBiblioMarc );
26 use Koha::Database;
27 use Koha::Biblios;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 $schema->storage->txn_begin;
33
34 subtest "Check MARC field length calculation" => sub {
35     plan tests => 3;
36
37     t::lib::Mocks->mock_preference( 'marcflavour', 'MARC21' );
38
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' ),
43     );
44
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' );
50 };
51
52 subtest "StripWhitespaceChars tests" => sub {
53     plan tests => 4;
54
55     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
56     t::lib::Mocks::mock_preference('StripWhitespaceChars', 0);
57
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" ),
63     );
64
65     my $title = $record->title;
66     is( $title, "  My\ntitle\n", 'Title has whitespace characters' );
67
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" );
73
74     t::lib::Mocks::mock_preference('StripWhitespaceChars', 1);
75
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" );
81
82     my $f003 = $record->field('003')->data;
83     is( $f003, "abcdefg\n", "Whitespace characters are not stripped from control fields" );
84 };
85
86 $schema->storage->txn_rollback;
87
88 subtest "record_source_id parameter tests" => sub {
89
90     plan tests => 5;
91
92     $schema->storage->txn_begin;
93
94     my $biblio = $builder->build_sample_biblio;
95     my $source = $builder->build_object( { class => 'Koha::RecordSources' } );
96
97     my $metadata = $biblio->metadata;
98
99     is( $metadata->record_source_id, undef, 'Record source not set for biblio' );
100
101     C4::Biblio::ModBiblioMarc( $metadata->record, $biblio->id, { record_source_id => undef } );
102     $metadata->discard_changes;
103
104     is( $metadata->record_source_id, undef, 'Record source not set for biblio' );
105
106     C4::Biblio::ModBiblioMarc( $metadata->record, $biblio->id, { record_source_id => $source->id } );
107     $metadata->discard_changes;
108
109     is( $metadata->record_source_id, $source->id, 'Record source set for biblio' );
110
111     C4::Biblio::ModBiblioMarc( $metadata->record, $biblio->id );
112     $metadata->discard_changes;
113
114     is( $metadata->record_source_id, $source->id, 'Record source param not passed, no change' );
115
116     C4::Biblio::ModBiblioMarc( $metadata->record, $biblio->id, { record_source_id => undef } );
117     $metadata->discard_changes;
118
119     is( $metadata->record_source_id, undef, 'Record source passed but undef, unset for biblio' );
120
121     $schema->storage->txn_rollback;
122 };