Bug 12428: (QA followup) regression tests.
[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 use Data::Printer;
26
27 use Test::More tests => 5;
28
29 BEGIN {
30     use_ok('C4::Items');
31 }
32
33 my $dbh = C4::Context->dbh;
34
35 subtest 'General Add, Get and Del tests' => sub {
36
37     plan tests => 6;
38
39     # Start transaction
40     $dbh->{AutoCommit} = 0;
41     $dbh->{RaiseError} = 1;
42
43     # Create a biblio instance for testing
44     my ($bibnum, $bibitemnum) = get_biblio();
45
46     # Add an item.
47     my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
48     cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
49     cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
50
51     # Get item.
52     my $getitem = GetItem($itemnumber);
53     cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
54     cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
55
56     # Modify item; setting barcode.
57     ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
58     my $moditem = GetItem($itemnumber);
59     cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
60
61     # Delete item.
62     DelItem($dbh, $bibnum, $itemnumber);
63     my $getdeleted = GetItem($itemnumber);
64     is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
65
66     $dbh->rollback;
67 };
68
69 subtest 'GetHiddenItemnumbers tests' => sub {
70
71     plan tests => 9;
72
73     # This sub is controlled by the OpacHiddenItems system preference.
74
75     # Start transaction
76     $dbh->{AutoCommit} = 0;
77     $dbh->{RaiseError} = 1;
78
79     # Create a new biblio
80     my ($biblionumber, $biblioitemnumber) = get_biblio();
81
82     # Add two items
83     my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
84             { homebranch => 'CPL',
85               holdingbranch => 'CPL',
86               withdrawn => 1 },
87             $biblionumber
88     );
89     my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem(
90             { homebranch => 'MPL',
91               holdingbranch => 'MPL',
92               withdrawn => 0 },
93             $biblionumber
94     );
95
96     my $opachiddenitems;
97     my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
98     my @hidden;
99     my @items;
100     push @items, GetItem( $item1_itemnumber );
101     push @items, GetItem( $item2_itemnumber );
102
103     # Empty OpacHiddenItems
104     C4::Context->set_preference('OpacHiddenItems','');
105     ok( !defined( GetHiddenItemnumbers( @items ) ),
106         "Hidden items list undef if OpacHiddenItems empty");
107
108     # Blank spaces
109     C4::Context->set_preference('OpacHiddenItems','  ');
110     ok( scalar GetHiddenItemnumbers( @items ) == 0,
111         "Hidden items list empty if OpacHiddenItems only contains blanks");
112
113     # One variable / value
114     $opachiddenitems = "
115         withdrawn: [1]";
116     C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
117     @hidden = GetHiddenItemnumbers( @items );
118     ok( scalar @hidden == 1, "Only one hidden item");
119     is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
120
121     # One variable, two values
122     $opachiddenitems = "
123         withdrawn: [1,0]";
124     C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
125     @hidden = GetHiddenItemnumbers( @items );
126     ok( scalar @hidden == 2, "Two items hidden");
127     is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
128
129     # Two variables, a value each
130     $opachiddenitems = "
131         withdrawn: [1]
132         homebranch: [MPL]
133     ";
134     C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
135     @hidden = GetHiddenItemnumbers( @items );
136     ok( scalar @hidden == 2, "Two items hidden");
137     is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch=MPL hidden");
138
139     # Valid OpacHiddenItems, empty list
140     @items = ();
141     @hidden = GetHiddenItemnumbers( @items );
142     ok( scalar @hidden == 0, "Empty items list, no item hidden");
143
144     $dbh->rollback;
145 };
146
147 subtest 'GetItemsInfo tests' => sub {
148
149     plan tests => 3;
150
151     # Start transaction
152     $dbh->{AutoCommit} = 0;
153     $dbh->{RaiseError} = 1;
154
155     my $homebranch    = 'CPL';
156     my $holdingbranch = 'MPL';
157
158     # Add a biblio
159     my $biblionumber = get_biblio();
160     # Add an item
161     my ($item_bibnum, $item_bibitemnum, $itemnumber)
162         = AddItem({
163                 homebranch    => $homebranch,
164                 holdingbranch => $holdingbranch
165             }, $biblionumber );
166
167     my $branch = GetBranchDetail( $homebranch );
168     $branch->{ opac_info } = "homebranch OPAC info";
169     ModBranch($branch);
170
171     $branch = GetBranchDetail( $holdingbranch );
172     $branch->{ opac_info } = "holdingbranch OPAC info";
173     ModBranch($branch);
174
175     my @results = GetItemsInfo( $biblionumber );
176     ok( @results, 'GetItemsInfo returns results');
177     is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
178         'GetItemsInfo returns the correct home branch OPAC info notice' );
179     is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
180         'GetItemsInfo returns the correct holding branch OPAC info notice' );
181
182     $dbh->rollback;
183 };
184
185 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
186
187     plan tests => 2;
188
189     # Start transaction
190     $dbh->{AutoCommit} = 0;
191     $dbh->{RaiseError} = 1;
192
193     my $schema = Koha::Database->new()->schema();
194
195     my $biblio =
196     $schema->resultset('Biblio')->create(
197         {
198             title       => "Test title",
199             biblioitems => [
200                 {
201                     itemtype => 'BIB_LEVEL',
202                     items    => [ { itype => "ITEM_LEVEL" } ]
203                 }
204             ]
205         }
206     );
207
208     my $biblioitem = $biblio->biblioitem();
209     my ( $item ) = $biblioitem->items();
210
211     C4::Context->set_preference( 'item-level_itypes', 0 );
212     ok( $item->effective_itemtype() eq 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
213
214     C4::Context->set_preference( 'item-level_itypes', 1 );
215     ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is disabled' );
216
217
218     $dbh->rollback;
219 };
220
221 # Helper method to set up a Biblio.
222 sub get_biblio {
223     my $bib = MARC::Record->new();
224     $bib->append_fields(
225         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
226         MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
227     );
228     my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
229     return ($bibnum, $bibitemnum);
230 }