Bug 13264: Follow up: in opac_utf8.t insert also delete of biblio
[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 => 6;
27
28 BEGIN {
29     use_ok('C4::Items');
30 }
31
32 my $dbh = C4::Context->dbh;
33 my $branches = GetBranches;
34 my ($branch1, $branch2) = keys %$branches;
35
36 subtest 'General Add, Get and Del tests' => sub {
37
38     plan tests => 6;
39
40     # Start transaction
41     $dbh->{AutoCommit} = 0;
42     $dbh->{RaiseError} = 1;
43
44     # Create a biblio instance for testing
45     C4::Context->set_preference('marcflavour', 'MARC21');
46     my ($bibnum, $bibitemnum) = get_biblio();
47
48     # Add an item.
49     my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch1, holdingbranch => $branch1 } , $bibnum);
50     cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
51     cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
52
53     # Get item.
54     my $getitem = GetItem($itemnumber);
55     cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
56     cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
57
58     # Modify item; setting barcode.
59     ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
60     my $moditem = GetItem($itemnumber);
61     cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
62
63     # Delete item.
64     DelItem({ biblionumber => $bibnum, itemnumber => $itemnumber });
65     my $getdeleted = GetItem($itemnumber);
66     is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
67
68     $dbh->rollback;
69 };
70
71 subtest 'GetHiddenItemnumbers tests' => sub {
72
73     plan tests => 9;
74
75     # This sub is controlled by the OpacHiddenItems system preference.
76
77     # Start transaction
78     $dbh->{AutoCommit} = 0;
79     $dbh->{RaiseError} = 1;
80
81     # Create a new biblio
82     C4::Context->set_preference('marcflavour', 'MARC21');
83     my ($biblionumber, $biblioitemnumber) = get_biblio();
84
85     # Add branches if they don't exist
86     if (not defined GetBranchDetail('CPL')) {
87         ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
88     }
89     if (not defined GetBranchDetail('MPL')) {
90         ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
91     }
92
93     # Add two items
94     my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
95             { homebranch => $branch1,
96               holdingbranch => $branch1,
97               withdrawn => 1 },
98             $biblionumber
99     );
100     my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem(
101             { homebranch => $branch2,
102               holdingbranch => $branch2,
103               withdrawn => 0 },
104             $biblionumber
105     );
106
107     my $opachiddenitems;
108     my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
109     my @hidden;
110     my @items;
111     push @items, GetItem( $item1_itemnumber );
112     push @items, GetItem( $item2_itemnumber );
113
114     # Empty OpacHiddenItems
115     C4::Context->set_preference('OpacHiddenItems','');
116     ok( !defined( GetHiddenItemnumbers( @items ) ),
117         "Hidden items list undef if OpacHiddenItems empty");
118
119     # Blank spaces
120     C4::Context->set_preference('OpacHiddenItems','  ');
121     ok( scalar GetHiddenItemnumbers( @items ) == 0,
122         "Hidden items list empty if OpacHiddenItems only contains blanks");
123
124     # One variable / value
125     $opachiddenitems = "
126         withdrawn: [1]";
127     C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
128     @hidden = GetHiddenItemnumbers( @items );
129     ok( scalar @hidden == 1, "Only one hidden item");
130     is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
131
132     # One variable, two values
133     $opachiddenitems = "
134         withdrawn: [1,0]";
135     C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
136     @hidden = GetHiddenItemnumbers( @items );
137     ok( scalar @hidden == 2, "Two items hidden");
138     is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
139
140     # Two variables, a value each
141     $opachiddenitems = "
142         withdrawn: [1]
143         homebranch: [$branch2]
144     ";
145     C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
146     @hidden = GetHiddenItemnumbers( @items );
147     ok( scalar @hidden == 2, "Two items hidden");
148     is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch=MPL hidden");
149
150     # Valid OpacHiddenItems, empty list
151     @items = ();
152     @hidden = GetHiddenItemnumbers( @items );
153     ok( scalar @hidden == 0, "Empty items list, no item hidden");
154
155     $dbh->rollback;
156 };
157
158 subtest 'GetItemsInfo tests' => sub {
159
160     plan tests => 4;
161
162     # Start transaction
163     $dbh->{AutoCommit} = 0;
164     $dbh->{RaiseError} = 1;
165
166     # Add a biblio
167     my ($biblionumber, $biblioitemnumber) = get_biblio();
168     # Add an item
169     my ($item_bibnum, $item_bibitemnum, $itemnumber)
170         = AddItem({
171                 homebranch    => $branch1,
172                 holdingbranch => $branch2
173             }, $biblionumber );
174
175     my $branch = GetBranchDetail( $branch1 );
176     $branch->{ opac_info } = "homebranch OPAC info";
177     ModBranch($branch);
178
179     $branch = GetBranchDetail( $branch2 );
180     $branch->{ opac_info } = "holdingbranch OPAC info";
181     ModBranch($branch);
182
183     my @results = GetItemsInfo( $biblionumber );
184     ok( @results, 'GetItemsInfo returns results');
185     is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
186         'GetItemsInfo returns the correct home branch OPAC info notice' );
187     is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
188         'GetItemsInfo returns the correct holding branch OPAC info notice' );
189     is( exists( $results[0]->{ onsite_checkout } ), 1,
190         'GetItemsInfo returns a onsite_checkout key' );
191
192     $dbh->rollback;
193 };
194
195 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
196
197     plan tests => 2;
198
199     # Start transaction
200     $dbh->{AutoCommit} = 0;
201     $dbh->{RaiseError} = 1;
202
203     my $schema = Koha::Database->new()->schema();
204
205     my $biblio =
206     $schema->resultset('Biblio')->create(
207         {
208             title       => "Test title",
209             biblioitems => [
210                 {
211                     itemtype => 'BIB_LEVEL',
212                     items    => [ { itype => "ITEM_LEVEL" } ]
213                 }
214             ]
215         }
216     );
217
218     my $biblioitem = $biblio->biblioitem();
219     my ( $item ) = $biblioitem->items();
220
221     C4::Context->set_preference( 'item-level_itypes', 0 );
222     ok( $item->effective_itemtype() eq 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
223
224     C4::Context->set_preference( 'item-level_itypes', 1 );
225     ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
226
227     $dbh->rollback;
228 };
229
230 subtest 'SearchItems test' => sub {
231     plan tests => 10;
232
233     # Start transaction
234     $dbh->{AutoCommit} = 0;
235     $dbh->{RaiseError} = 1;
236
237     C4::Context->set_preference('marcflavour', 'MARC21');
238     my ($biblionumber) = get_biblio();
239
240     # Add branches if they don't exist
241     if (not defined GetBranchDetail('CPL')) {
242         ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
243     }
244     if (not defined GetBranchDetail('MPL')) {
245         ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
246     }
247
248     my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
249
250     # Add two items
251     my (undef, undef, $item1_itemnumber) = AddItem({
252         homebranch => 'CPL',
253         holdingbranch => 'CPL',
254     }, $biblionumber);
255     my (undef, undef, $item2_itemnumber) = AddItem({
256         homebranch => 'MPL',
257         holdingbranch => 'MPL',
258     }, $biblionumber);
259
260     my ($items, $total_results);
261
262     ($items, $total_results) = SearchItems();
263     is($total_results, $initial_items_count + 2, "Created 2 new items");
264     is(scalar @$items, $total_results, "SearchItems() returns all items");
265
266     ($items, $total_results) = SearchItems(undef, {rows => 1});
267     is($total_results, $initial_items_count + 2);
268     is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
269
270     # Search all items where homebranch = 'CPL'
271     my $filter = {
272         field => 'homebranch',
273         query => 'CPL',
274         operator => '=',
275     };
276     ($items, $total_results) = SearchItems($filter);
277     ok($total_results > 0, "There is at least one CPL item");
278     my $all_items_are_CPL = 1;
279     foreach my $item (@$items) {
280         if ($item->{homebranch} ne 'CPL') {
281             $all_items_are_CPL = 0;
282             last;
283         }
284     }
285     ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
286
287     # Search all items where homebranch != 'CPL'
288     $filter = {
289         field => 'homebranch',
290         query => 'CPL',
291         operator => '!=',
292     };
293     ($items, $total_results) = SearchItems($filter);
294     ok($total_results > 0, "There is at least one non-CPL item");
295     my $all_items_are_not_CPL = 1;
296     foreach my $item (@$items) {
297         if ($item->{homebranch} eq 'CPL') {
298             $all_items_are_not_CPL = 0;
299             last;
300         }
301     }
302     ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
303
304     # Search all items where biblio title (245$a) is like 'Silence in the %'
305     $filter = {
306         field => 'marc:245$a',
307         query => 'Silence in the %',
308         operator => 'like',
309     };
310     ($items, $total_results) = SearchItems($filter);
311     ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
312
313     # Search all items where biblio title is 'Silence in the library'
314     # and homebranch is 'CPL'
315     $filter = {
316         conjunction => 'AND',
317         filters => [
318             {
319                 field => 'marc:245$a',
320                 query => 'Silence in the %',
321                 operator => 'like',
322             },
323             {
324                 field => 'homebranch',
325                 query => 'CPL',
326                 operator => '=',
327             },
328         ],
329     };
330     ($items, $total_results) = SearchItems($filter);
331     my $found = 0;
332     foreach my $item (@$items) {
333         if ($item->{itemnumber} == $item1_itemnumber) {
334             $found = 1;
335             last;
336         }
337     }
338     ok($found, "item1 found");
339
340     $dbh->rollback;
341 };
342
343 # Helper method to set up a Biblio.
344 sub get_biblio {
345     my $bib = MARC::Record->new();
346     $bib->append_fields(
347         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
348         MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
349     );
350     my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
351     return ($bibnum, $bibitemnum);
352 }