Bug 19040: Refactor GetMarcBiblio parameters
[koha.git] / t / db_dependent / Koha / Filter / EmbedItemsAvailability.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 => 1;
21 use Test::MockModule;
22
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use MARC::Record;
27
28 use C4::Biblio qw/AddBiblio GetMarcBiblio/;
29 use Koha::Database;
30 use Koha::RecordProcessor;
31
32 my $schema  = Koha::Database->schema();
33 my $builder = t::lib::TestBuilder->new();
34
35 subtest 'EmbedItemsAvailability tests' => sub {
36
37     plan tests => 6;
38
39     $schema->storage->txn_begin();
40
41     my $biblio = Test::MockModule->new('C4::Biblio');
42     $biblio->mock( 'GetMarcFromKohaField', sub {
43         my ( $kohafield, $frameworkcode ) = @_;
44         if ( $kohafield eq 'biblio.biblionumber' ) {
45             if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
46                 return ( '001', '@' );
47             }
48             else {
49                 return ( '999', 'c' );
50             }
51         }
52         else {
53             my $func_ref = $biblio->original( 'GetMarcFromKohaField' );
54             &$func_ref( $kohafield, $frameworkcode );
55         }
56     });
57
58     # MARC21 tests
59     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
60
61     # Create a dummy record
62     my ( $biblionumber, $biblioitemnumber ) = AddBiblio(MARC::Record->new(), '');
63
64     # Add some items with different onloan values
65     $builder->build(
66         {   source => 'Item',
67             value  => {
68                 biblionumber     => $biblionumber,
69                 biblioitemnumber => $biblioitemnumber,
70                 onloan           => '2017-01-01'
71             }
72         }
73     );
74     $builder->build(
75         {   source => 'Item',
76             value  => {
77                 biblionumber     => $biblionumber,
78                 biblioitemnumber => $biblioitemnumber,
79                 onloan           => undef
80             }
81         }
82     );
83     $builder->build(
84         {   source => 'Item',
85             value  => {
86                 biblionumber     => $biblionumber,
87                 biblioitemnumber => $biblioitemnumber,
88                 onloan           => '2017-01-02'
89             }
90         }
91     );
92
93     my $processor = Koha::RecordProcessor->new( { filters => ('EmbedItemsAvailability') } );
94     is( ref($processor), 'Koha::RecordProcessor', 'Created record processor' );
95
96     my $record = GetMarcBiblio({ biblionumber => $biblionumber });
97     ok( !defined $record->field('999')->subfield('x'), q{The record doesn't originally contain 999$x} );
98     # Apply filter
99     $processor->process($record);
100     is($record->field('999')->subfield('x'), 1, 'There is only one item with undef onloan');
101
102     $schema->storage->txn_rollback();
103
104     # UNIMARC tests (999 is not created)
105     t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
106
107     $schema->storage->txn_begin();
108
109     # Create a dummy record
110     ( $biblionumber, $biblioitemnumber ) = AddBiblio(MARC::Record->new(), '');
111
112     # Add some items with different onloan values
113     $builder->build(
114         {   source => 'Item',
115             value  => {
116                 biblionumber     => $biblionumber,
117                 biblioitemnumber => $biblioitemnumber,
118                 onloan           => '2017-01-01'
119             }
120         }
121     );
122     $builder->build(
123         {   source => 'Item',
124             value  => {
125                 biblionumber     => $biblionumber,
126                 biblioitemnumber => $biblioitemnumber,
127                 onloan           => undef
128             }
129         }
130     );
131     $builder->build(
132         {   source => 'Item',
133             value  => {
134                 biblionumber     => $biblionumber,
135                 biblioitemnumber => $biblioitemnumber,
136                 onloan           => '2017-01-02'
137             }
138         }
139     );
140
141     $processor = Koha::RecordProcessor->new( { filters => ('EmbedItemsAvailability') } );
142     is( ref($processor), 'Koha::RecordProcessor', 'Created record processor' );
143
144     $record = GetMarcBiblio({ biblionumber => $biblionumber });
145     ok( !defined $record->subfield('999', 'x'), q{The record doesn't originally contain 999$x} );
146     # Apply filter
147     $processor->process($record);
148     is($record->subfield('999', 'x'), 1, 'There is only one item with undef onloan');
149
150     $schema->storage->txn_rollback();
151 };
152