Bug 21133: Fix use statements order
[koha-ffzg.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 $dbh->{RaiseError} = 1;
37
38 $dbh->do("DELETE FROM reserves");
39 $dbh->do("DELETE FROM old_reserves");
40
41 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
42 my $itemtype = $builder->build(
43     { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
44
45 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
46 *C4::Context::userenv = \&Mock_userenv;
47
48 sub Mock_userenv {
49     my $userenv = { flags => 1, id => '1', branch => $branchcode };
50     return $userenv;
51 }
52
53 my $borrowers_count = 3;
54
55 # Create a biblio instance
56 my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio();
57
58 # Create an item
59 my $item_barcode = 'my_barcode';
60 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
61     {   homebranch    => $branchcode,
62         holdingbranch => $branchcode,
63         barcode       => $item_barcode,
64         itype         => $itemtype
65     },
66     $bibnum
67 );
68
69 # Create some borrowers
70 my $patron_category = $builder->build({ source => 'Category' });
71 my @borrowernumbers;
72 foreach my $i ( 1 .. $borrowers_count ) {
73     my $borrowernumber = Koha::Patron->new({
74         firstname    => 'my firstname',
75         surname      => 'my surname ' . $i,
76         categorycode => $patron_category->{categorycode},
77         branchcode   => $branchcode,
78     })->store->borrowernumber;
79     push @borrowernumbers, $borrowernumber;
80 }
81
82 my $biblionumber = $bibnum;
83
84 # Create five item level holds
85 foreach my $borrowernumber (@borrowernumbers) {
86     AddReserve(
87         $branchcode,
88         $borrowernumber,
89         $biblionumber,
90         my $bibitems   = q{},
91         my $priority,
92         my $resdate,
93         my $expdate,
94         my $notes = q{},
95         $title,
96         my $checkitem,
97         my $found,
98     );
99 }
100
101 ModReserveAffect( $itemnumber, $borrowernumbers[0] );
102 my $patron = Koha::Patrons->find( $borrowernumbers[1] )->unblessed;
103 C4::Circulation::AddIssue( $patron,
104     $item_barcode, my $datedue, my $cancelreserve = 'revert' );
105
106 my $priorities = $dbh->selectall_arrayref(
107     "SELECT priority FROM reserves ORDER BY priority ASC");
108 ok( scalar @$priorities == 2,   'Only 2 holds remain in the reserves table' );
109 ok( $priorities->[0]->[0] == 1, 'First hold has a priority of 1' );
110 ok( $priorities->[1]->[0] == 2, 'Second hold has a priority of 2' );
111
112 $schema->storage->txn_rollback;
113
114 # Helper method to set up a Biblio.
115 sub create_helper_biblio {
116     my $bib   = MARC::Record->new();
117     my $title = 'Silence in the library';
118     $bib->append_fields(
119         MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
120         MARC::Field->new( '245', ' ', ' ', a => $title ),
121     );
122     return ( $bibnum, $title, $bibitemnum ) = AddBiblio( $bib, '' );
123 }