3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 use Test::More tests => 3;
27 use C4::Reserves qw( AddReserve ModReserve ModReserveAffect );
29 use t::lib::TestBuilder;
32 my $schema = Koha::Database->schema;
33 $schema->storage->txn_begin;
34 my $builder = t::lib::TestBuilder->new;
35 my $dbh = C4::Context->dbh;
37 $dbh->do("DELETE FROM reserves");
38 $dbh->do("DELETE FROM old_reserves");
40 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
41 my $itemtype = $builder->build( { source => 'Itemtype', value => { notforloan => 0 } } )->{itemtype};
43 t::lib::Mocks::mock_userenv({ flags => 1, userid => '1', branchcode => $branchcode });
45 my $borrowers_count = 3;
47 my $biblio = $builder->build_sample_biblio();
48 my $item_barcode = 'my_barcode';
49 my $itemnumber = Koha::Item->new(
51 biblionumber => $biblio->biblionumber,
52 homebranch => $branchcode,
53 holdingbranch => $branchcode,
54 barcode => $item_barcode,
59 # Create some borrowers
60 my $patron_category = $builder->build({ source => 'Category' });
62 foreach my $i ( 1 .. $borrowers_count ) {
63 my $borrowernumber = Koha::Patron->new({
64 firstname => 'my firstname',
65 surname => 'my surname ' . $i,
66 categorycode => $patron_category->{categorycode},
67 branchcode => $branchcode,
68 })->store->borrowernumber;
69 push @borrowernumbers, $borrowernumber;
72 # Create five item level holds
73 foreach my $borrowernumber (@borrowernumbers) {
76 branchcode => $branchcode,
77 borrowernumber => $borrowernumber,
78 biblionumber => $biblio->biblionumber,
83 ModReserveAffect( $itemnumber, $borrowernumbers[0] );
84 my $patron = Koha::Patrons->find( $borrowernumbers[1] );
85 C4::Circulation::AddIssue( $patron, $item_barcode, undef, 'revert' );
87 my $priorities = $dbh->selectall_arrayref(
88 "SELECT priority FROM reserves ORDER BY priority ASC");
89 ok( scalar @$priorities == 2, 'Only 2 holds remain in the reserves table' );
90 ok( $priorities->[0]->[0] == 1, 'First hold has a priority of 1' );
91 ok( $priorities->[1]->[0] == 2, 'Second hold has a priority of 2' );
93 $schema->storage->txn_rollback;