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