Bug 11009: (follow-up) tweak wording and remove potential log noise
[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
24 use Test::More tests => 3;
25
26 BEGIN {
27     use_ok('C4::Items');
28 }
29
30 my $dbh = C4::Context->dbh;
31
32 subtest 'General Add, Get and Del tests' => sub {
33
34     plan tests => 6;
35
36     # Start transaction
37     $dbh->{AutoCommit} = 0;
38     $dbh->{RaiseError} = 1;
39
40     # Helper biblio.
41     diag("Creating biblio instance for testing.");
42     my ($bibnum, $bibitemnum) = get_biblio();
43
44     # Add an item.
45     my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
46     cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
47     cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
48
49     # Get item.
50     my $getitem = GetItem($itemnumber);
51     cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
52     cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
53
54     # Modify item; setting barcode.
55     ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
56     my $moditem = GetItem($itemnumber);
57     cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
58
59     # Delete item.
60     DelItem($dbh, $bibnum, $itemnumber);
61     my $getdeleted = GetItem($itemnumber);
62     is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
63
64     $dbh->rollback;
65 };
66
67 subtest 'GetHiddenItemnumbers tests' => sub {
68
69     plan tests => 9;
70
71     # This sub is controlled by the OpacHiddenItems system preference.
72
73     # Start transaction
74     $dbh->{AutoCommit} = 0;
75     $dbh->{RaiseError} = 1;
76
77     # Create a new biblio
78     my ($biblionumber, $biblioitemnumber) = get_biblio();
79
80     # Add two items
81     my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
82             { homebranch => 'CPL',
83               holdingbranch => 'CPL',
84               withdrawn => 1 },
85             $biblionumber
86     );
87     my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem(
88             { homebranch => 'MPL',
89               holdingbranch => 'MPL',
90               withdrawn => 0 },
91             $biblionumber
92     );
93
94     my $opachiddenitems;
95     my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
96     my @hidden;
97     my @items;
98     push @items, GetItem( $item1_itemnumber );
99     push @items, GetItem( $item2_itemnumber );
100
101     # Empty OpacHiddenItems
102     C4::Context->set_preference('OpacHiddenItems','');
103     ok( !defined( GetHiddenItemnumbers( @items ) ),
104         "Hidden items list undef if OpacHiddenItems empty");
105
106     # Blank spaces
107     C4::Context->set_preference('OpacHiddenItems','  ');
108     ok( scalar GetHiddenItemnumbers( @items ) == 0,
109         "Hidden items list empty if OpacHiddenItems only contains blanks");
110
111     # One variable / value
112     $opachiddenitems = "
113         withdrawn: [1]";
114     C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
115     @hidden = GetHiddenItemnumbers( @items );
116     ok( scalar @hidden == 1, "Only one hidden item");
117     is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
118
119     # One variable, two values
120     $opachiddenitems = "
121         withdrawn: [1,0]";
122     C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
123     @hidden = GetHiddenItemnumbers( @items );
124     ok( scalar @hidden == 2, "Two items hidden");
125     is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
126
127     # Two variables, a value each
128     $opachiddenitems = "
129         withdrawn: [1]
130         homebranch: [MPL]
131     ";
132     C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
133     @hidden = GetHiddenItemnumbers( @items );
134     ok( scalar @hidden == 2, "Two items hidden");
135     is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch=MPL hidden");
136
137     # Valid OpacHiddenItems, empty list
138     @items = ();
139     @hidden = GetHiddenItemnumbers( @items );
140     ok( scalar @hidden == 0, "Empty items list, no item hidden");
141
142     $dbh->rollback;
143 };
144
145 # Helper method to set up a Biblio.
146 sub get_biblio {
147     my $bib = MARC::Record->new();
148     $bib->append_fields(
149         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
150         MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
151     );
152     my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
153     return ($bibnum, $bibitemnum);
154 }