Bug 12583: DelItem prototype - Remove $dbh
[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 C4::Branch;
24 use Koha::Database;
25
26 use Test::More tests => 5;
27
28 BEGIN {
29     use_ok('C4::Items');
30 }
31
32 my $dbh = C4::Context->dbh;
33
34 subtest 'General Add, Get and Del tests' => sub {
35
36     plan tests => 6;
37
38     # Start transaction
39     $dbh->{AutoCommit} = 0;
40     $dbh->{RaiseError} = 1;
41
42     # Create a 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($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 'GetItemsInfo tests' => sub {
147
148     plan tests => 3;
149
150     # Start transaction
151     $dbh->{AutoCommit} = 0;
152     $dbh->{RaiseError} = 1;
153
154     my $homebranch    = 'CPL';
155     my $holdingbranch = 'MPL';
156
157     # Add a biblio
158     my $biblionumber = get_biblio();
159     # Add an item
160     my ($item_bibnum, $item_bibitemnum, $itemnumber)
161         = AddItem({
162                 homebranch    => $homebranch,
163                 holdingbranch => $holdingbranch
164             }, $biblionumber );
165
166     my $branch = GetBranchDetail( $homebranch );
167     $branch->{ opac_info } = "homebranch OPAC info";
168     ModBranch($branch);
169
170     $branch = GetBranchDetail( $holdingbranch );
171     $branch->{ opac_info } = "holdingbranch OPAC info";
172     ModBranch($branch);
173
174     my @results = GetItemsInfo( $biblionumber );
175     ok( @results, 'GetItemsInfo returns results');
176     is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
177         'GetItemsInfo returns the correct home branch OPAC info notice' );
178     is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
179         'GetItemsInfo returns the correct holding branch OPAC info notice' );
180
181     $dbh->rollback;
182 };
183
184 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
185
186     plan tests => 2;
187
188     # Start transaction
189     $dbh->{AutoCommit} = 0;
190     $dbh->{RaiseError} = 1;
191
192     my $schema = Koha::Database->new()->schema();
193
194     my $biblio =
195     $schema->resultset('Biblio')->create(
196         {
197             title       => "Test title",
198             biblioitems => [
199                 {
200                     itemtype => 'BIB_LEVEL',
201                     items    => [ { itype => "ITEM_LEVEL" } ]
202                 }
203             ]
204         }
205     );
206
207     my $biblioitem = $biblio->biblioitem();
208     my ( $item ) = $biblioitem->items();
209
210     C4::Context->set_preference( 'item-level_itypes', 0 );
211     ok( $item->effective_itemtype() eq 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
212
213     C4::Context->set_preference( 'item-level_itypes', 1 );
214     ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is disabled' );
215
216
217     $dbh->rollback;
218 };
219
220 # Helper method to set up a Biblio.
221 sub get_biblio {
222     my $bib = MARC::Record->new();
223     $bib->append_fields(
224         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
225         MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
226     );
227     my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
228     return ($bibnum, $bibitemnum);
229 }