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 => 3;
24 use t::lib::TestBuilder;
28 use_ok('Koha::Biblio::ItemGroup');
29 use_ok('Koha::Biblio::ItemGroups');
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 t::lib::Mocks::mock_preference('EnableItemGroups', 1);
37 subtest 'add_item() and items() tests' => sub {
41 $schema->storage->txn_begin;
43 my $biblio = $builder->build_sample_biblio();
44 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
45 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
47 my $item_group = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
49 my $items = $item_group->items;
50 is( $items->count, 0, 'Item group has no items');
52 $item_group->add_item({ item_id => $item_1->id });
53 my @items = $item_group->items->as_list();
54 is( scalar(@items), 1, 'Item group has one item');
55 is( $items[0]->id, $item_1->id, 'Item 1 is correct' );
57 $item_group->add_item({ item_id => $item_2->id });
58 @items = $item_group->items->as_list();
59 is( scalar(@items), 2, 'Item group has two items');
60 is( $items[0]->id, $item_1->id, 'Item 1 is correct' );
61 is( $items[1]->id, $item_2->id, 'Item 2 is correct' );
65 @items = $item_group->items->as_list();
66 is( scalar(@items), 1, 'Item group now has only one item');
67 is( $items[0]->id, $item_2->id, 'Item 2 is correct' );
69 $schema->storage->txn_rollback;