From 09905d64a9482936a6bd20134a68d8a7299354a9 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 16 Jul 2014 14:07:33 +0200 Subject: [PATCH] Bug 12583: DelItem prototype - Add unit tests DelItem should return 1 if the item has been deleted, otherwise 0. Test plan: Verify that t/db_dependent/Items/DelItem.t returns green Signed-off-by: Chris Cormack Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- C4/Items.pm | 14 +++++++--- t/db_dependent/Items/DelItem.t | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 t/db_dependent/Items/DelItem.t diff --git a/C4/Items.pm b/C4/Items.pm index 4aaa078727..fe058147dd 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -648,8 +648,11 @@ sub DelItem { $biblionumber = C4::Biblio::GetBiblionumberFromItemnumber($itemnumber); } + # If there is no biblionumber for the given itemnumber, there is nothing to delete + return 0 unless $biblionumber; + # FIXME check the item has no current issues - _koha_delete_item( $itemnumber ); + my $deleted = _koha_delete_item( $itemnumber ); # get the MARC record my $record = GetMarcBiblio($biblionumber); @@ -657,6 +660,7 @@ sub DelItem { #search item field code logaction("CATALOGUING", "DELETE", $itemnumber, "item") if C4::Context->preference("CataloguingLog"); + return $deleted; } =head2 CheckItemPreSave @@ -2355,6 +2359,10 @@ sub _koha_delete_item { my $sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?"); $sth->execute($itemnum); my $data = $sth->fetchrow_hashref(); + + # There is no item to delete + return 0 unless $data; + my $query = "INSERT INTO deleteditems SET "; my @bind = (); foreach my $key ( keys %$data ) { @@ -2368,8 +2376,8 @@ sub _koha_delete_item { # delete from items table $sth = $dbh->prepare("DELETE FROM items WHERE itemnumber=?"); - $sth->execute($itemnum); - return; + my $deleted = $sth->execute($itemnum); + return ( $deleted == 1 ) ? 1 : 0; } =head2 _marc_from_item_hash diff --git a/t/db_dependent/Items/DelItem.t b/t/db_dependent/Items/DelItem.t new file mode 100644 index 0000000000..35ff6e4383 --- /dev/null +++ b/t/db_dependent/Items/DelItem.t @@ -0,0 +1,49 @@ +use Modern::Perl; + +use MARC::Record; +use C4::Biblio; + +use Test::More tests => 7; + +BEGIN { + use_ok('C4::Items'); +} + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +my ( $biblionumber, $bibitemnum ) = get_biblio(); + +my ( $item_bibnum, $item_bibitemnum, $itemnumber ); +( $item_bibnum, $item_bibitemnum, $itemnumber ) = + AddItem( { homebranch => 'CPL', holdingbranch => 'CPL' }, $biblionumber ); + +my $deleted = DelItem( { biblionumber => $biblionumber, itemnumber => $itemnumber } ); +is( $deleted, 1, "DelItem should return 1 if the item has been deleted" ); +my $deleted_item = GetItem($itemnumber); +is( $deleted_item->{itemnumber}, undef, "DelItem with biblionumber parameter - the item should be deleted." ); + +( $item_bibnum, $item_bibitemnum, $itemnumber ) = + AddItem( { homebranch => 'CPL', holdingbranch => 'CPL' }, $biblionumber ); +$deleted = DelItem( { biblionumber => $biblionumber, itemnumber => $itemnumber } ); +is( $deleted, 1, "DelItem should return 1 if the item has been deleted" ); +$deleted_item = GetItem($itemnumber); +is( $deleted_item->{itemnumber}, undef, "DelItem without biblionumber parameter - the item should be deleted." ); + +$deleted = DelItem( { itemnumber => $itemnumber + 1} ); +is ( $deleted, 0, "DelItem should return 0 if no item has been deleted" ); + +$deleted = DelItem( { itemnumber => $itemnumber + 1, biblionumber => $biblionumber } ); +is ( $deleted, 0, "DelItem should return 0 if no item has been deleted" ); + +# 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 ); +} -- 2.39.5