efe3d17acc
Koha::Item->effective_itemtype should fallback to biblio-level itemtype even if item-level item types are set, in the case the item has no itemtype set (bad migration, bad old code). To test: - Run $ prove t/db_dependent/Items.t => FAIL: Koha::Item->effective_itemtype doesn't work properly Edit: Added a test for a warning when falling back as per QA request and because it made a lot of sense :-D Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz> Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
443 lines
15 KiB
Perl
Executable file
443 lines
15 KiB
Perl
Executable file
#!/usr/bin/perl
|
|
#
|
|
# This file is part of Koha.
|
|
#
|
|
# Koha is free software; you can redistribute it and/or modify it under the
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
# Foundation; either version 2 of the License, or (at your option) any later
|
|
# version.
|
|
#
|
|
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License along
|
|
# with Koha; if not, write to the Free Software Foundation, Inc.,
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
#
|
|
|
|
use Modern::Perl;
|
|
|
|
use MARC::Record;
|
|
use C4::Biblio;
|
|
use C4::Branch;
|
|
use Koha::Database;
|
|
|
|
use Test::More tests => 8;
|
|
use Test::Warn;
|
|
|
|
BEGIN {
|
|
use_ok('C4::Items');
|
|
use_ok('Koha::Items');
|
|
}
|
|
|
|
my $dbh = C4::Context->dbh;
|
|
my $branches = GetBranches;
|
|
my ($branch1, $branch2) = keys %$branches;
|
|
|
|
subtest 'General Add, Get and Del tests' => sub {
|
|
|
|
plan tests => 6;
|
|
|
|
# Start transaction
|
|
$dbh->{AutoCommit} = 0;
|
|
$dbh->{RaiseError} = 1;
|
|
|
|
# Create a biblio instance for testing
|
|
C4::Context->set_preference('marcflavour', 'MARC21');
|
|
my ($bibnum, $bibitemnum) = get_biblio();
|
|
|
|
# Add an item.
|
|
my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch1, holdingbranch => $branch1 } , $bibnum);
|
|
cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
|
|
cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
|
|
|
|
# Get item.
|
|
my $getitem = GetItem($itemnumber);
|
|
cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
|
|
cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
|
|
|
|
# Modify item; setting barcode.
|
|
ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
|
|
my $moditem = GetItem($itemnumber);
|
|
cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
|
|
|
|
# Delete item.
|
|
DelItem({ biblionumber => $bibnum, itemnumber => $itemnumber });
|
|
my $getdeleted = GetItem($itemnumber);
|
|
is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
|
|
|
|
$dbh->rollback;
|
|
};
|
|
|
|
subtest 'GetHiddenItemnumbers tests' => sub {
|
|
|
|
plan tests => 9;
|
|
|
|
# This sub is controlled by the OpacHiddenItems system preference.
|
|
|
|
# Start transaction
|
|
$dbh->{AutoCommit} = 0;
|
|
$dbh->{RaiseError} = 1;
|
|
|
|
# Create a new biblio
|
|
C4::Context->set_preference('marcflavour', 'MARC21');
|
|
my ($biblionumber, $biblioitemnumber) = get_biblio();
|
|
|
|
# Add branches if they don't exist
|
|
if (not defined GetBranchDetail('CPL')) {
|
|
ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
|
|
}
|
|
if (not defined GetBranchDetail('MPL')) {
|
|
ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
|
|
}
|
|
|
|
# Add two items
|
|
my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
|
|
{ homebranch => $branch1,
|
|
holdingbranch => $branch1,
|
|
withdrawn => 1 },
|
|
$biblionumber
|
|
);
|
|
my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem(
|
|
{ homebranch => $branch2,
|
|
holdingbranch => $branch2,
|
|
withdrawn => 0 },
|
|
$biblionumber
|
|
);
|
|
|
|
my $opachiddenitems;
|
|
my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
|
|
my @hidden;
|
|
my @items;
|
|
push @items, GetItem( $item1_itemnumber );
|
|
push @items, GetItem( $item2_itemnumber );
|
|
|
|
# Empty OpacHiddenItems
|
|
C4::Context->set_preference('OpacHiddenItems','');
|
|
ok( !defined( GetHiddenItemnumbers( @items ) ),
|
|
"Hidden items list undef if OpacHiddenItems empty");
|
|
|
|
# Blank spaces
|
|
C4::Context->set_preference('OpacHiddenItems',' ');
|
|
ok( scalar GetHiddenItemnumbers( @items ) == 0,
|
|
"Hidden items list empty if OpacHiddenItems only contains blanks");
|
|
|
|
# One variable / value
|
|
$opachiddenitems = "
|
|
withdrawn: [1]";
|
|
C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
|
|
@hidden = GetHiddenItemnumbers( @items );
|
|
ok( scalar @hidden == 1, "Only one hidden item");
|
|
is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
|
|
|
|
# One variable, two values
|
|
$opachiddenitems = "
|
|
withdrawn: [1,0]";
|
|
C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
|
|
@hidden = GetHiddenItemnumbers( @items );
|
|
ok( scalar @hidden == 2, "Two items hidden");
|
|
is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
|
|
|
|
# Two variables, a value each
|
|
$opachiddenitems = "
|
|
withdrawn: [1]
|
|
homebranch: [$branch2]
|
|
";
|
|
C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
|
|
@hidden = GetHiddenItemnumbers( @items );
|
|
ok( scalar @hidden == 2, "Two items hidden");
|
|
is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch=MPL hidden");
|
|
|
|
# Valid OpacHiddenItems, empty list
|
|
@items = ();
|
|
@hidden = GetHiddenItemnumbers( @items );
|
|
ok( scalar @hidden == 0, "Empty items list, no item hidden");
|
|
|
|
$dbh->rollback;
|
|
};
|
|
|
|
subtest 'GetItemsInfo tests' => sub {
|
|
|
|
plan tests => 4;
|
|
|
|
# Start transaction
|
|
$dbh->{AutoCommit} = 0;
|
|
$dbh->{RaiseError} = 1;
|
|
|
|
# Add a biblio
|
|
my ($biblionumber, $biblioitemnumber) = get_biblio();
|
|
# Add an item
|
|
my ($item_bibnum, $item_bibitemnum, $itemnumber)
|
|
= AddItem({
|
|
homebranch => $branch1,
|
|
holdingbranch => $branch2
|
|
}, $biblionumber );
|
|
|
|
my $branch = GetBranchDetail( $branch1 );
|
|
$branch->{ opac_info } = "homebranch OPAC info";
|
|
ModBranch($branch);
|
|
|
|
$branch = GetBranchDetail( $branch2 );
|
|
$branch->{ opac_info } = "holdingbranch OPAC info";
|
|
ModBranch($branch);
|
|
|
|
my @results = GetItemsInfo( $biblionumber );
|
|
ok( @results, 'GetItemsInfo returns results');
|
|
is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
|
|
'GetItemsInfo returns the correct home branch OPAC info notice' );
|
|
is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
|
|
'GetItemsInfo returns the correct holding branch OPAC info notice' );
|
|
is( exists( $results[0]->{ onsite_checkout } ), 1,
|
|
'GetItemsInfo returns a onsite_checkout key' );
|
|
|
|
$dbh->rollback;
|
|
};
|
|
|
|
subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
|
|
|
|
plan tests => 4;
|
|
|
|
# Start transaction
|
|
$dbh->{AutoCommit} = 0;
|
|
$dbh->{RaiseError} = 1;
|
|
|
|
my $schema = Koha::Database->new()->schema();
|
|
|
|
my $biblio =
|
|
$schema->resultset('Biblio')->create(
|
|
{
|
|
title => "Test title",
|
|
biblioitems => [
|
|
{
|
|
itemtype => 'BIB_LEVEL',
|
|
items => [ { itype => "ITEM_LEVEL" } ]
|
|
}
|
|
]
|
|
}
|
|
);
|
|
|
|
my @bi = $biblio->biblioitems();
|
|
my ( $item ) = $bi[0]->items();
|
|
|
|
C4::Context->set_preference( 'item-level_itypes', 0 );
|
|
ok( $item->effective_itemtype() eq 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
|
|
|
|
C4::Context->set_preference( 'item-level_itypes', 1 );
|
|
ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
|
|
|
|
# If itemtype is not defined and item-level_level item types are set
|
|
# fallback to biblio-level itemtype (Bug 14651) and warn
|
|
$item->itype( undef );
|
|
$item->update();
|
|
my $effective_itemtype;
|
|
warning_is { $effective_itemtype = $item->effective_itemtype() }
|
|
"item-level_itypes set but no itemtype set for item ($item->itemnumber)",
|
|
'->effective_itemtype() raises a warning when falling back to bib-level';
|
|
|
|
ok( defined $effective_itemtype &&
|
|
$effective_itemtype eq 'BIB_LEVEL',
|
|
'$item->effective_itemtype() falls back to biblioitems.itemtype when item-level_itypes is enabled but undef' );
|
|
|
|
$dbh->rollback;
|
|
};
|
|
|
|
subtest 'SearchItems test' => sub {
|
|
plan tests => 14;
|
|
|
|
# Start transaction
|
|
$dbh->{AutoCommit} = 0;
|
|
$dbh->{RaiseError} = 1;
|
|
|
|
C4::Context->set_preference('marcflavour', 'MARC21');
|
|
my ($biblionumber) = get_biblio();
|
|
|
|
# Add branches if they don't exist
|
|
if (not defined GetBranchDetail('CPL')) {
|
|
ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
|
|
}
|
|
if (not defined GetBranchDetail('MPL')) {
|
|
ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
|
|
}
|
|
|
|
my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
|
|
|
|
# Add two items
|
|
my (undef, undef, $item1_itemnumber) = AddItem({
|
|
homebranch => 'CPL',
|
|
holdingbranch => 'CPL',
|
|
}, $biblionumber);
|
|
my (undef, undef, $item2_itemnumber) = AddItem({
|
|
homebranch => 'MPL',
|
|
holdingbranch => 'MPL',
|
|
}, $biblionumber);
|
|
|
|
my ($items, $total_results);
|
|
|
|
($items, $total_results) = SearchItems();
|
|
is($total_results, $initial_items_count + 2, "Created 2 new items");
|
|
is(scalar @$items, $total_results, "SearchItems() returns all items");
|
|
|
|
($items, $total_results) = SearchItems(undef, {rows => 1});
|
|
is($total_results, $initial_items_count + 2);
|
|
is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
|
|
|
|
# Search all items where homebranch = 'CPL'
|
|
my $filter = {
|
|
field => 'homebranch',
|
|
query => 'CPL',
|
|
operator => '=',
|
|
};
|
|
($items, $total_results) = SearchItems($filter);
|
|
ok($total_results > 0, "There is at least one CPL item");
|
|
my $all_items_are_CPL = 1;
|
|
foreach my $item (@$items) {
|
|
if ($item->{homebranch} ne 'CPL') {
|
|
$all_items_are_CPL = 0;
|
|
last;
|
|
}
|
|
}
|
|
ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
|
|
|
|
# Search all items where homebranch != 'CPL'
|
|
$filter = {
|
|
field => 'homebranch',
|
|
query => 'CPL',
|
|
operator => '!=',
|
|
};
|
|
($items, $total_results) = SearchItems($filter);
|
|
ok($total_results > 0, "There is at least one non-CPL item");
|
|
my $all_items_are_not_CPL = 1;
|
|
foreach my $item (@$items) {
|
|
if ($item->{homebranch} eq 'CPL') {
|
|
$all_items_are_not_CPL = 0;
|
|
last;
|
|
}
|
|
}
|
|
ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
|
|
|
|
# Search all items where biblio title (245$a) is like 'Silence in the %'
|
|
$filter = {
|
|
field => 'marc:245$a',
|
|
query => 'Silence in the %',
|
|
operator => 'like',
|
|
};
|
|
($items, $total_results) = SearchItems($filter);
|
|
ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
|
|
|
|
# Search all items where biblio title is 'Silence in the library'
|
|
# and homebranch is 'CPL'
|
|
$filter = {
|
|
conjunction => 'AND',
|
|
filters => [
|
|
{
|
|
field => 'marc:245$a',
|
|
query => 'Silence in the %',
|
|
operator => 'like',
|
|
},
|
|
{
|
|
field => 'homebranch',
|
|
query => 'CPL',
|
|
operator => '=',
|
|
},
|
|
],
|
|
};
|
|
($items, $total_results) = SearchItems($filter);
|
|
my $found = 0;
|
|
foreach my $item (@$items) {
|
|
if ($item->{itemnumber} == $item1_itemnumber) {
|
|
$found = 1;
|
|
last;
|
|
}
|
|
}
|
|
ok($found, "item1 found");
|
|
|
|
my ($itemfield) = GetMarcFromKohaField('items.itemnumber', '');
|
|
|
|
# Create item subfield 'z' without link
|
|
$dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
|
|
$dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", "")', undef, $itemfield);
|
|
|
|
# Clear cache
|
|
$C4::Context::context->{marcfromkohafield} = undef;
|
|
$C4::Biblio::inverted_field_map = undef;
|
|
|
|
my $item3_record = new MARC::Record;
|
|
$item3_record->append_fields(
|
|
new MARC::Field($itemfield, '', '', 'z' => 'foobar')
|
|
);
|
|
my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
|
|
$biblionumber);
|
|
|
|
# Search item where item subfield z is "foobar"
|
|
$filter = {
|
|
field => 'marc:' . $itemfield . '$z',
|
|
query => 'foobar',
|
|
operator => 'like',
|
|
};
|
|
($items, $total_results) = SearchItems($filter);
|
|
ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
|
|
|
|
# Link $z to items.itemnotes (and make sure there is no other subfields
|
|
# linked to it)
|
|
$dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=""', undef, $itemfield);
|
|
$dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
|
|
|
|
# Clear cache
|
|
$C4::Context::context->{marcfromkohafield} = undef;
|
|
$C4::Biblio::inverted_field_map = undef;
|
|
|
|
ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
|
|
|
|
# Make sure the link is used
|
|
my $item3 = GetItem($item3_itemnumber);
|
|
ok($item3->{itemnotes} eq 'foobar', 'itemnotes eq "foobar"');
|
|
|
|
# Do the same search again.
|
|
# This time it will search in items.itemnotes
|
|
($items, $total_results) = SearchItems($filter);
|
|
ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"');
|
|
|
|
my $cpl_items = SearchItemsByField( 'homebranch', 'CPL');
|
|
is( ( $cpl_items and scalar( @$cpl_items ) ), 1, 'SearchItemsByField should return something' );
|
|
|
|
$dbh->rollback;
|
|
};
|
|
|
|
subtest 'Koha::Item(s) tests' => sub {
|
|
|
|
plan tests => 5;
|
|
|
|
# Start transaction
|
|
my $schema = Koha::Database->new()->schema();
|
|
$schema->storage->txn_begin();
|
|
$dbh->{RaiseError} = 1;
|
|
|
|
# Create a biblio and item for testing
|
|
C4::Context->set_preference('marcflavour', 'MARC21');
|
|
my ($bibnum, $bibitemnum) = get_biblio();
|
|
my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch1, holdingbranch => $branch2 } , $bibnum);
|
|
|
|
# Get item.
|
|
my $item = Koha::Items->find( $itemnumber );
|
|
is( ref($item), 'Koha::Item', "Got Koha::Item" );
|
|
|
|
my $homebranch = $item->home_branch();
|
|
is( ref($homebranch), 'Koha::Branch', "Got Koha::Branch from home_branch method" );
|
|
is( $homebranch->branchcode(), $branch1, "Home branch code matches homebranch" );
|
|
|
|
my $holdingbranch = $item->holding_branch();
|
|
is( ref($holdingbranch), 'Koha::Branch', "Got Koha::Branch from holding_branch method" );
|
|
is( $holdingbranch->branchcode(), $branch2, "Home branch code matches holdingbranch" );
|
|
};
|
|
|
|
# Helper method to set up a Biblio.
|
|
sub get_biblio {
|
|
my $bib = MARC::Record->new();
|
|
$bib->append_fields(
|
|
MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
|
|
MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
|
|
);
|
|
my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
|
|
return ($bibnum, $bibitemnum);
|
|
}
|