Bug 14385: (QA follow-up) Additional changes and fixes
[koha.git] / t / db_dependent / Koha / BiblioUtils / Iterator.t
blob9009b007e3a2422f6f2136f91215b1033f250298
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 3;
22 use_ok('Koha::BiblioUtils');
23 use_ok('Koha::BiblioUtils::Iterator');
25 use C4::Items;
26 use C4::Biblio;
27 use DBI;
28 use t::lib::TestBuilder;
29 use t::lib::Mocks;
31 my $schema = Koha::Database->new()->schema();
32 $schema->storage->txn_begin();
34 # Delete issues to prevent foreign constraint failures.
35 # blank all biblios, biblioitems, and items.
36 $schema->resultset('Issue')->delete();
37 $schema->resultset('Biblio')->delete();
39 my $location = 'My Location';
40 my $builder = t::lib::TestBuilder->new;
41 my $library = $builder->build({
42 source => 'Branch',
43 });
44 my $itemtype = $builder->build({
45 source => 'Itemtype',
46 });
48 # Create a biblio instance for testing
49 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
50 my ($bibnum, $bibitemnum) = get_biblio();
52 # Add an item.
53 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $bibnum);
55 my $rs = $schema->resultset('Biblioitem')->search({});
56 my $iterator = Koha::BiblioUtils::Iterator->new($rs, items => 1 );
57 my $record = $iterator->next();
58 my $expected_tags = [ 100, 245, 952, 999 ];
59 my @result_tags = map { $_->tag() } $record->field('...');
60 my @sorted_tags = sort @result_tags;
61 is_deeply(\@sorted_tags,$expected_tags, "Got the same tags as expected");
63 $schema->storage->txn_rollback();
65 # Helper method to set up a Biblio.
66 sub get_biblio {
67 my ( $frameworkcode ) = @_;
68 $frameworkcode //= '';
69 my $bib = MARC::Record->new();
70 $bib->append_fields(
71 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
72 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
74 my ($bibnum, $bibitemnum) = AddBiblio($bib, $frameworkcode);
75 return ($bibnum, $bibitemnum);