Bug 11518: Add new method to K::S::R::Item that will always return the correct itemtype
[koha.git] / t / db_dependent / Items.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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 #
18
19 use Modern::Perl;
20
21 use MARC::Record;
22 use C4::Biblio;
23 use Koha::Database;
24
25 use Test::More tests => 3;
26
27 BEGIN {
28     use_ok('C4::Items');
29 }
30
31 my $dbh = C4::Context->dbh;
32
33 subtest 'General Add, Get and Del tests' => sub {
34
35     plan tests => 6;
36
37     # Start transaction
38     $dbh->{AutoCommit} = 0;
39     $dbh->{RaiseError} = 1;
40
41     # Helper biblio.
42     diag("Creating biblio instance for testing.");
43     my ($bibnum, $bibitemnum) = get_biblio();
44
45     # Add an item.
46     my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
47     cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
48     cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
49
50     # Get item.
51     my $getitem = GetItem($itemnumber);
52     cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
53     cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
54
55     # Modify item; setting barcode.
56     ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
57     my $moditem = GetItem($itemnumber);
58     cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
59
60     # Delete item.
61     DelItem($dbh, $bibnum, $itemnumber);
62     my $getdeleted = GetItem($itemnumber);
63     is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
64
65     $dbh->rollback;
66 };
67
68 subtest 'GetHiddenItemnumbers tests' => sub {
69
70     plan tests => 9;
71
72     # This sub is controlled by the OpacHiddenItems system preference.
73
74     # Start transaction
75     $dbh->{AutoCommit} = 0;
76     $dbh->{RaiseError} = 1;
77
78     # Create a new biblio
79     my ($biblionumber, $biblioitemnumber) = get_biblio();
80
81     # Add two items
82     my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
83             { homebranch => 'CPL',
84               holdingbranch => 'CPL',
85               withdrawn => 1 },
86             $biblionumber
87     );
88     my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem(
89             { homebranch => 'MPL',
90               holdingbranch => 'MPL',
91               withdrawn => 0 },
92             $biblionumber
93     );
94
95     my $opachiddenitems;
96     my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
97     my @hidden;
98     my @items;
99     push @items, GetItem( $item1_itemnumber );
100     push @items, GetItem( $item2_itemnumber );
101
102     # Empty OpacHiddenItems
103     C4::Context->set_preference('OpacHiddenItems','');
104     ok( !defined( GetHiddenItemnumbers( @items ) ),
105         "Hidden items list undef if OpacHiddenItems empty");
106
107     # Blank spaces
108     C4::Context->set_preference('OpacHiddenItems','  ');
109     ok( scalar GetHiddenItemnumbers( @items ) == 0,
110         "Hidden items list empty if OpacHiddenItems only contains blanks");
111
112     # One variable / value
113     $opachiddenitems = "
114         withdrawn: [1]";
115     C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
116     @hidden = GetHiddenItemnumbers( @items );
117     ok( scalar @hidden == 1, "Only one hidden item");
118     is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
119
120     # One variable, two values
121     $opachiddenitems = "
122         withdrawn: [1,0]";
123     C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
124     @hidden = GetHiddenItemnumbers( @items );
125     ok( scalar @hidden == 2, "Two items hidden");
126     is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
127
128     # Two variables, a value each
129     $opachiddenitems = "
130         withdrawn: [1]
131         homebranch: [MPL]
132     ";
133     C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
134     @hidden = GetHiddenItemnumbers( @items );
135     ok( scalar @hidden == 2, "Two items hidden");
136     is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch=MPL hidden");
137
138     # Valid OpacHiddenItems, empty list
139     @items = ();
140     @hidden = GetHiddenItemnumbers( @items );
141     ok( scalar @hidden == 0, "Empty items list, no item hidden");
142
143     $dbh->rollback;
144 };
145
146 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
147
148     plan tests => 2;
149
150     # Start transaction
151     $dbh->{AutoCommit} = 0;
152     $dbh->{RaiseError} = 1;
153
154     my $schema = Koha::Database->new()->schema();
155
156     my $biblio =
157     $schema->resultset('Biblio')->create(
158         {
159             title       => "Test title",
160             biblioitems => [
161                 {
162                     itemtype => 'BIB_LEVEL',
163                     items    => [ { itype => "ITEM_LEVEL" } ]
164                 }
165             ]
166         }
167     );
168
169     my $biblioitem = $biblio->biblioitem();
170     my ( $item ) = $biblioitem->items();
171
172     C4::Context->set_preference( 'item-level_itypes', 0 );
173     ok( $item->itemtype() eq 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
174
175     C4::Context->set_preference( 'item-level_itypes', 1 );
176     ok( $item->itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is disabled' );
177
178
179     $dbh->rollback;
180 };
181
182 # Helper method to set up a Biblio.
183 sub get_biblio {
184     my $bib = MARC::Record->new();
185     $bib->append_fields(
186         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
187         MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
188     );
189     my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
190     return ($bibnum, $bibitemnum);
191 }