Bug 27993: Add unit tests
[koha.git] / t / db_dependent / Holds / RevertWaitingStatus.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19 use Test::More tests => 3;
20 use MARC::Record;
21
22 use Koha::Libraries;
23 use Koha::Patrons;
24 use C4::Context;
25 use C4::Items;
26 use C4::Biblio;
27 use C4::Reserves;
28
29 use t::lib::TestBuilder;
30 use t::lib::Mocks;
31
32 my $schema = Koha::Database->schema;
33 $schema->storage->txn_begin;
34 my $builder = t::lib::TestBuilder->new;
35 my $dbh = C4::Context->dbh;
36
37 $dbh->do("DELETE FROM reserves");
38 $dbh->do("DELETE FROM old_reserves");
39
40 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
41 my $itemtype = $builder->build(
42     { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
43
44 t::lib::Mocks::mock_userenv({ flags => 1, userid => '1', branchcode => $branchcode });
45
46 my $borrowers_count = 3;
47
48 my $biblio = $builder->build_sample_biblio();
49 my $item_barcode = 'my_barcode';
50 my $itemnumber = Koha::Item->new(
51     {
52         biblionumber  => $biblio->biblionumber,
53         homebranch    => $branchcode,
54         holdingbranch => $branchcode,
55         barcode       => $item_barcode,
56         itype         => $itemtype
57     },
58 )->store->itemnumber;
59
60 # Create some borrowers
61 my $patron_category = $builder->build({ source => 'Category' });
62 my @borrowernumbers;
63 foreach my $i ( 1 .. $borrowers_count ) {
64     my $borrowernumber = Koha::Patron->new({
65         firstname    => 'my firstname',
66         surname      => 'my surname ' . $i,
67         categorycode => $patron_category->{categorycode},
68         branchcode   => $branchcode,
69     })->store->borrowernumber;
70     push @borrowernumbers, $borrowernumber;
71 }
72
73 # Create five item level holds
74 foreach my $borrowernumber (@borrowernumbers) {
75     AddReserve(
76         {
77             branchcode     => $branchcode,
78             borrowernumber => $borrowernumber,
79             biblionumber   => $biblio->biblionumber,
80         }
81     );
82 }
83
84 ModReserveAffect( $itemnumber, $borrowernumbers[0] );
85 my $patron = Koha::Patrons->find( $borrowernumbers[1] )->unblessed;
86 C4::Circulation::AddIssue( $patron,
87     $item_barcode, my $datedue, my $cancelreserve = 'revert' );
88
89 my $priorities = $dbh->selectall_arrayref(
90     "SELECT priority FROM reserves ORDER BY priority ASC");
91 ok( scalar @$priorities == 2,   'Only 2 holds remain in the reserves table' );
92 ok( $priorities->[0]->[0] == 1, 'First hold has a priority of 1' );
93 ok( $priorities->[1]->[0] == 2, 'Second hold has a priority of 2' );
94
95 $schema->storage->txn_rollback;