From b7c928e2caa02262275939684d2e07a1c8997456 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Thu, 26 Nov 2015 11:00:22 +0100 Subject: [PATCH] Bug 15261: Add tests for PreventChechoutOnSameReservePeriod and PreventReservesOnSamePeriod Created 2 new tests files Circulation/CanBookBeIssued.t and Reserves/ReserveDates.t Signed-off-by: Laurence Rault --- t/db_dependent/Circulation/CanBookBeIssued.t | 144 +++++++++++++++++++ t/db_dependent/Reserves/ReserveDate.t | 138 ++++++++++++++++++ 2 files changed, 282 insertions(+) create mode 100755 t/db_dependent/Circulation/CanBookBeIssued.t create mode 100755 t/db_dependent/Reserves/ReserveDate.t diff --git a/t/db_dependent/Circulation/CanBookBeIssued.t b/t/db_dependent/Circulation/CanBookBeIssued.t new file mode 100755 index 0000000000..d5c2e0c180 --- /dev/null +++ b/t/db_dependent/Circulation/CanBookBeIssued.t @@ -0,0 +1,144 @@ +#!/usr/bin/env 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 3 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, see . + +use Modern::Perl; + +use Test::More tests => 2; +use Test::NoWarnings; +use C4::Members; +use C4::Reserves qw( AddReserve ReservesOnSamePeriod ); +use C4::Circulation qw( CanBookBeIssued ); +use Koha::DateUtils qw( dt_from_string ); + +use t::lib::Mocks; +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new(); + +subtest 'Tests for CanBookBeIssued with overlap reserves' => sub { + plan tests => 7; + + Koha::CirculationRules->set_rules( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + holds_per_record => 1, + } + } + ); + + t::lib::Mocks::mock_preference( 'PreventCheckoutOnSameReservePeriod', 1 ); + my $categorycode = $builder->build( { source => 'Category' } )->{categorycode}; + my $branch = $builder->build( { source => 'Branch', value => { branchcode => 'BRC' } } ); + my $branchcode = $branch->{branchcode}; + + my $patron_1 = $builder->build_object( + { + class => 'Koha::Patrons', + value => { + branchcode => $branchcode, + categorycode => $categorycode, + } + } + ); + + my $patron_2 = $builder->build_object( + { + class => 'Koha::Patrons', + value => { + branchcode => $branchcode, + categorycode => $categorycode, + } + } + ); + + my $biblio = $builder->build( { source => 'Biblio' } ); + my $biblioitem = $builder->build( + { + source => 'Biblioitem', + value => { + biblionumber => $biblio->{biblionumber}, + }, + } + ); + my $item = $builder->build( + { + source => 'Item', + value => { + biblionumber => $biblio->{biblionumber}, + biblioitemnumber => $biblioitem->{biblioitemnumber}, + withdrawn => 0, + itemlost => 0, + notforloan => 0, + }, + } + ); + + my $startdate = dt_from_string(); + $startdate->add_duration( DateTime::Duration->new( days => 4 ) ); + my $expdate = $startdate->clone(); + $expdate->add_duration( DateTime::Duration->new( days => 10 ) ); + + my $reserveid = AddReserve( + { + branchcode => $branchcode, + borrowernumber => $patron_1->borrowernumber, + biblionumber => $item->{biblionumber}, + priority => 1, + reservation_date => $startdate->ymd, + expiration_date => $expdate->ymd, + } + ); + is( defined $reserveid, 1, "Hold has been placed" ); + + my $non_overlap_duedate = dt_from_string(); + $non_overlap_duedate->add_duration( DateTime::Duration->new( days => 2 ) ); + my ( $error, $question, $alerts ) = + CanBookBeIssued( $patron_2, $item->{barcode}, $non_overlap_duedate, 1, 0 ); + + is_deeply( $error, {}, "" ); + is_deeply( $question, {}, "" ); + is_deeply( $alerts, {}, "" ); + + my $overlap_duedate = dt_from_string(); + $overlap_duedate->add_duration( DateTime::Duration->new( days => 5 ) ); + ( $error, $question, $alerts ) = + CanBookBeIssued( $patron_2, $item->{barcode}, $overlap_duedate, 1, 0 ); + + is_deeply( $error, {}, "" ); + my $expected = { + RESERVED => 1, + resfirstname => $patron_1->firstname, + ressurname => $patron_1->surname, + rescardnumber => $patron_1->cardnumber, + resborrowernumber => $patron_1->borrowernumber, + resbranchcode => $patron_1->branchcode, + resreservedate => $startdate->ymd, + reserve_id => $reserveid, + }; + + is_deeply( $question, $expected, "Need confirmation" ); + is_deeply( $alerts, {}, "" ); +}; + +$schema->storage->txn_rollback; diff --git a/t/db_dependent/Reserves/ReserveDate.t b/t/db_dependent/Reserves/ReserveDate.t new file mode 100755 index 0000000000..0a05735b43 --- /dev/null +++ b/t/db_dependent/Reserves/ReserveDate.t @@ -0,0 +1,138 @@ +#!/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 3 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, see . + +use Modern::Perl; + +use Test::More tests => 7; +use Test::Warn; +use Test::NoWarnings; + +use MARC::Record; +use DateTime::Duration; + +use C4::Biblio; +use C4::Items; +use C4::Members; +use C4::Circulation; +use C4::Reserves; +use Koha::Holds; +use t::lib::Mocks; +use t::lib::TestBuilder; + +use Koha::DateUtils; + +use_ok('C4::Reserves'); + +# Start transaction +my $database = Koha::Database->new(); +my $schema = $database->schema(); +$schema->storage->txn_begin(); +my $dbh = C4::Context->dbh; +$dbh->do('DELETE FROM circulation_rules'); + +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +my $builder = t::lib::TestBuilder->new; + +my $categorycode = $builder->build( { source => 'Category' } )->{categorycode}; +my $library = $builder->build( { source => 'Branch' } )->{branchcode}; +my $branchcode = Koha::Libraries->search->next->branchcode; + +my $frameworkcode = q//; + +my $borrower = $builder->build( + { + source => 'Borrower', + value => { + branchcode => $branchcode, + categorycode => $categorycode, + } + } +); + +my $borrower2 = $builder->build( + { + source => 'Borrower', + value => { + branchcode => $branchcode, + categorycode => $categorycode, + } + } +); + +my $borrowernumber = $borrower->{borrowernumber}; +my $borrowernumber2 = $borrower2->{borrowernumber}; + +# Create a helper biblio and item +my $bibnum = $builder->build_sample_biblio( { frameworkcode => $frameworkcode } )->biblionumber; +my $item = $builder->build_sample_item( { biblionumber => $bibnum, library => $branchcode, barcode => '333' } ); +my ( $item_bibnum, $bibitemnum, $itemnumber ) = ( $bibnum, $item->biblioitemnumber, $item->itemnumber ); + +C4::Context->set_preference( 'AllowHoldDateInFuture', 1 ); +t::lib::Mocks::mock_preference( 'PreventReservesOnSamePeriod', 1 ); + +C4::Reserves::AddReserve( + { + branchcode => $branchcode, + borrowernumber => $borrowernumber, + biblionumber => $bibnum, + biblioitemnumber => $bibitemnum, + priority => 1, + reservation_date => '2015-11-01', + expiration_date => '2015-11-20', + } +); + +is( + C4::Reserves::ReservesOnSamePeriod( $bibnum, undef, '2015-11-25', '2015-11-30' ), undef, + "Period doesn't overlaps" +); + +ok( C4::Reserves::ReservesOnSamePeriod( $bibnum, undef, '2015-11-02', '2015-11-10' ), "Period overlaps" ); + +my $item2 = $builder->build_sample_item( + { biblionumber => $bibnum, homebranch => $branchcode, holdingbranch => $branchcode, barcode => '444' } ); +my ( $item_bibnum2, $bibitemnum2, $itemnumber2 ) = ( $bibnum, $item2->biblioitemnumber, $item2->itemnumber ); + +is( + C4::Reserves::ReservesOnSamePeriod( $bibnum, undef, '2015-11-02', '2015-11-10' ), undef, + "Period overlaps but there is 2 items" +); + +C4::Reserves::AddReserve( + { + branchcode => $branchcode, + borrowernumber => $borrowernumber2, + biblionumber => $bibnum, + biblioitemnumber => $bibitemnum, + priority => 1, + reservation_date => '2016-02-01', + expiration_date => '2016-02-10', + itemnumber => $itemnumber, + } +); + +is( + C4::Reserves::ReservesOnSamePeriod( $bibnum, $itemnumber, '02/12/2015', '10/12/2015' ), undef, + "Period on item does not overlap (with metric date format)" +); + +my $reserve = C4::Reserves::ReservesOnSamePeriod( $bibnum, $itemnumber, '2016-01-31', '2016-02-05' ); +is( $reserve->[0]->{itemnumber}, $itemnumber, 'Period on item overlaps' ); + +$dbh->rollback; -- 2.39.5