Bug 23463: Replace AddItem calls with Koha::Item->store
[koha.git] / t / db_dependent / Koha / BiblioUtils / Iterator.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
22 use_ok('Koha::BiblioUtils');
23 use_ok('Koha::BiblioUtils::Iterator');
24
25 use C4::Items;
26 use C4::Biblio;
27 use DBI;
28 use t::lib::TestBuilder;
29 use t::lib::Mocks;
30
31 my $schema = Koha::Database->new()->schema();
32 $schema->storage->txn_begin();
33
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();
38
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 });
47
48 # Create a biblio instance for testing
49 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
50 my $biblio = $builder->build_sample_biblio();
51
52 # Add an item.
53 $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
54
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, 942, 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");
62
63 $schema->storage->txn_rollback();