Bug 24857: Add Koha Objects
[koha.git] / t / db_dependent / Koha / Biblio / ItemGroups.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 Koha::Database;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 BEGIN {
28     use_ok('Koha::Biblio::ItemGroup');
29     use_ok('Koha::Biblio::ItemGroups');
30 }
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 t::lib::Mocks::mock_preference('EnableItemGroups', 1);
36
37 subtest 'add_item() and items() tests' => sub {
38
39     plan tests => 8;
40
41     $schema->storage->txn_begin;
42
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 });
46
47     my $item_group = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
48
49     my $items = $item_group->items;
50     is( $items->count, 0, 'Item group has no items');
51
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' );
56
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' );
62
63     # Remove an item
64     $item_1->delete;
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' );
68
69     $schema->storage->txn_rollback;
70 };