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 => 1;
24 use t::lib::TestBuilder;
28 use C4::Biblio qw/AddBiblio GetMarcBiblio/;
30 use Koha::RecordProcessor;
32 my $schema = Koha::Database->schema();
33 my $builder = t::lib::TestBuilder->new();
35 subtest 'EmbedItemsAvailability tests' => sub {
39 $schema->storage->txn_begin();
41 my $biblio = Test::MockModule->new('C4::Biblio');
42 $biblio->mock( 'GetMarcFromKohaField', sub {
43 my ( $kohafield ) = @_;
44 if ( $kohafield eq 'biblio.biblionumber' ) {
45 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
46 return ( '001', '@' );
49 return ( '999', 'c' );
53 my $func_ref = $biblio->original( 'GetMarcFromKohaField' );
54 &$func_ref( $kohafield );
59 t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
61 # Create a dummy record
62 my ( $biblionumber, $biblioitemnumber ) = AddBiblio(MARC::Record->new(), '');
64 # Add some items with different onloan values
65 $builder->build_sample_item(
67 biblionumber => $biblionumber,
68 onloan => '2017-01-01'
71 $builder->build_sample_item(
73 biblionumber => $biblionumber,
77 $builder->build_sample_item(
79 biblionumber => $biblionumber,
80 onloan => '2017-01-02'
84 my $processor = Koha::RecordProcessor->new( { filters => ('EmbedItemsAvailability') } );
85 is( ref($processor), 'Koha::RecordProcessor', 'Created record processor' );
87 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
88 ok( !defined $record->field('999')->subfield('x'), q{The record doesn't originally contain 999$x} );
90 $processor->process($record);
91 is($record->field('999')->subfield('x'), 1, 'There is only one item with undef onloan');
93 $schema->storage->txn_rollback();
95 # UNIMARC tests (999 is not created)
96 t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
98 $schema->storage->txn_begin();
100 # Create a dummy record
101 ( $biblionumber, $biblioitemnumber ) = AddBiblio(MARC::Record->new(), '');
103 # Add some items with different onloan values
104 $builder->build_sample_item(
106 biblionumber => $biblionumber,
107 onloan => '2017-01-01'
110 $builder->build_sample_item(
112 biblionumber => $biblionumber,
116 $builder->build_sample_item(
118 biblionumber => $biblionumber,
119 onloan => '2017-01-02'
123 $processor = Koha::RecordProcessor->new( { filters => ('EmbedItemsAvailability') } );
124 is( ref($processor), 'Koha::RecordProcessor', 'Created record processor' );
126 $record = GetMarcBiblio({ biblionumber => $biblionumber });
127 ok( !defined $record->subfield('999', 'x'), q{The record doesn't originally contain 999$x} );
129 $processor->process($record);
130 is($record->subfield('999', 'x'), 1, 'There is only one item with undef onloan');
132 $schema->storage->txn_rollback();