|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
use Modern::Perl; |
| 4 |
|
| 5 |
use Test::More tests => 8; |
| 6 |
|
| 7 |
use t::lib::Mocks; |
| 8 |
use t::lib::TestBuilder; |
| 9 |
|
| 10 |
use MARC::Record; |
| 11 |
|
| 12 |
use C4::Context; |
| 13 |
use C4::Biblio; |
| 14 |
use C4::Items; |
| 15 |
use Koha::Database; |
| 16 |
use Koha::Holds; |
| 17 |
|
| 18 |
BEGIN { |
| 19 |
use FindBin; |
| 20 |
use lib $FindBin::Bin; |
| 21 |
use_ok('C4::Reserves'); |
| 22 |
} |
| 23 |
|
| 24 |
my $schema = Koha::Database->new->schema; |
| 25 |
$schema->storage->txn_begin; |
| 26 |
|
| 27 |
my $builder = t::lib::TestBuilder->new(); |
| 28 |
my $dbh = C4::Context->dbh; |
| 29 |
|
| 30 |
# Create two random branches |
| 31 |
my $library = $builder->build( { source => 'Branch' } ); |
| 32 |
my $branchcode = $library->{branchcode}; |
| 33 |
|
| 34 |
$dbh->do('DELETE FROM reserves'); |
| 35 |
$dbh->do('DELETE FROM message_queue'); |
| 36 |
|
| 37 |
# Create a biblio instance for testing |
| 38 |
my ($biblionumber) = create_helper_biblio('DUMMY'); |
| 39 |
|
| 40 |
# Create item instance for testing. |
| 41 |
my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = |
| 42 |
AddItem( { homebranch => $branchcode, holdingbranch => $branchcode }, |
| 43 |
$biblionumber ); |
| 44 |
|
| 45 |
my $patron_1 = $builder->build( { source => 'Borrower' } ); |
| 46 |
my $patron_2 = $builder->build( { source => 'Borrower' } ); |
| 47 |
my $patron_3 = $builder->build( { source => 'Borrower' } ); |
| 48 |
|
| 49 |
# Add a hold on the item for each of our patrons |
| 50 |
my $hold_1 = Koha::Hold->new( |
| 51 |
{ |
| 52 |
priority => 0, |
| 53 |
borrowernumber => $patron_1->{borrowernumber}, |
| 54 |
branchcode => $library->{branchcode}, |
| 55 |
biblionumber => $biblionumber, |
| 56 |
itemnumber => $itemnumber, |
| 57 |
found => 'W', |
| 58 |
reservedate => '1900-01-01', |
| 59 |
waitingdate => '1900-01-01', |
| 60 |
lowestPriority => 0, |
| 61 |
suspend => 0, |
| 62 |
} |
| 63 |
)->store(); |
| 64 |
my $hold_2 = Koha::Hold->new( |
| 65 |
{ |
| 66 |
priority => 1, |
| 67 |
borrowernumber => $patron_2->{borrowernumber}, |
| 68 |
branchcode => $library->{branchcode}, |
| 69 |
biblionumber => $biblionumber, |
| 70 |
itemnumber => $itemnumber, |
| 71 |
reservedate => '1900-01-01', |
| 72 |
lowestPriority => 0, |
| 73 |
suspend => 0, |
| 74 |
} |
| 75 |
)->store(); |
| 76 |
my $hold_3 = Koha::Hold->new( |
| 77 |
{ |
| 78 |
priority => 1, |
| 79 |
borrowernumber => $patron_2->{borrowernumber}, |
| 80 |
branchcode => $library->{branchcode}, |
| 81 |
biblionumber => $biblionumber, |
| 82 |
itemnumber => $itemnumber, |
| 83 |
reservedate => '1900-01-01', |
| 84 |
lowestPriority => 0, |
| 85 |
suspend => 0, |
| 86 |
} |
| 87 |
)->store(); |
| 88 |
|
| 89 |
# Test CancelExpiredReserves |
| 90 |
t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 ); |
| 91 |
t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', 1 ); |
| 92 |
t::lib::Mocks::mock_preference( 'ExpireReservesOnHolidays', 1 ); |
| 93 |
t::lib::Mocks::mock_preference( 'ExpireReservesAutoFill', 1 ); |
| 94 |
t::lib::Mocks::mock_preference( 'ExpireReservesAutoFillEmail', |
| 95 |
'kyle@example.com' ); |
| 96 |
|
| 97 |
CancelExpiredReserves(); |
| 98 |
|
| 99 |
my @holds = Koha::Holds->search( {}, { order_by => 'priority' } ); |
| 100 |
$hold_2 = $holds[0]; |
| 101 |
$hold_3 = $holds[1]; |
| 102 |
|
| 103 |
is( @holds, 2, 'Found 2 holds' ); |
| 104 |
is( $hold_2->priority, 0, 'Next hold in line now has priority of 0' ); |
| 105 |
is( $hold_2->found, 'W', 'Next hold in line is now set to waiting' ); |
| 106 |
|
| 107 |
my @messages = $schema->resultset('MessageQueue') |
| 108 |
->search( { letter_code => 'HOLD_CHANGED' } ); |
| 109 |
is( @messages, 1, 'Found 1 message in the message queue' ); |
| 110 |
is( $messages[0]->to_address, 'kyle@example.com', 'Message sent to correct email address' ); |
| 111 |
|
| 112 |
$hold_2->waitingdate('1900-01-01')->store(); |
| 113 |
|
| 114 |
CancelExpiredReserves(); |
| 115 |
|
| 116 |
@holds = Koha::Holds->search( {}, { order_by => 'priority' } ); |
| 117 |
$hold_3 = $holds[0]; |
| 118 |
|
| 119 |
is( @holds, 1, 'Found 1 hold' ); |
| 120 |
is( $hold_3->priority, 0, 'Next hold in line now has priority of 0' ); |
| 121 |
is( $hold_3->found, 'W', 'Next hold in line is now set to waiting' ); |
| 122 |
|
| 123 |
# Helper method to set up a Biblio. |
| 124 |
sub create_helper_biblio { |
| 125 |
my $itemtype = shift; |
| 126 |
my $bib = MARC::Record->new(); |
| 127 |
my $title = 'Silence in the library'; |
| 128 |
$bib->append_fields( |
| 129 |
MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ), |
| 130 |
MARC::Field->new( '245', ' ', ' ', a => $title ), |
| 131 |
MARC::Field->new( '942', ' ', ' ', c => $itemtype ), |
| 132 |
); |
| 133 |
return my ( $b, $t, $bi ) = AddBiblio( $bib, '' ); |
| 134 |
} |