Bug 14585: Fixing up online help on main page
[koha.git] / t / db_dependent / Items.t
blobdfb165416faeb6f085b97712dafe4c4870bbb2da
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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
19 use Modern::Perl;
21 use MARC::Record;
22 use C4::Biblio;
23 use C4::Branch;
24 use Koha::Database;
26 use Test::More tests => 6;
28 BEGIN {
29 use_ok('C4::Items');
32 my $dbh = C4::Context->dbh;
33 my $branches = GetBranches;
34 my ($branch1, $branch2) = keys %$branches;
36 subtest 'General Add, Get and Del tests' => sub {
38 plan tests => 6;
40 # Start transaction
41 $dbh->{AutoCommit} = 0;
42 $dbh->{RaiseError} = 1;
44 # Create a biblio instance for testing
45 C4::Context->set_preference('marcflavour', 'MARC21');
46 my ($bibnum, $bibitemnum) = get_biblio();
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.");
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.");
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'} . '.');
63 # Delete item.
64 DelItem({ biblionumber => $bibnum, itemnumber => $itemnumber });
65 my $getdeleted = GetItem($itemnumber);
66 is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
68 $dbh->rollback;
71 subtest 'GetHiddenItemnumbers tests' => sub {
73 plan tests => 9;
75 # This sub is controlled by the OpacHiddenItems system preference.
77 # Start transaction
78 $dbh->{AutoCommit} = 0;
79 $dbh->{RaiseError} = 1;
81 # Create a new biblio
82 C4::Context->set_preference('marcflavour', 'MARC21');
83 my ($biblionumber, $biblioitemnumber) = get_biblio();
85 # Add branches if they don't exist
86 if (not defined GetBranchDetail('CPL')) {
87 ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
89 if (not defined GetBranchDetail('MPL')) {
90 ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
93 # Add two items
94 my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
95 { homebranch => $branch1,
96 holdingbranch => $branch1,
97 withdrawn => 1 },
98 $biblionumber
100 my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem(
101 { homebranch => $branch2,
102 holdingbranch => $branch2,
103 withdrawn => 0 },
104 $biblionumber
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 );
114 # Empty OpacHiddenItems
115 C4::Context->set_preference('OpacHiddenItems','');
116 ok( !defined( GetHiddenItemnumbers( @items ) ),
117 "Hidden items list undef if OpacHiddenItems empty");
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");
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");
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");
140 # Two variables, a value each
141 $opachiddenitems = "
142 withdrawn: [1]
143 homebranch: [$branch2]
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");
150 # Valid OpacHiddenItems, empty list
151 @items = ();
152 @hidden = GetHiddenItemnumbers( @items );
153 ok( scalar @hidden == 0, "Empty items list, no item hidden");
155 $dbh->rollback;
158 subtest 'GetItemsInfo tests' => sub {
160 plan tests => 4;
162 # Start transaction
163 $dbh->{AutoCommit} = 0;
164 $dbh->{RaiseError} = 1;
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 );
175 my $branch = GetBranchDetail( $branch1 );
176 $branch->{ opac_info } = "homebranch OPAC info";
177 ModBranch($branch);
179 $branch = GetBranchDetail( $branch2 );
180 $branch->{ opac_info } = "holdingbranch OPAC info";
181 ModBranch($branch);
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' );
192 $dbh->rollback;
195 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
197 plan tests => 2;
199 # Start transaction
200 $dbh->{AutoCommit} = 0;
201 $dbh->{RaiseError} = 1;
203 my $schema = Koha::Database->new()->schema();
205 my $biblio =
206 $schema->resultset('Biblio')->create(
208 title => "Test title",
209 biblioitems => [
211 itemtype => 'BIB_LEVEL',
212 items => [ { itype => "ITEM_LEVEL" } ]
218 my $biblioitem = $biblio->biblioitem();
219 my ( $item ) = $biblioitem->items();
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' );
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' );
227 $dbh->rollback;
230 subtest 'SearchItems test' => sub {
231 plan tests => 13;
233 # Start transaction
234 $dbh->{AutoCommit} = 0;
235 $dbh->{RaiseError} = 1;
237 C4::Context->set_preference('marcflavour', 'MARC21');
238 my ($biblionumber) = get_biblio();
240 # Add branches if they don't exist
241 if (not defined GetBranchDetail('CPL')) {
242 ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
244 if (not defined GetBranchDetail('MPL')) {
245 ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
248 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
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);
260 my ($items, $total_results);
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");
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");
270 # Search all items where homebranch = 'CPL'
271 my $filter = {
272 field => 'homebranch',
273 query => 'CPL',
274 operator => '=',
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;
285 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
287 # Search all items where homebranch != 'CPL'
288 $filter = {
289 field => 'homebranch',
290 query => 'CPL',
291 operator => '!=',
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;
302 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
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',
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 %'");
313 # Search all items where biblio title is 'Silence in the library'
314 # and homebranch is 'CPL'
315 $filter = {
316 conjunction => 'AND',
317 filters => [
319 field => 'marc:245$a',
320 query => 'Silence in the %',
321 operator => 'like',
324 field => 'homebranch',
325 query => 'CPL',
326 operator => '=',
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;
338 ok($found, "item1 found");
340 my ($itemfield) = GetMarcFromKohaField('items.itemnumber', '');
342 # Create item subfield 'z' without link
343 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
344 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", "")', undef, $itemfield);
346 # Clear cache
347 $C4::Context::context->{marcfromkohafield} = undef;
348 $C4::Biblio::inverted_field_map = undef;
350 my $item3_record = new MARC::Record;
351 $item3_record->append_fields(
352 new MARC::Field($itemfield, '', '', 'z' => 'foobar')
354 my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
355 $biblionumber);
357 # Search item where item subfield z is "foobar"
358 $filter = {
359 field => 'marc:' . $itemfield . '$z',
360 query => 'foobar',
361 operator => 'like',
363 ($items, $total_results) = SearchItems($filter);
364 ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
366 # Link $z to items.itemnotes (and make sure there is no other subfields
367 # linked to it)
368 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=""', undef, $itemfield);
369 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
371 # Clear cache
372 $C4::Context::context->{marcfromkohafield} = undef;
373 $C4::Biblio::inverted_field_map = undef;
375 ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
377 # Make sure the link is used
378 my $item3 = GetItem($item3_itemnumber);
379 ok($item3->{itemnotes} eq 'foobar', 'itemnotes eq "foobar"');
381 # Do the same search again.
382 # This time it will search in items.itemnotes
383 ($items, $total_results) = SearchItems($filter);
384 ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"');
386 $dbh->rollback;
389 # Helper method to set up a Biblio.
390 sub get_biblio {
391 my $bib = MARC::Record->new();
392 $bib->append_fields(
393 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
394 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
396 my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
397 return ($bibnum, $bibitemnum);