From a9fa831ef89bd437ddc8309dc073f3aca22f3987 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 5 Nov 2013 17:22:26 +0100 Subject: [PATCH] Bug 11023: Add UT for C4::Items::ToggleNewStatus MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test plan: prove t/db_dependent/Items/ToggleNewStatus.t Signed-off-by: juliette et rémy --- t/db_dependent/Items/ToggleNewStatus.t | 260 +++++++++++++++++++++++++++++++++ 1 file changed, 260 insertions(+) create mode 100644 t/db_dependent/Items/ToggleNewStatus.t diff --git a/t/db_dependent/Items/ToggleNewStatus.t b/t/db_dependent/Items/ToggleNewStatus.t new file mode 100644 index 0000000..8ea14f7 --- /dev/null +++ b/t/db_dependent/Items/ToggleNewStatus.t @@ -0,0 +1,260 @@ +#!/usr/bin/perl + +use Modern::Perl; +use Test::More tests => 15; +use MARC::Record; +use MARC::Field; +use DateTime; +use DateTime::Duration; + +use C4::Biblio; +use C4::Context; +use C4::Items; +use Koha::DateUtils; + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +$dbh->do(q| + DELETE FROM marc_subfield_structure + WHERE kohafield = 'items.new' OR kohafield = 'items.stocknumber' +|); + +my $new_tagfield = 'i'; +$dbh->do(qq| + INSERT INTO marc_subfield_structure(tagfield, tagsubfield, kohafield, frameworkcode) + VALUES ( 952, '$new_tagfield', 'items.new', '' ) +|); + +my $record = MARC::Record->new(); +$record->append_fields( + MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), + MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'), +); +my ($biblionumber, undef) = C4::Biblio::AddBiblio($record, ''); + +my ($item_bibnum, $item_bibitemnum, $itemnumber) = C4::Items::AddItem( + { + homebranch => 'CPL', + holdingbranch => 'CPL', + new => 'new_value', + ccode => 'FIC', + }, + $biblionumber +); + +my $item = C4::Items::GetItem( $itemnumber ); +is ( $item->{new}, 'new_value', q|AddItem insert the 'new' field| ); + +my ( $tagfield, undef ) = GetMarcFromKohaField('items.itemnumber', ''); +my $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); +is( $marc_item->subfield($tagfield, $new_tagfield), 'new_value', q|Koha mapping is correct|); + +# Update the items.new field if items.ccode eq 'FIC' => should be updated +my @rules = ( + { + conditions => [ + { + field => 'items.ccode', + value => 'FIC', + }, + ], + substitutions => [ + { + field => 'items.new', + value => 'updated_value', + }, + ], + duration => '0', + }, +); + +C4::Items::ToggleNewStatus( { rules => \@rules } ); + +my $modified_item = C4::Items::GetItem( $itemnumber ); +is( $modified_item->{new}, 'updated_value', q|ToggleNewStatus: The new value is updated|); +$marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); +is( $marc_item->subfield($tagfield, $new_tagfield), 'updated_value', q|ToggleNewStatus: The new value is updated| ); + +# Update the items.new field if items.ccode eq 'DONT_EXIST' => should not be updated +@rules = ( + { + conditions => [ + { + field => 'items.ccode', + value => 'DONT_EXIST', + }, + ], + substitutions => [ + { + field => 'items.new', + value => 'new_updated_value', + }, + ], + duration => '0', + }, +); + +C4::Items::ToggleNewStatus( { rules => \@rules } ); + +$modified_item = C4::Items::GetItem( $itemnumber ); +is( $modified_item->{new}, 'updated_value', q|ToggleNewStatus: The new value is not updated|); +$marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); +is( $marc_item->subfield($tagfield, $new_tagfield), 'updated_value', q|ToggleNewStatus: The new value is not updated| ); + +# Play with duration +$item = C4::Items::GetItem( $itemnumber ); +my $dt_today = dt_from_string; +my $days5ago = $dt_today->add_duration( DateTime::Duration->new( days => -5 ) ); + +C4::Items::ModItem( { dateaccessioned => $days5ago }, $biblionumber, $itemnumber ); +$item = C4::Items::GetItem( $itemnumber ); + +@rules = ( + { + conditions => [ + { + field => 'items.ccode', + value => 'FIC', + }, + ], + substitutions => [ + { + field => 'items.new', + value => 'new_updated_value', + }, + ], + duration => '10', + }, +); +C4::Items::ToggleNewStatus( { rules => \@rules } ); +$modified_item = C4::Items::GetItem( $itemnumber ); +is( $modified_item->{new}, 'updated_value', q|ToggleNewStatus: Duration = 10 : The new value is not updated|); + +$rules[0]->{duration} = 5; +$rules[0]->{substitutions}[0]{value} = 'new_updated_value5'; +C4::Items::ToggleNewStatus( { rules => \@rules } ); +$modified_item = C4::Items::GetItem( $itemnumber ); +is( $modified_item->{new}, 'new_updated_value5', q|ToggleNewStatus: Duration = 5 : The new value is updated|); + +$rules[0]->{duration} = ''; +$rules[0]->{substitutions}[0]{value} = 'new_updated_value_empty_string'; +C4::Items::ToggleNewStatus( { rules => \@rules } ); +$modified_item = C4::Items::GetItem( $itemnumber ); +is( $modified_item->{new}, 'new_updated_value_empty_string', q|ToggleNewStatus: Duration = '' : The new value is updated|); + +$rules[0]->{duration} = undef; +$rules[0]->{substitutions}[0]{value} = 'new_updated_value_undef'; +C4::Items::ToggleNewStatus( { rules => \@rules } ); +$modified_item = C4::Items::GetItem( $itemnumber ); +is( $modified_item->{new}, 'new_updated_value_undef', q|ToggleNewStatus: Duration = undef : The new value is updated|); + +# Field deletion +@rules = ( + { + conditions => [ + { + field => 'items.ccode', + value => 'FIC', + }, + ], + substitutions => [ + { + field => 'items.new', + value => '', + }, + ], + duration => '0', + }, +); + +C4::Items::ToggleNewStatus( { rules => \@rules } ); + +$modified_item = C4::Items::GetItem( $itemnumber ); +is( $modified_item->{new}, '', q|ToggleNewStatus: The new value is empty|); +$marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber ); +is( $marc_item->subfield($tagfield, $new_tagfield), undef, q|ToggleNewStatus: The new field is removed from the item marc| ); + +# conditions multiple +@rules = ( + { + conditions => [ + { + field => 'items.ccode', + value => 'FIC', + }, + { + field => 'items.homebranch', + value => 'CPL', + }, + ], + substitutions => [ + { + field => 'items.new', + value => 'new_value', + }, + ], + duration => '0', + }, +); + +C4::Items::ToggleNewStatus( { rules => \@rules } ); + +$modified_item = C4::Items::GetItem( $itemnumber ); +is( $modified_item->{new}, 'new_value', q|ToggleNewStatus: conditions multiple: all match, the new value is updated|); + +@rules = ( + { + conditions => [ + { + field => 'items.ccode', + value => 'FIC', + }, + { + field => 'items.homebranch', + value => 'DONT_EXIST', + }, + ], + substitutions => [ + { + field => 'items.new', + value => 'new_updated_value', + }, + ], + duration => '0', + }, +); + +C4::Items::ToggleNewStatus( { rules => \@rules } ); + +$modified_item = C4::Items::GetItem( $itemnumber ); +is( $modified_item->{new}, 'new_value', q|ToggleNewStatus: conditions multiple: at least 1 condition does not match, the new value is not updated|); + +@rules = ( + { + conditions => [ + { + field => 'items.ccode', + value => 'FIC|NFIC', + }, + { + field => 'items.homebranch', + value => 'MPL|CPL', + }, + ], + substitutions => [ + { + field => 'items.new', + value => 'new_updated_value', + }, + ], + duration => '0', + }, +); + +C4::Items::ToggleNewStatus( { rules => \@rules } ); + +$modified_item = C4::Items::GetItem( $itemnumber ); +is( $modified_item->{new}, 'new_updated_value', q|ToggleNewStatus: conditions multiple: the 2 conditions match, the new value is updated|); + -- 2.1.0